Skip to content

Instantly share code, notes, and snippets.

@KushajveerSingh
Last active April 19, 2019 09:36
Show Gist options
  • Save KushajveerSingh/d5c9e6594ea8b0904f5bc783fd5f7d51 to your computer and use it in GitHub Desktop.
Save KushajveerSingh/d5c9e6594ea8b0904f5bc783fd5f7d51 to your computer and use it in GitHub Desktop.
PerceptualLoss to get features extracted from pretrained VGG19
class VGGLoss(nn.Module):
def __init__(self):
super().__init__()
self.vgg = VGG19().cuda()
self.criterion = nn.L1Loss()
self.weights = [1.0 / 32, 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0]
def forward(self, x, y):
x_vgg, y_vgg = self.vgg(x), self.vgg(y)
loss = 0
for i in range(len(x_vgg)):
loss += self.weights[i] * self.criterion(x_vgg[i], y_vgg[i].detach())
return loss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment