简体   繁体   中英

Transfer learning with Keras ResNet50 model making python to stop working

I am using Keras version 2.0.5 with theano as the backend on Python version 3.6.
I am trying to implement a transfer learning by using ResNet50 model, and have used the code given in the following example: https://keras.io/applications/ Adding the following line to the code is making python to stop working:

model = ResNet50(weights='imagenet')

I have tried changing the model definition as suggested in other links to:

model = ResNet50(include_top=False, weights='imagenet',input_shape=(3,224,224))

but this gives me another error:

ValueError: The input must have 3 channels; got `input_shape=(3, 224, 224)

The code is as follows:

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

model = ResNet50(include_top=False, weights='imagenet',input_shape=(3,224,224))

My theano config file:(theanorc)

[global]
floatX = float32
device = cpu

[nvcc]
compiler_bindir=C:\Program Files (x86)\Microsoft Visual 
Studio\Shared\14.0\VC\bin

My keras config file:(keras.json):

{
"epsilon": 1e-07,
"image_data_format": "channels_first",
"image_dim_ordering": "th",
"backend": "theano",
"floatx": "float32"
}

It comes from the fact that the networks believe you are working with tensorflow data_format. In tensorflow, the channels of the image come last (im_rows, im_cols, 3) while with Theano, channels come first (3, im_rows, im_cols) .

From the applications documentation (link you gave) :

All of these architectures (except Xception) are compatible with both TensorFlow and Theano, and upon instantiation the models will be built according to the image data format set in your Keras configuration file at ~/.keras/keras.json. For instance, if you have set image_data_format=channels_last, then any model loaded from this repository will get built according to the TensorFlow data format convention, "Width-Height-Depth".

I will advise you to try this, change the image_data_format to channels_first.

I hope this helps :)

I was able to implement the transfer learning by installing anaconda for python 3.5, with keras with the theano backend and it worked. Somehow Python 3.6 installation without anaconda did not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM