-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
192 lines (169 loc) · 5.66 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import torch
from torch import nn
from torch import autograd
from torch.nn.init import kaiming_normal, constant
from torchvision import models
""" Generator
"""
class ResBlock(nn.Module):
def __init__(self, n=64, s=1, f=3):
super(ResBlock,self).__init__()
self.relu = nn.PReLU()
self.conv1 = nn.Conv2d(
in_channels=n,
out_channels=n,
kernel_size=f,
stride=s,
padding=(f-1)//2
)
kaiming_normal(self.conv1.weight)
self.bn1 = nn.BatchNorm2d(n)
self.conv2 = nn.Conv2d(
in_channels=n,
out_channels=n,
kernel_size=f,
stride=s,
padding=(f-1)//2
)
kaiming_normal(self.conv2.weight)
self.bn2 = nn.BatchNorm2d(n)
def forward(self, x):
y = self.relu(self.bn1(self.conv1(x)))
y = self.bn2(self.conv2(x)) + x
return y
class DeconvBlock(nn.Module):
def __init__(self, n=64, f=3, upscale_factor=2):
super(DeconvBlock,self).__init__()
self.relu= nn.PReLU()
self.ps = nn.PixelShuffle(2)
self.conv = nn.Conv2d(
in_channels=n,
out_channels=n*(upscale_factor**2),
kernel_size=f,
stride=1,
padding=(f-1)//2)
kaiming_normal(self.conv.weight)
def forward(self, x):
return self.relu(self.ps(self.conv(x)))
class GenNet(nn.Module):
def __init__(self):
super(GenNet,self).__init__()
self.relu = nn.PReLU()
self.tanh = nn.Hardtanh()
#self.tanh = nn.Tanh()
self.conv1 = nn.Conv2d(3, 64, 9, 1, (9-1)//2)
kaiming_normal(self.conv1.weight)
layers = []
for i in range(16):
layers.append(ResBlock())
self.resblocks = nn.Sequential(*layers)
self.conv2 = nn.Conv2d(64, 64, 3, 1, 1)
kaiming_normal(self.conv2.weight)
self.bn = nn.BatchNorm2d(64)
self.deconv1 = DeconvBlock()
self.deconv2 = DeconvBlock()
self.conv3 = nn.Conv2d(64, 3, 9, 1, (9-1)//2)
kaiming_normal(self.conv3.weight)
def forward(self, x):
x=x*2-1.0
xs = self.relu(self.conv1(x))
x = self.resblocks(xs)
x = self.bn(self.conv2(x))
x = x + xs
x = self.deconv1(x)
x = self.deconv2(x)
x = self.conv3(x)
x = self.tanh(x)
x = (x+1)/2.0
return x
""" VGG
"""
class Skip(nn.Module):
def __init__(self):
super(Skip,self).__init__()
def forward(self, input):
return input
def vgg19_54():
model = models.vgg19(pretrained=True)
# remove last max pooling
model.features = nn.Sequential(*list(model.features.children())[:-1])
model.classifier = Skip()
return model
""" Discriminator
"""
netspec_opts = dict()
netspec_opts['input_channels'] = 3
netspec_opts['layer_type'] = ['conv', 'lrelu',
'conv', 'lrelu', 'bn',
'conv', 'lrelu', 'bn',
'conv', 'lrelu', 'bn',
'conv', 'lrelu', 'bn',
'conv', 'lrelu', 'bn',
'conv', 'lrelu', 'bn',
'conv', 'lrelu', 'bn']
netspec_opts['num_filters'] = [64, 0,
64, 0, 64,
128, 0, 128,
128, 0, 128,
256, 0, 256,
256, 0, 256,
512, 0, 512,
512, 0, 512]
netspec_opts['kernel_size'] = [3, 0,
3, 0, 0,
3, 0, 0,
3, 0, 0,
3, 0, 0,
3, 0, 0,
3, 0, 0,
3, 0, 0]
netspec_opts['stride'] = [1, 0,
2, 0, 0,
1, 0, 0,
2, 0, 0,
1, 0, 0,
2, 0, 0,
1, 0, 0,
2, 0, 0]
def make_layers(nopts):
n = len(nopts['layer_type'])
layers = []
prev_filters = nopts['input_channels']
for i in range(n):
if nopts['layer_type'][i] == 'conv':
curr_filters = nopts['num_filters'][i]
layers.append(nn.Conv2d(
prev_filters,
curr_filters,
nopts['kernel_size'][i],
nopts['stride'][i],
(nopts['kernel_size'][i]-1)//2,
))
prev_filters = curr_filters
elif nopts['layer_type'][i] == 'lrelu':
layers.append(nn.LeakyReLU(0.2))
elif nopts['layer_type'][i] == 'bn':
curr_filters = nopts['num_filters'][i]
layers.append(nn.BatchNorm2d(curr_filters))
prev_filters = curr_filters
return nn.Sequential(*layers)
class DisNet(nn.Module):
def __init__(self):
super(DisNet,self).__init__()
self.features = make_layers(netspec_opts)
self.classifier = nn.Sequential(
nn.Linear(16 * 16 * 512,2048),
nn.LeakyReLU(0.2),
nn.Linear(2048, 1),
nn.Sigmoid()
)
self._init_weights()
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
def _init_weights(self):
for module in self.modules():
if isinstance(module, nn.Conv2d) or isinstance(module,nn.Linear):
kaiming_normal(module.weight,0.2)