图像分割-Keras中的自定义损失函数 [英] Image segmentation - custom loss function in Keras

查看:258
本文介绍了图像分割-Keras中的自定义损失函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Keras实现的U-Net( https://arxiv.org/pdf/1505.04597.pdf )在显微镜图像中分割细胞器.为了使我的网络能够识别仅相隔1个像素的多个单个对象,我想对每个标签图像使用权重图(公式在出版物中给出).

I am using a in Keras implemented U-Net (https://arxiv.org/pdf/1505.04597.pdf) to segment cell organelles in microscopy images. In order for my network to recognize multiple single objects that are separated by only 1 pixel, I want to use weight maps for each label image (formula is given in publication).

据我所知,我必须创建自己的自定义损失函数(在我的情况下为交叉熵)以利用这些权重图.但是,自定义损失函数仅接受两个参数.如何在这样的函数中添加权重图值?

As far as I know, I have to create my own custom loss function (in my case crossentropy) to make use of these weight maps. However, the custom loss function only takes in two parameters. How can I add the weight map values in such a function?

以下是我的自定义损失函数的代码:

Below is the code for my custom loss function:

def pixelwise_crossentropy(self, ytrue, ypred):

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

    # manual computation of crossentropy
    _epsilon = tf.convert_to_tensor(epsilon, ypred.dtype.base_dtype)
    output = tf.clip_by_value(ypred, _epsilon, 1. - _epsilon)

    return - tf.reduce_sum(ytrue * tf.log(output))

是否有任何方法可以将权重图值与ytrue张量中的标签值组合在一起?

Is there any way to combine the weight map values together with the label values in the ytrue tensor?

如我所说,如果这个问题看起来很愚蠢,我深表歉意,我是这个游戏的新手.任何帮助或建议,将不胜感激!

I apologize if this questions seem stupid, as I said, I'm relatively new to the game. Any help or suggestions would be greatly appreciated!

推荐答案

如果您尝试实现二进制交叉熵加权损失,则可以使用张量流内置损失函数

if you are trying to implement binary cross entropy weighted loss you can use tensor flow inbuilt loss function

pos_weight = tf.constant([[1.0, 2.0]])
tensorflow.nn.weighted_cross_entropy_with_logits(y_true,
    y_pred,
    pos_weight,
    name=None) 

查看文档 https://www.tensorflow.org/api_docs/python/tf/nn/weighted_cross_entropy_with_logits

喀拉拉邦实现

def pixel_wise_loss(y_true, y_pred):
    pos_weight = tf.constant([[1.0, 2.0]])
    loss = tf.nn.weighted_cross_entropy_with_logits(
        y_true,
        y_pred,
        pos_weight,
        name=None
    )

    return K.mean(loss,axis=-1)

如果您尝试实现softmax_cross_entropy_with_logits,则请遵循指向先前说明的链接 softmax_cross_entropy_with_logits

if you are trying to implement softmax_cross_entropy_with_logits then follow the link to previous where it is explained softmax_cross_entropy_with_logits

这篇关于图像分割-Keras中的自定义损失函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆