Last active
April 19, 2019 09:36
-
-
Save KushajveerSingh/d5c9e6594ea8b0904f5bc783fd5f7d51 to your computer and use it in GitHub Desktop.
PerceptualLoss to get features extracted from pretrained VGG19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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