gpt4 book ai didi

python - 训练 uNet 模型预测只有黑色

转载 作者:行者123 更新时间:2023-12-05 07:15:39 27 4
gpt4 key购买 nike

我正在训练用于分割的 uNet 模型。训练模型后,输出全为零,我不明白为什么。

我看到建议我应该使用特定的损失函数,所以我使用了 dice 损失函数。这是因为黑色区域 (0) 比白色区域 (1) 大得多。

我做错了什么吗?

我的模型是:

Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
input_1 (InputLayer) (None, 80, 80, 1) 0
__________________________________________________________________________________________________
conv2d_1 (Conv2D) (None, 80, 80, 64) 640 input_1[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D) (None, 80, 80, 64) 36928 conv2d_1[0][0]
__________________________________________________________________________________________________
max_pooling2d_1 (MaxPooling2D) (None, 40, 40, 64) 0 conv2d_2[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D) (None, 40, 40, 128) 73856 max_pooling2d_1[0][0]
__________________________________________________________________________________________________
conv2d_4 (Conv2D) (None, 40, 40, 128) 147584 conv2d_3[0][0]
__________________________________________________________________________________________________
max_pooling2d_2 (MaxPooling2D) (None, 20, 20, 128) 0 conv2d_4[0][0]
__________________________________________________________________________________________________
conv2d_5 (Conv2D) (None, 20, 20, 256) 295168 max_pooling2d_2[0][0]
__________________________________________________________________________________________________
conv2d_6 (Conv2D) (None, 20, 20, 256) 590080 conv2d_5[0][0]
__________________________________________________________________________________________________
max_pooling2d_3 (MaxPooling2D) (None, 10, 10, 256) 0 conv2d_6[0][0]
__________________________________________________________________________________________________
conv2d_7 (Conv2D) (None, 10, 10, 512) 1180160 max_pooling2d_3[0][0]
__________________________________________________________________________________________________
conv2d_8 (Conv2D) (None, 10, 10, 512) 2359808 conv2d_7[0][0]
__________________________________________________________________________________________________
dropout_1 (Dropout) (None, 10, 10, 512) 0 conv2d_8[0][0]
__________________________________________________________________________________________________
max_pooling2d_4 (MaxPooling2D) (None, 5, 5, 512) 0 dropout_1[0][0]
__________________________________________________________________________________________________
conv2d_9 (Conv2D) (None, 5, 5, 1024) 4719616 max_pooling2d_4[0][0]
__________________________________________________________________________________________________
conv2d_10 (Conv2D) (None, 5, 5, 1024) 9438208 conv2d_9[0][0]
__________________________________________________________________________________________________
dropout_2 (Dropout) (None, 5, 5, 1024) 0 conv2d_10[0][0]
__________________________________________________________________________________________________
conv2d_transpose_1 (Conv2DTrans (None, 10, 10, 512) 2097664 dropout_2[0][0]
__________________________________________________________________________________________________
concatenate_1 (Concatenate) (None, 10, 10, 1024) 0 dropout_1[0][0]
conv2d_transpose_1[0][0]
__________________________________________________________________________________________________
conv2d_11 (Conv2D) (None, 10, 10, 512) 4719104 concatenate_1[0][0]
__________________________________________________________________________________________________
conv2d_12 (Conv2D) (None, 10, 10, 512) 2359808 conv2d_11[0][0]
__________________________________________________________________________________________________
conv2d_transpose_2 (Conv2DTrans (None, 20, 20, 256) 524544 conv2d_12[0][0]
__________________________________________________________________________________________________
concatenate_2 (Concatenate) (None, 20, 20, 512) 0 conv2d_6[0][0]
conv2d_transpose_2[0][0]
__________________________________________________________________________________________________
conv2d_13 (Conv2D) (None, 20, 20, 256) 1179904 concatenate_2[0][0]
__________________________________________________________________________________________________
conv2d_14 (Conv2D) (None, 20, 20, 256) 590080 conv2d_13[0][0]
__________________________________________________________________________________________________
conv2d_transpose_3 (Conv2DTrans (None, 40, 40, 128) 131200 conv2d_14[0][0]
__________________________________________________________________________________________________
concatenate_3 (Concatenate) (None, 40, 40, 256) 0 conv2d_4[0][0]
conv2d_transpose_3[0][0]
__________________________________________________________________________________________________
conv2d_15 (Conv2D) (None, 40, 40, 128) 295040 concatenate_3[0][0]
__________________________________________________________________________________________________
conv2d_16 (Conv2D) (None, 40, 40, 128) 147584 conv2d_15[0][0]
__________________________________________________________________________________________________
conv2d_transpose_4 (Conv2DTrans (None, 80, 80, 64) 32832 conv2d_16[0][0]
__________________________________________________________________________________________________
concatenate_4 (Concatenate) (None, 80, 80, 128) 0 conv2d_2[0][0]
conv2d_transpose_4[0][0]
__________________________________________________________________________________________________
conv2d_17 (Conv2D) (None, 80, 80, 64) 73792 concatenate_4[0][0]
__________________________________________________________________________________________________
conv2d_18 (Conv2D) (None, 80, 80, 64) 36928 conv2d_17[0][0]
__________________________________________________________________________________________________
conv2d_19 (Conv2D) (None, 80, 80, 2) 1154 conv2d_18[0][0]
__________________________________________________________________________________________________
conv2d_20 (Conv2D) (None, 80, 80, 1) 3 conv2d_19[0][0]
==================================================================================================

损失函数

def dice_loss_v2(y_true, y_pred):
numerator = 2 * tf.reduce_sum(y_true * y_pred, axis=(1,2,3))
denominator = tf.reduce_sum(y_true + y_pred, axis=(1,2,3))

return 1 - numerator / denominator

激活

    model.compile(optimizer='adam',
loss=dice_loss_v2,
metrics=['accuracy', iou_loss_core])

预定义的学习率是 LR=0.001

额外的信息:

        datagen = ImageDataGenerator(
rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1)
datagen.fit(X_train)

model.fit_generator(datagen.flow(X_train, y_train, batch_size=100), steps_per_epoch=len(X_train),
epochs=4, validation_data=(X_test, y_test))

最佳答案

由于像素预测为 0 和 1,掩码可能全黑。因为 0 和 1 在颜色空间 (0,255) 中都接近黑色。因此,将掩码乘以 255,然后尝试保存/显示它(所有 1 都转换为 255)。您将获得所需的输出。

关于python - 训练 uNet 模型预测只有黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59555793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com