Wei Xu
Wei Xu

Reputation: 1659

Keras Transfer learning with Resnet50 fail with Exception

I am using Resnet50 to do transfer learning. The backend is tensorflow. I tried to stack three more layers on top of the Resnet but fail with following error:

Exception: The shape of the input to "Flatten" is not fully defined (got (None, None, 2048). 
Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.

The code for stacking two models are as following:

model = ResNet50(include_top=False, weights='imagenet')

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(1, activation='sigmoid'))
top_model.load_weights(top_model_weights_path)

model = Model(input=model.input, output=top_model(model.output))

Upvotes: 2

Views: 2253

Answers (2)

emanuele
emanuele

Reputation: 2589

You must explicit input_shape when instantiate of the Resnet. model = ResNet50(include_top=False, weights='imagenet',input_shape=(224,224,3))

In case you have theano as backend you must set the number of channel as first: model = ResNet50(include_top=False, weights='imagenet',input_shape=(3,224,224))

Upvotes: 2

Dennis Sakva
Dennis Sakva

Reputation: 1467

The last layer of resnet with include_top=False option is already flattened and you don't need another flattening layer.

Upvotes: 1

Related Questions