Home > database >  How to extract the bottleneck layer from the below architecture?
How to extract the bottleneck layer from the below architecture?

Time:03-27

I have created an model (down below). And after training, I want to get the output tensor from the bottleneck layers of this model. So I am trying to create a model of the extracted layers and use this model for predicting.

from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Dense, Activation

nstrides = (1,1)
inputs = layers.Input(imshape)

conv01 = layers.Conv2D(32, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(inputs)
conv1 = layers.Conv2D(32, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv01)
pool1 = layers.MaxPooling2D(pool_size=(2, 2))(conv1)

.
.


#block4
conv04 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(pool3)
conv4 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv04)
pool4 = layers.MaxPooling2D(pool_size=(2, 2))(conv4)

#bottlneck
conv05 = layers.Conv2D(512, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(pool4)
conv5 = layers.Conv2D(512, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv05)
upconv5 = layers.Conv2DTranspose(256, kernel_size=(2, 2), 
                       strides = (2,2))(conv5)

#upblock 1
conc6 = layers.concatenate([upconv5, conv4])
conv06 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conc6)
conv6 = layers.Conv2D(256, 4, activation = 'relu', 
                       strides = nstrides, padding="same")(conv06)
up7 = layers.Conv2DTranspose(126, kernel_size=(2, 2), 
 strides = (2,2))(conv7)
.
.
.

#combine the model together
model = Model(inputs, outputs)

CodePudding user response:

First, in order to locate the desired layer which would be the new output tensor, you can first do

for i, layer in enumerate(model.layers):
    print(i, layer.name)
...
...
12 max_pooling2d_15
13 conv2d_65
14 conv2d_66
15 conv2d_transpose_12
16 concatenate_12
17 conv2d_67
...
...

Here, the layer index from 13 to 15 is from the bottleneck layer of your model. If you want to get the output tensor from this bottleneck layer, you can do:

new_model = Model(model.input, 
                model.get_layer(index=15).output)
# or, 
new_model = Model(model.input,
                model.get_layer(name='conv2d_transpose_12').output)

Both are the same, the first one is by index and the second one is by layer name.

CodePudding user response:

The below code worked for me but I need more information on

extracted_layers = extracted_layers([x])[0] --> This line of code. What is [x]?

The output I wanted was a array of tensors but didnt understand what does [x][0]mean.

from tensorflow.keras import backend as K

extracted_layers = K.function([model.layers[0].input], [model.layers[14].output])

extracted_layers = extracted_layers([x])[0]

  • Related