-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
1416 lines (1184 loc) · 60.1 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torch
import torch.nn as nn
import copy
import torchvision.models as models
import torch.nn.functional as F
from transformers import BertModel, BertPreTrainedModel
from torch.nn.utils import weight_norm as wn
from torch.nn.parameter import Parameter
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence
import numpy as np
import pdb
from typing import List, Optional
class CrossReplaceTransformerLayer(nn.Module):
def __init__(self, d_model, nhead, theta, skip_connection=False, use_quantile=False, dim_feedforward=2048, dropout=0.1, activation=F.relu, layer_norm_eps=1e-5,
batch_first=True, norm_first=False, device=None, dtype=None):
factory_kwargs = {'device': device, 'dtype': dtype}
super(CrossReplaceTransformerLayer, self).__init__()
self.multihead_attn = nn.MultiheadAttention(
d_model, nhead, dropout=dropout, batch_first=batch_first, **factory_kwargs)
# feedforward model
self.linear1 = nn.Linear(d_model, dim_feedforward, **factory_kwargs)
self.dropout = nn.Dropout(dropout)
self.linear2 = nn.Linear(dim_feedforward, d_model, **factory_kwargs)
self.norm_first = norm_first
self.norm1 = nn.LayerNorm(
d_model, eps=layer_norm_eps, **factory_kwargs)
self.norm2 = nn.LayerNorm(
d_model, eps=layer_norm_eps, **factory_kwargs)
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
self.activatiion = activation
self.theta = theta # select the attention value
self.skip_connection = skip_connection
self.use_quantile = use_quantile
# self-attention block
def _sa_block(self, x, attn_mask, key_padding_mask):
x, attn_weight = self.multihead_attn(
x, x, x, attn_mask=attn_mask, key_padding_mask=key_padding_mask, need_weights=True)
return self.dropout1(x), attn_weight
# feedforward block
def _ff_block(self, x):
x = self.linear2(self.dropout2(self.activatiion(self.linear1(x))))
return self.dropout2(x)
# cross-replace block
# notice: batch_first must be true
def _cr_block(self, x1, x2, attn_weight1, attn_weight2):
cls_weight1 = attn_weight1[:, 0, :]
cls_weight2 = attn_weight2[:, 0, :]
# print('cls_weight1', cls_weight1.shape)
# print(cls_weight1)
# print('cls_weight2', cls_weight2.shape)
# print(cls_weight2)
x1_mean = torch.mean(x1, dim=-2)
x2_mean = torch.mean(x2, dim=-2)
for i in range(cls_weight1.shape[0]):
if self.use_quantile:
theta1 = np.quantile(
cls_weight1[i][1:].detach().cpu().numpy(), self.theta)
theta2 = np.quantile(
cls_weight2[i][1:].detach().cpu().numpy(), self.theta)
else:
theta1 = self.theta
theta2 = self.theta
# except the first token, namely [cls]
for j in range(1, cls_weight1.shape[1]):
if cls_weight1[i][j] < theta1:
x1[i][j] = x2_mean[i] + \
x1[i][j] if self.skip_connection else x2_mean[i]
if cls_weight2[i][j] < theta2:
x2[i][j] = x1_mean[i] + \
x2[i][j] if self.skip_connection else x1_mean[i]
return x1, x2
def forward(self, src1, src2, replace=False, src1_mask=None, src1_key_padding_mask=None, src2_mask=None, src2_key_padding_mask=None):
x1 = src1
x2 = src2
if self.norm_first:
res1, attn_weight1 = self._sa_block(
self.norm1(x1), src1_mask, src1_key_padding_mask)
res2, attn_weight2 = self._sa_block(
self.norm1(x2), src2_mask, src2_key_padding_mask)
x1 = x1 + res1
x2 = x2 + res2
if replace:
x1, x2 = self._cr_block(x1, x2, attn_weight1, attn_weight2)
x1 = x1 + self._ff_block(self.norm2(x1))
x2 = x2 + self._ff_block(self.norm2(x2))
else:
res1, attn_weight1 = self._sa_block(
x1, src1_mask, src1_key_padding_mask)
res2, attn_weight2 = self._sa_block(
x2, src2_mask, src2_key_padding_mask)
x1 = self.norm1(x1 + res1)
x2 = self.norm1(x2 + res2)
if replace:
x1, x2 = self._cr_block(x1, x2, attn_weight1, attn_weight2)
x1 = self.norm2(x1 + self._ff_block(x1))
x2 = self.norm2(x2 + self._ff_block(x2))
return x1, x2
def _get_clones(module, N):
return nn.ModuleList([copy.deepcopy(module) for i in range(N)])
class CrossReplaceTransformer(nn.Module):
def __init__(self, encoder_layer, num_layers, replace_start, replace_end, norm=None):
super(CrossReplaceTransformer, self).__init__()
self.layers = _get_clones(encoder_layer, num_layers)
self.num_layers = num_layers
self.norm = norm
self.replace_start = replace_start
self.replace_end = replace_end
def forward(self, src1, src2, mask1=None, src1_key_padding_mask=None, mask2=None, src2_key_padding_mask=None):
output1 = src1
output2 = src2
# print('output1', output1.shape)
# print(output1)
# print('output2', output2.shape)
# print(output2)
for i, mod in enumerate(self.layers):
if i >= self.replace_start and i <= self.replace_end:
replace = True
else:
replace = False
output1, output2 = mod(output1, output2, replace=replace, src1_mask=mask1,
src1_key_padding_mask=src1_key_padding_mask, src2_mask=mask2, src2_key_padding_mask=src2_key_padding_mask)
# print('output1', output1.shape)
# print(output1)
# print('output2', output2.shape)
# print(output2)
if self.norm is not None:
output1 = self.norm(output1)
output2 = self.norm(output2)
return output1, output2
class ImageEncoder(nn.Module):
def __init__(self, resnet_pretrained_dir=None, encoded_image_size=8):
super(ImageEncoder, self).__init__()
self.enc_image_size = encoded_image_size
resnet = models.resnet152()
resnet.load_state_dict(torch.load(resnet_pretrained_dir))
# Remove linear and pool layers (since we're not doing classification)
modules = list(resnet.children())[:-2]
self.resnet = nn.Sequential(*modules)
# Resize image to fixed size to allow input images of variable size
self.adaptive_pool = nn.AdaptiveAvgPool2d(
(encoded_image_size, encoded_image_size))
self.fine_tune()
self.fc = nn.Linear(2048, 768)
def forward(self, images):
"""
Forward propagation.
:param images: images, a tensor of dimensions (batch_size, 3, image_size, image_size)
:return: encoded images
"""
out = self.resnet(
images) # (batch_size, 2048, image_size/32, image_size/32)
# (batch_size, 2048, encoded_image_size, encoded_image_size)
out = self.adaptive_pool(out)
# (batch_size, encoded_image_size, encoded_image_size, 2048)
out = out.permute(0, 2, 3, 1)
out = self.fc(out)
return out
def fine_tune(self, fine_tune=True):
"""
Allow or prevent the computation of gradients for convolutional blocks 2 through 4 of the encoder.
:param fine_tune: Allow?
"""
for p in self.resnet.parameters():
p.requires_grad = False
# If fine-tuning, only fine-tune convolutional blocks 2 through 4
for c in list(self.resnet.children())[5:]:
for p in c.parameters():
p.requires_grad = fine_tune
class Attention(nn.Module):
"""
Attention Network.
"""
def __init__(self, encoder_dim, decoder_dim, attention_dim):
"""
:param encoder_dim: feature size of encoded images
:param decoder_dim: size of decoder's RNN
:param attention_dim: size of the attention network
"""
super(Attention, self).__init__()
# linear layer to transform encoded image
self.encoder_att = nn.Linear(encoder_dim, attention_dim)
# linear layer to transform decoder's output
self.decoder_att = nn.Linear(decoder_dim, attention_dim)
# linear layer to calculate values to be softmax-ed
self.full_att = nn.Linear(attention_dim, 1)
self.relu = nn.ReLU()
self.softmax = nn.Softmax(dim=1) # softmax layer to calculate weights
def forward(self, encoder_out, decoder_hidden):
"""
Forward propagation.
:param encoder_out: encoded images, a tensor of dimension (batch_size, num_pixels, encoder_dim)
:param decoder_hidden: previous decoder output, a tensor of dimension (batch_size, decoder_dim)
:return: attention weighted encoding, weights
"""
att1 = self.encoder_att(
encoder_out) # (batch_size, num_pixels, attention_dim)
att2 = self.decoder_att(decoder_hidden) # (batch_size, attention_dim)
# (batch_size, num_pixels)
att = self.full_att(self.relu(att1 + att2.unsqueeze(1))).squeeze(2)
alpha = self.softmax(att) # (batch_size, num_pixels)
attention_weighted_encoding = (
encoder_out * alpha.unsqueeze(2)).sum(dim=1) # (batch_size, encoder_dim)
return attention_weighted_encoding, alpha
class TextDecoder(nn.Module):
"""
Decoder.
"""
def __init__(self, attention_dim, embed_dim, decoder_dim, vocab_size, encoder_dim=768, dropout=0.5, device=None):
"""
:param attention_dim: size of attention network
:param embed_dim: embedding size
:param decoder_dim: size of decoder's RNN
:param vocab_size: size of vocabulary
:param encoder_dim: feature size of encoded images
:param dropout: dropout
"""
super(TextDecoder, self).__init__()
self.encoder_dim = encoder_dim
self.attention_dim = attention_dim
self.embed_dim = embed_dim
self.decoder_dim = decoder_dim
self.vocab_size = vocab_size
self.dropout = dropout
self.device = device
self.attention = Attention(
encoder_dim, decoder_dim, attention_dim) # attention network
self.embedding = nn.Embedding(vocab_size, embed_dim) # embedding layer
self.dropout = nn.Dropout(p=self.dropout)
self.decode_step = nn.LSTMCell(
embed_dim + encoder_dim, decoder_dim, bias=True) # decoding LSTMCell
# linear layer to find initial hidden state of LSTMCell
self.init_h = nn.Linear(encoder_dim, decoder_dim)
# linear layer to find initial cell state of LSTMCell
self.init_c = nn.Linear(encoder_dim, decoder_dim)
# linear layer to create a sigmoid-activated gate
self.f_beta = nn.Linear(decoder_dim, encoder_dim)
self.sigmoid = nn.Sigmoid()
# linear layer to find scores over vocabulary
self.fc = nn.Linear(decoder_dim, vocab_size)
self.init_weights() # initialize some layers with the uniform distribution
def init_weights(self):
"""
Initializes some parameters with values from the uniform distribution, for easier convergence.
"""
self.embedding.weight.data.uniform_(-0.1, 0.1)
self.fc.bias.data.fill_(0)
self.fc.weight.data.uniform_(-0.1, 0.1)
def load_pretrained_embeddings(self, embeddings):
"""
Loads embedding layer with pre-trained embeddings.
:param embeddings: pre-trained embeddings
"""
self.embedding.weight = nn.Parameter(embeddings)
def fine_tune_embeddings(self, fine_tune=True):
"""
Allow fine-tuning of embedding layer? (Only makes sense to not-allow if using pre-trained embeddings).
:param fine_tune: Allow?
"""
for p in self.embedding.parameters():
p.requires_grad = fine_tune
def init_hidden_state(self, encoder_out):
"""
Creates the initial hidden and cell states for the decoder's LSTM based on the encoded images.
:param encoder_out: encoded images, a tensor of dimension (batch_size, num_pixels, encoder_dim)
:return: hidden state, cell state
"""
mean_encoder_out = encoder_out.mean(dim=1) # Averaging the second dimension, if flattened, is equivalent to finding the average of all pixels in each layer
h = self.init_h(mean_encoder_out) # (batch_size, decoder_dim) (batch_size,2048)==>(batch_size,512)
c = self.init_c(mean_encoder_out) # Hidden layer h, starting value of c
return h, c
def forward(self, encoder_out, encoded_captions, caption_lengths):
"""
Forward propagation.
:param encoder_out: encoded images, a tensor of dimension (batch_size, enc_image_size, enc_image_size, encoder_dim)
:param encoded_captions: encoded captions, a tensor of dimension (batch_size, max_caption_length)
:param caption_lengths: caption lengths, a tensor of dimension (batch_size, 1)
:return: scores for vocabulary, sorted encoded captions, decode lengths, weights, sort indices
"""
batch_size = encoder_out.size(0)
encoder_dim = encoder_out.size(-1)
vocab_size = self.vocab_size
# Flatten image
# (batch_size, num_pixels, encoder_dim)
encoder_out = encoder_out.view(batch_size, -1, encoder_dim)
num_pixels = encoder_out.size(1)
# Sort input data by decreasing lengths; why? apparent below
caption_lengths, sort_ind = caption_lengths.squeeze(
1).sort(dim=0, descending=True)
encoder_out = encoder_out[sort_ind]
encoded_captions = encoded_captions[sort_ind]
# Embedding
# (batch_size, max_caption_length, embed_dim) Each word is represented by a 512 dimensional vector
embeddings = self.embedding(encoded_captions)
# Initialize LSTM state
h, c = self.init_hidden_state(encoder_out) # (batch_size, decoder_dim)
# We won't decode at the <end> position, since we've finished generating as soon as we generate <end>
# So, decoding lengths are actual lengths - 1
decode_lengths = (caption_lengths - 1).tolist()
# Create tensors to hold word predicion scores and alphas
predictions = torch.zeros(batch_size, max(
decode_lengths), vocab_size).to(self.device)
alphas = torch.zeros(batch_size, max(
decode_lengths), num_pixels).to(self.device)
# At each time-step, decode by
# attention-weighing the encoder's output based on the decoder's previous hidden state output
# then generate a new word in the decoder with the previous word and the attention weighted encoding
# print('embeddings', embeddings.shape)
for t in range(max(decode_lengths)):
batch_size_t = sum([l > t for l in decode_lengths])
attention_weighted_encoding, alpha = self.attention(encoder_out[:batch_size_t],
h[:batch_size_t])
# gating scalar, (batch_size_t, encoder_dim)
gate = self.sigmoid(self.f_beta(h[:batch_size_t]))
attention_weighted_encoding = gate * attention_weighted_encoding
# print('batch_size_t', batch_size_t)
# print('t', t)
h, c = self.decode_step(
torch.cat([embeddings[:batch_size_t, t, :],
attention_weighted_encoding], dim=1),
(h[:batch_size_t], c[:batch_size_t])) # (batch_size_t, decoder_dim)
preds = self.fc(self.dropout(h)) # (batch_size_t, vocab_size)
predictions[:batch_size_t, t, :] = preds # Generate predictions for each layer
alphas[:batch_size_t, t, :] = alpha # Key areas for each pixel
return predictions, encoded_captions, decode_lengths, alphas, sort_ind
class TextEncoder(nn.Module):
def __init__(self, bert, use_xlmr=False):
super(TextEncoder, self).__init__()
self.bert = bert
self.use_xlmr = use_xlmr
def forward(self, input_ids, attention_mask=None, token_type_ids=None):
if not self.use_xlmr:
outputs = self.bert(
input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids)
else:
outputs = self.bert(input_ids=input_ids,
attention_mask=attention_mask)
# print('outputs type:', type(outputs))
# print('outputs:', outputs)
return outputs.last_hidden_state, outputs.pooler_output
def concat_elu(x):
""" like concatenated ReLU (http://arxiv.org/abs/1603.05201), but then with ELU """
# Pytorch ordering
axis = len(x.size()) - 3
return F.elu(torch.cat([x, -x], dim=axis))
def log_sum_exp(x):
""" numerically stable log_sum_exp implementation that prevents overflow """
# TF ordering
axis = len(x.size()) - 1
m, _ = torch.max(x, dim=axis)
m2, _ = torch.max(x, dim=axis, keepdim=True)
return m + torch.log(torch.sum(torch.exp(x - m2), dim=axis))
def log_prob_from_logits(x):
""" numerically stable log_softmax implementation that prevents overflow """
# TF ordering
axis = len(x.size()) - 1
m, _ = torch.max(x, dim=axis, keepdim=True)
return x - m - torch.log(torch.sum(torch.exp(x - m), dim=axis, keepdim=True))
def discretized_mix_logistic_loss(x, l):
""" log-likelihood for mixture of discretized logistics, assumes the data has been rescaled to [-1,1] interval """
# Pytorch ordering
x = x.permute(0, 2, 3, 1)
l = l.permute(0, 2, 3, 1)
xs = [int(y) for y in x.size()]
ls = [int(y) for y in l.size()]
# here and below: unpacking the params of the mixture of logistics
nr_mix = int(ls[-1] / 10)
logit_probs = l[:, :, :, :nr_mix]
l = l[:, :, :, nr_mix:].contiguous().view(
xs + [nr_mix * 3]) # 3 for mean, scale, coef
means = l[:, :, :, :, :nr_mix]
# log_scales = torch.max(l[:, :, :, :, nr_mix:2 * nr_mix], -7.)
log_scales = torch.clamp(l[:, :, :, :, nr_mix:2 * nr_mix], min=-7.)
coeffs = F.tanh(l[:, :, :, :, 2 * nr_mix:3 * nr_mix])
# here and below: getting the means and adjusting them based on preceding
# sub-pixels
x = x.contiguous()
x = x.unsqueeze(-1) + Variable(torch.zeros(xs +
[nr_mix]).cuda(), requires_grad=False)
m2 = (means[:, :, :, 1, :] + coeffs[:, :, :, 0, :]
* x[:, :, :, 0, :]).view(xs[0], xs[1], xs[2], 1, nr_mix)
m3 = (means[:, :, :, 2, :] + coeffs[:, :, :, 1, :] * x[:, :, :, 0, :] +
coeffs[:, :, :, 2, :] * x[:, :, :, 1, :]).view(xs[0], xs[1], xs[2], 1, nr_mix)
means = torch.cat((means[:, :, :, 0, :].unsqueeze(3), m2, m3), dim=3)
centered_x = x - means
inv_stdv = torch.exp(-log_scales)
plus_in = inv_stdv * (centered_x + 1. / 255.)
cdf_plus = torch.sigmoid(plus_in)
min_in = inv_stdv * (centered_x - 1. / 255.)
cdf_min = torch.sigmoid(min_in)
# log probability for edge case of 0 (before scaling)
log_cdf_plus = plus_in - F.softplus(plus_in)
# log probability for edge case of 255 (before scaling)
log_one_minus_cdf_min = -F.softplus(min_in)
cdf_delta = cdf_plus - cdf_min # probability for all other cases
mid_in = inv_stdv * centered_x
# log probability in the center of the bin, to be used in extreme cases
# (not actually used in our code)
log_pdf_mid = mid_in - log_scales - 2. * F.softplus(mid_in)
# now select the right output: left edge case, right edge case, normal
# case, extremely low prob case (doesn't actually happen for us)
# this is what we are really doing, but using the robust version below for extreme cases in other applications and to avoid NaN issue with tf.select()
# log_probs = tf.select(x < -0.999, log_cdf_plus, tf.select(x > 0.999, log_one_minus_cdf_min, tf.log(cdf_delta)))
# robust version, that still works if probabilities are below 1e-5 (which never happens in our code)
# tensorflow backpropagates through tf.select() by multiplying with zero instead of selecting: this requires use to use some ugly tricks to avoid potential NaNs
# the 1e-12 in tf.maximum(cdf_delta, 1e-12) is never actually used as output, it's purely there to get around the tf.select() gradient issue
# if the probability on a sub-pixel is below 1e-5, we use an approximation
# based on the assumption that the log-density is constant in the bin of
# the observed sub-pixel value
inner_inner_cond = (cdf_delta > 1e-5).float()
inner_inner_out = inner_inner_cond * torch.log(torch.clamp(
cdf_delta, min=1e-12)) + (1. - inner_inner_cond) * (log_pdf_mid - np.log(127.5))
inner_cond = (x > 0.999).float()
inner_out = inner_cond * log_one_minus_cdf_min + \
(1. - inner_cond) * inner_inner_out
cond = (x < -0.999).float()
log_probs = cond * log_cdf_plus + (1. - cond) * inner_out
log_probs = torch.sum(log_probs, dim=3) + log_prob_from_logits(logit_probs)
return -torch.sum(log_sum_exp(log_probs))
def discretized_mix_logistic_loss_1d(x, l):
""" log-likelihood for mixture of discretized logistics, assumes the data has been rescaled to [-1,1] interval """
# Pytorch ordering
x = x.permute(0, 2, 3, 1)
l = l.permute(0, 2, 3, 1)
xs = [int(y) for y in x.size()]
ls = [int(y) for y in l.size()]
# here and below: unpacking the params of the mixture of logistics
nr_mix = int(ls[-1] / 3)
logit_probs = l[:, :, :, :nr_mix]
l = l[:, :, :, nr_mix:].contiguous().view(
xs + [nr_mix * 2]) # 2 for mean, scale
means = l[:, :, :, :, :nr_mix]
log_scales = torch.clamp(l[:, :, :, :, nr_mix:2 * nr_mix], min=-7.)
# here and below: getting the means and adjusting them based on preceding
# sub-pixels
x = x.contiguous()
x = x.unsqueeze(-1) + Variable(torch.zeros(xs +
[nr_mix]).cuda(), requires_grad=False)
# means = torch.cat((means[:, :, :, 0, :].unsqueeze(3), m2, m3), dim=3)
centered_x = x - means
inv_stdv = torch.exp(-log_scales)
plus_in = inv_stdv * (centered_x + 1. / 255.)
cdf_plus = torch.sigmoid(plus_in)
min_in = inv_stdv * (centered_x - 1. / 255.)
cdf_min = torch.sigmoid(min_in)
# log probability for edge case of 0 (before scaling)
log_cdf_plus = plus_in - F.softplus(plus_in)
# log probability for edge case of 255 (before scaling)
log_one_minus_cdf_min = -F.softplus(min_in)
cdf_delta = cdf_plus - cdf_min # probability for all other cases
mid_in = inv_stdv * centered_x
# log probability in the center of the bin, to be used in extreme cases
# (not actually used in our code)
log_pdf_mid = mid_in - log_scales - 2. * F.softplus(mid_in)
inner_inner_cond = (cdf_delta > 1e-5).float()
inner_inner_out = inner_inner_cond * torch.log(torch.clamp(
cdf_delta, min=1e-12)) + (1. - inner_inner_cond) * (log_pdf_mid - np.log(127.5))
inner_cond = (x > 0.999).float()
inner_out = inner_cond * log_one_minus_cdf_min + \
(1. - inner_cond) * inner_inner_out
cond = (x < -0.999).float()
log_probs = cond * log_cdf_plus + (1. - cond) * inner_out
log_probs = torch.sum(log_probs, dim=3) + log_prob_from_logits(logit_probs)
return -torch.sum(log_sum_exp(log_probs))
def to_one_hot(tensor, n, fill_with=1.):
# we perform one hot encore with respect to the last axis
one_hot = torch.FloatTensor(tensor.size() + (n,)).zero_()
if tensor.is_cuda:
one_hot = one_hot.cuda()
one_hot.scatter_(len(tensor.size()), tensor.unsqueeze(-1), fill_with)
return Variable(one_hot)
def sample_from_discretized_mix_logistic_1d(l, nr_mix):
# Pytorch ordering
l = l.permute(0, 2, 3, 1)
ls = [int(y) for y in l.size()]
xs = ls[:-1] + [1] # [3]
# unpack parameters
logit_probs = l[:, :, :, :nr_mix]
l = l[:, :, :, nr_mix:].contiguous().view(
xs + [nr_mix * 2]) # for mean, scale
# sample mixture indicator from softmax
temp = torch.FloatTensor(logit_probs.size())
if l.is_cuda:
temp = temp.cuda()
temp.uniform_(1e-5, 1. - 1e-5)
temp = logit_probs.data - torch.log(- torch.log(temp))
_, argmax = temp.max(dim=3)
one_hot = to_one_hot(argmax, nr_mix)
sel = one_hot.view(xs[:-1] + [1, nr_mix])
# select logistic parameters
means = torch.sum(l[:, :, :, :, :nr_mix] * sel, dim=4)
log_scales = torch.clamp(torch.sum(
l[:, :, :, :, nr_mix:2 * nr_mix] * sel, dim=4), min=-7.)
u = torch.FloatTensor(means.size())
if l.is_cuda:
u = u.cuda()
u.uniform_(1e-5, 1. - 1e-5)
u = Variable(u)
x = means + torch.exp(log_scales) * (torch.log(u) - torch.log(1. - u))
x0 = torch.clamp(torch.clamp(x[:, :, :, 0], min=-1.), max=1.)
out = x0.unsqueeze(1)
return out
def sample_from_discretized_mix_logistic(l, nr_mix):
# Pytorch ordering
l = l.permute(0, 2, 3, 1)
ls = [int(y) for y in l.size()]
xs = ls[:-1] + [3]
# unpack parameters
logit_probs = l[:, :, :, :nr_mix]
l = l[:, :, :, nr_mix:].contiguous().view(xs + [nr_mix * 3])
# sample mixture indicator from softmax
temp = torch.FloatTensor(logit_probs.size())
if l.is_cuda:
temp = temp.cuda()
temp.uniform_(1e-5, 1. - 1e-5)
temp = logit_probs.data - torch.log(- torch.log(temp))
_, argmax = temp.max(dim=3)
one_hot = to_one_hot(argmax, nr_mix)
sel = one_hot.view(xs[:-1] + [1, nr_mix])
# select logistic parameters
means = torch.sum(l[:, :, :, :, :nr_mix] * sel, dim=4)
log_scales = torch.clamp(torch.sum(
l[:, :, :, :, nr_mix:2 * nr_mix] * sel, dim=4), min=-7.)
coeffs = torch.sum(F.tanh(
l[:, :, :, :, 2 * nr_mix:3 * nr_mix]) * sel, dim=4)
# sample from logistic & clip to interval
# we don't actually round to the nearest 8bit value when sampling
u = torch.FloatTensor(means.size())
if l.is_cuda:
u = u.cuda()
u.uniform_(1e-5, 1. - 1e-5)
u = Variable(u)
x = means + torch.exp(log_scales) * (torch.log(u) - torch.log(1. - u))
x0 = torch.clamp(torch.clamp(x[:, :, :, 0], min=-1.), max=1.)
x1 = torch.clamp(torch.clamp(
x[:, :, :, 1] + coeffs[:, :, :, 0] * x0, min=-1.), max=1.)
x2 = torch.clamp(torch.clamp(
x[:, :, :, 2] + coeffs[:, :, :, 1] * x0 + coeffs[:, :, :, 2] * x1, min=-1.), max=1.)
out = torch.cat([x0.view(xs[:-1] + [1]),
x1.view(xs[:-1] + [1]), x2.view(xs[:-1] + [1])], dim=3)
# put back in Pytorch ordering
out = out.permute(0, 3, 1, 2)
return out
''' utilities for shifting the image around, efficient alternative to masking convolutions '''
def down_shift(x, pad=None):
# Pytorch ordering
xs = [int(y) for y in x.size()]
# when downshifting, the last row is removed
x = x[:, :, :xs[2] - 1, :]
# padding left, padding right, padding top, padding bottom
pad = nn.ZeroPad2d((0, 0, 1, 0)) if pad is None else pad
return pad(x)
def right_shift(x, pad=None):
# Pytorch ordering
xs = [int(y) for y in x.size()]
# when righshifting, the last column is removed
x = x[:, :, :, :xs[3] - 1]
# padding left, padding right, padding top, padding bottom
pad = nn.ZeroPad2d((1, 0, 0, 0)) if pad is None else pad
return pad(x)
def load_part_of_model(model, path):
params = torch.load(path)
added = 0
for name, param in params.items():
if name in model.state_dict().keys():
try:
model.state_dict()[name].copy_(param)
added += 1
except Exception as e:
print(e)
pass
print('added %s of params:' %
(added / float(len(model.state_dict().keys()))))
class nin(nn.Module):
def __init__(self, dim_in, dim_out):
super(nin, self).__init__()
self.lin_a = wn(nn.Linear(dim_in, dim_out))
self.dim_out = dim_out
def forward(self, x):
og_x = x
# assumes pytorch ordering
""" a network in network layer (1x1 CONV) """
# TODO : try with original ordering
x = x.permute(0, 2, 3, 1)
shp = [int(y) for y in x.size()]
out = self.lin_a(x.contiguous().view(shp[0]*shp[1]*shp[2], shp[3]))
shp[-1] = self.dim_out
out = out.view(shp)
return out.permute(0, 3, 1, 2)
class down_shifted_conv2d(nn.Module):
def __init__(self, num_filters_in, num_filters_out, filter_size=(2, 3), stride=(1, 1),
shift_output_down=False, norm='weight_norm'):
super(down_shifted_conv2d, self).__init__()
assert norm in [None, 'batch_norm', 'weight_norm']
self.conv = nn.Conv2d(
num_filters_in, num_filters_out, filter_size, stride)
self.shift_output_down = shift_output_down
self.norm = norm
self.pad = nn.ZeroPad2d((int((filter_size[1] - 1) / 2), # pad left
int((filter_size[1] - 1) / 2), # pad right
filter_size[0] - 1, # pad top
0)) # pad down
if norm == 'weight_norm':
self.conv = wn(self.conv)
elif norm == 'batch_norm':
self.bn = nn.BatchNorm2d(num_filters_out)
if shift_output_down:
self.down_shift = lambda x: down_shift(
x, pad=nn.ZeroPad2d((0, 0, 1, 0)))
def forward(self, x):
x = self.pad(x)
x = self.conv(x)
x = self.bn(x) if self.norm == 'batch_norm' else x
return self.down_shift(x) if self.shift_output_down else x
class down_shifted_deconv2d(nn.Module):
def __init__(self, num_filters_in, num_filters_out, filter_size=(2, 3), stride=(1, 1)):
super(down_shifted_deconv2d, self).__init__()
self.deconv = wn(nn.ConvTranspose2d(num_filters_in, num_filters_out, filter_size, stride,
output_padding=1))
self.filter_size = filter_size
self.stride = stride
def forward(self, x):
x = self.deconv(x)
xs = [int(y) for y in x.size()]
return x[:, :, :(xs[2] - self.filter_size[0] + 1),
int((self.filter_size[1] - 1) / 2):(xs[3] - int((self.filter_size[1] - 1) / 2))]
class down_right_shifted_conv2d(nn.Module):
def __init__(self, num_filters_in, num_filters_out, filter_size=(2, 2), stride=(1, 1),
shift_output_right=False, norm='weight_norm'):
super(down_right_shifted_conv2d, self).__init__()
assert norm in [None, 'batch_norm', 'weight_norm']
self.pad = nn.ZeroPad2d((filter_size[1] - 1, 0, filter_size[0] - 1, 0))
self.conv = nn.Conv2d(
num_filters_in, num_filters_out, filter_size, stride=stride)
self.shift_output_right = shift_output_right
self.norm = norm
if norm == 'weight_norm':
self.conv = wn(self.conv)
elif norm == 'batch_norm':
self.bn = nn.BatchNorm2d(num_filters_out)
if shift_output_right:
self.right_shift = lambda x: right_shift(
x, pad=nn.ZeroPad2d((1, 0, 0, 0)))
def forward(self, x):
x = self.pad(x)
x = self.conv(x)
x = self.bn(x) if self.norm == 'batch_norm' else x
return self.right_shift(x) if self.shift_output_right else x
class down_right_shifted_deconv2d(nn.Module):
def __init__(self, num_filters_in, num_filters_out, filter_size=(2, 2), stride=(1, 1),
shift_output_right=False):
super(down_right_shifted_deconv2d, self).__init__()
self.deconv = wn(nn.ConvTranspose2d(num_filters_in, num_filters_out, filter_size,
stride, output_padding=1))
self.filter_size = filter_size
self.stride = stride
def forward(self, x):
x = self.deconv(x)
xs = [int(y) for y in x.size()]
x = x[:, :, :(xs[2] - self.filter_size[0] + 1):,
:(xs[3] - self.filter_size[1] + 1)]
return x
'''
skip connection parameter : 0 = no skip connection
1 = skip connection where skip input size === input size
2 = skip connection where skip input size === 2 * input size
'''
class gated_resnet(nn.Module):
def __init__(self, num_filters, conv_op, nonlinearity=concat_elu, skip_connection=0, h_dim=768):
super(gated_resnet, self).__init__()
self.skip_connection = skip_connection
self.nonlinearity = nonlinearity
self.conv_input = conv_op(
2 * num_filters, num_filters) # cuz of concat elu
if skip_connection != 0:
self.nin_skip = nin(2 * skip_connection * num_filters, num_filters)
self.dropout = nn.Dropout2d(0.5)
self.conv_out = conv_op(2 * num_filters, 2 * num_filters)
hw_normal = torch.normal(mean=0, std=0.05, size=(h_dim, num_filters*2))
self.hw = Parameter(hw_normal, requires_grad=True)
self.num_filters = num_filters
def forward(self, og_x, a=None, h=None):
x = self.conv_input(self.nonlinearity(og_x))
if a is not None:
x += self.nin_skip(self.nonlinearity(a))
x = self.nonlinearity(x)
x = self.dropout(x)
x = self.conv_out(x)
if h is not None:
# print('h', h.shape)
# print('hw', self.hw.shape)
# print('x', x.shape)
# print('matmul', torch.matmul(h, self.hw).shape)
# print('hw_view', torch.matmul(h, self.hw).view(og_x.shape[0], 2*self.num_filters, 1, 1).shape)
h = torch.matmul(h, self.hw).view(
og_x.shape[0], 2*self.num_filters, 1, 1)
h = h.repeat(1, 1, x.shape[-1], x.shape[-1])
x += h
a, b = torch.chunk(x, 2, dim=1)
# print('a', a.shape)
# print('b', b.shape)
c3 = a * torch.sigmoid(b)
# print('og_x', og_x.shape)
# print('c3', c3.shape)
# print('og_x+c3', (og_x+c3).shape)
return og_x + c3
class PixelCNNLayer_up(nn.Module):
def __init__(self, nr_resnet, nr_filters, resnet_nonlinearity):
super(PixelCNNLayer_up, self).__init__()
self.nr_resnet = nr_resnet
# stream from pixels above
self.u_stream = nn.ModuleList([gated_resnet(nr_filters, down_shifted_conv2d,
resnet_nonlinearity, skip_connection=0)
for _ in range(nr_resnet)])
# stream from pixels above and to thes left
self.ul_stream = nn.ModuleList([gated_resnet(nr_filters, down_right_shifted_conv2d,
resnet_nonlinearity, skip_connection=1)
for _ in range(nr_resnet)])
def forward(self, u, ul, h=None):
u_list, ul_list = [], []
for i in range(self.nr_resnet):
u = self.u_stream[i](u, a=None, h=h)
ul = self.ul_stream[i](ul, a=u, h=h)
u_list += [u]
ul_list += [ul]
return u_list, ul_list
class PixelCNNLayer_down(nn.Module):
def __init__(self, nr_resnet, nr_filters, resnet_nonlinearity):
super(PixelCNNLayer_down, self).__init__()
self.nr_resnet = nr_resnet
# stream from pixels above
self.u_stream = nn.ModuleList([gated_resnet(nr_filters, down_shifted_conv2d,
resnet_nonlinearity, skip_connection=1)
for _ in range(nr_resnet)])
# stream from pixels above and to thes left
self.ul_stream = nn.ModuleList([gated_resnet(nr_filters, down_right_shifted_conv2d,
resnet_nonlinearity, skip_connection=2)
for _ in range(nr_resnet)])
def forward(self, u, ul, u_list, ul_list, h=None):
for i in range(self.nr_resnet):
u = self.u_stream[i](u, a=u_list.pop(), h=h)
ul = self.ul_stream[i](ul, a=torch.cat((u, ul_list.pop()), 1), h=h)
return u, ul
class ImageDecoder(nn.Module):
def __init__(self, nr_resnet=5, nr_filters=80, nr_logistic_mix=10,
resnet_nonlinearity='concat_elu', input_channels=3):
super(ImageDecoder, self).__init__()
if resnet_nonlinearity == 'concat_elu':
self.resnet_nonlinearity = lambda x: concat_elu(x)
else:
raise Exception(
'right now only concat elu is supported as resnet nonlinearity.')
self.nr_filters = nr_filters
self.input_channels = input_channels
self.nr_logistic_mix = nr_logistic_mix
self.right_shift_pad = nn.ZeroPad2d((1, 0, 0, 0))
self.down_shift_pad = nn.ZeroPad2d((0, 0, 1, 0))
down_nr_resnet = [nr_resnet] + [nr_resnet + 1] * 2
self.down_layers = nn.ModuleList([PixelCNNLayer_down(down_nr_resnet[i], nr_filters,
self.resnet_nonlinearity) for i in range(3)])
self.up_layers = nn.ModuleList([PixelCNNLayer_up(nr_resnet, nr_filters,
self.resnet_nonlinearity) for _ in range(3)])
self.downsize_u_stream = nn.ModuleList([down_shifted_conv2d(nr_filters, nr_filters,
stride=(2, 2)) for _ in range(2)])
self.downsize_ul_stream = nn.ModuleList([down_right_shifted_conv2d(nr_filters,
nr_filters, stride=(2, 2)) for _ in range(2)])
self.upsize_u_stream = nn.ModuleList([down_shifted_deconv2d(nr_filters, nr_filters,
stride=(2, 2)) for _ in range(2)])
self.upsize_ul_stream = nn.ModuleList([down_right_shifted_deconv2d(nr_filters,
nr_filters, stride=(2, 2)) for _ in range(2)])
self.u_init = down_shifted_conv2d(input_channels + 1, nr_filters, filter_size=(2, 3),
shift_output_down=True)
self.ul_init = nn.ModuleList([down_shifted_conv2d(input_channels + 1, nr_filters,
filter_size=(1, 3), shift_output_down=True),
down_right_shifted_conv2d(input_channels + 1, nr_filters,
filter_size=(2, 1), shift_output_right=True)])
num_mix = 3 if self.input_channels == 1 else 10
self.nin_out = nin(nr_filters, num_mix * nr_logistic_mix)
self.init_padding = None
def forward(self, x, h=None, sample=False):
# similar as done in the tf repo :
if self.init_padding is None and not sample:
xs = [int(y) for y in x.size()]
padding = Variable(torch.ones(
xs[0], 1, xs[2], xs[3]), requires_grad=False)
self.init_padding = padding.cuda() if x.is_cuda else padding
if sample:
xs = [int(y) for y in x.size()]
padding = Variable(torch.ones(
xs[0], 1, xs[2], xs[3]), requires_grad=False)
padding = padding.cuda() if x.is_cuda else padding
x = torch.cat((x, padding), 1)
### UP PASS ###
x = x if sample else torch.cat((x, self.init_padding), 1)
u_list = [self.u_init(x)]
ul_list = [self.ul_init[0](x) + self.ul_init[1](x)]
for i in range(3):
# resnet block
u_out, ul_out = self.up_layers[i](u_list[-1], ul_list[-1], h=h)
u_list += u_out
ul_list += ul_out
if i != 2:
# downscale (only twice)
u_list += [self.downsize_u_stream[i](u_list[-1])]
ul_list += [self.downsize_ul_stream[i](ul_list[-1])]
### DOWN PASS ###
u = u_list.pop()
ul = ul_list.pop()
for i in range(3):
# resnet block
u, ul = self.down_layers[i](u, ul, u_list, ul_list, h=h)
# upscale (only twice)
if i != 2:
u = self.upsize_u_stream[i](u)
ul = self.upsize_ul_stream[i](ul)
x_out = self.nin_out(F.elu(ul))
assert len(u_list) == len(ul_list) == 0, pdb.set_trace()
return x_out
def LOSS_IT(scores, alphas, sorted_cap, decode_lengths, criterion):
sorted_cap = sorted_cap[:, 1:]
scores = pack_padded_sequence(
scores, decode_lengths, batch_first=True).data
targets = pack_padded_sequence(
sorted_cap, decode_lengths, batch_first=True).data
loss = criterion(scores, targets)
loss += 1.0 * ((1. - alphas.sum(dim=1)) ** 2).mean()