[英]how to change the input shape of bottleneck features after importing from Vgg16 net
我正在尝试使用VGG16 Net训练模型进行图像分类,并希望使用此代码将没有密集层的权重转移到我的图像集中。
model1 = applications.VGG16(include_top=False, weights='imagenet', input_shape=(img_width,img_height,3))
了解瓶颈功能后,模型的最后几层是:
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)
。 这将是我的“密集”层的输入。
model1 = Sequential()
model1.add(Flatten(input_shape=train_data.shape[1:]))
因此,模型的输入形状为(3,3,512)
。 我的问题是,当我尝试预测图像时,输入图像的大小为(224,224,3)
。 那么如何将输入图像的形状转换为模型的输入形状?
当我尝试预测它时,这是我收到的错误:
ValueError: Error when checking input: expected flatten_1_input to have a shape (3, 3, 512) but got array with shape (224, 224, 3)
如何更改必须预测的模型的输入形状或输入图像的输入形状?
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 base的输出),而不是图像。 因此,您必须正确设置input_shape
参数:
model = Sequentail()
model.add(Flatten(input_shape=(3,3,512))
# the rest of the layers
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.