使用Keras ResNet50模型转移学习使python停止工作

时间:2017-07-17 03:44:23

标签: python keras theano resnet

我使用Keras版本2.0.5与theano作为Python版本3.6的后端 我正在尝试使用ResNet50模型实现转移学习,并使用了以下示例中给出的代码:https://keras.io/applications/ 在代码中添加以下行会使python停止工作:

model = ResNet50(weights='imagenet')

我已尝试按其他链接中的建议更改模型定义:

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

但是这给了我另一个错误:

  

ValueError:输入必须有3个通道;得到了`input_shape =(3,224,   224)

代码如下:

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

我的theano配置文件:(theanorc)

[global]
floatX = float32
device = cpu

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

我的keras配置文件:(keras.json):

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

2 个答案:

答案 0 :(得分:0)

这是因为网络认为您正在使用tensorflow data_format。在tensorflow中,图像的通道持续(im_rows, im_cols, 3),而使用Theano,通道首先(3, im_rows, im_cols)

从应用程序文档(您提供的链接):

  

所有这些体系结构(Xception除外)都与TensorFlow和Theano兼容,并且在实例化时,模型将根据Keras配置文件〜/ .keras / keras.json中设置的图像数据格式构建。例如,如果您设置了image_data_format = channels_last,则将根据TensorFlow数据格式约定“Width-Height-Depth”构建从此存储库加载的任何模型。

我会建议您尝试这个,将image_data_format更改为channels_first。

我希望这会有所帮助:)

答案 1 :(得分:0)

我能够通过安装anaconda for python 3.5来实现传输学习,keras与theano后端一起工作。不知何故没有anaconda的Python 3.6安装不起作用。