我正在尝试使用 VGG-19 模型作为语义分割模型,即像素级分类。我准备好了以下数据集:
x_trn.shape, y_trn.shape, x_val.shape, y_val.shape
((3883, 128, 128, 3),
(3883, 128, 128, 10),
(1237, 128, 128, 3),
(1237, 128, 128, 10))
我有 3 个输入图像通道,输出有 10 个可能的类别,每个类别的值可以是 0 or 1
。已经是 one-hot 编码了
我正在使用以下模型架构:
model = VGG19(include_top=False,
weights=None,
input_tensor=Input(shape=(128,128,3)))
headModel = model.output
headModel = AveragePooling2D(pool_size=(4, 4))(headModel)
headModel = Flatten(name="flatten")(headModel)
headModel = Dense(128, activation="relu")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Dense(10, activation="softmax")(headModel)
combined_model = Model(inputs=model.input, outputs=headModel)
combined_model.compile(Adam(0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
模型摘要:
输入:
输出:
我不确定这里有什么问题,但这会导致形状不匹配错误。
combined_model.fit(x=x_trn,
y=y_trn,
batch_size=10,
epochs=10,
verbose=1,
callbacks=callbacks,
validation_data=(x_val, y_val),
shuffle=True)
/home/ubuntu/anaconda3/envs/tensorflow2_latest_p37/gpu/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:201 wrapper
return target(*args, **kwargs)
/home/ubuntu/anaconda3/envs/tensorflow2_latest_p37/gpu/lib/python3.7/site-packages/tensorflow/python/keras/backend.py:4687 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/home/ubuntu/anaconda3/envs/tensorflow2_latest_p37/gpu/lib/python3.7/site-packages/tensorflow/python/framework/tensor_shape.py:1134 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 128, 128, 10) and (None, 10) are incompatible
答案 0 :(得分:0)
代码中的概念错误。逐像素分类需要输出大小与输入大小匹配,即输出层应为 128x128x10
。使用 Conv2DTranspose
我们可以上采样回 128x128
我做了一些这样的改变:
model = VGG19(include_top=False,
weights=None,
input_tensor=Input(shape=(128,128,3)))
headModel = model.output
headModel = Conv2DTranspose(512, (3, 3), strides=(2, 2), padding="same")(headModel)
headModel = Dropout(0.2)(headModel)
headModel = Conv2DTranspose(256, (3, 3), strides=(2, 2), padding="same")(headModel)
headModel = Dropout(0.5)(headModel)
headModel = Conv2DTranspose(128, (3, 3), strides=(2, 2), padding="same")(headModel)
headModel = Dropout(0.7)(headModel)
headModel = Conv2DTranspose(64, (3, 3), strides=(2, 2), padding="same")(headModel)
headModel = Dropout(0.8)(headModel)
headModel = Conv2DTranspose(32, (3, 3), strides=(2, 2), padding="same")(headModel)
headModel = Conv2D(10, (1,1), activation = 'softmax')(headModel)
combined_model = Model(inputs=model.input, outputs=headModel)