-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmodel.py
executable file
·262 lines (222 loc) · 8.72 KB
/
model.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
253
254
255
256
257
258
259
260
261
262
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
def init_weights(m):
if isinstance(m, nn.Conv2d) or isinstance(m, nn.Linear):
if m.weight.requires_grad:
m.weight.data.normal_(std=0.02)
if m.bias is not None and m.bias.requires_grad:
m.bias.data.fill_(0)
elif isinstance(m, nn.BatchNorm2d) and m.affine:
if m.weight.requires_grad:
m.weight.data.normal_(1, 0.02)
if m.bias.requires_grad:
m.bias.data.fill_(0)
class ResidualBlock(nn.Module):
def __init__(self, ndim):
super(ResidualBlock, self).__init__()
self.encoder = nn.Sequential(
nn.Conv2d(ndim, ndim, 3, padding=1, bias=False),
nn.BatchNorm2d(ndim),
nn.ReLU(inplace=True),
nn.Conv2d(ndim, ndim, 3, padding=1, bias=False),
nn.BatchNorm2d(ndim)
)
def forward(self, x):
return x + self.encoder(x)
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
# encoder
self.encoder = nn.Sequential(
nn.Conv2d(3, 64, 3, 1, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(64, 128, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Conv2d(128, 256, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Conv2d(256, 512, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True)
)
# residual blocks
self.residual_blocks = nn.Sequential(
nn.Conv2d(512 + 128, 512, 3, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
ResidualBlock(512),
ResidualBlock(512),
ResidualBlock(512),
ResidualBlock(512)
)
# decoder
self.decoder = nn.Sequential(
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(512, 256, 3, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(256, 128, 3, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
nn.Upsample(scale_factor=2, mode='nearest'),
nn.Conv2d(128, 64, 3, padding=1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 3, 3, padding=1),
nn.Tanh()
)
# conditioning augmentation
self.mu = nn.Sequential(
nn.Linear(512, 128),
nn.LeakyReLU(0.2, inplace=True)
)
self.log_sigma = nn.Sequential(
nn.Linear(512, 128),
nn.LeakyReLU(0.2, inplace=True)
)
self.txt_encoder_f = nn.GRUCell(300, 512)
self.txt_encoder_b = nn.GRUCell(300, 512)
self.apply(init_weights)
def forward(self, img, txt):
# image encoder
e = self.encoder(img)
# text encoder
if type(txt) is not tuple:
raise TypeError('txt must be tuple (txt_data, txt_len).')
txt_data = txt[0]
txt_len = txt[1]
hi_f = torch.zeros(txt_data.size(1), 512, device=txt_data.device)
hi_b = torch.zeros(txt_data.size(1), 512, device=txt_data.device)
h_f = []
h_b = []
mask = []
for i in range(txt_data.size(0)):
mask_i = (txt_data.size(0) - 1 - i < txt_len).float().unsqueeze(1)
mask.append(mask_i)
hi_f = self.txt_encoder_f(txt_data[i], hi_f)
h_f.append(hi_f)
hi_b = mask_i * self.txt_encoder_b(txt_data[-i - 1], hi_b) + (1 - mask_i) * hi_b
h_b.append(hi_b)
mask = torch.stack(mask[::-1])
h_f = torch.stack(h_f) * mask
h_b = torch.stack(h_b[::-1])
h = (h_f + h_b) / 2
cond = h.sum(0) / mask.sum(0)
z_mean = self.mu(cond)
z_log_stddev = self.log_sigma(cond)
z = torch.randn(cond.size(0), 128, device=txt_data.device)
cond = z_mean + z_log_stddev.exp() * z
# residual blocks
cond = cond.unsqueeze(-1).unsqueeze(-1)
merge = self.residual_blocks(torch.cat((e, cond.repeat(1, 1, e.size(2), e.size(3))), 1))
# decoder
d = self.decoder(e + merge)
return d, (z_mean, z_log_stddev)
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.eps = 1e-7
self.encoder_1 = nn.Sequential(
nn.Conv2d(3, 64, 4, 2, padding=1),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(64, 128, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.LeakyReLU(0.2, inplace=True),
nn.Conv2d(128, 256, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True)
)
self.encoder_2 = nn.Sequential(
nn.Conv2d(256, 512, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True)
)
self.encoder_3 = nn.Sequential(
nn.Conv2d(512, 512, 4, 2, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True)
)
self.GAP_1 = nn.Sequential(
nn.Conv2d(256, 256, 3, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.LeakyReLU(0.2, inplace=True)
)
self.GAP_2 = nn.Sequential(
nn.Conv2d(512, 512, 3, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True)
)
self.GAP_3 = nn.Sequential(
nn.Conv2d(512, 512, 3, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.LeakyReLU(0.2, inplace=True)
)
# text feature
self.txt_encoder_f = nn.GRUCell(300, 512)
self.txt_encoder_b = nn.GRUCell(300, 512)
self.gen_filter = nn.ModuleList([
nn.Linear(512, 256 + 1),
nn.Linear(512, 512 + 1),
nn.Linear(512, 512 + 1)
])
self.gen_weight = nn.Sequential(
nn.Linear(512, 3),
nn.Softmax(-1)
)
self.classifier = nn.Conv2d(512, 1, 4)
self.apply(init_weights)
def forward(self, img, txt, len_txt, negative=False):
img_feat_1 = self.encoder_1(img)
img_feat_2 = self.encoder_2(img_feat_1)
img_feat_3 = self.encoder_3(img_feat_2)
img_feats = [self.GAP_1(img_feat_1), self.GAP_2(img_feat_2), self.GAP_3(img_feat_3)]
D = self.classifier(img_feat_3).squeeze()
# text attention
u, m, mask = self._encode_txt(txt, len_txt)
att_txt = (u * m.unsqueeze(0)).sum(-1)
att_txt_exp = att_txt.exp() * mask.squeeze(-1)
att_txt = (att_txt_exp / att_txt_exp.sum(0, keepdim=True))
weight = self.gen_weight(u).permute(2, 1, 0)
sim = 0
sim_n = 0
idx = np.arange(0, img.size(0))
idx_n = torch.tensor(np.roll(idx, 1), dtype=torch.long, device=txt.device)
for i in range(3):
img_feat = img_feats[i]
W_cond = self.gen_filter[i](u).permute(1, 0, 2)
W_cond, b_cond = W_cond[:, :, :-1], W_cond[:, :, -1].unsqueeze(-1)
img_feat = img_feat.mean(-1).mean(-1).unsqueeze(-1)
if negative:
W_cond_n, b_cond_n, weight_n = W_cond[idx_n], b_cond[idx_n], weight[i][idx_n]
sim_n += torch.sigmoid(torch.bmm(W_cond_n, img_feat) + b_cond_n).squeeze(-1) * weight_n
sim += torch.sigmoid(torch.bmm(W_cond, img_feat) + b_cond).squeeze(-1) * weight[i]
if negative:
att_txt_n = att_txt[:, idx_n]
sim_n = torch.clamp(sim_n + self.eps, max=1).t().pow(att_txt_n).prod(0)
sim = torch.clamp(sim + self.eps, max=1).t().pow(att_txt).prod(0)
if negative:
return D, sim, sim_n
return D, sim
def _encode_txt(self, txt, len_txt):
hi_f = torch.zeros(txt.size(1), 512, device=txt.device)
hi_b = torch.zeros(txt.size(1), 512, device=txt.device)
h_f = []
h_b = []
mask = []
for i in range(txt.size(0)):
mask_i = (txt.size(0) - 1 - i < len_txt).float().unsqueeze(1)
mask.append(mask_i)
hi_f = self.txt_encoder_f(txt[i], hi_f)
h_f.append(hi_f)
hi_b = mask_i * self.txt_encoder_b(txt[-i - 1], hi_b) + (1 - mask_i) * hi_b
h_b.append(hi_b)
mask = torch.stack(mask[::-1])
h_f = torch.stack(h_f) * mask
h_b = torch.stack(h_b[::-1])
u = (h_f + h_b) / 2
m = u.sum(0) / mask.sum(0)
return u, m, mask