尝试安装类似VGG的模型时输入错误

时间:2017-08-05 08:52:34

标签: keras theano conv-neural-network

当我尝试使用以下模型时:

model = Sequential([
    Lambda(vgg_preprocess, input_shape=(3,244,244)),
    Conv2D(64,3,3, activation='relu'),
    BatchNormalization(axis=1),
    Conv2D(64,3,3, activation='relu'),
    MaxPooling2D(),
    BatchNormalization(axis=1),
    Conv2D(128,3,3, activation='relu'),
    BatchNormalization(axis=1),
    Conv2D(128,3,3, activation='relu'),
    MaxPooling2D(),
    BatchNormalization(axis=1),
    Conv2D(256,3,3, activation='relu'),
    BatchNormalization(axis=1),
    Conv2D(256,3,3, activation='relu'),
    MaxPooling2D(),
    Flatten(),
    BatchNormalization(),
    Dense(1024, activation='relu'),
    BatchNormalization(),
    Dropout(0.5),
    Dense(1024, activation='relu'),
    BatchNormalization(),
    Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])

我收到此错误:

TypeError: Cannot convert Type TensorType(float32, 4D) (of Variable AbstractConv2d_gradInputs{convdim=2, border_mode='valid', subsample=(1, 1), filter_flip=True, imshp=(None, 256, 56, 56), kshp=(256, 256, 3, 3), filter_dilation=(1, 1)}.0) into Type TensorType(float64, 4D). You can try to manually convert AbstractConv2d_gradInputs{convdim=2, border_mode='valid', subsample=(1, 1), filter_flip=True, imshp=(None, 256, 56, 56), kshp=(256, 256, 3, 3), filter_dilation=(1, 1)}.0 into a TensorType(float64, 4D).

这就是我的拟合方式:

 model.fit_generator(train_batches, train_batches.n, nb_epoch=1, validation_data=test_batches, nb_val_samples=test_batches.n)

这是vgg_preprocess函数:

vgg_mean = np.array([123.68, 116.779, 103.939]).reshape((3,1,1))

def vgg_preprocess(x):
    x = x - vgg_mean  #Subtract the mean of each channel
    return x[:, ::-1] #Inverse the channel order to suit that of VGG RGB->BGR

这是什么意思,如何解决?

1 个答案:

答案 0 :(得分:1)

问题在于vgg_mean.dtype = 'float64,而大多数DL包中的标准浮点决策是float32

环境:

vgg_mean = np.array(vgg_mean, dtype='float32')

应该解决你的问题。