5

我正在尝试实现一个自定义损失函数,该函数给出加权像素交叉熵值。

我已经用“sample_weight_mode='temporal'”编译了模型,并为每个单独的图像传递了不同的权重。为此,我有以下内容:

model.fit_generator(train_generator,...)

其中,train_generator 产生具有以下维度的 (input_image, target_label, weight) 元组:

input_image  ->  (48, 64, 64, 1)
target_label ->  (196608, 1)
weight       ->  (196608,)

是否可以实现一个损失函数,获得这个相应的权重作为参数?谁能告诉我是否可以实现如下所示的功能?

def pixelwise_cross_entropy(target, output, weight):
    #~ target = tf.convert_to_tensor(target, dtype=tf.float32)
    #~ output = tf.convert_to_tensor(output, dtype=tf.float32)
    _EPSILON = 10e-8

    output /= tf.reduce_sum(output, axis=len(output.get_shape()) - 1, keep_dims=True)

    epsilon = tf.cast(tf.convert_to_tensor(_EPSILON), output.dtype.base_dtype)
    output = tf.clip_by_value(output, epsilon, 1. - epsilon)

    return - tf.reduce_sum(target * tf.log(output) * weight, axis=len(output.get_shape()) - 1)

权重图是目标的形态侵蚀版本。我试图在自定义损失函数本身中创建权重图。由于我的数据是 3D,我不能使用 tensorflow 的“tf.nn.erosion2d”。

而且我无法将张量对象转换为 numpy 数组来执行此操作,因为它给出了以下负维度错误。

tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape [-1,-1,-1] has negative dimensions
 [[Node: mask_target = Placeholder[dtype=DT_FLOAT, shape=[?,?,?], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

如果您对此有所了解或遇到类似问题,有人可以帮忙吗?

4

0 回答 0