-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloss_network.py
30 lines (23 loc) · 930 Bytes
/
loss_network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from collections import namedtuple
import torch
import torchvision.models.vgg as vgg
LossOutput = namedtuple("LossOutput", ["relu1_2", "relu2_2", "relu3_2", "relu4_2"])
# https://discuss.pytorch.org/t/how-to-extract-features-of-an-image-from-a-trained-model/119/3
class LossNetwork(torch.nn.Module):
def __init__(self):
super(LossNetwork, self).__init__()
vgg_model = vgg.vgg16(pretrained=True)
self.vgg_layers = vgg_model.features
self.layer_name_mapping = {
'3': "relu1_2",
'8': "relu2_2",
'13': "relu3_2",
'20': "relu4_2"
}
def forward(self, x):
output = {}
for name, module in self.vgg_layers._modules.items():
x = module(x)
if name in self.layer_name_mapping:
output[self.layer_name_mapping[name]] = x
return LossOutput(**output)