-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
139 lines (127 loc) · 4.07 KB
/
models.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import torch
import torch.nn as nn
import copy
normalised_vgg_relu5_1 = nn.Sequential(
nn.Conv2d(3, 3, 1),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(3, 64, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 64, 3),
nn.ReLU(),
nn.MaxPool2d(2, ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 128, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 128, 3),
nn.ReLU(),
nn.MaxPool2d(2, ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 256, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, 3),
nn.ReLU(),
nn.MaxPool2d(2, ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 512, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, 3),
nn.ReLU(),
nn.MaxPool2d(2, ceil_mode=True),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, 3),
nn.ReLU()
)
class NormalisedVGG(nn.Module):
def __init__(self, pretrained_path=None):
super().__init__()
self.net = normalised_vgg_relu5_1
if pretrained_path is not None:
self.net.load_state_dict(torch.load(pretrained_path, map_location=lambda storage, loc: storage))
def forward(self, x, target):
if target == 'relu1_1':
return self.net[:4](x)
elif target == 'relu2_1':
return self.net[:11](x)
elif target == 'relu3_1':
return self.net[:18](x)
elif target == 'relu4_1':
return self.net[:31](x)
elif target == 'relu5_1':
return self.net(x)
vgg_decoder_relu5_1 = nn.Sequential(
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, 3),
nn.ReLU(),
nn.Upsample(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 512, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(512, 256, 3),
nn.ReLU(),
nn.Upsample(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 256, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(256, 128, 3),
nn.ReLU(),
nn.Upsample(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 128, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(128, 64, 3),
nn.ReLU(),
nn.Upsample(scale_factor=2),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 64, 3),
nn.ReLU(),
nn.ReflectionPad2d((1, 1, 1, 1)),
nn.Conv2d(64, 3, 3)
)
class Decoder(nn.Module):
def __init__(self, target, pretrained_path=None):
super().__init__()
if target == 'relu1_1':
self.net = nn.Sequential(*copy.deepcopy(list(vgg_decoder_relu5_1.children())[-5:])) # current -2
elif target == 'relu2_1':
self.net = nn.Sequential(*copy.deepcopy(list(vgg_decoder_relu5_1.children())[-9:]))
elif target == 'relu3_1':
self.net = nn.Sequential(*copy.deepcopy(list(vgg_decoder_relu5_1.children())[-16:]))
elif target == 'relu4_1':
self.net = nn.Sequential(*copy.deepcopy(list(vgg_decoder_relu5_1.children())[-29:]))
elif target == 'relu5_1':
self.net = nn.Sequential(*copy.deepcopy(list(vgg_decoder_relu5_1.children())))
if pretrained_path is not None:
self.net.load_state_dict(torch.load(pretrained_path, map_location=lambda storage, loc: storage))
def forward(self, x):
return self.net(x)