首页 文章

微调VGG,得到:从1中减去2引起的负尺寸大小

提问于
浏览
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)

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

这是我的整个模型:#更改输入形状:inputs =输入(shape =(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')

但它对我不起作用 .

我的代码出了什么问题?

1 回答

  • 1

    当我们进行分类时,我们应该在输出上使用softmax激活 .

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

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

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

    你导致错误的第二个错误是关于你的输入大小 . 根据keras vgg19,您的最小图像尺寸不应小于48 .

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

相关问题