-
Notifications
You must be signed in to change notification settings - Fork 19
/
vgg19.py
97 lines (78 loc) · 3.32 KB
/
vgg19.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import torch.nn as nn
import torch
class VGG19(nn.Module):
"""
Simplified version of the VGG19 "feature" block
This module's only job is to return the "feature loss" for the inputs
"""
def __init__(self, channel_in=3, width=64):
super(VGG19, self).__init__()
self.conv1 = nn.Conv2d(channel_in, width, 3, 1, 1)
self.conv2 = nn.Conv2d(width, width, 3, 1, 1)
self.conv3 = nn.Conv2d(width, 2 * width, 3, 1, 1)
self.conv4 = nn.Conv2d(2 * width, 2 * width, 3, 1, 1)
self.conv5 = nn.Conv2d(2 * width, 4 * width, 3, 1, 1)
self.conv6 = nn.Conv2d(4 * width, 4 * width, 3, 1, 1)
self.conv7 = nn.Conv2d(4 * width, 4 * width, 3, 1, 1)
self.conv8 = nn.Conv2d(4 * width, 4 * width, 3, 1, 1)
self.conv9 = nn.Conv2d(4 * width, 8 * width, 3, 1, 1)
self.conv10 = nn.Conv2d(8 * width, 8 * width, 3, 1, 1)
self.conv11 = nn.Conv2d(8 * width, 8 * width, 3, 1, 1)
self.conv12 = nn.Conv2d(8 * width, 8 * width, 3, 1, 1)
self.conv13 = nn.Conv2d(8 * width, 8 * width, 3, 1, 1)
self.conv14 = nn.Conv2d(8 * width, 8 * width, 3, 1, 1)
self.conv15 = nn.Conv2d(8 * width, 8 * width, 3, 1, 1)
self.conv16 = nn.Conv2d(8 * width, 8 * width, 3, 1, 1)
self.mp = nn.MaxPool2d(kernel_size=2, stride=2)
self.relu = nn.ReLU()
self.load_params_()
def load_params_(self):
# Download and load Pytorch's pre-trained weights
state_dict = torch.hub.load_state_dict_from_url('https://download.pytorch.org/models/vgg19-dcbb9e9d.pth')
for ((name, source_param), target_param) in zip(state_dict.items(), self.parameters()):
target_param.data = source_param.data
target_param.requires_grad = False
def feature_loss(self, x):
return (x[:x.shape[0] // 2] - x[x.shape[0] // 2:]).pow(2).mean()
def forward(self, x):
"""
:param x: Expects x to be the target and source to concatenated on dimension 0
:return: Feature loss
"""
x = self.conv1(x)
loss = self.feature_loss(x)
x = self.conv2(self.relu(x))
loss += self.feature_loss(x)
x = self.mp(self.relu(x)) # 64x64
x = self.conv3(x)
loss += self.feature_loss(x)
x = self.conv4(self.relu(x))
loss += self.feature_loss(x)
x = self.mp(self.relu(x)) # 32x32
x = self.conv5(x)
loss += self.feature_loss(x)
x = self.conv6(self.relu(x))
loss += self.feature_loss(x)
x = self.conv7(self.relu(x))
loss += self.feature_loss(x)
x = self.conv8(self.relu(x))
loss += self.feature_loss(x)
x = self.mp(self.relu(x)) # 16x16
x = self.conv9(x)
loss += self.feature_loss(x)
x = self.conv10(self.relu(x))
loss += self.feature_loss(x)
x = self.conv11(self.relu(x))
loss += self.feature_loss(x)
x = self.conv12(self.relu(x))
loss += self.feature_loss(x)
x = self.mp(self.relu(x)) # 8x8
x = self.conv13(x)
loss += self.feature_loss(x)
x = self.conv14(self.relu(x))
loss += self.feature_loss(x)
x = self.conv15(self.relu(x))
loss += self.feature_loss(x)
x = self.conv16(self.relu(x))
loss += self.feature_loss(x)
return loss/16