alexandre_d
alexandre_d

Reputation: 155

Keras shape error in applications Inception Resnet v2

I'm using Keras 2.1.3 and I'm trying to fine tune a Inception Resnetv2 with Keras application.

So I load the pretrained model from keras.applications

input_tensor = Input(shape=(299,299,3))
model = applications.inception_resnet_v2.InceptionResNetV2(weights='imagenet', 
                                                        include_top=False,
                                                        input_tensor=input_tensor,
                                                        input_shape=(299, 299,3))

I create the bottleneck for my problem :

top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(40, activation='softmax'))

And finally create a new model to concatenate the two parts :

new_model = Sequential()
for l in model.layers:
      new_model.add(l)

At this step, I got an error

ValueError: Input 0 is incompatible with layer conv2d_7: expected axis -1 of input shape to have value 192 but got shape (None, 35, 35, 64)

So I printed each layer shape and I have

Layer n-1 : Input : (None, 35, 35, 64), Output : (None, 35, 35, 64)

Layer n : Input : (None, 35, 35, 192), Output : (None, 35, 35, 48)

As you can see shapes dismatch and it seems weird that come from Keras.

Upvotes: 2

Views: 1595

Answers (1)

Simbarashe Timothy Motsi
Simbarashe Timothy Motsi

Reputation: 1525

I am not sure top_model.add(Flatten(input_shape=model.output_shape[1:])) is passing the required dimensions.
An alternative way would be to try.

ResNetV2_model_output = model.output
new_concatenated_model = Flatten()(ResNetV2_model_output)
new_concatenated_model = (Dense(256, activation='relu'))(new_concatenated_model)
new_concatenated_model = ((Dropout(0.5)))(new_concatenated_model)
new_concatenated_model = (Dense(40, activation='softmax'))(new_concatenated_model)

Upvotes: 1

Related Questions