-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
261 lines (217 loc) · 7.64 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
## import models
import torch
from torch import nn
from torch import optim
from torch.autograd import Variable
# parameters
# initial channel numbers
n_channel = 3
# discriminator base output-16
n_disc = 16
# generator base output 64
n_gen = 64
# encoder base 64
n_encode = 64
# label number
n_l = 10
# latent vector size
n_z = 50
# size of image
img_size = 128
# batchsize with which training takes place
batchSize = 20
use_cuda = torch.cuda.is_available()
# age category is 50/10 = 5
n_age = int(n_z/n_l)
# gender is 50/2 = 25
n_gender = int(n_z/2)
# adopted from
# https://github.com/dxyang/StyleTransfer/blob/master/network.py
# Conv Layer
class ConvLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride):
super(ConvLayer, self).__init__()
padding = kernel_size // 2
self.reflection_pad = nn.ReflectionPad2d(padding)
self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size, stride) #, padding)
def forward(self, x):
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
# Upsample Conv Layer
class UpsampleConvLayer(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, stride, upsample=None):
super(UpsampleConvLayer, self).__init__()
self.upsample = upsample
if upsample:
self.upsample = nn.Upsample(scale_factor=upsample, mode='nearest')
reflection_padding = kernel_size // 2
self.reflection_pad = nn.ReflectionPad2d(reflection_padding)
self.conv2d = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
def forward(self, x):
if self.upsample:
x = self.upsample(x)
out = self.reflection_pad(x)
out = self.conv2d(out)
return out
# Residual Block
# adapted from pytorch tutorial
# https://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/02-
# intermediate/deep_residual_network/main.py
class ResidualBlock(nn.Module):
def __init__(self, channels):
super(ResidualBlock, self).__init__()
self.conv1 = ConvLayer(channels, channels, kernel_size=3, stride=1)
self.in1 = nn.InstanceNorm2d(channels, affine=True)
self.relu = nn.ReLU()
self.conv2 = ConvLayer(channels, channels, kernel_size=3, stride=1)
self.in2 = nn.InstanceNorm2d(channels, affine=True)
def forward(self, x):
residual = x
out = self.relu(self.in1(self.conv1(x)))
out = self.in2(self.conv2(out))
out = out + residual
out = self.relu(out)
return out
# Image Transform Network
class ImageTransformNet(nn.Module):
def __init__(self):
super(ImageTransformNet, self).__init__()
# nonlineraity
self.relu = nn.ReLU()
self.tanh = nn.Tanh()
# encoding layers
self.conv1 = ConvLayer(3, 32, kernel_size=9, stride=1)
self.in1_e = nn.InstanceNorm2d(32, affine=True)
self.conv2 = ConvLayer(32, 64, kernel_size=3, stride=2)
self.in2_e = nn.InstanceNorm2d(64, affine=True)
self.conv3 = ConvLayer(64, 128, kernel_size=3, stride=2)
self.in3_e = nn.InstanceNorm2d(128, affine=True)
# residual layers
self.res1 = ResidualBlock(128)
self.res2 = ResidualBlock(128)
self.res3 = ResidualBlock(128)
self.res4 = ResidualBlock(128)
self.res5 = ResidualBlock(128)
# decoding layers
self.deconv3 = UpsampleConvLayer(128, 64, kernel_size=3, stride=1, upsample=2 )
self.in3_d = nn.InstanceNorm2d(64, affine=True)
self.deconv2 = UpsampleConvLayer(64, 32, kernel_size=3, stride=1, upsample=2 )
self.in2_d = nn.InstanceNorm2d(32, affine=True)
self.deconv1 = UpsampleConvLayer(32, 3, kernel_size=9, stride=1)
self.in1_d = nn.InstanceNorm2d(3, affine=True)
def forward(self, x):
# encode
y = self.relu(self.in1_e(self.conv1(x)))
y = self.relu(self.in2_e(self.conv2(y)))
y = self.relu(self.in3_e(self.conv3(y)))
# residual layers
y = self.res1(y)
y = self.res2(y)
y = self.res3(y)
y = self.res4(y)
y = self.res5(y)
# decode
y = self.relu(self.in3_d(self.deconv3(y)))
y = self.relu(self.in2_d(self.deconv2(y)))
#y = self.tanh(self.in1_d(self.deconv1(y)))
y = self.deconv1(y)
return y
class Vgg16(nn.Module):
def __init__(self):
super(Vgg16, self).__init__()
features = models.vgg16(pretrained=True).features
self.to_relu_1_2 = nn.Sequential()
self.to_relu_2_2 = nn.Sequential()
self.to_relu_3_3 = nn.Sequential()
self.to_relu_4_3 = nn.Sequential()
for x in range(4):
self.to_relu_1_2.add_module(str(x), features[x])
for x in range(4, 9):
self.to_relu_2_2.add_module(str(x), features[x])
for x in range(9, 16):
self.to_relu_3_3.add_module(str(x), features[x])
for x in range(16, 23):
self.to_relu_4_3.add_module(str(x), features[x])
# don't need the gradients, just want the features
for param in self.parameters():
param.requires_grad = False
def forward(self, x):
h = self.to_relu_1_2(x)
h_relu_1_2 = h
h = self.to_relu_2_2(h)
h_relu_2_2 = h
h = self.to_relu_3_3(h)
h_relu_3_3 = h
h = self.to_relu_4_3(h)
h_relu_4_3 = h
out = (h_relu_1_2, h_relu_2_2, h_relu_3_3, h_relu_4_3)
return out
class discriminator(nn.Module):
def __init__(self):
super(Vgg16,self).__init__()
class Vgg16(nn.Module):
def __init__(self):
super(Vgg16, self).__init__()
features = models.vgg16(pretrained=True).features
self.to_relu_1_2 = nn.Sequential()
self.to_relu_2_2 = nn.Sequential()
self.to_relu_3_3 = nn.Sequential()
self.to_relu_4_3 = nn.Sequential()
for x in range(4):
self.to_relu_1_2.add_module(str(x), features[x])
for x in range(4, 9):
self.to_relu_2_2.add_module(str(x), features[x])
for x in range(9, 16):
self.to_relu_3_3.add_module(str(x), features[x])
for x in range(16, 23):
self.to_relu_4_3.add_module(str(x), features[x])
# don't need the gradients, just want the features
for param in self.parameters():
param.requires_grad = False
def forward(self, x):
h = self.to_relu_1_2(x)
h_relu_1_2 = h
h = self.to_relu_2_2(h)
h_relu_2_2 = h
h = self.to_relu_3_3(h)
h_relu_3_3 = h
h = self.to_relu_4_3(h)
h_relu_4_3 = h
out = (h_relu_1_2, h_relu_2_2, h_relu_3_3, h_relu_4_3)
return out
def pathway1(self,x):
h = self.to_relu_1_2(x)
h_relu_1_2 = h
path1=(h_relu_1_2)
return path1
def pathway2(self,x):
h = self.to_relu_1_2(x)
h_relu_1_2 = h
h = self.to_relu_2_2(h)
h_relu_2_2 = h
path2=(h_relu_2_2)
return path2
def pathway3(self,x):
h = self.to_relu_1_2(x)
h_relu_1_2 = h
h = self.to_relu_2_2(h)
h_relu_2_2 = h
h = self.to_relu_3_3(h)
h_relu_3_3 = h
path3=(h_relu_3_3)
return path3
def pathway4(self,x):
h = self.to_relu_1_2(x)
h_relu_1_2 = h
h = self.to_relu_2_2(h)
h_relu_2_2 = h
h = self.to_relu_3_3(h)
h_relu_3_3 = h
h = self.to_relu_4_3(h)
h_relu_4_3 = h
path4=(h_relu_4_3)
return path4
def concat():
final=path1+path2+path3+path4
return final