分割损失函数
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
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 thumbnn.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)
, whiletarget
tensor shape is expected to be(N, H, W)
.