Path: blob/master/Bag-Of-Tricks-For-Image-Classification/model/losses.py
3442 views
# MIT License1# Copyright (c) 2018 Haitong Li234import torch5import torch.nn as nn6import torch.nn.functional as F789# Based on https://github.com/peterliht/knowledge-distillation-pytorch/blob/master/model/net.py10class KnowledgeDistillationLoss(nn.Module):11def __init__(self, alpha, T, criterion):12super().__init__()13self.criterion = criterion14self.KLDivLoss = nn.KLDivLoss(reduction="batchmean")15self.alpha = alpha16self.T = T1718def forward(self, input, target, teacher_target):19loss = self.KLDivLoss(20F.log_softmax(input / self.T, dim=1),21F.softmax(teacher_target / self.T, dim=1),22) * (self.alpha * self.T * self.T) + self.criterion(input, target) * (231.0 - self.alpha24)25return loss262728class MixUpAugmentationLoss(nn.Module):29def __init__(self, criterion):30super().__init__()31self.criterion = criterion3233def forward(self, input, target, *args):34# Validation step35if isinstance(target, torch.Tensor):36return self.criterion(input, target, *args)37target_a, target_b, lmbd = target38return lmbd * self.criterion(input, target_a, *args) + (391 - lmbd40) * self.criterion(input, target_b, *args)414243# Based on https://github.com/pytorch/pytorch/issues/745544class LabelSmoothingLoss(nn.Module):45def __init__(self, n_classes, smoothing=0.0, dim=-1):46super(LabelSmoothingLoss, self).__init__()47self.confidence = 1.0 - smoothing48self.smoothing = smoothing49self.cls = n_classes50self.dim = dim5152def forward(self, output, target, *args):53output = output.log_softmax(dim=self.dim)54with torch.no_grad():55# Create matrix with shapes batch_size x n_classes56true_dist = torch.zeros_like(output)57# Initialize all elements with epsilon / N - 158true_dist.fill_(self.smoothing / (self.cls - 1))59# Fill correct class for each sample in the batch with 1 - epsilon60true_dist.scatter_(1, target.data.unsqueeze(1), self.confidence)61return torch.mean(torch.sum(-true_dist * output, dim=self.dim))626364