import numpy import torch import torch.nn.functional as F from torchvision import models class Vgg19(torch.nn.Module): def __init__(self, requires_grad=False): super(Vgg19, self).__init__() vgg_pretrained_features = models.vgg19(pretrained=True).features self.slice1 = torch.nn.Sequential() self.slice2 = torch.nn.Sequential() self.slice3 = torch.nn.Sequential() self.slice4 = torch.nn.Sequential() self.slice5 = torch.nn.Sequential() for x in range(2): self.slice1.add_module(str(x), vgg_pretrained_features[x]) for x in range(2, 7): self.slice2.add_module(str(x), vgg_pretrained_features[x]) for x in range(7, 12): self.slice3.add_module(str(x), vgg_pretrained_features[x]) for x in range(12, 21): self.slice4.add_module(str(x), vgg_pretrained_features[x]) for x in range(21, 30): self.slice5.add_module(str(x), vgg_pretrained_features[x]) if not requires_grad: for param in self.parameters(): param.requires_grad = False def forward(self, X): h_relu1 = self.slice1(X) # relu1_1 h_relu2 = self.slice2(h_relu1) # relu2_1 h_relu3 = self.slice3(h_relu2) # relu3_1 h_relu4 = self.slice4(h_relu3) # relu4_1 h_relu5 = self.slice5(h_relu4) # relu5_1 out = [h_relu1, h_relu2, h_relu3, h_relu4, h_relu5] return out class LossNetwork(torch.nn.Module): def __init__(self, device): super(LossNetwork, self).__init__() self.vgg = Vgg19().to(device) self.L1 = torch.nn.L1Loss() self.weight = [1.0 / 32, 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0] def forward(self, pred, gt, input): loss = [] pred_features = self.vgg(pred) gt_features = self.vgg(gt) input_features = self.vgg(input) for i in range(len(pred_features)): pred_gt = self.L1(pred_features[i], gt_features[i]) pred_input = self.L1(pred_features[i], input_features[i]) per_loss = pred_gt / (pred_input + 1e-7) loss.append(self.weight[i] * per_loss) # loss.append(self.weight[i] * pred_gt) return sum(loss)
标签:Loss,vgg,features,nn,pred,self,torch,contrast From: https://www.cnblogs.com/yyhappy/p/17589822.html