-
Notifications
You must be signed in to change notification settings - Fork 4
/
vgg.py
75 lines (63 loc) · 2.31 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
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
# Copyright 2020 by Andrey Ignatov. All Rights Reserved.
from torchvision import models
import torch.nn as nn
import torch
from torchvision import models
import torch.nn as nn
CONTENT_LAYER = 'relu_16'
class VGG16_perceptual(torch.nn.Module):
def __init__(self, requires_grad=False):
super(VGG16_perceptual, self).__init__()
vgg_pretrained_features = models.vgg16(pretrained=True).features
self.slice1 = torch.nn.Sequential()
self.slice2 = torch.nn.Sequential()
self.slice3 = torch.nn.Sequential()
self.slice4 = torch.nn.Sequential()
for x in range(2):
self.slice1.add_module(str(x), vgg_pretrained_features[x])
for x in range(2, 4):
self.slice2.add_module(str(x), vgg_pretrained_features[x])
for x in range(4, 14):
self.slice3.add_module(str(x), vgg_pretrained_features[x])
for x in range(14, 21):
self.slice4.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.slice1(X)
h_relu1_1 = h
h = self.slice2(h)
h_relu1_2 = h
h = self.slice3(h)
h_relu3_2 = h
h = self.slice4(h)
h_relu4_2 = h
return h_relu1_1, h_relu1_2, h_relu3_2, h_relu4_2
def vgg_19(device):
vgg_19 = models.vgg19(pretrained=True).features
model = nn.Sequential()
i = 0
for layer in vgg_19.children():
if isinstance(layer, nn.Conv2d):
i += 1
name = 'conv_{}'.format(i)
elif isinstance(layer, nn.ReLU):
name = 'relu_{}'.format(i)
layer = nn.ReLU(inplace=False)
elif isinstance(layer, nn.MaxPool2d):
name = 'pool_{}'.format(i)
elif isinstance(layer, nn.BatchNorm2d):
name = 'bn_{}'.format(i)
else:
raise RuntimeError('Unrecognized layer: {}'.format(layer.__class__.__name__))
model.add_module(name, layer)
if name == CONTENT_LAYER:
break
model = model.to(device)
model = torch.nn.DataParallel(model)
for param in model.parameters():
param.requires_grad = False
for param in vgg_19.parameters():
param.requires_grad = False
return model