question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transfer learning with Resnet50 fail with Exception

See original GitHub issue

Hi, 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))

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

17reactions
MrXucommented, Nov 25, 2016

I solved the problem after reading through source code for Resnet50.py. If anyone encounter the same problem, please add the input_shape parameter when you call Resnet50():

model = ResNet50(include_top=False, weights='imagenet', input_tensor=Input(shape=(224,224,3)))

If you are using Keras ImageDataGenerator, the input shape will be automatically inferred.

3reactions
alonaljcommented, Nov 14, 2018

Regarding the error:

input size must be at least 197x197,

Had the same issue, but disappeared after upgrading keras to 2.2.4 (with backend tensorflow 1.12.0 in case it matters).

Read more comments on GitHub >

github_iconTop Results From Across the Web

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...
Read more >
Keras resnet50 transfer learning - kaggle kernel could not ...
I am trying to use resnet50 pretrained model on Kaggle kernel. But, when I run the following code, Error occurs and it could...
Read more >
Keras vs PyTorch: how to distinguish Aliens vs Predators with ...
As training from scratch is unfeasible in most cases (as it is very data hungry), we'll perform transfer learning using ResNet-50 pre-trained on...
Read more >
Resnet50 and other pre-trained models with cross-validation
Resnet50, densnet201, resnet18, vgg16, exception net, test network are pre-trained models with cross-validation Matlab code. all evaluation metrics are ...
Read more >
What Makes Transfer Learning Work for Medical Images
Transfer learning is a standard technique to transfer knowledge from one domain to another. For applications in medical imaging, transfer from ImageNet has ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found