-
Notifications
You must be signed in to change notification settings - Fork 3
/
vgg.py
47 lines (37 loc) · 1.44 KB
/
vgg.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
from collections import namedtuple
import torch
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.relu1_1 = torch.nn.Sequential()
self.relu2_1 = torch.nn.Sequential()
self.relu3_1 = torch.nn.Sequential()
self.relu4_1 = torch.nn.Sequential()
self.relu5_1 = torch.nn.Sequential()
for x in range(2):
self.relu1_1.add_module(str(x), vgg_pretrained_features[x])
for x in range(2,7):
self.relu2_1.add_module(str(x), vgg_pretrained_features[x])
for x in range(7,12):
self.relu3_1.add_module(str(x), vgg_pretrained_features[x])
for x in range(12,21):
self.relu4_1.add_module(str(x), vgg_pretrained_features[x])
for x in range(21,30):
self.relu5_1.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 = self.relu1_1(X)
h_relu1_1 = h
h = self.relu2_1(h)
h_relu2_1 = h
h = self.relu3_1(h)
h_relu3_1 = h
h = self.relu4_1(h)
h_relu4_1 = h
h = self.relu5_1(h)
h_relu5_1 = h
return [h_relu1_1, h_relu2_1, h_relu3_1, h_relu4_1, h_relu5_1]