【问题标题】:Pre-trained CNN for multi-output models用于多输出模型的预训练 CNN
【发布时间】:2021-10-13 03:18:25
【问题描述】:

如何将Keras Applications 用于多输出模型?

我正在尝试将 VGG16 用于多输出模型,但出现值错误 ValueError: Input 0 of layer block1_conv1 is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 64, 64, 1)。我有点迷茫,似乎在 VGG16 或任何其他 Keras 预训练应用程序的多输出上找不到任何东西。

我以此为指导:Transfer Learning in Keras with Computer Vision

# load model without classifier layers
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(IMG_HEIGHT, IMG_WIDTH,3))
# add new classifier layers
sex_x = base_model.output
sex_x = GlobalAveragePooling2D()(sex_x)
sex_x = Dropout(0.5)(gender_x)
output_sex = Dense(2, activation='sigmoid')(sex_x)

weight_x = base_model.output
weight_x = GlobalAveragePooling2D()(weight_x)
weight_x = Dropout(0.5)(weight_x)
output_weight = Dense(1, activation='linear')(weight_x)
# define new model
model = Model(inputs=base_model.inputs, outputs=[output_sex, output_weight])

model.compile(optimizer = 'adam', loss =['binary_crossentropy','mae',],metrics=['accuracy'])
history = model.fit(x_train,[y_train[:,0],y_train[:,1]],validation_data=(x_test,[y_test[:,0],y_test[:,1]]),epochs = 10, batch_size=128,shuffle = True)

关于我做错了什么有什么想法吗? `

【问题讨论】:

    标签: keras conv-neural-network image-classification


    【解决方案1】:

    根据此错误,您的模型输入询问3 channel RGB,但您的数据似乎在1 channel(灰度)中。确保您的数据具有 RGB 图像。

    【讨论】:

    • 您可以添加tf.image.grayscale_to_rgb 可用于将1 chanel 灰度转换为3 chanel RGB 的答案
    • 谢谢。这解决了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 2018-09-25
    • 2020-07-22
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    相关资源
    最近更新 更多