-
Notifications
You must be signed in to change notification settings - Fork 2
/
composer.py
1899 lines (1635 loc) · 74.4 KB
/
composer.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
# # built-in modules
import random
import os
from typing import Callable, Union
import math
import csv
# # Torch modules
import torch
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, datasets
from torch.nn.functional import one_hot, conv2d
# # other modules
from PIL import Image as PILImage
def do_n_it(x: Union[torch.Tensor, int], n: int):
if isinstance(x, torch.Tensor):
return x.unsqueeze(0).expand(n, -1, -1, -1)
elif isinstance(x, int):
return torch.tensor([x]).long().expand(n)
elif isinstance(x, (list, tuple)):
return torch.tensor([list(x)]).long().expand(n, -1)
else:
raise ValueError(f"Invalid type {type(x)}")
def get_classes(dataset_: Dataset, n: int = 10):
# get the indices of each class
classes = [[] for _ in range(n)]
for i, (_, y) in enumerate(dataset_):
classes[y].append(i)
return classes
def pink(shape: tuple, c: float, a: float, actfun: Callable = None):
# Creates a 2D correlated noise with the given shape
# c: exponent, a: amplitude, actfun: activation function over the image
if len(shape) == 2:
pink = torch.zeros((1, *shape))
elif len(shape) == 3:
pink = torch.zeros(shape)
else:
return
if actfun is None:
actfun = lambda y: y
if actfun == "scale":
actfun = lambda y: (y - y.min())/(y.max() - y.min() + 1e-6)
channels, height, width = pink.shape
f_height = torch.linspace(1/height, 1.0, height)
f_width = torch.linspace(1/width, 1.0, width)
freq = torch.stack(torch.meshgrid((f_height, f_width), indexing="ij")).sum(dim=0) / 2
for i in range(channels):
phases = 2 * torch.pi * (torch.rand((height, width)) - 0.5)
combi = a * (phases * (1 + 1j)) / freq.pow(c)
pink[i] = torch.fft.ifft2(combi).real
pink = pink.squeeze()
return actfun(pink)
def bezier_generator(p: torch.Tensor, t: torch.Tensor):
"""General Bézier curves
p: points
t: line range (something like: torch.linspace(0.0, 1.0, 100))
"""
assert torch.all((0.0 <= t) * (t <= 1.0)), "`t` must be between 0.0 and 1.0 !"
assert p.shape[1] == 2, "2D curve"
assert p.shape[2] == 1, "singleton dimension"
n = p.shape[0] - 1 # order of Bezier curve
c = ((1.0 - t) ** n) * p[0] # first term
for i in range(1, n + 1):
c += math.comb(n, i) * ((1.0 - t) ** (n - i)) * (t ** i) * p[i]
return c
def center_of_mass(x: torch.tensor):
"""
:param x: 2-3D tensor
:return: center of mass of the tensor
"""
assert x.ndim == 2 or (x.ndim == 3 and x.size(0) == 1)
d = 10 # min distance from the edges (boundaries)
h, w = x.shape[-2:]
eps = 1e-9
x = x.squeeze()
normalizer = torch.sum(x) + eps
grids = torch.meshgrid(torch.arange(h), torch.arange(w), indexing="ij")
c = [int(torch.sum(x * grids[dir].float()).item() / normalizer) for dir in range(2)]
return [max(min(c[0], h - d), d), max(min(c[1], w - d), d)]
def coord_to_points(x: torch.tensor, i: int, j: int, k: int):
*_, h, w = x.shape
p = torch.zeros_like(x)
p[:, max(i-k, 0):min(h, i+k), max(j-k, 0):min(w, j+k)] = 1.0
return p
def routine_01(composites: torch.Tensor, masks: torch.Tensor, noise: float = 0.0):
# adding noise and clamping
composites += torch.rand(1) * noise * torch.rand_like(composites)
composites = torch.clamp(composites, 0.0, 1.0)
masks = torch.clamp(masks, 0.0, 1.0)
masks = 2.0 * (masks - 0.5)
return composites, masks
class FixPoints:
"""Creates a fix point for a given object (digit) with size k x k
such that the fix point is contiguous with the object."""
def __init__(self, k: int = 3):
self.k = k
self.p = (k // 2) + (k % 1)
self.a = k * k # area of the fix points
self.ck = torch.ones((1, 1, k, k))
def find_fix_points(self, x: torch.tensor, s: int = 0):
conv_mask = torch.conv2d(x, self.ck, padding='same')
return conv_mask.where(conv_mask >= self.a - 1 - s, torch.tensor(0.0)).nonzero()
def get_rand_fix_point(self, x: torch.tensor):
s, max_s = 0, self.a
while s < max_s:
fix_points = self.find_fix_points(x, s)
if fix_points.size(0) > 0:
break
s += 1
_, i, j = random.choice(fix_points) if s < max_s else [None, torch.zeros(self.p), torch.zeros(self.p)]
return [i.item(), j.item()]
def fix_points(self, x: torch.tensor):
fix_points = torch.zeros_like(x)
i, j = self.get_rand_fix_point(x)
fix_points[:, i-self.p:i+self.p, j-self.p:j+self.p] = 1.0
return fix_points
class IOR_DS(Dataset):
def __init__(self,
mnist_dataset: Dataset,
n_digits: int, # number of digits
n_attend: int, # number of attend iterations per digit
noise: float = 0.25, # noise scale
overlap: float = 0.0, # maximum permissible overlap between digits
):
super().__init__()
self.dataset = mnist_dataset
self.n_digits = n_digits
self.n_attend = n_attend
self.n_iter = n_digits * n_attend
self.noise = noise
self.overlap = overlap
self.pad = 34
self.c, h, w = self.dataset[0][0].shape
self.h, self.w = self.pad + h + self.pad, self.pad + w + self.pad
self.transform = transforms.Compose([
transforms.Pad(self.pad),
transforms.RandomAffine(degrees=(-15, 15), translate=(0.3, 0.3), scale=(0.9, 1.1))])
def build_valid_test(self):
"""The noise is set to zero for validation and testing."""
self.noise = 0.0
def get_random_digit(self):
i = torch.randint(0, self.dataset.__len__(), (1,)).item()
return self.dataset.__getitem__(i)
def __len__(self):
return self.dataset.__len__()
def __getitem__(self, idx: int):
# pre-allocation
composites = torch.zeros(self.n_iter, 3, self.h, self.w)
components = torch.zeros(self.n_digits, self.c, self.h, self.w)
masks = torch.zeros(self.n_digits, 1, self.h, self.w)
labels = torch.zeros(self.n_digits).long()
hot_labels = 0
composites += (0.5 * torch.rand(1, 3, 1, 1))
# composing while avoiding overlap
for i in range(self.n_digits):
j, max_j = 0, 17
while j < max_j:
x, y = self.get_random_digit()
x = self.transform(x)
if (components.sum(0) * x).sum() <= self.overlap:
break
j += 1
rgb = torch.rand(3, 1, 1) # random.choice(self.rgbs)
rgb /= rgb.max()
composites[:] = (1.0 - x) * composites + x * rgb
components[i] = x
labels[i] = y
masks[i] = x
# adding noise and clamping
composites, masks = routine_01(composites, masks, self.noise)
return composites, labels, masks, components, hot_labels
class Arrow_DS(Dataset):
def __init__(self,
mnist_dataset: Dataset, # MNIST datasets
n_iter: tuple, # number of iterations
noise: float = 0.25, # noise scale
directory: str = r"./data/", # directory of the arrow images
):
super().__init__()
self.directory = os.path.join(directory, "arrows")
self.dataset = mnist_dataset
self.n_iter = n_iter
self.noise = noise
self.pad = 2
self.c, h, w = self.dataset[0][0].shape
self.h, self.w = 96, 96
self.transform = transforms.Pad(self.pad)
self.n_classes = 10
self.class_ids = get_classes(self.dataset, 10)
self.arrows = self.get_arrows()
self.arrow_pos = [5, 0, 4,
2, 1,
7, 3, 6]
self.digit_pos = [(0, 0), (0, 32), (0, 64),
(32, 0), (32, 64),
(64, 0), (64, 32), (64, 64)]
def rand_sample(self, y: int, exclude: bool = False):
if exclude:
t = list(range(10))
t.remove(y)
cls = random.choice(t)
else:
cls = y
i = self.class_ids[cls][torch.randint(0, len(self.class_ids[cls]), (1, )).item()]
return self.dataset.__getitem__(i)
def get_arrows(self):
arrows = torch.zeros(8, 1, 32, 32)
for i, file in enumerate(["n", "e", "w", "s", "ne", "nw", "se", "sw"]):
arrows[i, :, 1:-1, 1:-1] = 1.0 * (transforms.ToTensor()(PILImage.open(os.path.join(self.directory, f"{file}.png")))[0] > 0.0)
return arrows
def build_valid_test(self):
self.noise = 0.0
def __len__(self):
return self.dataset.__len__()
def __getitem__(self, idx: int):
x, y = self.dataset.__getitem__(idx)
x = self.transform(x)
# pre-allocation
composites = torch.zeros(self.n_iter, 3, self.h, self.w)
components = 0
masks = torch.zeros(self.n_iter, 1, self.h, self.w)
labels = torch.zeros(self.n_iter).long()
rgbs = torch.rand(9, 3, 1, 1)
labels[:] = y
hot_labels = 0
b_rgb = torch.rand(3, 1, 1) * 0.5
t = random.choice(range(8))
arrow_i, pos_ij = self.arrow_pos[t], self.digit_pos[t]
composites[:, :, 32:64, 32:64] = self.arrows[arrow_i] * rgbs[0] + (1.0 - self.arrows[arrow_i]) * b_rgb
composites[:, :, pos_ij[0]:pos_ij[0]+32, pos_ij[1]:pos_ij[1]+32] = x * rgbs[1] + (1.0 - x) * b_rgb
masks[:, :, pos_ij[0]:pos_ij[0]+32, pos_ij[1]:pos_ij[1]+32] += x
j = 2
for i in range(8):
if i != t:
x, _ = self.rand_sample(y, exclude=True)
x = self.transform(x)
pos_ij = self.digit_pos[i]
composites[:, :, pos_ij[0]:pos_ij[0]+32, pos_ij[1]:pos_ij[1]+32] = x * rgbs[j] + (1.0 - x) * b_rgb
j += 1
# adding noise and clamping
composites, masks = routine_01(composites, masks, self.noise)
return composites, labels, masks, components, hot_labels
class Cue_DS(Dataset):
def __init__(self,
mnist_dataset: Dataset, # MNIST datasets
fix_attend: tuple, # number of fixate and attend iterations
n_digits: int, # number of digits
noise: float = 0.25, # noise scale
overlap: float = 0.0, # maximum permissible overlap between digits
):
super().__init__()
self.dataset = mnist_dataset
self.fixate, self.attend = fix_attend
self.n_iter = sum(fix_attend)
self.n_digits = n_digits
self.noise = noise
self.overlap = overlap
self.pad = 34
self.c, h, w = self.dataset[0][0].shape
self.h, self.w = self.pad + h + self.pad, self.pad + w + self.pad
self.transform = transforms.Compose([
transforms.Pad(self.pad),
transforms.RandomAffine(degrees=(-15, 15), translate=(0.3, 0.3), scale=(0.9, 1.1))])
self.fix_pointer = FixPoints(5)
def build_valid_test(self):
self.noise = 0.0
def get_random_digit(self):
i = torch.randint(0, self.dataset.__len__(), (1,)).item()
return self.dataset.__getitem__(i)
def __len__(self):
return self.dataset.__len__()
def __getitem__(self, idx: int):
# pre-allocation
composites = torch.zeros(self.n_iter, 3, self.h, self.w)
components = torch.zeros(self.n_digits, self.c, self.h, self.w)
masks = torch.zeros(self.n_iter, 1, self.h, self.w)
labels = torch.zeros(self.n_iter).long()
hot_labels = 0
y_list = []
composites += (0.5 * torch.rand(1, 3, 1, 1))
# composing while avoiding overlap
for i in range(self.n_digits):
j, max_j = 0, 17
while j < max_j:
x, y = self.get_random_digit()
x = self.transform(x)
if (components.sum(0) * x).sum() <= self.overlap:
break
j += 1
# rgb = random.choice(self.rgbs)
components[i] += x
y_list.append(y)
target_id = random.choice(range(self.n_digits))
target = components[target_id]
fixpoint = self.fix_pointer.fix_points(target)
composites[:self.fixate] = fixpoint + (1.0 - fixpoint) * composites[:self.fixate]
masks[:self.fixate] = fixpoint
rgb = torch.rand(self.n_digits, 3, 1, 1)
rgb /= rgb.max(dim=1, keepdims=True).values
composites[self.fixate:] = (components * rgb).sum(0) + (1.0 - components.sum(0)) * composites[self.fixate:]
masks[self.fixate:] = target
labels[:] = y_list[target_id]
# adding noise and clamping
composites, masks = routine_01(composites, masks, self.noise)
return composites, labels, masks, components, hot_labels
class Recognition_DS(Dataset):
def __init__(self,
mnist_dataset: Dataset, # MNIST datasets
n_iter: int, # number of iterations
stride: int, # stride of background and foreground motion per iteration
blank: bool = False, # whether to the foreground is visible (blank) or not
static: bool = False, # whether the background and foreground are static
background: bool = True, # whether to use background or not
noise: float = 0.25, # noise scale
):
super().__init__()
self.dataset = mnist_dataset
self.n_iter = n_iter
self.stride = stride
self.blank = blank
self.static = static
self.background = background
self.noise = noise
self.pad = 34
self.c, h, w = self.dataset[0][0].shape
self.h, self.w = self.pad + h + self.pad, self.pad + w + self.pad
self.transform = transforms.Compose([
transforms.Pad(self.pad),
transforms.RandomAffine(degrees=(-15, 15), translate=(0.3, 0.3), scale=(1.3, 1.5))])
def make_foreground(self):
window_shape = (self.h, self.w + self.stride * self.n_iter)
pink_fore = pink(window_shape, 2.5, 0.6, torch.cos).unsqueeze(0)
pink_fore = 1.0 * ((0.5 + pink_fore / 2) > 0.7)
return pink_fore
def make_background(self):
window_shape = (self.h, self.w + self.stride * self.n_iter)
a, b, c = torch.rand(3)
pink_back = pink(window_shape, 2.0 + a, 0.25 + 0.5 * b, torch.cos).unsqueeze(0)
pink_back = 1.0 * ((0.5 + pink_back / 2) > 0.25 + 0.5 * c)
return pink_back
def build_valid_test(self):
self.noise = 0.0
def __len__(self):
return self.dataset.__len__()
def __getitem__(self, idx: int):
x, y = self.dataset.__getitem__(idx)
x = self.transform(x)
# pre-allocation
composites = torch.zeros(self.n_iter, 3, self.h, self.w)
components = 0
masks = torch.zeros(self.n_iter, 1, self.h, self.w)
labels = torch.zeros(self.n_iter).long()
hot_labels = 0
# get background and foreground
digit_color = torch.rand(3, 1, 1)
background_color = torch.rand(3, 1, 1)
foreground_color = 1.0 - background_color
foreground_color /= (foreground_color.max() + 1e-6) * 2
background_color /= (background_color.max() + 1e-6) * 2
digit_color /= (digit_color.max() + 1e-6)
if self.background:
background = self.make_background() * background_color
else:
background = torch.zeros(3, self.h, self.w + self.stride * self.n_iter)
foreground = self.make_foreground()
# assignment
labels[:] = y
masks[:] = x
z = digit_color * x
# background
if self.static:
composites[:] = background_color * background[:, :, :self.w] * (1.0 - x) + z
else:
r_stride = random.randint(1, self.stride)
d = random.randint(0, 1)
for i in range(self.n_iter):
if d == 0:
slc = slice(i * r_stride, self.w + i * r_stride)
else:
slc = slice((self.n_iter - i) * r_stride, self.w + (self.n_iter - i) * r_stride)
composites[i] = background_color * background[:, :, slc] * (1.0 - x) + z
# foreground
r_stride = random.randint(1, self.stride)
d = random.randint(0, 1)
for i in range(self.n_iter):
if self.static:
slc = slice(0, self.w)
else:
if d == 0:
slc = slice(i * r_stride, self.w + i * r_stride)
else:
slc = slice((self.n_iter - i) * r_stride, self.w + (self.n_iter - i) * r_stride)
if self.blank:
composites[i] = (composites[i] * (1.0 - foreground[:, :, slc]))
else:
composites[i] = (composites[i] * (1.0 - foreground[:, :, slc])) + foreground_color * foreground[:, :, slc]
# adding noise and clamping
composites, masks = routine_01(composites, masks, self.noise)
return composites, labels, masks, components, hot_labels
class Search_DS(Dataset):
def __init__(self,
mnist_dataset: Dataset, # MNIST datasets
n_iter: tuple, # number of iterations
n_digits: int, # number of digits
noise: float = 0.25, # noise scale
overlap: float = 1.0, # maximum permissible overlap between digits
):
super().__init__()
self.dataset = mnist_dataset
self.n_iter = n_iter
self.n_digits = n_digits
self.noise = noise
self.overlap = overlap
self.pad = 34
self.c, h, w = self.dataset[0][0].shape
self.h, self.w = self.pad + h + self.pad, self.pad + w + self.pad
self.transform = transforms.Compose([
transforms.Pad(self.pad),
transforms.RandomAffine(degrees=(-15, 15), translate=(0.3, 0.3), scale=(0.9, 1.1))])
self.classes = get_classes(self.dataset, 10)
def build_valid_test(self):
self.noise = 0.0
def get_random_digit(self, ind_list: list = None):
if ind_list is None:
i = torch.randint(0, self.dataset.__len__(), (1,)).item()
else:
i = random.choice(ind_list)
return self.dataset.__getitem__(i)
def __len__(self):
return self.dataset.__len__()
def __getitem__(self, idx: int):
# pre-allocation
composites = torch.zeros(self.n_iter, 3, self.h, self.w)
components = torch.zeros(self.n_digits, self.c, self.h, self.w)
masks = torch.zeros(self.n_iter, 1, self.h, self.w)
labels = torch.zeros(self.n_iter).long()
hot_labels = torch.zeros(self.n_iter, 10).float()
y_list = []
b_rgb = torch.rand(3, 1, 1) * 0.5
# composing while avoiding overlap
rand_classes = random.sample(self.classes, k=self.n_digits)
for i, s in enumerate(rand_classes):
j, max_j = 0, 17
while j < max_j:
x, y = self.get_random_digit(s)
x = self.transform(x)
if (components.sum(0) * x).sum() <= self.overlap:
break
j += 1
components[i] += x
y_list.append(y)
rgb = torch.rand(self.n_digits, 3, 1, 1)
rgb /= rgb.max(dim=1, keepdims=True).values
composites[:] = (components * rgb).sum(0) + (1.0 - components.sum(0)) * b_rgb
# selecting the target
target_id = random.choice(range(self.n_digits))
target_label = y_list[target_id]
masks[:] = components[target_id]
labels[:] = target_label
hot_labels[:] = one_hot(labels[0], 10).squeeze().float()
# adding noise and clamping
composites, masks = routine_01(composites, masks, self.noise)
return composites, labels, masks, components, hot_labels
class Tracking_DS(Dataset):
def __init__(self,
mnist_dataset: Dataset, # MNIST datasets
fix_attend: tuple = (2, 5), # number of fixate and attend iterations
n_digits: int = 3, # number of distractor images
noise: float = 0.25, # noise scale
):
super().__init__()
assert n_digits < 8
self.dataset = mnist_dataset # MNIST datasets
self.fix_attend = fix_attend
self.fix, self.attend = fix_attend # [fixation, attention]
self.n_iter = sum(fix_attend) # number of episodes (recurrent iterations)
self.n_digits = n_digits # number of distractor images
self.noise = noise # noise scale
self.pad = 34
self.c, h, w = self.dataset[0][0].shape
self.h, self.w = self.pad + h + self.pad, self.pad + w + self.pad
self.classes = get_classes(self.dataset, 10)
self.zones = {"nw": ((0, 22), (0, 22), ( "ne", "ee", "sw", "ss", "se")),
"nn": ((0, 22), (23, 55), ( "sw", "ss", "se")),
"ne": ((0, 22), (56, 68), ("nw", "ww", "sw", "ss", "se")),
"ww": ((23, 55), (0, 22), ( "ne", "ee", "se")),
"ee": ((23, 55), (56, 68), ("nw", "ww", "sw", )),
"sw": ((56, 68), (0, 22), ("nw", "nn", "ne", "ee", "se")),
"ss": ((56, 68), (23, 55), ("nw", "nn", "ne", )),
"se": ((56, 68), (56, 68), ("nw", "nn", "ne", "ww", "sw", )),
}
self.bezier_order, self.bezier_res = 2, min(self.h, self.w)
self.bezier_inds = torch.linspace(0, self.bezier_res - 1, self.attend).long()
def pick_start_end(self, sz: str):
"""sz: start zone
"""
si = random.randint(*self.zones[sz][0]) # start i
sj = random.randint(*self.zones[sz][1]) # start j
ez = random.choice(self.zones[sz][2]) # end zone
ei = random.randint(*self.zones[ez][0]) # end i
ej = random.randint(*self.zones[ez][1]) # end j
return (si, sj), (ei, ej)
def make_path(self, start, end):
t_ = torch.linspace(0., 1.0, self.bezier_res) # parameter
c_ = torch.rand(self.bezier_order + 1, 2, 1) # coordinates
c_[0] = torch.tensor([start[0]/self.h, start[1]/self.w]).view(2, 1)
c_[-1] = torch.tensor([end[0]/self.h, end[1]/self.w]).view(2, 1)
b_ = bezier_generator(c_, t_) # Bezier curve
b_h, b_w = (b_[0] * self.h).long().tolist(), (b_[1] * self.w).long().tolist()
return b_h, b_w
def put_digits_on_curve(self, z, x, c, b_h, b_w):
"""
z: composite (self.n_iter, 3, self.h, self.w)
x: digit (1, 28, 28)
c: color (3, 1, ,1)
b_h: h (i) indices (self.bezier_res)
b_w: w (j) indices (self.bezier_res)
"""
for k, b in enumerate(self.bezier_inds):
ih, iw = min(b_h[b], self.h-28), min(b_w[b], self.w-28)
z[k+self.fix, :, ih:ih+28, iw:iw+28] += x * c
return z
def build_valid_test(self):
self.noise = 0.0
def get_random_digit(self, ind_list: list = None):
if ind_list is None:
i = torch.randint(0, self.dataset.__len__(), (1,)).item()
else:
i = random.choice(ind_list)
return self.dataset.__getitem__(i)
def __len__(self):
return self.dataset.__len__()
def __getitem__(self, idx: int):
x, y = self.dataset.__getitem__(idx)
# coloring
rgbs = torch.rand(self.n_digits, 3, 1, 1)
rgbs /= torch.linalg.norm(rgbs, dim=1, keepdim=True)
# pre-allocation
composites = torch.zeros(self.n_iter, 3, self.h, self.w)
components = 0
masks = torch.zeros(self.n_iter, 1, self.h, self.w)
labels = torch.zeros(self.n_iter).long()
hot_labels = 0
labels[:] = y
# location
positions = []
s_zones = random.sample(sorted(self.zones), self.n_digits)
for sz in s_zones:
(si, sj), (ei, ej) = self.pick_start_end(sz)
b_h, b_w = self.make_path((si, sj), (ei, ej))
positions.append((b_h, b_w))
# assignment
i, j = positions[0][0][0], positions[0][1][0]
composites[:self.fix, :, i:i+28, j:j+28] = x * rgbs[0]
composites = self.put_digits_on_curve(composites, x, rgbs[0], *positions[0])
masks = 1.0 * (composites.sum(dim=1, keepdim=True) > 0.0)
for i in range(1, self.n_digits):
x, y = self.get_random_digit()
composites = self.put_digits_on_curve(composites, x, rgbs[i], *positions[i])
where_digits = composites * (composites > 0.0)
composites = 0.5 * (1.0 - where_digits) * torch.rand(1, 3, 1, 1) + where_digits
# adding noise and clamping
composites, masks = routine_01(composites, masks, self.noise)
return composites, labels, masks, components, hot_labels
class Popout_DS(Dataset):
def __init__(self,
mnist_dataset: Dataset, # MNIST datasets
n_iter: tuple, # number of iterations
noise: float = 0.25, # noise scale
):
super().__init__()
self.dataset = mnist_dataset
self.n_iter = n_iter
self.noise = noise
self.pad = 2
self.h, self.w = 96, 96
self.transform = transforms.Pad(self.pad)
self.n_classes = 10
self.class_ids = get_classes(self.dataset, 10)
self.index_pos = [0, 1, 2, 3, 4, 5, 6, 7, 8]
self.digit_pos = [(0, 0), (0, 32), (0, 64),
(32, 0), (32, 32), (32, 64),
(64, 0), (64, 32), (64, 64)]
def rand_sample(self, y: int, exclude: bool = False):
if exclude:
t = list(range(10))
t.remove(y)
cls = random.choice(t)
else:
cls = y
i = self.class_ids[cls][torch.randint(0, len(self.class_ids[cls]), (1, )).item()]
return self.dataset.__getitem__(i)
def build_valid_test(self):
self.noise = 0.0
def __len__(self):
return self.dataset.__len__()
def __getitem__(self, idx: int):
x, y = self.dataset.__getitem__(idx)
x = self.transform(x)
# pre-allocation
composites = torch.zeros(self.n_iter, 3, self.h, self.w)
components = 0
masks = torch.zeros(self.n_iter, 1, self.h, self.w)
labels = torch.zeros(self.n_iter).long()
rgbs = torch.rand(2, 3, 1, 1)
labels[:] = y
hot_labels = 0
b_rgb = 0.5 * torch.rand(3, 1, 1)
t = random.choice(self.index_pos)
pos_ij = self.digit_pos[t]
composites[:, :, pos_ij[0]:pos_ij[0]+32, pos_ij[1]:pos_ij[1]+32] = (x * rgbs[0]) + (1.0 - x) * b_rgb
masks[:, :, pos_ij[0]:pos_ij[0]+32, pos_ij[1]:pos_ij[1]+32] += x
x, _ = self.rand_sample(y, exclude=True)
x = self.transform(x)
for i in self.index_pos:
if i != t:
pos_ij = self.digit_pos[i]
composites[:, :, pos_ij[0]:pos_ij[0]+32, pos_ij[1]:pos_ij[1]+32] = x * rgbs[1] + (1.0 - x) * b_rgb
# adding noise and clamping
composites, masks = routine_01(composites, masks, self.noise)
return composites, labels, masks, components, hot_labels
class CelebACrop(Dataset):
def __init__(self,
dataset: Dataset,
n_iter: int,
hair_dir: str = None,
in_dims: tuple = (3, 178, 178),
padding: int = 19,
noise: float = 0.125,
kind: str = "train",
which: int = 0,):
super().__init__()
self.dataset = dataset
self.n_iter = n_iter
self.hair_dir = hair_dir if hair_dir is not None else r"./data"
self.in_dims = in_dims
_, self.h, self.w = self.in_dims
self.padding = padding
self.noise = noise if kind == "train" else 0.0
self.kind = kind # train, valid, test
self.which = which # 0: all, 1: fblonde, 2: fblack, 3: mblonde, 4: mblack
self.which_names = ["all", "fblonde", "fblack", "mblonde", "mblack"]
self.len_which = len(self.which_names)
assert self.which in range(self.len_which), f"which must be between 0 and {self.len_which-1} but got {self.which}!"
self.at_list = ['Male', 'Black_Hair', 'Blond_Hair']
self.gender_i, self.black_i, self.blonde_i =(self.dataset.attr_names.index(x) for x in self.at_list)
self.hair_ids = self.get_hair()
self.transform = transforms.Compose([
transforms.Resize((self.h - 2*self.padding, self.w - 2*self.padding), antialias=True),
transforms.RandomHorizontalFlip(p=0.5 if kind == "train" else 0.0),
# transforms.RandomVerticalFlip(p=0.5 if kind == "train" else 0.0),
])
def get_hair(self):
if self.hair_dir and os.path.exists(os.path.join(self.hair_dir, f"celeba/{self.kind}_hair_ids.pt")):
print(f'Loading {self.kind}_hair_ids.pt from file!')
return torch.load(os.path.join(self.hair_dir, f"celeba/{self.kind}_hair_ids.pt"))
else:
print(f'Creating {self.kind}_hair_ids.pt file!')
hair_ids = [[], [], [], [], []] # [all, fblonde, fblack, mblonde, mblack]
for i, (_, y) in enumerate(self.dataset):
if y[self.blonde_i] == 1 or y[self.black_i] == 1:
hair_ids[0].append(i)
if self.kind == "train" and y[self.gender_i] == 1: # since the dataset is not balanced
hair_ids[0].append(i)
g, b = y[self.gender_i], y[self.black_i]
hair_ids[1+b+2*g].append(i)
if self.kind == "train":
random.shuffle(hair_ids[0]) # shuffle the training set only # # # #
for i, n in enumerate(self.which_names):
print(f"{n}: {len(hair_ids[i])}")
torch.save(hair_ids, os.path.join(self.hair_dir, f"celeba/{self.kind}_hair_ids.pt"))
print(f'{self.kind}_hair_ids.pt saved to file!')
return hair_ids
def __len__(self):
return len(self.hair_ids[self.which])
def __getitem__(self, idx: int):
x, y = self.dataset[self.hair_ids[self.which][idx]]
x = x[:, 20:-20, :]
composites = torch.zeros(self.n_iter, 3, self.h, self.w)
composites[:] = self.transform(x)
labels = torch.zeros(self.n_iter).long()
labels[:] = y[self.gender_i]
composites += torch.rand(1) * self.noise * torch.rand_like(composites)
composites = torch.clamp(composites, 0.0, 1.0)
return composites, labels, 0, 0, 0
class COCOTokens:
def __init__(self,
directory: str,
animals: bool = True,
split: float = 0.9,
):
from src.pycocotools.coco import COCO
self.split = split
self.directory = os.path.join(directory, "coco")
self.coco = COCO(os.path.join(self.directory, "annotations/instances_train2017.json"))
self.coco_test = COCO(os.path.join(self.directory, "annotations/instances_val2017.json"))
if animals:
self.entities = ['bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe']
else:
self.entities = list(o["name"] for o in self.coco.loadCats(self.coco.getCatIds()))
self.ids = self.coco.getCatIds(catNms=self.entities)
def get_tokens(self):
trvl_tokens = self._get_tokens(self.coco, False)
test_tokens = self._get_tokens(self.coco_test, True)
train_tokens, valid_tokens = self._split(trvl_tokens)
return train_tokens, valid_tokens, test_tokens
def _split(self, x: Union[list, tuple]):
n_train = int(len(x) * self.split)
return (x[:n_train], x[n_train:])
def _get_tokens(self, ds_, test: bool = False):
tokens = []
for id in self.ids:
tokens += ds_.getImgIds(catIds=[id])
(258322 in tokens) and tokens.remove(258322)
(214520 in tokens) and tokens.remove(214520)
not test and random.shuffle(tokens)
return torch.tensor(tokens)
class COCOAnimals(Dataset):
def __init__(self,
in_dims: tuple,
directory: str,
kind: int, # 0: train, 1: valid, 2: test
tokens: torch.Tensor,
animals: bool = True,
min_area: float = 1.0/64.0,
):
super().__init__()
in_dims = in_dims if len(in_dims) == 2 else in_dims[1:]
from src.pycocotools.coco import COCO
self.h, self.w = in_dims
self.kind = kind
self.tokens = tokens
self.min_area = min_area
self.directory = os.path.join(directory, "coco")
self.file_type = "val2017" if kind == 2 else "train2017"
self.coco = COCO(os.path.join(self.directory, f"annotations/instances_{self.file_type}.json"))
if animals:
self.entities = ['bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe']
else:
self.entities = list(o["name"] for o in self.coco.loadCats(self.coco.getCatIds()))
self.ids = self.coco.getCatIds(catNms=self.entities)
self.n_classes = len(self.entities)
self.classes = list(range(self.n_classes))
self.id_to_label = dict(zip(self.ids, self.classes))
self.transform = transforms.ToTensor()
self.fix_kernel = torch.ones(1, 1, 7, 7)
self.triplets = None # labels list of triplets [token, ann_id, label]
self.tk_cls_anns = None # list of token, label, list of ann_ids
self.tk_cls_sann = None # list of token, label, single ann_id
self.tk_cls_osann = None # list of token, label, only single ann_id
self.class_weights = None
self._len_ = 0
def _is_it_good(self, ann: dict, token: int, crowdisok: bool = False):
height, width = self.coco.loadImgs(token)[0]['height'], self.coco.loadImgs(token)[0]['width']
category_id = ann['category_id']
area = ann["area"] / (height * width)
isnotcrowd = (True, False)[ann['iscrowd']] or crowdisok
its_good = (category_id in self.ids and isnotcrowd and area > self.min_area)
return its_good
def _get_ann(self, ann_id: int):
return self.coco.loadAnns([ann_id])[0]
def _load_anns(self, token: int):
return self.coco.loadAnns(self.coco.getAnnIds([token]))
def _crop_box(self, x: torch.Tensor, ann: dict):
x_min, y_min, width, height = ann['bbox']
x_min, y_min = int(x_min), int(y_min)
width, height = min(int(width) + 1, x.size(-1)), min(int(height) + 1, x.size(-2))
return x[:, y_min:y_min+height, x_min:x_min+width]
def _get_image(self, token: int):
meta_data = self.coco.loadImgs(token)[0]
file_name = meta_data["file_name"]
PIL_file = PILImage.open(os.path.join(self.directory, "images/{}/{}".format(self.file_type, file_name)))
return self.transform(PIL_file.convert("RGB"))
def _get_mask(self, ann: dict):
return (1.0 * (self.transform(self.coco.annToMask(ann)) > 0.0))
def _get_targets(self, token: int, ann_id: int):
ann_ = self._load_anns(token)[ann_id]
label = self.id_to_label[ann_["category_id"]]
mask = self._get_mask(ann_)
return label, mask
def _get_class_weights(self):
assert (self.class_weights > 0.0).all(), "class weights are not set or a class has 0 instance!"
self.class_weights = len(self.tokens) / self.class_weights
self.class_weights = self.class_weights / self.class_weights.sum()
def _get_tokens(self, shuffle: bool = False):
if self.tokens is None:
self.tokens = []
for id in self.ids:
self.tokens += self.coco.getImgIds(catIds=[id])
if self.file_type == "train2017": # # droping the two problematic images
(258322 in self.tokens) and self.tokens.remove(258322)
(214520 in self.tokens) and self.tokens.remove(214520)
shuffle and random.shuffle(self.tokens)
return self.tokens
def _get_triplets(self):
if self.triplets is None:
self.class_weights = torch.zeros(self.n_classes)
self.triplets = []
self.triplet_classes = list([] for _ in range(self.n_classes))
i = 0
for token_ in self.tokens:
token_ = token_.item()
for ann_ in self._load_anns(token_):
if self._is_it_good(ann_, token_):
label_ = self.id_to_label[ann_['category_id']]
self.triplets.append((token_, ann_['id'], label_))
self.triplet_classes[label_].append(i)
self.class_weights[label_] += 1
i += 1
self._get_class_weights()
self._len_ = max(len(self.triplets), self._len_)
return torch.tensor(self.triplets)
def _get_tk_cls_anns(self, crowdisok: bool = False):
if self.tk_cls_anns is None:
self.tk_cls_anns = []
for token_ in self.tokens:
token_ = token_.item()
for cls in self.classes:
anns_ids = []
for ann_ in self._load_anns(token_):
if self._is_it_good(ann_, token_, crowdisok):
if self.id_to_label[ann_['category_id']] == cls:
anns_ids.append(ann_['id'])
if len(anns_ids) > 0:
self.tk_cls_anns.append((token_, cls, anns_ids))
self._len_ = max(len(self.tk_cls_anns), self._len_)
return self.tk_cls_anns
def _get_tk_cls_sann(self, crowdisok: bool = False):
if self.tk_cls_sann is None:
self.tk_cls_sann = []
for token_ in self.tokens:
token_ = token_.item()
for cls in self.classes:
the_ann_id = None
a = 0.0
for ann_ in self._load_anns(token_):
if self._is_it_good(ann_, token_, crowdisok):
if self.id_to_label[ann_['category_id']] == cls:
if ann_['area'] > a:
the_ann_id = ann_['id']
a = ann_['area']
if the_ann_id is not None:
self.tk_cls_sann.append((token_, cls, the_ann_id))
self._len_ = max(len(self.tk_cls_sann), self._len_)
return torch.tensor(self.tk_cls_sann)
def _get_tk_cls_osann(self, crowdisok: bool = False):
if self.tk_cls_osann is None:
self.tk_cls_osann = []
for token_ in self.tokens:
token_ = token_.item()
for cls in self.classes:
anns_ids = []
for ann_ in self._load_anns(token_):
if self._is_it_good(ann_, token_, crowdisok):
if self.id_to_label[ann_['category_id']] == cls:
anns_ids.append(ann_['id'])
if len(anns_ids) == 1:
self.tk_cls_osann.append((token_, cls, anns_ids[0]))
self._len_ = max(len(self.tk_cls_osann), self._len_)
return torch.tensor(self.tk_cls_osann)
def _crop_square(self, x: torch.Tensor):
_, x_h, x_w = x.shape
hw = min(x_h, x_w)
top = torch.randint(0, x_h - hw, (1, )).item() if x_h > hw else 0
left = torch.randint(0, x_w - hw, (1, )).item() if x_w > hw else 0
return x[:, top:top+hw, left:left+hw]