2017-08-27 84 views
0
我使用keras构建基于所述Resnet50模型

匹配滤波器的相应尺寸,下面的代码如下所示数量的输入通道的不Keras

input_crop = Input(shape=(3, 224, 224)) 

# extract feature from image crop 
resnet = ResNet50(include_top=False, weights='imagenet') 
for layer in resnet.layers: # set resnet as non-trainable 
    layer.trainable = False 

crop_encoded = resnet(input_crop) 

然而,我得到一个错误

'ValueError: number of input channels does not match corresponding dimension of filter, 224 != 3'

我该如何解决?

+0

什么后台是你使用和你的keras.json文件的内容是什么? – maz

回答

3

由于Theano & TensorFlow backends针对Keras使用的图像格式不同,因此会定期生成此类错误。在你的情况下,图像显然是channels_first格式(Theano),而最有可能使用TensorFlow后端,它需要channels_last格式。

在Keras的MNIST CNN example提供了一个很好的方法,使你的代码不受这样的问题,即工作都Theano & TensorFlow后端 - 这里是为您的数据适应:

from keras import backend as K 

img_rows, img_cols = 224, 224 

if K.image_data_format() == 'channels_first': 
    input_crop = input_crop.reshape(input_crop.shape[0], 3, img_rows, img_cols) 
    input_shape = (3, img_rows, img_cols) 
else: 
    input_crop = input_crop.reshape(input_crop.shape[0], img_rows, img_cols, 3) 
    input_shape = (img_rows, img_cols, 3) 

input_crop = Input(shape=input_shape)