首先与Keras合作?

Rob*_*rat 1 shape keras

我正在尝试将我的keras模型与keras到caffe转换脚本一起使用;每当我尝试运行脚本时,它都会加载我的模型,然后给我一个错误,提示“仅支持通道优先”。我正在用模型(24,24,3)填充模型图像-但它想要(3,24,24)。

每当我尝试在形状为(3,24,24)的图像上训练模型时,都会出现此错误(我认为,它认为我会向它提供带有24个通道的3x24图像);

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].
Run Code Online (Sandbox Code Playgroud)

我如何喂养我的keras模型频道优先图像?

(如果有人需要,请使用模型代码:我只是在做一个简单的分类问题)

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)
Run Code Online (Sandbox Code Playgroud)

Dan*_*ler 5

每个卷积层都接受该论点data_format='channels_first'

您也可以在中找到您的keras.json文件,<yourUserFolder>/.keras并将其设置为默认配置。

编辑:@Gal的评论很有趣。如果您打算使用多台计算机,最好在代码中设置配置:keras.backend.set_image_data_format('channels_first')

  • 在另一台机器上使用代码时,更改keras.json中的配置可能会导致问题。我认为使用以下内容会更好:keras.backend.set_image_data_format('channels_first') (6认同)