分割损失函数

发布于 2025-01-31 13:21:23 字数 479 浏览 4 评论 0原文

pred = model(x)['out']
loss_value=loss(pred, target.squeeze(1))

嗨,我正在尝试从Pytorch训练DeepLabv3_Resnet50进行两堂课(背景和狗只是为了尝试更好地预测)。据我所知,pred给了我们形状的张量:(批处理,num_classes,高度,宽度)。现在,我需要选择一个损失功能:例如,它将是Torch.nn.Crossentropyloss。它需要原始输入,只有一个分段掩码,其中所有类都具有其值。那么,为什么只有两个类别的一个掩码,如果还有更多的“两个”类,该怎么办?我认为它需要两个口罩,背景为1和狗0,反之亦然。 Crossentropyloss如何使用?也许这将有助于向我解释。

PS。我问这个问题是因为

pred = model(x)['out']
loss_value=loss(pred, target.squeeze(1))

Hi, i am trying to train deeplabv3_resnet50 from pytorch for two classes (background and dog just to try make predictions better). As i understand pred gives us tensor with shape: (batch, num_classes, height, width). Now i need to choose a loss function: for example it will be torch.nn.CrossEntropyLoss. It needs raw input, and only ONE segmentation mask with all classes with their values. So, why it's only one mask for two classes, what if there is more then 'two' classes? I thought it needs two masks, where background is 1 and dog 0, and vice versa. How CrossEntropyLoss works with this? Maybe this will help to explain it to me.

PS. I asked this question because DiceBCELoss instead of one segmentation mask wants two as i understand

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2025-02-07 13:21:23

使用 nn.crossentropyloss,您必须以密集格式提供标签,即: 不是 以单速编码格式,这是您在插图中显示的内容目标张量。作为一般的经验法则,nn.Crossentropyloss希望目标的一个维度小于预测张量。也就是说,它包含所有维度的大小完全相同的大小,但包含与每个类相对应的logits值的尺寸。换句话说,目标张量包含标签整数值,其中预测张量具有概率图。例如,对于2D预测,预测张量(n,c,c,h,w),而target tensor张量形状为(n,h,w)

When using nn.CrossEntropyLoss, you are required to provide the labels in dense format, that is: not in one-hot-encoding format, which is what you've shown in your illustration for the target tensor. As a general rule of thumb nn.CrossEntropyLoss expects the target to have one dimension less than the prediction tensor. That is it contains exactly the same sizes for all dimensions but the one that contains the logits value corresponding to each class. In other words, the target tensor contains the label integer value where the prediction tensor has the probability maps. For example for 2D prediction, prediction tensor shape (N, C, H, W), while target tensor shape is expected to be (N, H, W).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文