简体   繁体   English

使用Keras ResNet50模型制作Python转移学习以停止工作

[英]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. 我使用的是Keras版本2.0.5和theano作为Python版本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: 我正在尝试通过使用ResNet50模型来实现转移学习,并使用了以下示例中给出的代码: https : ResNet50在代码中添加以下ResNet50 python停止工作:

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; ValueError:输入必须具有3个通道。 got `input_shape=(3, 224, 224) 得到了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) 我的theano配置文件:(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): 我的keras配置文件:(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. 这是因为网络相信您正在使用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) . 在tensorflow中,图像的通道排在最后(im_rows, im_cols, 3)而在Theano中,通道排在第一位(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. 所有这些架构(Xception除外)都与TensorFlow和Theano兼容,并且在实例化时,将根据在〜/ .keras / keras.json中的Keras配置文件中设置的图像数据格式来构建模型。 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". 例如,如果您设置了image_data_format = channels_last,则将从该存储库加载的任何模型都将根据TensorFlow数据格式约定“ Width-Height-Depth”进行构建。

I will advise you to try this, change the image_data_format to channels_first. 我建议您尝试一下,将image_data_format更改为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. 通过安装用于蟒蛇3.5的anaconda,带有theano后端的keras,我能够实现迁移学习。 Somehow Python 3.6 installation without anaconda did not work. 不安装anaconda的Python 3.6无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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