首页 文章

从Vgg16网络导入后如何更改瓶颈功能的输入形状

提问于
浏览
1

我正在尝试使用VGG16 Net训练模型进行图像分类,并希望使用此代码将没有Dense图层的权重传输到我的图像集 .

model1 = applications.VGG16(include_top=False, weights='imagenet', input_shape=(img_width,img_height,3))

After learning the bottleneck features the last few layers of the model are:

block5_conv2 (Conv2D)        (None, 6, 6, 512)         2359808   
_________________________________________________________________
block5_conv3 (Conv2D)        (None, 6, 6, 512)         2359808   
_________________________________________________________________
block5_pool (MaxPooling2D)   (None, 3, 3, 512)         0         
=================================================================

最后一层维度是 (None,3,3,512) . 这将是我的Dense图层的输入 .

model1 = Sequential()
model1.add(Flatten(input_shape=train_data.shape[1:]))

所以模型的输入形状是 (3,3,512) . 我的问题是,当我试图预测图像时,输入图像的大小是 (224,224,3) . 那么如何将输入图像的形状转换为模型的输入形状呢?

When I try to predict it, this is the error I received:

ValueError: Error when checking input: expected flatten_1_input to have a shape (3, 3, 512) but got array with shape (224, 224, 3)

如何更改模型的输入形状或输入图像的输入形状,我必须预测?

1 回答

  • 0

    Flatten图层的输入是VGG16模型的输出(实际上它是卷积基础,因为你要删除顶部的密集分类器)而不是你的图像 . VGG16模型已经有一个输入层,因此无需再创建另一个输入层 . 因此你可以这样做:

    vgg_base = applications.VGG16(include_top=False, weights='imagenet', input_shape=(img_width,img_height,3))
    
    model = Sequentail(vgg_base)
    model.add(Flatten())
    # the rest of the layers you want to add
    

    VGG16是一个顺序模型,因此上述方法有效 . 但是,对于没有顺序体系结构的其他模型,您需要使用Keras Functional API .


    我从你的帖子中无法理解你是在同时进行特征提取分类,或者你已经提取了这些特征,现在你想用它们来分类你的图像 . 上述方法适用于前一种情况 . 但是对于后一种情况,正如我之前提到的,Flatten层的输入是提取的特征(即VGG16基础的输出)而不是图像 . 因此,您必须正确设置 input_shape 参数:

    model = Sequentail()
    model.add(Flatten(input_shape=(3,3,512))
    # the rest of the layers
    

相关问题