-
Notifications
You must be signed in to change notification settings - Fork 2
/
modules_tro.py
321 lines (276 loc) · 12.2 KB
/
modules_tro.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
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 numpy as np
import os
import torch
from torch import nn
from blocks import LinearBlock, Conv2dBlock, ResBlocks, ActFirstResBlock
from vgg_tro_channel3_modi import vgg19_bn
from recognizer.models.encoder_vgg import Encoder as rec_encoder
from recognizer.models.decoder import Decoder as rec_decoder
from recognizer.models.seq2seq import Seq2Seq as rec_seq2seq
from recognizer.models.attention import locationAttention as rec_attention
from load_data import OUTPUT_MAX_LEN, IMG_HEIGHT, IMG_WIDTH, vocab_size, index2letter, num_tokens
import cv2
gpu = torch.device('cuda')
def normalize(tar):
tar = (tar - tar.min())/(tar.max()-tar.min())
tar = tar * 255
tar = tar.astype(np.uint8)
return tar
def fine(label_list):
if type(label_list) != type([]):
return [label_list]
else:
return label_list
def write_image(imga, imgb, xg_list, pred_list, label_list, title):
folder = 'imgs'
if not os.path.exists(folder):
os.makedirs(folder)
batch_size = imga.shape[0]
imga = imga.cpu().numpy()
imgb = imgb.detach().cpu().numpy()
for i in range(len(xg_list)):
xg_list[i] = xg_list[i].cpu().numpy()
for i in range(len(pred_list)):
pred_list[i] = torch.topk(pred_list[i], 1, dim=-1)[1].squeeze(-1) # b,t,83 -> b,t,1 -> b,t
pred_list[i] = pred_list[i].cpu().numpy()
for i in range(len(label_list)):
label_list[i] = label_list[i].cpu().numpy()
outs = list()
for i in range(batch_size):
imgaa = imga[i].squeeze(0)
imgaa = normalize(imgaa)
imgbb = imgb[i].squeeze(0)
imgbb = normalize(imgbb)
new_xg = []
new_pred = []
new_label = []
for xg in xg_list:
new_xg.append(normalize(xg[i].squeeze(0)))
for pred in pred_list:
tmp_pred = fine(pred[i].tolist())
for j in range(num_tokens):
tmp_pred = list(filter(lambda x: x!=j, tmp_pred))
tmp_pred = ''.join([index2letter[c-num_tokens] for c in tmp_pred])
pred_img = np.zeros_like(imgaa)
cv2.putText(pred_img, tmp_pred, (5, 55), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
new_pred.append(pred_img)
for label in label_list:
tmp_label = fine(label[i].tolist())
for j in range(num_tokens):
tmp_label = list(filter(lambda x: x!=j, tmp_label))
tmp_label = ''.join([index2letter[c-num_tokens] for c in tmp_label])
label_img = np.zeros_like(imgaa)
cv2.putText(label_img, tmp_label, (5, 55), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
new_label.append(label_img)
triples = zip(new_xg, new_pred, new_label)
final_triples = []
for triple in triples:
final_triples.append(np.vstack(triple))
final_triples_out = np.vstack(final_triples)
out = np.vstack([imgaa, imgbb, final_triples_out])
out = 255 - out
outs.append(out)
final_out = np.hstack(outs)
cv2.imwrite(folder+'/'+title+'.png', final_out)
def assign_adain_params(adain_params, model):
# assign the adain_params to the AdaIN layers in model
for m in model.modules():
if m.__class__.__name__ == "AdaptiveInstanceNorm2d":
mean = adain_params[:, :m.num_features]
std = adain_params[:, m.num_features:2*m.num_features]
m.bias = mean.contiguous().view(-1)
m.weight = std.contiguous().view(-1)
if adain_params.size(1) > 2*m.num_features:
adain_params = adain_params[:, 2*m.num_features:]
def get_num_adain_params(model):
# return the number of AdaIN parameters needed by the model
num_adain_params = 0
for m in model.modules():
if m.__class__.__name__ == "AdaptiveInstanceNorm2d":
num_adain_params += 2*m.num_features
return num_adain_params
class DisModel(nn.Module):
def __init__(self):
super(DisModel, self).__init__()
self.n_layers = 6
self.final_size = 1024
nf = 16
cnn_f = [Conv2dBlock(1, nf, 7, 1, 3,
pad_type='reflect',
norm='none',
activation='none')]
for i in range(self.n_layers - 1):
nf_out = np.min([nf * 2, 1024])
cnn_f += [ActFirstResBlock(nf, nf, None, 'lrelu', 'none')]
cnn_f += [ActFirstResBlock(nf, nf_out, None, 'lrelu', 'none')]
cnn_f += [nn.ReflectionPad2d(1)]
cnn_f += [nn.AvgPool2d(kernel_size=3, stride=2)]
nf = np.min([nf * 2, 1024])
nf_out = np.min([nf * 2, 1024])
cnn_f += [ActFirstResBlock(nf, nf, None, 'lrelu', 'none')]
cnn_f += [ActFirstResBlock(nf, nf_out, None, 'lrelu', 'none')]
cnn_c = [Conv2dBlock(nf_out, self.final_size, IMG_HEIGHT//(2**(self.n_layers-1)), IMG_WIDTH//(2**(self.n_layers-1))+1,
norm='none',
activation='lrelu',
activation_first=True)]
self.cnn_f = nn.Sequential(*cnn_f)
self.cnn_c = nn.Sequential(*cnn_c)
self.bce = nn.BCEWithLogitsLoss()
def forward(self, x):
feat = self.cnn_f(x)
out = self.cnn_c(feat)
return out.squeeze(-1).squeeze(-1) # b,1024 maybe b is also 1, so cannnot out.squeeze()
def calc_dis_fake_loss(self, input_fake):
label = torch.zeros(input_fake.shape[0], self.final_size).to(gpu)
resp_fake = self.forward(input_fake)
fake_loss = self.bce(resp_fake, label)
return fake_loss
def calc_dis_real_loss(self, input_real):
label = torch.ones(input_real.shape[0], self.final_size).to(gpu)
resp_real = self.forward(input_real)
real_loss = self.bce(resp_real, label)
return real_loss
def calc_gen_loss(self, input_fake):
label = torch.ones(input_fake.shape[0], self.final_size).to(gpu)
resp_fake = self.forward(input_fake)
fake_loss = self.bce(resp_fake, label)
return fake_loss
class WriterClaModel(nn.Module):
def __init__(self, num_writers):
super(WriterClaModel, self).__init__()
self.n_layers = 6
nf = 16
cnn_f = [Conv2dBlock(1, nf, 7, 1, 3,
pad_type='reflect',
norm='none',
activation='none')]
for i in range(self.n_layers - 1):
nf_out = np.min([nf * 2, 1024])
cnn_f += [ActFirstResBlock(nf, nf, None, 'lrelu', 'none')]
cnn_f += [ActFirstResBlock(nf, nf_out, None, 'lrelu', 'none')]
cnn_f += [nn.ReflectionPad2d(1)]
cnn_f += [nn.AvgPool2d(kernel_size=3, stride=2)]
nf = np.min([nf * 2, 1024])
nf_out = np.min([nf * 2, 1024])
cnn_f += [ActFirstResBlock(nf, nf, None, 'lrelu', 'none')]
cnn_f += [ActFirstResBlock(nf, nf_out, None, 'lrelu', 'none')]
cnn_c = [Conv2dBlock(nf_out, num_writers, IMG_HEIGHT//(2**(self.n_layers-1)), IMG_WIDTH//(2**(self.n_layers-1))+1,
norm='none',
activation='lrelu',
activation_first=True)]
self.cnn_f = nn.Sequential(*cnn_f)
self.cnn_c = nn.Sequential(*cnn_c)
self.cross_entropy = nn.CrossEntropyLoss()
def forward(self, x, y):
feat = self.cnn_f(x)
out = self.cnn_c(feat) # b,310,1,1
loss = self.cross_entropy(out.squeeze(-1).squeeze(-1), y)
return loss
'''VGG19_IN tro'''
class ImageEncoder(nn.Module):
def __init__(self):
super(ImageEncoder, self).__init__()
self.model = vgg19_bn(False)
self.output_dim = 512
def forward(self, x):
return self.model(x)
class GenModel_FC(nn.Module):
def __init__(self, text_max_len):
super(GenModel_FC, self).__init__()
self.enc_image = ImageEncoder().to(gpu)
self.rec = RecModel().to(gpu)
self.enc_text = self.rec.enc
self.dec = Decoder().to(gpu)
self.linear_mix = nn.Linear(1024, 512)
def decode(self, content, adain_params):
# decode content and style codes to an image
assign_adain_params(adain_params, self.dec)
images = self.dec(content)
return images
def mix(self, feat_xs, feat_embed):
feat_mix = torch.cat([feat_xs, feat_embed], dim=1) # b,1024,8,27
f = feat_mix.permute(0, 2, 3, 1)
ff = self.linear_mix(f) # b,8,27,1024->b,8,27,512
return ff.permute(0, 3, 1, 2)
class RecModel(nn.Module):
def __init__(self, pretrain=False):
super(RecModel, self).__init__()
hidden_size_enc = hidden_size_dec = 512
embed_size = 60
self.enc = rec_encoder(hidden_size_enc, IMG_HEIGHT, IMG_WIDTH, True, None, False).to(gpu)
self.dec = rec_decoder(hidden_size_dec, embed_size, vocab_size, rec_attention, None).to(gpu)
self.seq2seq = rec_seq2seq(self.enc, self.dec, OUTPUT_MAX_LEN, vocab_size).to(gpu)
if pretrain:
model_file = 'recognizer/save_weights/seq2seq-72.model_5.79.bak'
print('Loading RecModel', model_file)
self.seq2seq.load_state_dict(torch.load(model_file))
# mode: image o feature
def forward(self, inp, label, img_width, mode):
self.seq2seq.train()
output, attn_weights = self.seq2seq(inp, label, img_width, mode, teacher_rate=False, train=False)
return output.permute(1, 0, 2) # t,b,83->b,t,83
class TextEncoder_FC(nn.Module):
def __init__(self, text_max_len):
super(TextEncoder_FC, self).__init__()
embed_size = 64
self.embed = nn.Embedding(vocab_size, embed_size)
self.fc = nn.Sequential(
nn.Linear(text_max_len*embed_size, 1024),
nn.BatchNorm1d(1024),
nn.ReLU(inplace=True),
nn.Linear(1024, 2048),
nn.BatchNorm1d(2048),
nn.ReLU(inplace=True),
nn.Linear(2048, 4096)
)
'''embed content force'''
self.linear = nn.Linear(embed_size, 512)
def forward(self, x):
xx = self.embed(x) # b,t,embed
batch_size = xx.shape[0]
xxx = xx.reshape(batch_size, -1) # b,t*embed
out = self.fc(xxx)
'''embed content force'''
xx_new = self.linear(xx) # b,9,512
ts = xx_new.shape[1]
tensor_list = list()
for i in range(ts):
# hard code mierda!!! 3=27/9 img:b,512,8,27 text:b,9,512
tmp = torch.cat([xx_new[:, i:i+1]]*3, dim=1)
tensor_list.append(tmp)
res = torch.cat(tensor_list, dim=1) # b,3*9,512
res = res.permute(0, 2, 1).unsqueeze(2) # b,512,1,3*9
final_res = torch.cat([res]*8, dim=2) # hard code mierda!!!
return out, final_res
class Decoder(nn.Module):
def __init__(self, ups=3, n_res=2, dim=512, out_dim=1, res_norm='adain', activ='relu', pad_type='reflect'):
super(Decoder, self).__init__()
self.model = []
self.model += [ResBlocks(n_res, dim, res_norm,
activ, pad_type=pad_type)]
for i in range(ups):
self.model += [nn.Upsample(scale_factor=2),
Conv2dBlock(dim, dim // 2, 5, 1, 2,
norm='in',
activation=activ,
pad_type=pad_type)]
dim //= 2
self.model += [Conv2dBlock(dim, out_dim, 7, 1, 3,
norm='none',
activation='tanh',
pad_type=pad_type)]
self.model = nn.Sequential(*self.model)
def forward(self, x):
return self.model(x)
class MLP(nn.Module):
def __init__(self, in_dim=64, out_dim=4096, dim=256, n_blk=3, norm='none', activ='relu'):
super(MLP, self).__init__()
self.model = []
self.model += [LinearBlock(in_dim, dim, norm=norm, activation=activ)]
for i in range(n_blk - 2):
self.model += [LinearBlock(dim, dim, norm=norm, activation=activ)]
self.model += [LinearBlock(dim, out_dim,
norm='none', activation='none')]
self.model = nn.Sequential(*self.model)
def forward(self, x):
return self.model(x.view(x.size(0), -1))