def get_reduction(m): result = getattr(m, 'reduction', None) if result is None: result = _Reduction.legacy_get_string(getattr(m, 'sizeAverage', None), True, emit_warning=False) assert result is not None return result
def ms_ssim_loss(input, target, max_val, filter_size=11, k1=0.01, k2=0.03, sigma=1.5, size_average=None, reduce=None, reduction='mean'): # type: (Tensor, Tensor, float, int, float, float, float, Optional[bool], Optional[bool], str) -> Tensor r"""ms_ssim_loss(input, target, max_val, filter_size, k1, k2, sigma, size_average=None, reduce=None, reduction='mean') -> Tensor Measures the multi-scale structural similarity index (MS-SSIM) error. See :class:`~torch.nn.MSSSIMLoss` for details. """ dim = input.dim() if dim != 4: raise ValueError( 'Expected 4 dimensions (got {}) from input'.format(dim)) if input.size() != target.size(): raise ValueError( 'Expected input size ({}) to match target size ({}).'.format( input.size(0), target.size(0))) if size_average is not None or reduce is not None: reduction = _Reduction.legacy_get_string(size_average, reduce) _, channel, _, _ = input.size() kernel = _fspecial_gaussian(filter_size, channel, sigma) weights = torch.tensor([0.0448, 0.2856, 0.3001, 0.2363, 0.1333]) weights = weights.unsqueeze(-1).unsqueeze(-1) levels = weights.size(0) mssim = [] mcs = [] for _ in range(levels): ssim, cs = _ssim(input, target, max_val, k1, k2, channel, kernel) ssim = ssim.mean((2, 3)) cs = cs.mean((2, 3)) mssim.append(ssim) mcs.append(cs) input = avg_pool2d(input, (2, 2)) target = avg_pool2d(target, (2, 2)) mssim = torch.stack(mssim) mcs = torch.stack(mcs) p1 = mcs**weights p2 = mssim**weights ret = torch.prod(p1[:-1], 0) * p2[-1] if reduction != 'none': ret = torch.mean(ret) if reduction == 'mean' else torch.sum(ret) return ret
def multilabel_nb_loss(nb_mat, input, target, weight=None, size_average=None, reduce=None, reduction='mean', scaling_c=nb_scale): # type: (Tensor, Tensor, Optional[Tensor], Optional[bool], Optional[bool], str) -> Tensor # multilabel_nb_loss(input, target, weight=None, size_average=None) -> Tensor if size_average is not None or reduce is not None: reduction = _Reduction.legacy_get_string(size_average, reduce) # An example of target labels: [person, horse, car] -> one-hot encoding # For each class present in the true label, look up P(all labels|class). # This corresponds to a full column in nb_mat (The printed, transposed # version has them as rows instead). # The loss for a particular class x decreases when there are occurrences # of other classes c. Therefore a high value of P(x|c) will contribute # to decreased loss for class x. loss = -(target * logsigmoid(input) + (1 - target) * logsigmoid(-input)) # For each sample, find the class indices (where target is 1). # If there is more than one class, update the loss via this equation: # loss = loss - nb_mat[c,x], where c is found in company of x. for row in range(target.shape[0]): class_indices = target[row].nonzero() if len(class_indices) > 1: for x in class_indices: for c in class_indices: if not torch.equal(x, c): loss[row, x] -= nb_mat[c, x] * scaling_c if weight is not None: loss = loss * weight loss = loss.sum(dim=1) / input.size(1) # only return N loss values if reduction == 'none': ret = loss elif reduction == 'mean': ret = loss.mean() elif reduction == 'sum': ret = loss.sum() else: ret = input raise ValueError(reduction + " is not valid") torch.set_printoptions(profile="default") return ret
def ssim_loss(input, target, max_val, filter_size=11, k1=0.01, k2=0.03, sigma=1.5, size_average=None, reduce=None, reduction='mean'): # type: (Tensor, Tensor, float, int, float, float, float, Optional[bool], Optional[bool], str) -> Tensor r"""ssim_loss(input, target, max_val, filter_size, k1, k2, sigma, size_average=None, reduce=None, reduction='mean') -> Tensor Measures the structural similarity index (SSIM) error. See :class:`~torch.nn.SSIMLoss` for details. """ if input.size() != target.size(): raise ValueError( 'Expected input size ({}) to match target size ({}).'.format( input.size(0), target.size(0))) if input.device != target.device: raise RuntimeError( f'The input device {input.device} and target device {target.device} do not match.' ) dim = input.dim() if dim == 2: # Expand dims if the inputs have too few of them. This does not copy data. input = input.expand(1, 1, input.dim(-2), input.dim(-1)) target = target.expand(1, 1, target.dim(-2), target.dim(-1)) elif dim == 3: input = input.expand(1, input.dim(-3), input.dim(-2), input.dim(-1)) target = target.expand(1, target.dim(-3), target.dim(-2), target.dim(-1)) elif dim != 4: raise ValueError('Expected 2, 3, or 4 dimensions (got {})'.format(dim)) if size_average is not None or reduce is not None: reduction = _Reduction.legacy_get_string(size_average, reduce) channel = input.size(1) kernel = _fspecial_gaussian(filter_size, channel, sigma, input.device) ret, _ = _ssim(input, target, max_val, k1, k2, channel, kernel) if reduction != 'none': ret = torch.mean(ret) if reduction == 'mean' else torch.sum(ret) return ret
def ssim_loss(input, target, max_val, filter_size=11, k1=0.01, k2=0.03, sigma=1.5, kernel=None, size_average=None, reduce=None, reduction='mean'): # type: (Tensor, Tensor, float, int, float, float, float, Tensor, Optional[bool], Optional[bool], str) -> Tensor r"""ssim_loss(input, target, max_val, filter_size, k1, k2, sigma, kernel=None, size_average=None, reduce=None, reduction='mean') -> Tensor Measures the structural similarity index (SSIM) error. See :class:`~torch.nn.SSIMLoss` for details. """ if input.size() != target.size(): raise ValueError( 'Expected input size ({}) to match target size ({}).'.format( input.size(0), target.size(0))) if size_average is not None or reduce is not None: reduction = _Reduction.legacy_get_string(size_average, reduce) dim = input.dim() if dim == 2: input = input.expand(1, 1, -1, -1) target = target.expand(1, 1, -1, -1) elif dim == 3: input = input.expand(1, -1, -1, -1) target = target.expand(1, -1, -1, -1) elif dim != 4: raise ValueError('Expected 2, 3, or 4 dimensions (got {})'.format(dim)) if kernel is None: kernel = _fspecial_gaussian(filter_size, sigma) kernel = kernel.to(device=input.device) ret = _ssim(input, target, max_val, k1, k2, kernel)[0] if reduction != 'none': ret = torch.mean(ret) if reduction == 'mean' else torch.sum(ret) return ret
def __init__(self, weight=None, size_average=True, batch_average=True, ignore_index=255, cuda=False, reduce=None): super(SegmentationCELosses, self).__init__() # if size_average is not None or reduce is not None: self.reduction = _Reduction.legacy_get_string(size_average, reduce) # else: # self.reduction = reduction self.ignore_index = ignore_index self.weight = weight self.size_average = size_average self.batch_average = batch_average self.cuda = cuda
def ms_ssim_loss(input, target, max_val, filter_size=11, k1=0.01, k2=0.03, sigma=1.5, size_average=None, reduce=None, reduction='mean'): # type: (Tensor, Tensor, float, int, float, float, float, Optional[bool], Optional[bool], str) -> Tensor r"""ms_ssim_loss(input, target, max_val, filter_size, k1, k2, sigma, size_average=None, reduce=None, reduction='mean') -> Tensor Measures the multi-scale structural similarity index (MS-SSIM) error. See :class:`~torch.nn.MSSSIMLoss` for details. """ if input.size() != target.size(): raise ValueError( 'Expected input size ({}) to match target size ({}).'.format( input.size(0), target.size(0))) if input.device != target.device: raise RuntimeError( f'The input device {input.device} and target device {target.device} do not match.' ) dim = input.dim() if dim == 2: # Expand does not copy data. input = input.expand(1, 1, input.dim(-2), input.dim(-1)) target = target.expand(1, 1, target.dim(-2), target.dim(-1)) elif dim == 3: input = input.expand(1, input.dim(-3), input.dim(-2), input.dim(-1)) target = target.expand(1, target.dim(-3), target.dim(-2), target.dim(-1)) elif dim != 4: raise ValueError('Expected 2, 3, or 4 dimensions (got {})'.format(dim)) if size_average is not None or reduce is not None: reduction = _Reduction.legacy_get_string(size_average, reduce) channel = input.size(1) kernel = _fspecial_gaussian(filter_size, channel, sigma, input.device) # It would be nice if this did not have to be initialized every time that the function was called. # Copying from host (CPU) to device (GPU) is a major bottleneck. weights = torch.tensor([0.0448, 0.2856, 0.3001, 0.2363, 0.1333], device=input.device) weights = weights.unsqueeze(-1).unsqueeze(-1) levels = weights.size(0) mssim = list() mcs = list() for _ in range(levels): ssim, cs = _ssim(input, target, max_val, k1, k2, channel, kernel) ssim = ssim.mean((2, 3)) cs = cs.mean((2, 3)) mssim.append(ssim) mcs.append(cs) input = avg_pool2d(input, (2, 2)) target = avg_pool2d(target, (2, 2)) mssim = torch.stack(mssim) mcs = torch.stack(mcs) p1 = mcs**weights p2 = mssim**weights ret = torch.prod(p1[:-1], 0) * p2[-1] if reduction != 'none': ret = torch.mean(ret) if reduction == 'mean' else torch.sum(ret) return ret