Getting intermediate layer output from a nested network - Keras

Viewed 228

I have a U-net network with VGG16 encoder architecture with pre-trained imagenet weights. Since my input images are grayscale, I added in a convolutional layer with depth 3 prior to sending the input to the U-net model.

Now, I'm trying to get the output of an intermediate layer within the U-net network. I create an intermediate model whose output is the output of the layer that I'm interested in. Here is my code:

base_model = sm.Unet('vgg16', encoder_weights='imagenet', classes=1, activation='sigmoid')
inp = Input(shape=(448, 224, 1))
l1 = Conv2D(3, (1,1))(inp)
out = base_model(l1)
model = Model(inp, out)
model.summary()

intermediate_layer_model = Model(inputs=model.layers[0].input,                                   
                               outputs=model.get_layer('model_1').get_layer('center_block2_relu').output)

Here is the output:

Layer (type)                 Output Shape              Param #
=================================================================
input_2 (InputLayer)         (None, 448, 224, 1)       0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 448, 224, 3)       6
_________________________________________________________________
model_1 (Model)              multiple                  23752273
=================================================================
Total params: 23,752,279
Trainable params: 23,748,247
Non-trainable params: 4,032
_________________________________________________________________

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(?, ?, ?, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []

It seems to me that there is an issue with the U-net model having an input layer (input_1) and I'm not supplying this information during the construction of intermediate_layer_model. However, I expect that the intermediate model to take only the grayscale images as input and not require an additional 3-channel input.

Any help would be appreciated.

0 Answers
Related