我正在尝试使用ResNet50预训练的网络来解决细分问题。 我删除最后一层并添加所需的层。但是当我尝试适应时,出现以下错误:
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'])
答案 0 :(得分: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的信息,了解您缺少的内容。