解决分割任务时,喀拉拉邦的度量“准确性”抛出错误“形状不兼容”

时间:2018-12-18 13:56:10

标签: keras deep-learning dataset

我使用Keras来完成细分任务。
groundtruth贴图是二进制图像,请注意,黑色表示背景,白色表示对象。
因此,我在Keras中使用datagenerator如下,在此之前,我将0设置为背景像素,将1设置为对象:

train_label_datagen = ImageDataGenerator(
    horizontal_flip= True,
)
train_label_generaror = train_label_datagen.flow_from_directory(
    directory=os.path.join(FLAGS.dataset_dir, FLAGS.cropped_dir, "training",
                           FLAGS.labels_folder_name),
    target_size=(FLAGS.resize_size, FLAGS.resize_size),
    class_mode= None, 
    color_mode= 'grayscale',
    batch_size= FLAGS.batch_size,
    seed = FLAGS.seed,
    shuffle= False
)  

model.compile函数的详细信息如下:

sgd = SGD(lr= FLAGS.lr, decay=FLAGS.decay, momentum=FLAGS.momentum, 
          nesterov=True)
model.compile(optimizer= sgd,
              loss = losses.sparse_categorical_crossentropy,
              metrics=['acc', miou])  

model.output是一个概率图。
我的问题是:训练模型时出现错误

tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [602112] vs. [12,224,224]
 [[Node: metrics/acc/Equal = Equal[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](metrics/acc/Reshape, metrics/acc/Cast)]]
 [[Node: metrics/miou/confusion_matrix/assert_non_negative_1/assert_less_equal/Assert/AssertGuard/Assert/Switch_1/_167 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_544_m...t/Switch_1", tensor_type=DT_INT64, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]  

我尝试将损失更改为“ binary_crossentropy”,并将output.activation更改为“ Sigmoid”,然后它起作用了。
所以我想知道是什么导致了这个错误。

1 个答案:

答案 0 :(得分:0)

通过尝试不同的方式,并分析了 confusion_matrix ,我已经解决了问题。
代码如下:

import keras.backend as K
from tensorflow.contrib.metrics import confusion_matrix
def custom_accuracy(y_true, y_pred, nb_classes):
    class_labels= K.reshape(y_true, shape=[-1])
    class_pred = K.resshape(y_pred, axis= -1)
    class_pred = K.reshape(class_pred, shape=[-1])

    confusion_matrix = confusion_matrix(labels= class_labels,
                                        prediction= class_pred,
                                        num_classes= nb_classes)
    TP = 0
    all = 0
    for class_index in range(0, nb_classes):
        TP += confusion_matrix[class_index, class_index]
        all += confusion_matrix[class_index]

    return K.sum(TP) / K.sum(all)  

通过自定义的准确性指标,它可以正常工作。