在Inception V3模型的顶部添加卷积层

时间:2020-05-20 19:15:17

标签: python tensorflow machine-learning keras multilabel-classification

我需要使用Keras的Inception V3模型训练图像分类器。在进入预训练的Inception V3模型之前,图像会经过5个Conv2D层和2个MaxPool2D层。但是我的代码给我一个ValueError: Depth of input (64) is not a multiple of input depth of filter (3) for 'inception_v3_4/conv2d_123/convolution' (op: 'Conv2D') with input shapes: [?,2,2,224], [3,3,3,32]

的错误

我认为先前图层的输出形状与Inception所需的输入形状不兼容。但是我无法解决它,或者甚至有可能解决这个错误。我是机器学习的初学者,在这方面的任何见解将不胜感激。

我的代码如下:

inception_model = inception_v3.InceptionV3(weights='imagenet', include_top = False)

for layer in inception_model.layers:
    layer.trainable = False

input_layer = Input(shape=(224,224,3)) #Image resolution is 224x224 pixels
x = Conv2D(128, (7, 7), padding='same', activation='relu', strides=(2, 2))(input_layer)
x = Conv2D(128, (7, 7), padding='same', activation='relu', strides=(2, 2))(x)
x = Conv2D(64, (7, 7), padding='same', activation='relu', strides=(2, 2))(x)
x = MaxPool2D((3, 3), padding='same',strides=(2, 2))(x)
x = Conv2D(64, (7, 7), padding='same', activation='relu', strides=(2, 2))(x)
x = Conv2D(64, (7, 7), padding='same', activation='relu', strides=(2, 2))(x)
x = MaxPool2D((4, 4), padding='same', strides=(2, 2))(x)

x = inception_model (x) #Error in this line
x = GlobalAveragePooling2D()(x)

predictions = Dense(11, activation='softmax')(x) #I have 11 classes of image to classify

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

model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['acc'])
model.summary()

1 个答案:

答案 0 :(得分:1)

就像@CAFEBABE一样,这样做几乎没有用,因为要素地图可以具有几乎3个值,但是如果您仍然想尝试,则可以执行以下操作:

x = Conv2D(3, (7, 7), padding='same', activation='relu', strides=(2, 2))(input_layer)

您还需要记住的另一件事是,就像您在上面使用了5个Conv2D图层和2个MaxPooling图层一样,但是您不能这样做,因为即使在Inception模型中,也存在Conv2D和max-pooling图层,这些图层会将尺寸缩小为负数。并给出一个错误。我尝试使用2个Conv2D图层,但出现错误,因此最多可以使用1。

还要在指定InceptionV3模型时指定输入形状。

input_layer = Input(shape=(224,224,3)) #Image resolution is 224x224 pixels
x = Conv2D(128, (7, 7), padding='same', activation='relu', strides=(2, 2))(input_layer)

inception_model = tf.keras.applications.InceptionV3(weights='imagenet', include_top = False, input_shape=x.shape[0])
for layer in inception_model.layers:
    layer.trainable = False

x = inception_model (x)
x = GlobalAveragePooling2D()(x)

predictions = Dense(11, activation='softmax')(x) #I have 11 classes of image to classify

model = Model(inputs = input_layer, outputs=predictions)
model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['acc'])
model.summary()

这会起作用,但我怀疑它会帮助模型。无论如何尝试谁知道会发生什么。