文章目錄[隱藏]
「語義分割」介紹
語義(yi) 分割是計算機視覺領域中的一項任務,旨在將圖像中的每個(ge) 像素分類為(wei) 不同的語義(yi) 類別。與(yu) 對象檢測任務不同,語義(yi) 分割不僅(jin) 需要識別圖像中的物體(ti) ,還需要對每個(ge) 像素進行分類,從(cong) 而實現對圖像的細粒度理解和分析。
語義(yi) 分割可以被看作是像素級別的圖像分割,其目標是為(wei) 圖像中的每個(ge) 像素分配一個(ge) 特定的語義(yi) 類別標簽。每個(ge) 像素都被視為(wei) 圖像的基本單位,因此語義(yi) 分割可以提供更詳細和準確的圖像分析結果。
「語義分割」vs「分類」
- 在語義分割任務中,由於需要對每個像素進行分類,因此需要使用像素級別的損失函數。
- 語義分割任務中,圖像中各個類別的像素數量通常不均衡,例如背景像素可能占據了大部分。
- 語義分割任務需要對圖像中的每個像素進行分類,同時保持空間連續性。
「語義分割」損失函數
Dice Loss
Dice Loss(Dice損失)是一種常用的損失函數,主要用於(yu) 語義(yi) 分割任務中,衡量模型生成分割結果與(yu) 真實分割標簽之間的相似度。它基於(yu) Dice係數(Dice coefficient),也稱為(wei) Sørensen-Dice係數,用於(yu) 度量兩(liang) 個(ge) 集合的相似性。
Dice Loss的取值範圍為(wei) 0到1之間。當預測結果與(yu) 真實標簽完全一致時,Dice Loss為(wei) 0;當兩(liang) 者完全不一致時,Dice Loss為(wei) 1。因此,Dice Loss越小表示模型的分割結果與(yu) 真實標簽越相似,表示模型性能越好。
class DiceLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(DiceLoss, self).__init__() def forward(self, inputs, targets, smooth=1): #comment out if your model contains a sigmoid or equivalent activation layer inputs = F.sigmoid(inputs) #flatten label and prediction tensors inputs = inputs.view(-1) targets = targets.view(-1) intersection = (inputs * targets).sum() dice = (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth) return 1 - dice
BCE-Dice Loss
BCE-Dice Loss是將Dice Loss和標準的二元交叉熵(Binary Cross-Entropy, BCE)損失結合在一起的一種損失函數,通常用於(yu) 分割模型中。
BCE-Dice Loss的優(you) 點在於(yu) ,它結合了兩(liang) 種不同的損失函數,可以綜合考慮像素級別的分類準確性(通過BCE損失)和分割結果的相似性(通過Dice Loss)。BCE損失在訓練初期具有較好的穩定性,有助於(yu) 加快模型的收斂速度。而Dice Loss則更關(guan) 注於(yu) 像素級別的相似性,可以促使模型生成更平滑和連續的分割結果。
class DiceBCELoss(nn.Module): def __init__(self, weight=None, size_average=True): super(DiceBCELoss, self).__init__() def forward(self, inputs, targets, smooth=1): #comment out if your model contains a sigmoid or equivalent activation layer inputs = F.sigmoid(inputs) #flatten label and prediction tensors inputs = inputs.view(-1) targets = targets.view(-1) intersection = (inputs * targets).sum() dice_loss = 1 - (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth) BCE = F.binary_cross_entropy(inputs, targets, reduction='mean') Dice_BCE = BCE + dice_loss return Dice_BCE
Jaccard/Intersection over Union (IoU) Loss
Jaccard Loss,也稱為(wei) Intersection over Union (IoU) Loss,是一種常用的損失函數,用於(yu) 語義(yi) 分割任務中評估模型的分割結果與(yu) 真實分割標簽之間的相似性。它基於(yu) Jaccard指數(Jaccard Index),也稱為(wei) IoU指標,用於(yu) 度量兩(liang) 個(ge) 集合之間的重疊程度。
Jaccard Loss的取值範圍為(wei) 0到1之間。當預測結果與(yu) 真實標簽完全一致時,Jaccard Loss為(wei) 0;當兩(liang) 者完全不一致時,Jaccard Loss為(wei) 1。因此,Jaccard Loss越小表示模型的分割結果與(yu) 真實標簽越相似,表示模型性能越好。
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 layer inputs = F.sigmoid(inputs) #flatten label and prediction tensors inputs = 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
Focal Loss
Focal Loss是一種用於(yu) 解決(jue) 分類任務中類別不平衡問題的損失函數。它被廣泛用於(yu) 目標檢測任務中,特別是在處理少數類別樣本較多的情況下。
Focal Loss基於(yu) 交叉熵損失進行擴展,將樣本的權重進行動態調整。與(yu) 交叉熵損失函數相比,Focal Loss引入了一個(ge) 衰減因子 ,其中 是預測的概率值。這個(ge) 衰減因子能夠使得易分類的樣本( 較高)的權重降低,從(cong) 而減少對分類正確樣本的貢獻。
ALPHA = 0.8 GAMMA = 2class FocalLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(FocalLoss, self).__init__() def forward(self, inputs, targets, alpha=ALPHA, gamma=GAMMA, smooth=1): #comment out if your model contains a sigmoid or equivalent activation layer inputs = F.sigmoid(inputs) #flatten label and prediction tensors inputs = inputs.view(-1) targets = targets.view(-1) #first compute binary cross-entropy BCE = F.binary_cross_entropy(inputs, targets, reduction='mean') BCE_EXP = torch.exp(-BCE) focal_loss = alpha * (1-BCE_EXP)gamma * BCE return focal_loss
Tversky Loss
Tversky Loss的設計靈感來自Tversky指數(Tversky index),它是一種用於(yu) 度量集合之間相似性的指標。Tversky Loss使用了兩(liang) 個(ge) 常數(alpha和beta),用於(yu) 調整在損失函數中對誤分類的懲罰程度。
當alpha=beta=0.5時,Tversky指數簡化為(wei) Dice係數,該係數也等於(yu) F1得分。當alpha=beta=1時,公式轉化為(wei) Tanimoto係數,而當alpha+beta=1時,得到一組F-beta得分。
ALPHA = 0.5 BETA = 0.5class TverskyLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(TverskyLoss, self).__init__() def forward(self, inputs, targets, smooth=1, alpha=ALPHA, beta=BETA): #comment out if your model contains a sigmoid or equivalent activation layer inputs = F.sigmoid(inputs) #flatten label and prediction tensors inputs = inputs.view(-1) targets = targets.view(-1) #True Positives, False Positives & False Negatives TP = (inputs * targets).sum() FP = ((1-targets) * inputs).sum() FN = (targets * (1-inputs)).sum() Tversky = (TP + smooth) / (TP + alpha*FP + beta*FN + smooth) return 1 - Tversky
Lovasz Hinge Loss
Lovasz Hinge Loss的設計思想是,在計算IoU得分之前,根據預測誤差對預測結果進行排序,然後累積計算每個(ge) 誤差對IoU得分的影響。然後,將該梯度向量與(yu) 初始誤差向量相乘,以最大程度地懲罰降低IoU得分的預測結果。
https://github.com/bermanmaxim/LovaszSoftmax
Combo Loss
Combo Loss的設計目標是處理多器官分割任務中輸入和輸出不平衡的情況。它綜合了Dice Loss和修改後的交叉熵損失函數,以平衡對假陽性和假陰性的懲罰。該損失函數具有額外的常數,用於(yu) 調整對錯誤預測的懲罰程度。
ALPHA = 0.5 # < 0.5 penalises FP more, > 0.5 penalises FN more CE_RATIO = 0.5 #weighted contribution of modified CE loss compared to Dice loss class ComboLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(ComboLoss, self).__init__() def forward(self, inputs, targets, smooth=1, alpha=ALPHA, beta=BETA, eps=1e-9): #flatten label and prediction tensors inputs = inputs.view(-1) targets = targets.view(-1) #True Positives, False Positives & False Negatives intersection = (inputs * targets).sum() dice = (2. * intersection + smooth) / (inputs.sum() + targets.sum() + smooth) inputs = torch.clamp(inputs, eps, 1.0 - eps) out = - (ALPHA * ((targets * torch.log(inputs)) + ((1 - ALPHA) * (1.0 - targets) * torch.log(1.0 - inputs)))) weighted_ce = out.mean(-1) combo = (CE_RATIO * weighted_ce) - ((1 - CE_RATIO) * dice) return combo
損失函數選擇方法
-
任務需求:根據特定的分割任務的需求和特點,選擇適合的損失函數。例如,對於(yu) 類別不平衡的數據集,可以考慮使用Tversky Loss或Combo Loss等能夠處理不平衡情況的損失函數。
-
實驗評估:在實驗中,使用不同的損失函數進行訓練,並評估它們(men) 在驗證集或測試集上的性能。比較它們(men) 在IoU、準確率、召回率等指標上的表現,選擇性能最佳的損失函數。
-
超參數調整:一些損失函數具有額外的超參數,如Tversky Loss中的alpha和beta,可以通過調整這些超參數來進一步優(you) 化損失函數的性能。
評論已經被關(guan) 閉。