shawar nawaz
shawar nawaz

Reputation: 51

"ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor" - Getting this error when i concatenate vgg16 base to my own FC layer

I have this model where I concatenated vgg16 base model to 1 FC layer followed by my output layer. This is what the whole model looks like. enter image description here

And if I expand on vgg16, i have this:

enter image description here

Im trying to use the functional api to take the model input as my input and 2 outputs, the first being the 'block5_conv3' layer and the 2nd being the model output (dense_19).

My code for that is as follows:-

model = Model(inputs=[clf.get_layer('vgg16').input],
  outputs = [clf.get_layer('vgg16').get_layer('block5_conv3').output, clf.output])

For which im getting the following error:- enter image description here

Cant figure out what I'm doing wrong here.

Upvotes: 1

Views: 635

Answers (1)

Marco Cerliani
Marco Cerliani

Reputation: 22031

You should firstly define an intermediate model from your VGG16 that returns all the desired outputs (in your case: block5_conv3 and the final output)

vgg16 = VGG16(input_shape=(96,96,3), weights='imagenet', include_top=False)
extraction_model = Model(
    inputs = [vgg16.input],
    outputs = [vgg16.get_layer('block5_conv3').output, vgg16.output])

Then you can use it as a standard feature extractor in your new architecture:

inp = Input((96,96,3))
block5_conv3, x =  extraction_model(inp)
x = GlobalAveragePooling2D()(x)
x = Dense(64)(x)
x = Dropout(0.3)(x)
x = BatchNormalization()(x)
output = Dense(2, activation='softmax')(x)
model = Model(inputs=inp, outputs=[block5_conv3, output])

Upvotes: 1

Related Questions