-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectModelSmall.py
More file actions
657 lines (563 loc) · 36.1 KB
/
ProjectModelSmall.py
File metadata and controls
657 lines (563 loc) · 36.1 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
import torch
import torch.nn as nn
import numpy as np
import random as rn
import pickle
import torchvision
from torch.utils.data import Dataset, DataLoader
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
TESTING_PATH = r'C:\Users\royya\python projects\DeepLearningHW\testfile'
rn.seed(0)
torch.manual_seed(0)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
class Model(nn.Module):
def __init__(self, nearest_neighbors=7, similarity_metrics=3, temporal_depth=1000, image_hight=32, image_width=32, domain_hight=32, domain_width=32, patch_hight=8, patch_width=8, patch_stride=4):
super(Model, self).__init__()
self.temporal_depth = temporal_depth
self.image_hight = image_hight
self.image_width = image_width
self.domain_hight = domain_hight
self.domain_width = domain_width
self.patch_hight = patch_hight
self.patch_width = patch_width
self.patch_stride = patch_stride
# Folder Unfolder
self.folder = nn.Fold(output_size=(image_hight, image_width), kernel_size=(
patch_hight, patch_width), stride=patch_stride)
self.unfolder = nn.Unfold(kernel_size=(
patch_hight, patch_width), stride=patch_stride)
self.fold_unfold_divisor = self.folder(self.unfolder(
torch.ones(size=(1, 3, image_hight, image_width))))
# Noising parameters
self.variance_schedual = np.array(
[(t*(0.02-0.0001)/temporal_depth)+0.0001 for t in range(temporal_depth+1)])
self.mean_schedual = np.array(
[np.sqrt(1-var) for var in self.variance_schedual])
self.alpha_bar = np.array(
[np.prod(1-self.variance_schedual[:el+1]) for el in range(temporal_depth)])
# optional leared temporal encoding
self.temporal_encoding_layer = nn.Linear(
temporal_depth, 3*image_hight*image_width)
# first nearest neighbor block
# first metric
self.first_metric_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.first_metric_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.first_metric_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.first_metric_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=40, kernel_size=2, stride=patch_stride, padding='valid')
# second metric
self.second_metric_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.second_metric_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.second_metric_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.second_metric_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=40, kernel_size=2, stride=patch_stride, padding='valid')
# third metric
self.third_metric_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.third_metric_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.third_metric_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.third_metric_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=40, kernel_size=2, stride=patch_stride, padding='valid')
# temperature
self.first_temperature_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.first_temperature_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.first_temperature_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.first_temperature_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=3, kernel_size=2, stride=patch_stride, padding='valid')
# first sequence of standard layers
self.first_standard_conv_layer = nn.Conv2d(
in_channels=3*(similarity_metrics*nearest_neighbors+1), out_channels=64, kernel_size=3, padding='same')
self.second_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.third_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.fourth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.fifth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.sixth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=3, kernel_size=3, padding='same')
# second nearest neighbor block
# fourth metric
self.fourth_metric_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.fourth_metric_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.fourth_metric_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.fourth_metric_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=40, kernel_size=2, stride=patch_stride, padding='valid')
# fifth metric
self.fifth_metric_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.fifth_metric_conv_layer_2 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.fifth_metric_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.fifth_metric_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=40, kernel_size=2, stride=patch_stride, padding='valid')
# sixth metric
self.sixth_metric_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.sixth_metric_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.sixth_metric_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.sixth_metric_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=40, kernel_size=2, stride=patch_stride, padding='valid')
# second temperature
self.second_temperature_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.second_temperature_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.second_temperature_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.second_temperature_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=3, kernel_size=2, stride=patch_stride, padding='valid')
# second sequence of standard layers
self.seventh_standard_conv_layer = nn.Conv2d(
in_channels=3*(similarity_metrics*nearest_neighbors+1), out_channels=64, kernel_size=3, padding='same')
self.eigth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.ninth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.tenth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.eleventh_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.twelveth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=3, kernel_size=3, padding='same')
# third nearest neighbor block
# seventh metric
self.seventh_metric_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.seventh_metric_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.seventh_metric_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.seventh_metric_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=40, kernel_size=2, stride=patch_stride, padding='valid')
# eigth metric
self.eigth_metric_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.eigth_metric_conv_layer_2 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.eigth_metric_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.eigth_metric_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=40, kernel_size=2, stride=patch_stride, padding='valid')
# ninth metric
self.ninth_metric_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.ninth_metric_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.ninth_metric_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.ninth_metric_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=40, kernel_size=2, stride=patch_stride, padding='valid')
# third temperature
self.third_temperature_conv_layer_1 = nn.Conv2d(
in_channels=3, out_channels=10, kernel_size=3, padding='valid')
self.third_temperature_conv_layer_2 = nn.Conv2d(
in_channels=10, out_channels=20, kernel_size=3, padding='valid')
self.third_temperature_conv_layer_3 = nn.Conv2d(
in_channels=20, out_channels=30, kernel_size=3, padding='valid')
self.third_temperature_conv_layer_4 = nn.Conv2d(
in_channels=30, out_channels=3, kernel_size=2, stride=patch_stride, padding='valid')
# third sequence of standard layers
self.thirteenth_standard_conv_layer = nn.Conv2d(
in_channels=3*(similarity_metrics*nearest_neighbors+1), out_channels=64, kernel_size=3, padding='same')
self.fourteenth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.fifteenth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.sixteenth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.seventeenth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=64, kernel_size=3, padding='same')
self.eighteenth_standard_conv_layer = nn.Conv2d(
in_channels=64, out_channels=3, kernel_size=3, padding='same')
# batch normalisations
self.first_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.second_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.third_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.fourth_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.fifth_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.sixth_batch_norm = nn.BatchNorm2d(num_features=3, affine=True)
self.seventh_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.eigth_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.ninth_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.tenth_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.eleventh_batch_norm = nn.BatchNorm2d(num_features=64, affine=True)
self.twelveth_batch_norm = nn.BatchNorm2d(num_features=3, affine=True)
self.thirteenth_batch_norm = nn.BatchNorm2d(
num_features=64, affine=True)
self.fourteenth_batch_norm = nn.BatchNorm2d(
num_features=64, affine=True)
self.fifteenth_batch_norm = nn.BatchNorm2d(
num_features=64, affine=True)
self.sixteenth_batch_norm = nn.BatchNorm2d(
num_features=64, affine=True)
self.seventeenth_batch_norm = nn.BatchNorm2d(
num_features=64, affine=True)
self.eighteenth_batch_norm = nn.BatchNorm2d(
num_features=3, affine=True)
# activation function
self.relu = nn.ReLU()
# loss functions
self.l2 = nn.MSELoss()
self.l1 = nn.L1Loss()
self.huber_loss = nn.HuberLoss()
# TEMPORAL ENCODING METHODS:
def static_temporal_encoding(self, input_tensor, time_list):
B, _, _, _ = input_tensor.size()
hight_temporal_encoding = []
width_temporal_encoding = []
for time in time_list:
hight_encoding = []
for i in range(self.image_hight):
if i % 2 == 0:
hight_encoding.append(np.sin(time/(i/self.image_hight)))
else:
hight_encoding.append(np.cos(time/((i-1)/self.image_hight)))
width_encoding = []
for j in range(self.image_width):
if j % 2 == 0:
width_encoding.append(np.sin(time/(j/self.image_hight)))
else:
width_encoding.append(
np.cos(time/((j-1)/self.image_hight)))
hight_temporal_encoding.append(hight_encoding)
width_temporal_encoding.append(width_encoding)
hight_temporal_encoding = torch.tensor(
hight_temporal_encoding, device=device, dtype=torch.float32)
width_temporal_encoding = torch.tensor(
width_temporal_encoding, device=device, dtype=torch.float32)
hight_temporal_encoding = hight_temporal_encoding.view(
B, 1, self.image_hight, 1)
width_temporal_encoding = width_temporal_encoding.view(
B, 1, 1, self.image_width)
total_temporal_encoding = hight_temporal_encoding*width_temporal_encoding
output = input+total_temporal_encoding
return output
def dynamic_temporal_encoding(self, time_hot_one_encoding):
B, _ = time_hot_one_encoding.size()
out = self.temporal_encoding_layer(time_hot_one_encoding)
out = out.view(B, self.image_hight, self.image_width)
return out
# FORWARD PASS METHODS:
def nearest_neighbor_forward(self, input, temperature_layers, first_metric_layers, second_metric_layers, third_metric_layers):
# assining constants used for numerical stability
log_epsilon = torch.tensor(0.1**45, dtype=torch.float32)
dev_epsilon = torch.tensor(0.01, dtype=torch.float32)
# temperatures processing
temperatures = temperature_layers[0](input)
temperatures = temperature_layers[1](temperatures)
temperatures = temperature_layers[2](temperatures)
temperatures = torch.abs(temperature_layers[3](temperatures))
B, C, H, W = temperatures.size()
temperatures = temperatures.view(B, C, H, W, 1, 1)
# first metric processing
metric_1 = first_metric_layers[0](input)
metric_1 = first_metric_layers[1](metric_1)
metric_1 = first_metric_layers[2](metric_1)
metric_1 = first_metric_layers[3](metric_1)
B, C, H, W = metric_1.size()
distance_tensor = torch.pow(metric_1.view(
B, C, H, W, 1, 1)-metric_1.view(B, C, 1, 1, H, W), 2)
distance_tensor = torch.sum(distance_tensor, dim=1)
# change dimension for normalisation purpuses and for summing over the different patches at the end
distance_tensor = distance_tensor.view(B, H, W, H*W, 1)
# perform nearest neighbor distribution calculation
first_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(dev_epsilon+temperatures[:, 0, :, :, :, :])), p=1, dim=3)
distance_tensor += torch.log(1 -
first_nearest_neighbor_tensor+log_epsilon)
second_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(dev_epsilon+temperatures[:, 0, :, :, :, :])), p=1, dim=3)
distance_tensor += torch.log(1 -
second_nearest_neighbor_tensor+log_epsilon)
third_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(dev_epsilon+temperatures[:, 0, :, :, :, :])), p=1, dim=3)
distance_tensor += torch.log(1 -
third_nearest_neighbor_tensor+log_epsilon)
fourth_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(dev_epsilon+temperatures[:, 0, :, :, :, :])), p=1, dim=3)
distance_tensor += torch.log(1 -
fourth_nearest_neighbor_tensor+log_epsilon)
fifth_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(dev_epsilon+temperatures[:, 0, :, :, :, :])), p=1, dim=3)
distance_tensor += torch.log(1 -
fifth_nearest_neighbor_tensor+log_epsilon)
sixth_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(dev_epsilon+temperatures[:, 0, :, :, :, :])), p=1, dim=3)
distance_tensor += torch.log(1 -
sixth_nearest_neighbor_tensor+log_epsilon)
seventh_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(dev_epsilon+temperatures[:, 0, :, :, :, :])), p=1, dim=3)
# set up folder and unfolder
Unfolder = self.unfolder
Folder = self.folder
divisor = self.fold_unfold_divisor
# performing nearest neighbor averaging
unfolded_input = Unfolder(input).view(
B, 1, 1, H*W, 3*self.patch_hight*self.patch_width)
unfolded_input = torch.broadcast_to(
unfolded_input, (B, H, W, H*W, 3*self.patch_hight*self.patch_width))
first_nearest_neighbor_tensor = torch.sum(
first_nearest_neighbor_tensor*unfolded_input, dim=3)
first_nearest_neighbor_tensor = Folder(
first_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
second_nearest_neighbor_tensor = torch.sum(
second_nearest_neighbor_tensor*unfolded_input, dim=3)
second_nearest_neighbor_tensor = Folder(
second_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
third_nearest_neighbor_tensor = torch.sum(
third_nearest_neighbor_tensor*unfolded_input, dim=3)
third_nearest_neighbor_tensor = Folder(
third_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
fourth_nearest_neighbor_tensor = torch.sum(
fourth_nearest_neighbor_tensor*unfolded_input, dim=3)
fourth_nearest_neighbor_tensor = Folder(
fourth_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
fifth_nearest_neighbor_tensor = torch.sum(
fifth_nearest_neighbor_tensor*unfolded_input, dim=3)
fifth_nearest_neighbor_tensor = Folder(
fifth_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
sixth_nearest_neighbor_tensor = torch.sum(
sixth_nearest_neighbor_tensor*unfolded_input, dim=3)
sixth_nearest_neighbor_tensor = Folder(
sixth_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
seventh_nearest_neighbor_tensor = torch.sum(
seventh_nearest_neighbor_tensor*unfolded_input, dim=3)
seventh_nearest_neighbor_tensor = Folder(
seventh_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
output = torch.cat((input, first_nearest_neighbor_tensor, second_nearest_neighbor_tensor, third_nearest_neighbor_tensor,
fourth_nearest_neighbor_tensor, fifth_nearest_neighbor_tensor, sixth_nearest_neighbor_tensor, seventh_nearest_neighbor_tensor), dim=1)
# second metric processing
metric_2 = second_metric_layers[0](input)
metric_2 = second_metric_layers[1](metric_2)
metric_2 = second_metric_layers[2](metric_2)
metric_2 = second_metric_layers[3](metric_2)
B, C, H, W = metric_2.size()
distance_tensor = torch.pow(metric_2.view(
B, C, H, W, 1, 1)-metric_2.view(B, C, 1, 1, H, W), 2)
distance_tensor = torch.sum(distance_tensor, dim=1)
# change dimension for normalisation purpuses and for summing over the different patches at the end
distance_tensor = distance_tensor.view(B, H, W, H*W, 1)
# perform nearest neighbor distribution calculation
first_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 1, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
first_nearest_neighbor_tensor+log_epsilon)
second_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 1, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
second_nearest_neighbor_tensor+log_epsilon)
third_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 1, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
third_nearest_neighbor_tensor+log_epsilon)
fourth_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 1, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
fourth_nearest_neighbor_tensor+log_epsilon)
fifth_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 1, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
fifth_nearest_neighbor_tensor+log_epsilon)
sixth_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 1, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
sixth_nearest_neighbor_tensor+log_epsilon)
seventh_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 1, :, :, :, :]+dev_epsilon)), p=1, dim=3)
# performing nearest neighbor averaging
unfolded_input = Unfolder(input).view(
B, 1, 1, H*W, 3*self.patch_hight*self.patch_width)
unfolded_input = torch.broadcast_to(
unfolded_input, (B, H, W, H*W, 3*self.patch_hight*self.patch_width))
first_nearest_neighbor_tensor = torch.sum(
first_nearest_neighbor_tensor*unfolded_input, dim=3)
first_nearest_neighbor_tensor = Folder(
first_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
second_nearest_neighbor_tensor = torch.sum(
second_nearest_neighbor_tensor*unfolded_input, dim=3)
second_nearest_neighbor_tensor = Folder(
second_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
third_nearest_neighbor_tensor = torch.sum(
third_nearest_neighbor_tensor*unfolded_input, dim=3)
third_nearest_neighbor_tensor = Folder(
third_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
fourth_nearest_neighbor_tensor = torch.sum(
fourth_nearest_neighbor_tensor*unfolded_input, dim=3)
fourth_nearest_neighbor_tensor = Folder(
fourth_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
fifth_nearest_neighbor_tensor = torch.sum(
fifth_nearest_neighbor_tensor*unfolded_input, dim=3)
fifth_nearest_neighbor_tensor = Folder(
fifth_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
sixth_nearest_neighbor_tensor = torch.sum(
sixth_nearest_neighbor_tensor*unfolded_input, dim=3)
sixth_nearest_neighbor_tensor = Folder(
sixth_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
seventh_nearest_neighbor_tensor = torch.sum(
seventh_nearest_neighbor_tensor*unfolded_input, dim=3)
seventh_nearest_neighbor_tensor = Folder(
seventh_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
output = torch.cat((output, first_nearest_neighbor_tensor, second_nearest_neighbor_tensor, third_nearest_neighbor_tensor,
fourth_nearest_neighbor_tensor, fifth_nearest_neighbor_tensor, sixth_nearest_neighbor_tensor, seventh_nearest_neighbor_tensor), dim=1)
# third metric processing
metric_3 = third_metric_layers[0](input)
metric_3 = third_metric_layers[1](metric_3)
metric_3 = third_metric_layers[2](metric_3)
metric_3 = third_metric_layers[3](metric_3)
B, C, H, W = metric_3.size()
distance_tensor = torch.pow(metric_3.view(
B, C, H, W, 1, 1)-metric_3.view(B, C, 1, 1, H, W), 2)
distance_tensor = torch.sum(distance_tensor, dim=1)
# change dimension for normalisation purpuses and for summing over the different patches at the end
distance_tensor = distance_tensor.view(B, H, W, H*W, 1)
# perform nearest neighbor distribution calculation
first_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 2, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
first_nearest_neighbor_tensor+log_epsilon)
second_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 2, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
second_nearest_neighbor_tensor+log_epsilon)
third_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 2, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
third_nearest_neighbor_tensor+log_epsilon)
fourth_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 2, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
fourth_nearest_neighbor_tensor+log_epsilon)
fifth_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 2, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
fifth_nearest_neighbor_tensor+log_epsilon)
sixth_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 2, :, :, :, :]+dev_epsilon)), p=1, dim=3)
distance_tensor += torch.log(1 -
sixth_nearest_neighbor_tensor+log_epsilon)
seventh_nearest_neighbor_tensor = nn.functional.normalize(
torch.exp(distance_tensor/(temperatures[:, 2, :, :, :, :]+dev_epsilon)), p=1, dim=3)
# performing nearest neighbor averaging
unfolded_input = Unfolder(input).view(
B, 1, 1, H*W, 3*self.patch_hight*self.patch_width)
unfolded_input = torch.broadcast_to(
unfolded_input, (B, H, W, H*W, 3*self.patch_hight*self.patch_width))
first_nearest_neighbor_tensor = torch.sum(
first_nearest_neighbor_tensor*unfolded_input, dim=3)
first_nearest_neighbor_tensor = Folder(
first_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
second_nearest_neighbor_tensor = torch.sum(
second_nearest_neighbor_tensor*unfolded_input, dim=3)
second_nearest_neighbor_tensor = Folder(
second_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
third_nearest_neighbor_tensor = torch.sum(
third_nearest_neighbor_tensor*unfolded_input, dim=3)
third_nearest_neighbor_tensor = Folder(
third_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
fourth_nearest_neighbor_tensor = torch.sum(
fourth_nearest_neighbor_tensor*unfolded_input, dim=3)
fourth_nearest_neighbor_tensor = Folder(
fourth_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
fifth_nearest_neighbor_tensor = torch.sum(
fifth_nearest_neighbor_tensor*unfolded_input, dim=3)
fifth_nearest_neighbor_tensor = Folder(
fifth_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
sixth_nearest_neighbor_tensor = torch.sum(
sixth_nearest_neighbor_tensor*unfolded_input, dim=3)
sixth_nearest_neighbor_tensor = Folder(
sixth_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
seventh_nearest_neighbor_tensor = torch.sum(
seventh_nearest_neighbor_tensor*unfolded_input, dim=3)
seventh_nearest_neighbor_tensor = Folder(
seventh_nearest_neighbor_tensor.view(B, 3*self.patch_hight*self.patch_width, H*W))/divisor
output = torch.cat((output, first_nearest_neighbor_tensor, second_nearest_neighbor_tensor, third_nearest_neighbor_tensor,
fourth_nearest_neighbor_tensor, fifth_nearest_neighbor_tensor, sixth_nearest_neighbor_tensor, seventh_nearest_neighbor_tensor), dim=1)
return output
def standart_layers_forward(self, standart_layers, batchnorm_layers, input):
temp_out = standart_layers[0](input)
temp_out = self.relu(temp_out)
temp_out = batchnorm_layers[0](temp_out)
out = temp_out+input
temp_out = standart_layers[1](out)
temp_out = self.relu(temp_out)
temp_out = batchnorm_layers[1](temp_out)
out += temp_out
temp_out = standart_layers[2](out)
temp_out = self.relu(temp_out)
temp_out = batchnorm_layers[2](temp_out)
out += temp_out
temp_out = standart_layers[3](out)
temp_out = self.relu(temp_out)
temp_out = batchnorm_layers[3](temp_out)
out += temp_out
temp_out = standart_layers[4](out)
temp_out = self.relu(temp_out)
temp_out = batchnorm_layers[4](temp_out)
out += temp_out
temp_out = standart_layers[5](out)
temp_out = self.relu(temp_out)
temp_out = batchnorm_layers[5](temp_out)
out += temp_out
return out
def estimate_epsilon(self, input, t, static_temporal_encoding=False):
if static_temporal_encoding:
temporal_encoding = self.static_temporal_encoding(t)
else:
temporal_encoding = self.dynamic_temporal_encoding(
t)+self.static_temporal_encoding(nn.functional.one_hot(torch.tensor(t, dtype=torch.long, device=device), num_classes=self.temporal_depth))
out = self.nearest_neighbor_forward(input, [self.first_temperature_conv_layer_1, self.first_temperature_conv_layer_2, self.first_temperature_conv_layer_3, self.first_temperature_conv_layer_4], [self.first_metric_conv_layer_1, self.first_metric_conv_layer_2, self.first_metric_conv_layer_3, self.first_metric_conv_layer_4], [
self.second_metric_conv_layer_1, self.second_metric_conv_layer_2, self.second_metric_conv_layer_3, self.second_metric_conv_layer_4], [self.third_metric_conv_layer_1, self.third_metric_conv_layer_2, self.third_metric_conv_layer_3, self.third_metric_conv_layer_4])
out = self.standart_layers_forward([self.first_standard_conv_layer, self.second_standard_conv_layer, self.third_standard_conv_layer, self.fourth_standard_conv_layer, self.fifth_standard_conv_layer, self.sixth_standard_conv_layer], [
self.first_batch_norm, self.second_batch_norm, self.third_batch_norm, self.fourth_batch_norm, self.fifth_batch_norm, self.sixth_batch_norm], out+temporal_encoding)
out = self.nearest_neighbor_forward(out, [self.second_temperature_conv_layer_1, self.second_temperature_conv_layer_2, self.second_temperature_conv_layer_3, self.second_temperature_conv_layer_4], [self.fourth_metric_conv_layer_1, self.fourth_metric_conv_layer_2, self.fourth_metric_conv_layer_3, self.fourth_metric_conv_layer_4], [
self.fifth_metric_conv_layer_1, self.fifth_metric_conv_layer_2, self.fifth_metric_conv_layer_3, self.fifth_metric_conv_layer_4], [self.sixth_metric_conv_layer_1, self.sixth_metric_conv_layer_2, self.sixth_metric_conv_layer_3, self.sixth_metric_conv_layer_4])
out = self.standart_layers_forward([self.seventh_standard_conv_layer, self.eigth_standard_conv_layer, self.ninth_standard_conv_layer, self.tenth_standard_conv_layer, self.eleventh_standard_conv_layer, self.twelveth_standard_conv_layer], [
self.seventh_batch_norm, self.eigth_batch_norm, self.ninth_batch_norm, self.tenth_batch_norm, self.eleventh_batch_norm, self.twelveth_batch_norm], out+temporal_encoding)
out = self.nearest_neighbor_forward(out, [self.third_temperature_conv_layer_1, self.third_temperature_conv_layer_2, self.third_temperature_conv_layer_3, self.third_temperature_conv_layer_4], [self.seventh_metric_conv_layer_1, self.seventh_metric_conv_layer_2, self.seventh_metric_conv_layer_3, self.seventh_metric_conv_layer_4], [
self.eigth_metric_conv_layer_1, self.eigth_metric_conv_layer_2, self.eigth_metric_conv_layer_3, self.eigth_metric_conv_layer_4], [self.ninth_metric_conv_layer_1, self.ninth_metric_conv_layer_2, self.ninth_metric_conv_layer_3, self.ninth_metric_conv_layer_4])
out = self.standart_layers_forward([self.thirteenth_standard_conv_layer, self.fourteenth_standard_conv_layer, self.fifteenth_standard_conv_layer, self.sixteenth_standard_conv_layer, self.seventeenth_standard_conv_layer, self.eighteenth_standard_conv_layer], [
self.thirteenth_batch_norm, self.fourteenth_batch_norm, self.fifteenth_batch_norm, self.sixteenth_batch_norm, self.seventeenth_batch_norm, self.eighteenth_batch_norm], out+temporal_encoding)
return out
# IMAGE SAMPLING METHODS:
def sample_noised_image(self, input_image, t):
output = torch.normal(
mean=self.alpha_bar[t]*input_image, std=torch.sqrt(1-self.alpha_bar[t]))
return output
def sample_epsilon(self, input_image, t):
noised_imaged = self.sample_noised_image(input_image, t)
epsilon = (
noised_imaged-np.sqrt(self.alpha_bar[t])*input_image/np.sqrt(1-self.alpha_bar[t]))
return epsilon
def sample_one_step_denoising(self, input, estimated_epsilon, t):
normaly_distributed_tensor = torch.normal(mean=torch.zeros(
(self.image_hight, self.image_width)), std=np.sqrt(self.variance_schedual[t]))
output = (1/np.sqrt(self.variance_schedual[t]))*(input-(self.variance_schedual[t]/np.sqrt(
1-self.alpha_bar[t]))*estimated_epsilon)+normaly_distributed_tensor
return output
def denoise_image(self, input, t, static_temporal_encoding=False):
if t == 0:
return input
else:
new_image = self.sample_one_step_denoising(
input, self.estimate_epsilon(input, t, static_temporal_encoding=static_temporal_encoding), t)
return self.denoise_image(new_image, t-1)