微调VGG,got:Negative维度大小由1减2引起

Fine-tuning VGG, got:Negative dimension size caused by subtracting 2 from 1

我正在为 MNIST 任务微调 VGG19 模型。 MNIST 中的图像是 (28,28,1),这是一个通道。 但是VGG要输入的是(?,?,3),也就是三个通道。

所以,我的方法是在所有 VGG 层之前再添加一个 Conv2D 层,将 (28,28,1) 数据更改为 (28,28,3),这是我的代码:

inputs = Input(shape=(28,28,1))
x = Conv2D(3,kernel_size=(1,1),activation='relu')(inputs)
print(x.shape)
# out: (?, 28, 28, 3)

现在我输入的形状是正确的(我认为)。

这是我的整个模型: # 改变输入形状: 输入=输入(形状=(28,28,1)) x = Conv2D(3,kernel_size=(1,1),activation='relu')(输入)

# add POOL and FC layers:
x = base_model(x)
x = GlobalMaxPooling2D()(x)
x = Dense(1024,activation='relu')(x)
predictions = Dense(10,activation='softmax')(x)

model = Model(inputs=inputs, outputs=predictions)

# freeze the base_model:
for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='adam',loss='categorical_crossentropy',metric=['accuracy'])

我得到了:

InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'vgg19_10/block5_pool/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,512].

我搜索了这个问题,一种解决方案是添加

from keras import backend as K
K.set_image_dim_ordering('th')

但它对我不起作用。

我的代码有什么问题?

当我们进行分类时,我们应该对输出使用 softmax 激活。

将最后一层的激活更改为 softmax

来自

predictions = Dense(10,activation='relu')(x)

predictions = Dense(10,activation='softmax')(x)

导致错误的第二个错误与您的输入大小有关。 根据 keras vgg19 你的最小图像大小应该不小于 48。

inputs = Input(shape=(48,48,1))