Tensorflow VGG19错误:值错误:形状(无、128、128、10)和(无、10)不兼容

Tensorflow VGG19错误:值错误:形状(无、128、128、10)和(无、10)不兼容,tensorflow,keras,neural-network,conv-neural-network,vgg-net,Tensorflow,Keras,Neural Network,Conv Neural Network,Vgg Net,我尝试使用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或1。这已经是一个热门话题了 我使用以下模型架构: model = VGG

我尝试使用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或1
。这已经是一个热门话题了

我使用以下模型架构:

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

代码中的概念错误。像素分类要求输出大小与输入大小匹配,即输出层应为
128x128x10
。使用
conv2dtranpse
我们可以将采样回
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)