keras预先训练了ResNet50目标形状

问题描述 投票:0回答:1

我正在尝试使用ResNet50 Pretrained网络进行分段问题。我删除了最后一层并添加了我想要的图层。但是当我尝试适应时,我收到以下错误:

ValueError:检查目标时出错:预期conv2d_1具有形状(16,16,1)但是具有形状的数组(512,512,1)

我有两个文件夹:图像和面具。图像为RGB,掩模为灰度。所有图像的形状均为512x512。我无法确定我做错了哪一部分。

任何帮助将不胜感激。

from keras.applications.resnet50 import ResNet50
image_input=Input(shape=(512, 512, 3))
model = ResNet50(input_tensor=image_input,weights='imagenet',include_top=False)
x = model.output
x = Conv2D(1, (1,1), padding="same", activation="sigmoid")(x)
model = Model(inputs=model.input, outputs=x)
model.summary()

conv2d_1 (Conv2D)           (None, 16, 16, 1)    2049 activation_49[0][0]              

for layer in model.layers[:-1]:
    layer.trainable = False

for layer in model.layers[-1:]:
    layer.trainable = True
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
keras keras-layer
1个回答
1
投票

你的网络提供了形状(16, 16, 1)的输出,但你的y(目标)有形状(512, 512, 1)

运行以下命令以查看此内容。

from keras.applications.resnet50 import ResNet50
from keras.layers import Input

image_input=Input(shape=(512, 512, 3))
model = ResNet50(input_tensor=image_input,weights='imagenet',include_top=False)
model.summary()

# Output shows that the ResNet50 network has output of shape (16,16,2048)

from keras.layers import Conv2D

conv2d = Conv2D(1, (1,1), padding="same", activation="sigmoid")
conv2d.compute_output_shape((None, 16, 16, 2048))

# Output shows the shape your network's output will have.

您的y或您使用ResNet50的方式必须改变。阅读有关ResNet50的信息,了解您所缺少的内容。

© www.soinside.com 2019 - 2024. All rights reserved.