-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
321 lines (262 loc) · 10.5 KB
/
model.py
File metadata and controls
321 lines (262 loc) · 10.5 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import torch
import torch.nn as nn
import torch.utils.model_zoo as model_zoo
import torch.nn.functional as F
from torchvision import models
from torch.autograd import Variable
from config import cfg
class INCEPTION_V3(nn.Module):
"""
Using the ImageNet pretrained Inception Network to analyse the Inception Score of the
generated images while training. The final evaluation is done by a fine-tined Inception model.
"""
def __init__(self):
super(INCEPTION_V3, self).__init__()
self.model = models.inception_v3()
url = 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth'
# print(next(model.parameters()).data)
state_dict = \
model_zoo.load_url(url, map_location=lambda storage, loc: storage)
self.model.load_state_dict(state_dict)
for param in self.model.parameters():
param.requires_grad = False
print('Load pretrained model from ', url)
# print(next(self.model.parameters()).data)
# print(self.model)
def forward(self, input):
# [-1.0, 1.0] --> [0, 1.0]
x = input * 0.5 + 0.5
# mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225]
# --> mean = 0, std = 1
x[:, 0] = (x[:, 0] - 0.485) / 0.229
x[:, 1] = (x[:, 1] - 0.456) / 0.224
x[:, 2] = (x[:, 2] - 0.406) / 0.225
#
# --> fixed-size input: batch x 3 x 299 x 299
x = nn.Upsample(size=(299, 299), mode='bilinear')(x)
# 299 x 299 x 3
x = self.model(x)
x = nn.Softmax()(x)
return x
def conv3x3(in_planes, out_planes):
"3x3 convolution with padding"
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=1,
padding=1, bias=False)
def Block3x3_leakRelu(in_planes, out_planes):
block = nn.Sequential(
conv3x3(in_planes, out_planes),
nn.BatchNorm2d(out_planes),
nn.LeakyReLU(0.2, inplace=True)
)
return block
# -- Preparing Generator -- #
class GLU(nn.Module):
def __init__(self):
super(GLU, self).__init__()
def forward(self, x):
nc = x.size(1)
assert nc % 2 == 0, 'channels dont divide 2!'
nc = int(nc/2)
return x[:, :nc] * F.sigmoid(x[:, nc:])
# Keep the spatial size
def Block3x3_relu(in_planes, out_planes):
block = nn.Sequential(
conv3x3(in_planes, out_planes * 2),
nn.BatchNorm2d(out_planes * 2),
GLU()
)
return block
# Upsale the spatial size by a factor of 2
def upBlock(in_planes, out_planes):
block = nn.Sequential(
nn.Upsample(scale_factor=2, mode='nearest'),
conv3x3(in_planes, out_planes * 2),
nn.BatchNorm2d(out_planes * 2),
GLU()
)
return block
class CA_NET(nn.Module):
# some code is modified from vae examples
# (https://github.com/pytorch/examples/blob/master/vae/main.py)
def __init__(self):
super(CA_NET, self).__init__()
self.t_dim = cfg.TEXT.DIMENSION
self.ef_dim = cfg.GAN.EMBEDDING_DIM
self.fc = nn.Linear(self.t_dim, self.ef_dim * 4, bias=True)
self.relu = GLU()
def encode(self, text_embedding):
x = self.relu(self.fc(text_embedding))
mu = x[:, :self.ef_dim]
logvar = x[:, self.ef_dim:]
return mu, logvar
def reparametrize(self, mu, logvar):
std = logvar.mul(0.5).exp_()
if cfg.CUDA:
eps = torch.cuda.FloatTensor(std.size()).normal_()
else:
eps = torch.FloatTensor(std.size()).normal_()
eps = Variable(eps)
return eps.mul(std).add_(mu)
def forward(self, text_embedding):
mu, logvar = self.encode(text_embedding)
c_code = self.reparametrize(mu, logvar)
return c_code, mu, logvar
class ResBlock(nn.Module):
def __init__(self, channel_num):
super(ResBlock, self).__init__()
self.block = nn.Sequential(
conv3x3(channel_num, channel_num * 2),
nn.BatchNorm2d(channel_num * 2),
GLU(),
conv3x3(channel_num, channel_num),
nn.BatchNorm2d(channel_num)
)
def forward(self, x):
residual = x
out = self.block(x)
out += residual
return out
class NewHiddenLayer(nn.Module):
def __init__(self, ngf=cfg.GAN.GF_DIM, num_residual=1):
super(NewHiddenLayer, self).__init__()
self.ngf = ngf
self.num_residual = num_residual
self.initial_depth = 256 + 1 # (Depth of the current_image_features + 1)
self.jointConv = Block3x3_relu(self.initial_depth, self.ngf)
self.residual = self._make_layer(ResBlock, self.ngf)
self.single_out = nn.Conv2d(self.ngf, 1, kernel_size=1)
def _make_layer(self, block, channel_num):
layers = []
for i in range(self.num_residual):
layers.append(block(channel_num))
return nn.Sequential(*layers)
def forward(self, prev_hidden_state, current_image_features):
stack = torch.cat((prev_hidden_state, current_image_features),1)
out_code = self.jointConv(stack)
out_code = self.residual(out_code)
out_code = self.single_out(out_code)
return out_code
class FinalImageLayer(nn.Module):
def __init__(self, ngf):
super(FinalImageLayer, self).__init__()
self.gf_dim = ngf
self.img = nn.Sequential(
conv3x3(ngf, 3),
nn.Tanh()
)
def forward(self, h_code):
out_img = self.img(h_code)
return out_img
class GenerateImage(nn.Module):
def __init__(self, num_residual=2):
super(GenerateImage, self).__init__()
self.ef_dim = cfg.GAN.EMBEDDING_DIM
self.gf_dim = cfg.GAN.GF_DIM
self.num_residual = num_residual
self.joinConv = Block3x3_relu(1 + self.ef_dim, self.gf_dim * 4)
self.upsample1 = upBlock(self.gf_dim * 4, self.gf_dim * 2)
self.upsample2 = upBlock(self.gf_dim * 2, self.gf_dim)
self.upsample3 = upBlock(self.gf_dim, self.gf_dim)
self.imageLayer = FinalImageLayer(self.gf_dim)
def forward(self, hidden_state, caption_vector):
s_size = hidden_state.size(2)
c_code = caption_vector.view(-1, self.ef_dim, 1, 1)
c_code = c_code.repeat(1, 1, s_size, s_size)
h_c_code = torch.cat((c_code, hidden_state), 1)
out_code_8 = self.joinConv(h_c_code)
out_code_16 = self.upsample1(out_code_8)
out_code_32 = self.upsample2(out_code_16)
out_code_64 = self.upsample3(out_code_32)
img = self.imageLayer(out_code_64)
return img, out_code_8
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.gen_image_stage = GenerateImage()
self.hidden_state_update_stage = NewHiddenLayer()
if cfg.GAN.TEXT_CONDITION:
self.ca_net = CA_NET()
def forward(self, hidden_state, all_caption_vecs):
c_codes = []
mus = []
logvars = []
images = []
# Building Recurrence
for i in range(all_caption_vecs.size(1)):
caption_vec = all_caption_vecs[:, i, :]
c, m, l = self.ca_net(caption_vec)
# Generate Image
img, out_code_16 = self.gen_image_stage(hidden_state, c)
# Update Hidden State
hidden_state = self.hidden_state_update_stage(hidden_state, out_code_16)
c_codes.append(c)
mus.append(m)
logvars.append(l)
images.append(img)
return images, mus, logvars
# -- Preparing Discriminator -- #
# Downscale the spatial size by a factor of 8
def encode_image_by_8times(ndf):
encode_img = nn.Sequential(
# --> state size. ndf x in_size/2 x in_size/2
nn.Conv2d(3, ndf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# --> state size 2ndf x x in_size/4 x in_size/4
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# --> state size 4ndf x in_size/8 x in_size/8
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
)
return encode_img
# Downsale the spatial size by a factor of 16
def encode_image_by_16times(ndf):
encode_img = nn.Sequential(
# --> state size. ndf x in_size/2 x in_size/2
nn.Conv2d(3, ndf, 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
# --> state size 2ndf x x in_size/4 x in_size/4
nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 2),
nn.LeakyReLU(0.2, inplace=True),
# --> state size 4ndf x in_size/8 x in_size/8
nn.Conv2d(ndf * 2, ndf * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 4),
nn.LeakyReLU(0.2, inplace=True),
# --> state size 8ndf x in_size/16 x in_size/16
nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(ndf * 8),
nn.LeakyReLU(0.2, inplace=True)
)
return encode_img
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.ndf = cfg.GAN.DF_DIM # Granular depth of Discriminator Feature maps
self.embed_dim = cfg.GAN.EMBEDDING_DIM # Depth of the text embedding
# self.img_s4 = encode_image_by_8times(self.ndf) # img_s4 contains image of size 4x4 (32/8=4)
self.img_s16 = encode_image_by_16times(self.ndf) # img_s4 contains image of size 4x4 (64/16=4)
self.logits = nn.Sequential(nn.Conv2d(self.ndf * 8, 1, kernel_size=4, stride=4),
nn.Sigmoid())
if cfg.GAN.TEXT_CONDITION:
self.jointConv = Block3x3_leakRelu(self.ndf * 8 + self.embed_dim, self.ndf * 8)
self.uncond_logits = nn.Sequential(nn.Conv2d(self.ndf * 8, 1, kernel_size=4, stride=4),
nn.Sigmoid())
def forward(self, image, caption_code=None):
# img_code = self.img_s4(image)
img_code = self.img_s16(image)
if cfg.GAN.TEXT_CONDITION and caption_code is not None:
caption_code = caption_code.view(-1, self.embed_dim, 1, 1)
caption_code = caption_code.repeat(1, 1, 4, 4)
h_code = torch.cat((caption_code, img_code), 1)
h_code = self.jointConv(h_code)
else:
h_code = img_code
output = self.logits(h_code)
if cfg.GAN.TEXT_CONDITION:
output_uncond = self.uncond_logits(img_code)
return [output.view(-1), output_uncond.view(-1)]
else:
return [output.view(-1)]