forked from HopefulRational/DeepCaps-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepcaps.py
More file actions
1198 lines (942 loc) · 46 KB
/
deepcaps.py
File metadata and controls
1198 lines (942 loc) · 46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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
# coding: utf-8
# In[1]:
'''
Authors: HopefulRational and team
'''
import torch
import torch.nn as nn
import torch.nn.functional as func
from torch.autograd import Variable
# import torch.autograd as grad
from torchvision import datasets, transforms
import pandas as pd
import numpy as np
import math
#import skimage.transform
#import matplotlib.pyplot as plt
# from tqdm import tqdm_notebook as tqdm
#get_ipython().magic('matplotlib inline')
eps = 1e-8
cf = 1
# ONLY cuda runnable
device = torch.device("cuda:0")
# norm_squared = torch.sum(s**2, dim=dim, keepdim=True)
# return ((norm_squared /(1 + norm_squared + eps)) * (s / (torch.sqrt(norm_squared) + eps)))
# In[2]:
"""
From github: https://gist.github.com/ncullen93/425ca642955f73452ebc097b3b46c493
"""
"""
Affine transforms implemented on torch tensors, and
only requiring one interpolation
Included:
- Affine()
- AffineCompose()
- Rotation()
- Translation()
- Shear()
- Zoom()
- Flip()
"""
import math
import random
import torch
# necessary now, but should eventually not be
import scipy.ndimage as ndi
import numpy as np
def transform_matrix_offset_center(matrix, x, y):
"""Apply offset to a transform matrix so that the image is
transformed about the center of the image.
NOTE: This is a fairly simple operaion, so can easily be
moved to full torch.
Arguments
---------
matrix : 3x3 matrix/array
x : integer
height dimension of image to be transformed
y : integer
width dimension of image to be transformed
"""
o_x = float(x) / 2 + 0.5
o_y = float(y) / 2 + 0.5
offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
transform_matrix = np.dot(np.dot(offset_matrix, matrix), reset_matrix)
return transform_matrix
def apply_transform(x, transform, fill_mode='nearest', fill_value=0.):
"""Applies an affine transform to a 2D array, or to each channel of a 3D array.
NOTE: this can and certainly should be moved to full torch operations.
Arguments
---------
x : np.ndarray
array to transform. NOTE: array should be ordered CHW
transform : 3x3 affine transform matrix
matrix to apply
"""
x = x.astype('float32')
transform = transform_matrix_offset_center(transform, x.shape[1], x.shape[2])
final_affine_matrix = transform[:2, :2]
final_offset = transform[:2, 2]
channel_images = [ndi.interpolation.affine_transform(x_channel, final_affine_matrix,
final_offset, order=0, mode=fill_mode, cval=fill_value) for x_channel in x]
x = np.stack(channel_images, axis=0)
return x
class Affine(object):
def __init__(self,
rotation_range=None,
translation_range=None,
shear_range=None,
zoom_range=None,
fill_mode='constant',
fill_value=0.,
target_fill_mode='nearest',
target_fill_value=0.):
"""Perform an affine transforms with various sub-transforms, using
only one interpolation and without having to instantiate each
sub-transform individually.
Arguments
---------
rotation_range : one integer or float
image will be rotated between (-degrees, degrees) degrees
translation_range : a float or a tuple/list w/ 2 floats between [0, 1)
first value:
image will be horizontally shifted between
(-height_range * height_dimension, height_range * height_dimension)
second value:
Image will be vertically shifted between
(-width_range * width_dimension, width_range * width_dimension)
shear_range : float
radian bounds on the shear transform
zoom_range : list/tuple with two floats between [0, infinity).
first float should be less than the second
lower and upper bounds on percent zoom.
Anything less than 1.0 will zoom in on the image,
anything greater than 1.0 will zoom out on the image.
e.g. (0.7, 1.0) will only zoom in,
(1.0, 1.4) will only zoom out,
(0.7, 1.4) will randomly zoom in or out
fill_mode : string in {'constant', 'nearest'}
how to fill the empty space caused by the transform
ProTip : use 'nearest' for discrete images (e.g. segmentations)
and use 'constant' for continuous images
fill_value : float
the value to fill the empty space with if fill_mode='constant'
target_fill_mode : same as fill_mode, but for target image
target_fill_value : same as fill_value, but for target image
"""
self.transforms = []
if translation_range:
translation_tform = Translation(translation_range, lazy=True)
self.transforms.append(translation_tform)
if rotation_range:
rotation_tform = Rotation(rotation_range, lazy=True)
self.transforms.append(rotation_tform)
if shear_range:
shear_tform = Shear(shear_range, lazy=True)
self.transforms.append(shear_tform)
if zoom_range:
zoom_tform = Translation(zoom_range, lazy=True)
self.transforms.append(zoom_tform)
self.fill_mode = fill_mode
self.fill_value = fill_value
self.target_fill_mode = target_fill_mode
self.target_fill_value = target_fill_value
def __call__(self, x, y=None):
# collect all of the lazily returned tform matrices
tform_matrix = self.transforms[0](x)
for tform in self.transforms[1:]:
tform_matrix = np.dot(tform_matrix, tform(x))
x = torch.from_numpy(apply_transform(x.numpy(), tform_matrix,
fill_mode=self.fill_mode, fill_value=self.fill_value))
if y:
y = torch.from_numpy(apply_transform(y.numpy(), tform_matrix,
fill_mode=self.target_fill_mode, fill_value=self.target_fill_value))
return x, y
else:
return x
class AffineCompose(object):
def __init__(self,
transforms,
fill_mode='constant',
fill_value=0.,
target_fill_mode='nearest',
target_fill_value=0.):
"""Apply a collection of explicit affine transforms to an input image,
and to a target image if necessary
Arguments
---------
transforms : list or tuple
each element in the list/tuple should be an affine transform.
currently supported transforms:
- Rotation()
- Translation()
- Shear()
- Zoom()
fill_mode : string in {'constant', 'nearest'}
how to fill the empty space caused by the transform
fill_value : float
the value to fill the empty space with if fill_mode='constant'
"""
self.transforms = transforms
# set transforms to lazy so they only return the tform matrix
for t in self.transforms:
t.lazy = True
self.fill_mode = fill_mode
self.fill_value = fill_value
self.target_fill_mode = target_fill_mode
self.target_fill_value = target_fill_value
def __call__(self, x, y=None):
# collect all of the lazily returned tform matrices
tform_matrix = self.transforms[0](x)
for tform in self.transforms[1:]:
tform_matrix = np.dot(tform_matrix, tform(x))
x = torch.from_numpy(apply_transform(x.numpy(), tform_matrix,
fill_mode=self.fill_mode, fill_value=self.fill_value))
if y:
y = torch.from_numpy(apply_transform(y.numpy(), tform_matrix,
fill_mode=self.target_fill_mode, fill_value=self.target_fill_value))
return x, y
else:
return x
class Rotation(object):
def __init__(self,
rotation_range,
fill_mode='constant',
fill_value=0.,
target_fill_mode='nearest',
target_fill_value=0.,
lazy=False):
"""Randomly rotate an image between (-degrees, degrees). If the image
has multiple channels, the same rotation will be applied to each channel.
Arguments
---------
rotation_range : integer or float
image will be rotated between (-degrees, degrees) degrees
fill_mode : string in {'constant', 'nearest'}
how to fill the empty space caused by the transform
fill_value : float
the value to fill the empty space with if fill_mode='constant'
lazy : boolean
if true, perform the transform on the tensor and return the tensor
if false, only create the affine transform matrix and return that
"""
self.rotation_range = rotation_range
self.fill_mode = fill_mode
self.fill_value = fill_value
self.target_fill_mode = target_fill_mode
self.target_fill_value = target_fill_value
self.lazy = lazy
def __call__(self, x, y=None):
degree = random.uniform(-self.rotation_range, self.rotation_range)
theta = math.pi / 180 * degree
rotation_matrix = np.array([[math.cos(theta), -math.sin(theta), 0],
[math.sin(theta), math.cos(theta), 0],
[0, 0, 1]])
if self.lazy:
return rotation_matrix
else:
x_transformed = torch.from_numpy(apply_transform(x.numpy(), rotation_matrix,
fill_mode=self.fill_mode, fill_value=self.fill_value))
if y:
y_transformed = torch.from_numpy(apply_transform(y.numpy(), rotation_matrix,
fill_mode=self.target_fill_mode, fill_value=self.target_fill_value))
return x_transformed, y_transformed
else:
return x_transformed
class Translation(object):
def __init__(self,
translation_range,
fill_mode='constant',
fill_value=0.,
target_fill_mode='nearest',
target_fill_value=0.,
lazy=False):
"""Randomly translate an image some fraction of total height and/or
some fraction of total width. If the image has multiple channels,
the same translation will be applied to each channel.
Arguments
---------
translation_range : two floats between [0, 1)
first value:
fractional bounds of total height to shift image
image will be horizontally shifted between
(-height_range * height_dimension, height_range * height_dimension)
second value:
fractional bounds of total width to shift image
Image will be vertically shifted between
(-width_range * width_dimension, width_range * width_dimension)
fill_mode : string in {'constant', 'nearest'}
how to fill the empty space caused by the transform
fill_value : float
the value to fill the empty space with if fill_mode='constant'
lazy : boolean
if true, perform the transform on the tensor and return the tensor
if false, only create the affine transform matrix and return that
"""
if isinstance(translation_range, float):
translation_range = (translation_range, translation_range)
self.height_range = translation_range[0]
self.width_range = translation_range[1]
self.fill_mode = fill_mode
self.fill_value = fill_value
self.target_fill_mode = target_fill_mode
self.target_fill_value = target_fill_value
self.lazy = lazy
def __call__(self, x, y=None):
# height shift
if self.height_range > 0:
tx = random.uniform(-self.height_range, self.height_range) * x.size(1)
else:
tx = 0
# width shift
if self.width_range > 0:
ty = random.uniform(-self.width_range, self.width_range) * x.size(2)
else:
ty = 0
translation_matrix = np.array([[1, 0, tx],
[0, 1, ty],
[0, 0, 1]])
if self.lazy:
return translation_matrix
else:
x_transformed = torch.from_numpy(apply_transform(x.numpy(),
translation_matrix, fill_mode=self.fill_mode, fill_value=self.fill_value))
if y:
y_transformed = torch.from_numpy(apply_transform(y.numpy(), translation_matrix,
fill_mode=self.target_fill_mode, fill_value=self.target_fill_value))
return x_transformed, y_transformed
else:
return x_transformed
class Shear(object):
def __init__(self,
shear_range,
fill_mode='constant',
fill_value=0.,
target_fill_mode='nearest',
target_fill_value=0.,
lazy=False):
"""Randomly shear an image with radians (-shear_range, shear_range)
Arguments
---------
shear_range : float
radian bounds on the shear transform
fill_mode : string in {'constant', 'nearest'}
how to fill the empty space caused by the transform
fill_value : float
the value to fill the empty space with if fill_mode='constant'
lazy : boolean
if true, perform the transform on the tensor and return the tensor
if false, only create the affine transform matrix and return that
"""
self.shear_range = shear_range
self.fill_mode = fill_mode
self.fill_value = fill_value
self.target_fill_mode = target_fill_mode
self.target_fill_value = target_fill_value
self.lazy = lazy
def __call__(self, x, y=None):
shear = random.uniform(-self.shear_range, self.shear_range)
shear_matrix = np.array([[1, -math.sin(shear), 0],
[0, math.cos(shear), 0],
[0, 0, 1]])
if self.lazy:
return shear_matrix
else:
x_transformed = torch.from_numpy(apply_transform(x.numpy(),
shear_matrix, fill_mode=self.fill_mode, fill_value=self.fill_value))
if y:
y_transformed = torch.from_numpy(apply_transform(y.numpy(), shear_matrix,
fill_mode=self.target_fill_mode, fill_value=self.target_fill_value))
return x_transformed, y_transformed
else:
return x_transformed
class Zoom(object):
def __init__(self,
zoom_range,
fill_mode='constant',
fill_value=0,
target_fill_mode='nearest',
target_fill_value=0.,
lazy=False):
"""Randomly zoom in and/or out on an image
Arguments
---------
zoom_range : tuple or list with 2 values, both between (0, infinity)
lower and upper bounds on percent zoom.
Anything less than 1.0 will zoom in on the image,
anything greater than 1.0 will zoom out on the image.
e.g. (0.7, 1.0) will only zoom in,
(1.0, 1.4) will only zoom out,
(0.7, 1.4) will randomly zoom in or out
fill_mode : string in {'constant', 'nearest'}
how to fill the empty space caused by the transform
fill_value : float
the value to fill the empty space with if fill_mode='constant'
lazy : boolean
if true, perform the transform on the tensor and return the tensor
if false, only create the affine transform matrix and return that
"""
if not isinstance(zoom_range, list) and not isinstance(zoom_range, tuple):
raise ValueError('zoom_range must be tuple or list with 2 values')
self.zoom_range = zoom_range
self.fill_mode = fill_mode
self.fill_value = fill_value
self.target_fill_mode = target_fill_mode
self.target_fill_value = target_fill_value
self.lazy = lazy
def __call__(self, x, y=None):
zx = random.uniform(self.zoom_range[0], self.zoom_range[1])
zy = random.uniform(self.zoom_range[0], self.zoom_range[1])
zoom_matrix = np.array([[zx, 0, 0],
[0, zy, 0],
[0, 0, 1]])
if self.lazy:
return zoom_matrix
else:
x_transformed = torch.from_numpy(apply_transform(x.numpy(),
zoom_matrix, fill_mode=self.fill_mode, fill_value=self.fill_value))
if y:
y_transformed = torch.from_numpy(apply_transform(y.numpy(), zoom_matrix,
fill_mode=self.target_fill_mode, fill_value=self.target_fill_value))
return x_transformed, y_transformed
else:
return x_transformed
# In[3]:
print("\nclass trans")
class trans(object):
def __init__(self,
rotation_range=None,
translation_range=None,
shear_range=None,
zoom_range=None,
fill_mode='constant',
fill_value=0.,
target_fill_mode='nearest',
target_fill_value=0.
):
self.affine = Affine(rotation_range, translation_range, shear_range, zoom_range)
def __call__(self, data):
data = transforms.ToTensor()(data)
return self.affine(data)
# In[4]:
print("\nsquash -> Tensor")
print("softmax_3d -> Tensor")
print("one_hot -> numpy.array")
def squash(s, dim=-1):
norm = torch.norm(s, dim=dim, keepdim=True)
return (norm /(1 + norm**2 + eps)) * s
# not being used anymore. instead using nn.functional.softmax
def softmax_3d(x, dim):
return (torch.exp(x) / torch.sum(torch.sum(torch.sum(torch.exp(x), dim=dim[0], keepdim=True), dim=dim[1], keepdim=True), dim=dim[2], keepdim=True))
def one_hot(tensor, num_classes=10):
return torch.eye(num_classes).cuda().index_select(dim=0, index=tensor.cuda()) # One-hot encode
# return torch.eye(num_classes).index_select(dim=0, index=tensor).numpy() # One-hot encode
# In[5]:
print("class ConvertToCaps")
class ConvertToCaps(nn.Module):
def __init__(self):
super().__init__()
def forward(self, inputs):
# channels first
return torch.unsqueeze(inputs, 2)
# In[6]:
print("class FlattenCaps")
class FlattenCaps(nn.Module):
def __init__(self):
super().__init__()
def forward(self, inputs):
# inputs.shape = (batch, channels, dimensions, height, width)
batch, channels, dimensions, height, width = inputs.shape
inputs = inputs.permute(0, 3, 4, 1, 2).contiguous()
output_shape = (batch, channels * height * width, dimensions)
return inputs.view(*output_shape)
# In[7]:
print("class CapsToScalars")
class CapsToScalars(nn.Module):
def __init__(self):
super().__init__()
def forward(self, inputs):
# inputs.shape = (batch, num_capsules, dimensions)
return torch.norm(inputs, dim=2)
# In[8]:
print("class Conv2DCaps")
# padding should be 'SAME'
# LATER correct: DONT PASS h, w FOR conv2d OPERATION
class Conv2DCaps(nn.Module):
def __init__(self, h, w, ch_i, n_i, ch_j, n_j, kernel_size=3, stride=1, r_num=1):
super().__init__()
self.ch_i = ch_i
self.n_i = n_i
self.ch_j = ch_j
self.n_j = n_j
self.kernel_size = kernel_size
self.stride = stride
self.r_num = r_num
in_channels = self.ch_i * self.n_i
out_channels = self.ch_j * self.n_j
self.pad = 1
# self.w = nn.Parameter(torch.randn(ch_j, n_j, ch_i, n_i, kernel_size, kernel_size) * 0.01).cuda()
# self.w_reshaped = self.w.view(ch_j*n_j, ch_i*n_i, kernel_size, kernel_size)
self.conv1 = nn.Conv2d(in_channels=in_channels,
out_channels=out_channels,
kernel_size=self.kernel_size,
stride=self.stride,
padding=self.pad).cuda()
def forward(self, inputs):
# check if happened properly
# inputs.shape: (batch, channels, dimension, hight, width)
self.batch, self.ch_i, self.n_i, self.h_i, self.w_i = inputs.shape
in_size = self.h_i
x = inputs.view(self.batch, self.ch_i * self.n_i, self.h_i, self.w_i)
x = self.conv1(x)
width = x.shape[2]
x = x.view(inputs.shape[0], self.ch_j, self.n_j, width, width)
return squash(x,dim=2)# squash(x).shape: (batch, channels, dimension, ht, wdth)
# In[9]:
print("class ConvCapsLayer3D")
# SEE kernel_initializer,
class ConvCapsLayer3D(nn.Module):
def __init__(self, ch_i, n_i, ch_j=32, n_j=4, kernel_size=3, r_num=3):
super().__init__()
self.ch_i = ch_i
self.n_i = n_i
self.ch_j = ch_j
self.n_j = n_j
self.kernel_size = kernel_size
self.r_num = r_num
in_channels = 1
out_channels = self.ch_j * self.n_j
stride = (n_i, 1, 1)
pad = (0, 1, 1)
# self.w = nn.Parameter(torch.randn(ch_j*n_j, 1, n_i, 3, 3)).cuda()
self.conv1 = nn.Conv3d(in_channels=in_channels,
out_channels=out_channels,
kernel_size=self.kernel_size,
stride=stride,
padding=pad).cuda()
def forward(self, inputs):
# x.shape = (batch, channels, dimension, height, width)
self.batch, self.ch_i, self.n_i, self.h_i, self.w_i = inputs.shape
in_size = self.h_i
out_size = self.h_i
x = inputs.view(self.batch, self.ch_i * self.n_i, self.h_i, self.w_i)
x = x.unsqueeze(1)
x = self.conv1(x)
self.width = x.shape[-1]
x = x.permute(0,2,1,3,4)
x = x.view(self.batch, self.ch_i, self.ch_j, self.n_j, self.width, self.width)
x = x.permute(0, 4, 5, 3, 2, 1).contiguous()
self.B = x.new(x.shape[0], self.width, self.width, 1, self.ch_j, self.ch_i).zero_()
x = self.update_routing(x, self.r_num)
return x
def update_routing(self, x, itr=3):
# x.shape = (batch, width, width, n_j, ch_j, ch_i)
for i in range(itr):
# softmax of self.B along (1,2,4)
tmp = self.B.permute(0,5,3,1,2,4).contiguous().reshape(x.shape[0],self.ch_i,1,self.width*self.width*self.ch_j)
#k = softmax_3d(self.B, (1,2,4)) # (batch, width, width, 1, ch_j, ch_i)
#k = func.softmax(self.B, dim=4)
k = func.softmax(tmp,dim=-1)
k = k.reshape(x.shape[0],self.ch_i,1,self.width,self.width,self.ch_j).permute(0,3,4,2,5,1).contiguous()
S_tmp = k * x
S = torch.sum(S_tmp, dim=-1, keepdim=True)
S_hat = squash(S)
if i < (itr-1):
agrements = (S_hat * x).sum(dim=3, keepdim=True) # sum over n_j dimension
self.B = self.B + agrements
S_hat = S_hat.squeeze(-1)
#batch, h_j, w_j, n_j, ch_j = S_hat.shape
return S_hat.permute(0, 4, 3, 1, 2).contiguous()
# In[10]:
print("class Mask_CID")
class Mask_CID(nn.Module):
def __init__(self):
super().__init__()
def forward(self, x, target=None):
# x.shape = (batch, classes, dim)
# one-hot required
if target is None:
classes = torch.norm(x, dim=2)
max_len_indices = classes.max(dim=1)[1].squeeze()
else:
max_len_indices = target.max(dim=1)[1]
# print("max_len_indices: ", max_len_indices)
increasing = torch.arange(start=0, end=x.shape[0]).cuda()
m = torch.stack([increasing, max_len_indices], dim=1)
masked = torch.zeros((x.shape[0], 1) + x.shape[2:])
for i in increasing:
masked[i] = x[m[i][0], m[i][1], :].unsqueeze(0)
return masked.squeeze(-1), max_len_indices # dim: (batch, 1, capsule_dim)
# In[11]:
print("class CapsuleLayer")
class CapsuleLayer(nn.Module):
def __init__(self, num_capsules=10, num_routes=640, in_channels=8, out_channels=16, routing_iters=3):
# in_channels: input_dim; out_channels: output_dim.
super().__init__()
self.num_capsules = num_capsules
self.num_routes = num_routes
self.routing_iters = routing_iters
self.W = nn.Parameter(torch.randn(1, num_routes, num_capsules, out_channels, in_channels) * 0.01)
self.bias = nn.Parameter(torch.rand(1, 1, num_capsules, out_channels) * 0.01)
def forward(self, x):
# x: [batch_size, 32, 16] -> [batch_size, 32, 1, 16]
# -> [batch_size, 32, 1, 16, 1]
# print("CapsuleLayer_x.shape: ", x.shape)
x = x.unsqueeze(2).unsqueeze(dim=4)
u_hat = torch.matmul(self.W, x).squeeze() # u_hat -> [batch_size, 32, 10, 32]
# b_ij = torch.zeros((batch_size, self.num_routes, self.num_capsules, 1))
b_ij = x.new(x.shape[0], self.num_routes, self.num_capsules, 1).zero_()
for itr in range(self.routing_iters):
c_ij = func.softmax(b_ij, dim=2)
s_j = (c_ij * u_hat).sum(dim=1, keepdim=True) + self.bias
v_j = squash(s_j, dim=-1)
if itr < self.routing_iters-1:
a_ij = (u_hat * v_j).sum(dim=-1, keepdim=True)
b_ij = b_ij + a_ij
v_j = v_j.squeeze() #.unsqueeze(-1)
return v_j # dim: (batch, num_capsules, out_channels or dim_capsules)
# In[12]:
print("class Decoder_mnist")
class Decoder_mnist(nn.Module):
def __init__(self, caps_size=16, num_caps=1, img_size=28, img_channels=1):
super().__init__()
self.num_caps = num_caps
self.img_channels = img_channels
self.img_size = img_size
self.dense = torch.nn.Linear(caps_size*num_caps, 7*7*16).cuda(device)
self.relu = nn.ReLU(inplace=True)
self.reconst_layers1 = nn.Sequential(nn.BatchNorm2d(num_features=16, momentum=0.8),
nn.ConvTranspose2d(in_channels=16, out_channels=64,
kernel_size=3, stride=1, padding=1
)
)
self.reconst_layers2 = nn.ConvTranspose2d(in_channels=64, out_channels=32,
kernel_size=3, stride=2, padding=1
)
self.reconst_layers3 = nn.ConvTranspose2d(in_channels=32, out_channels=16,
kernel_size=3, stride=2, padding=1
)
self.reconst_layers4 = nn.ConvTranspose2d(in_channels=16, out_channels=1,
kernel_size=3, stride=1, padding=1
)
self.reconst_layers5 = nn.ReLU()
def forward(self, x):
# x.shape = (batch, 1, capsule_dim(=32 for MNIST))
batch = x.shape[0]
x = x.type(torch.FloatTensor)
x = x.cuda()
x = self.dense(x)
x = self.relu(x)
x = x.reshape(-1, 16, 7, 7)
x = self.reconst_layers1(x)
x = self.reconst_layers2(x)
# padding
p2d = (1, 0, 1, 0)
x = func.pad(x, p2d, "constant", 0)
x = self.reconst_layers3(x)
# padding
p2d = (1, 0, 1, 0)
x = func.pad(x, p2d, "constant", 0)
x = self.reconst_layers4(x)
x = self.reconst_layers5(x)
x = x.reshape(-1, 1, self.img_size, self.img_size)
return x # dim: (batch, 1, imsize, imsize)
class Decoder_mnist32x32(nn.Module):
def __init__(self, caps_size=16, num_caps=1, img_size=28, img_channels=1):
super().__init__()
self.num_caps = num_caps
self.img_channels = img_channels
self.img_size = img_size
self.dense = torch.nn.Linear(caps_size*num_caps, 8*8*16).cuda(device)
self.relu = nn.ReLU(inplace=True)
self.reconst_layers1 = nn.Sequential(nn.BatchNorm2d(num_features=16, momentum=0.8),
nn.ConvTranspose2d(in_channels=16, out_channels=64,
kernel_size=3, stride=1, padding=1
)
)
self.reconst_layers2 = nn.ConvTranspose2d(in_channels=64, out_channels=32,
kernel_size=3, stride=2, padding=1
)
self.reconst_layers3 = nn.ConvTranspose2d(in_channels=32, out_channels=16,
kernel_size=3, stride=2, padding=1
)
self.reconst_layers4 = nn.ConvTranspose2d(in_channels=16, out_channels=3,
kernel_size=3, stride=1, padding=1
)
# self.reconst_layers4 = nn.ConvTranspose2d(in_channels=8, out_channels=3,
# kernel_size=3, stride=1, padding=1
# )
self.reconst_layers5 = nn.ReLU()
def forward(self, x):
# x.shape = (batch, 1, capsule_dim(=32 for MNIST))
batch = x.shape[0]
x = x.type(torch.FloatTensor)
x = x.cuda()
x = self.dense(x)
x = self.relu(x)
x = x.reshape(-1, 16, 8, 8)
x = self.reconst_layers1(x)
x = self.reconst_layers2(x)
# padding
p2d = (1, 0, 1, 0)
x = func.pad(x, p2d, "constant", 0)
x = self.reconst_layers3(x)
# padding
p2d = (1, 0, 1, 0)
x = func.pad(x, p2d, "constant", 0)
x = self.reconst_layers4(x)
# x = self.reconst_layers5(x)
x = x.reshape(-1, self.img_channels, self.img_size, self.img_size)
return x # dim: (batch, 1, imsize, imsize)
# In[13]:
print("class Model")
class Model(nn.Module):
def __init__(self):
super().__init__()
self.conv2d = nn.Conv2d(in_channels=1, out_channels=128,
kernel_size=3, stride=1, padding=1)
self.batchNorm = torch.nn.BatchNorm2d(num_features=128, eps=1e-08, momentum=0.99)
self.toCaps = ConvertToCaps()
self.conv2dCaps1_nj_4_strd_2 = Conv2DCaps(h=28, w=28, ch_i=128, n_i=1, ch_j=32, n_j=4, kernel_size=3, stride=2, r_num=1)
self.conv2dCaps1_nj_4_strd_1_1 = Conv2DCaps(h=14, w=14, ch_i=32, n_i=4, ch_j=32, n_j=4, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps1_nj_4_strd_1_2 = Conv2DCaps(h=14, w=14, ch_i=32, n_i=4, ch_j=32, n_j=4, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps1_nj_4_strd_1_3 = Conv2DCaps(h=14, w=14, ch_i=32, n_i=4, ch_j=32, n_j=4, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps2_nj_8_strd_2 = Conv2DCaps(h=14, w=14, ch_i=32, n_i=4, ch_j=32, n_j=8, kernel_size=3, stride=2, r_num=1)
self.conv2dCaps2_nj_8_strd_1_1 = Conv2DCaps(h=7, w=7, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps2_nj_8_strd_1_2 = Conv2DCaps(h=7, w=7, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps2_nj_8_strd_1_3 = Conv2DCaps(h=7, w=7, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps3_nj_8_strd_2 = Conv2DCaps(h=7, w=7, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=2, r_num=1)
self.conv2dCaps3_nj_8_strd_1_1 = Conv2DCaps(h=4, w=4, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps3_nj_8_strd_1_2 = Conv2DCaps(h=4, w=4, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps3_nj_8_strd_1_3 = Conv2DCaps(h=4, w=4, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps4_nj_8_strd_2 = Conv2DCaps(h=4, w=4, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=2, r_num=1)
self.conv3dCaps4_nj_8 = ConvCapsLayer3D(ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, r_num=3)
self.conv2dCaps4_nj_8_strd_1_1 = Conv2DCaps(h=2, w=2, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps4_nj_8_strd_1_2 = Conv2DCaps(h=2, w=2, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.decoder = Decoder_mnist(caps_size=16, num_caps=1, img_size=28, img_channels=1)
self.flatCaps = FlattenCaps()
self.digCaps = CapsuleLayer(num_capsules=10, num_routes=640, in_channels=8, out_channels=16, routing_iters=3)
self.capsToScalars = CapsToScalars()
self.mask = Mask_CID()
self.mse_loss = nn.MSELoss(reduction="none")
def forward(self, x, target=None):
x = self.conv2d(x)
x = self.batchNorm(x)
x = self.toCaps(x)
x = self.conv2dCaps1_nj_4_strd_2(x)
x_skip = self.conv2dCaps1_nj_4_strd_1_1(x)
x = self.conv2dCaps1_nj_4_strd_1_2(x)
x = self.conv2dCaps1_nj_4_strd_1_3(x)
x = x + x_skip
x = self.conv2dCaps2_nj_8_strd_2(x)
x_skip = self.conv2dCaps2_nj_8_strd_1_1(x)
x = self.conv2dCaps2_nj_8_strd_1_2(x)
x = self.conv2dCaps2_nj_8_strd_1_3(x)
x = x + x_skip
x = self.conv2dCaps3_nj_8_strd_2(x)
x_skip = self.conv2dCaps3_nj_8_strd_1_1(x)
x = self.conv2dCaps3_nj_8_strd_1_2(x)
x = self.conv2dCaps3_nj_8_strd_1_3(x)
x = x + x_skip
x1 = x
x = self.conv2dCaps4_nj_8_strd_2(x)
x_skip = self.conv3dCaps4_nj_8(x)
x = self.conv2dCaps4_nj_8_strd_1_1(x)
x = self.conv2dCaps4_nj_8_strd_1_2(x)
x = x + x_skip
x2 = x
xa = self.flatCaps(x1)
xb = self.flatCaps(x2)
x = torch.cat((xa, xb), dim=-2)
dig_caps = self.digCaps(x)
x = self.capsToScalars(dig_caps)
masked, indices = self.mask(dig_caps, target)
decoded = self.decoder(masked)
return dig_caps, masked, decoded, indices
def margin_loss(self, x, labels, lamda, m_plus, m_minus):
v_c = torch.norm(x, dim=2, keepdim=True)
tmp1 = func.relu(m_plus - v_c).view(x.shape[0], -1) ** 2
tmp2 = func.relu(v_c - m_minus).view(x.shape[0], -1) ** 2
loss = labels*tmp1 + lamda*(1-labels)*tmp2
loss = loss.sum(dim=1)
return loss
def reconst_loss(self, recnstrcted, data):
loss = self.mse_loss(recnstrcted.view(recnstrcted.shape[0], -1), data.view(recnstrcted.shape[0], -1))
return 0.4 * loss.sum(dim=1)
def loss(self, x, recnstrcted, data, labels, lamda=0.5, m_plus=0.9, m_minus=0.1):
loss = self.margin_loss(x, labels, lamda, m_plus, m_minus) + self.reconst_loss(recnstrcted, data)
return loss.mean()
####################################################################################################################################################
####################################################################################################################################################
class Model32x32(nn.Module):
def __init__(self):
super().__init__()
self.conv2d = nn.Conv2d(in_channels=3, out_channels=128,
kernel_size=3, stride=1, padding=1)
self.batchNorm = torch.nn.BatchNorm2d(num_features=128, eps=1e-08, momentum=0.99)
self.toCaps = ConvertToCaps()
self.conv2dCaps1_nj_4_strd_2 = Conv2DCaps(h=32, w=32, ch_i=128, n_i=1, ch_j=32, n_j=4, kernel_size=3, stride=2, r_num=1)
self.conv2dCaps1_nj_4_strd_1_1 = Conv2DCaps(h=16, w=16, ch_i=32, n_i=4, ch_j=32, n_j=4, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps1_nj_4_strd_1_2 = Conv2DCaps(h=16, w=16, ch_i=32, n_i=4, ch_j=32, n_j=4, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps1_nj_4_strd_1_3 = Conv2DCaps(h=16, w=16, ch_i=32, n_i=4, ch_j=32, n_j=4, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps2_nj_8_strd_2 = Conv2DCaps(h=16, w=16, ch_i=32, n_i=4, ch_j=32, n_j=8, kernel_size=3, stride=2, r_num=1)
self.conv2dCaps2_nj_8_strd_1_1 = Conv2DCaps(h=8, w=8, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps2_nj_8_strd_1_2 = Conv2DCaps(h=8, w=8, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps2_nj_8_strd_1_3 = Conv2DCaps(h=8, w=8, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps3_nj_8_strd_2 = Conv2DCaps(h=8, w=8, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=2, r_num=1)
self.conv2dCaps3_nj_8_strd_1_1 = Conv2DCaps(h=4, w=4, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps3_nj_8_strd_1_2 = Conv2DCaps(h=4, w=4, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps3_nj_8_strd_1_3 = Conv2DCaps(h=4, w=4, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps4_nj_8_strd_2 = Conv2DCaps(h=4, w=4, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=2, r_num=1)
self.conv3dCaps4_nj_8 = ConvCapsLayer3D(ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, r_num=3)
self.conv2dCaps4_nj_8_strd_1_1 = Conv2DCaps(h=2, w=2, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)
self.conv2dCaps4_nj_8_strd_1_2 = Conv2DCaps(h=2, w=2, ch_i=32, n_i=8, ch_j=32, n_j=8, kernel_size=3, stride=1, r_num=1)