问题描述
我正在尝试将我的keras模型与keras到caffe转换脚本一起使用;每当我尝试运行脚本时,它都会加载我的模型,然后给我一个错误,提示仅支持通道优先".我正在给模型图像添加形状(24,24,3)-但它想要的形状(3,24,24).
I'm trying to use my keras model with a keras-to-caffe conversion script; Whenever I try to run the script, it loads my model and then gives me an error that says "only channels-first is supported". I'm feeding my model images with the shape (24,24,3) - but it wants (3,24,24).
每当我尝试在形状为(3,24,24)的图像上训练模型时,都会出现此错误(我认为,我认为它会向它提供具有24个通道的3x24图像);
Whenever I try to train my model on images of the shape (3,24,24), i get this error (it thinks im feeding it a 3x24 image with 24 channels, i believe);
ValueError: Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convolution' (op: 'Conv2D') with input shapes: [?,1,22,32], [3,3,32,64].
我如何喂养我的keras模型频道优先图像?
How can i feed my keras model channels-first images?
(如果有人需要,请提供模型代码:我只是在做一个简单的分类问题)
(Model code in case anyone needs it: i'm just doing a simple classification problem)
input_1 = Input(shape=input_shape) # input shape is 24,24,3 - needs to be 3,24,24
conv_1 = Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape)(input_1)
conv_2 = Conv2D(64, (3, 3), activation='relu')(conv_1)
pool_1 = MaxPooling2D(pool_size=(2, 2))(conv_2)
drop_1 = Dropout(0.25)(pool_1)
flatten_1 = Flatten()(drop_1)
dense_1 = Dense(128, activation='relu')(flatten_1)
drop_2 = Dropout(0.5)(dense_1)
dense_2 = Dense(128, activation='relu')(drop_2)
drop_3 = Dropout(0.5)(dense_2)
dense_3 = Dense(num_classes, activation='softmax')(drop_3)
model = Model(inputs=input_1, outputs=dense_3)
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test),
verbose=1)
推荐答案
每个卷积层都接受参数 data_format ='channels_first'
.
Every convolutional layer accepts the argument data_format='channels_first'
.
您还可以找到您的 keras.json
文件(在< yourUserFolder>/.keras
中)并将其设置为默认配置.
You can also find your keras.json
file (in <yourUserFolder>/.keras
) and set this as a default configuration.
@Gal的评论很有趣.如果您打算使用多台计算机,最好在代码中设置配置: keras.backend.set_image_data_format('channels_first')
@Gal's comment is a very interesting point. If you're planning on using more than one computer, it might be better to set the config in your code: keras.backend.set_image_data_format('channels_first')
这篇关于首先与Keras合作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!