语义分割损失函数系列(2):IoU损失
生活随笔
收集整理的這篇文章主要介紹了
语义分割损失函数系列(2):IoU损失
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
交叉熵損失函數好用是好用,但當數據存在嚴重的不平衡問題的時候,會導致結果特別糟糕,特別是在醫學圖像分割問題中,一般目標區域都比較小,背景區域比較大,此時用交叉熵損失函數的話得到的結果就不太好。
IOU loss介紹
IOU即是交并比,用相交的部分去除上并集,就得到IOU的值,1-IOU的值就是IOU Loss。至于IOU的數學定義去看百度百科吧,舉個例子:
上面兩張圖求IOU,白色區域為目標區域,就是用兩張圖片的白色區域的交集去比上白色部分的并集,就得到了白色類別的IOU值。在實際工程中,一般黑色像素為類別0,白色為類別1。可以使用代碼輕松的求出IOU值。
Pytorch代碼
import numpy import torch import torch.nn as nn import torch.nn.functional as F class IoULoss(nn.Module):def __init__(self, weight=None, size_average=True):super(IoULoss, self).__init__()def forward(self, inputs, targets, smooth=1):#comment out if your model contains a sigmoid or equivalent activation layerinputs = F.sigmoid(inputs) #flatten label and prediction tensorsinputs = inputs.view(-1)targets = targets.view(-1)#intersection is equivalent to True Positive count#union is the mutually inclusive area of all labels & predictions intersection = (inputs * targets).sum()total = (inputs + targets).sum()union = total - intersection IoU = (intersection + smooth)/(union + smooth)return 1 - IoU總結
以上是生活随笔為你收集整理的语义分割损失函数系列(2):IoU损失的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pytorch使用Vision Tran
- 下一篇: 怎么判断日出时间早晚_早晚都要擦精华液?