-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpc_train.py
717 lines (557 loc) · 35.2 KB
/
cpc_train.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
# -*- coding: utf-8 -*-
"""
This file contains implementations of the functions used to train the multi directional CPC model:
train_cpc - Function used to facilitate the training of the Multi Directional Contrastive Predictive Coding model.
test_cpc - Function used for the testing of training Multi Directional Contrastive Predictive Coding model.
"""
# Built-in/Generic Imports
import os
import time
# Library Imports
import torch
from torch import optim
from torch.cuda import amp
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
# Own Modules Imports
from utils import *
from models import *
from dataset import *
__author__ = "Jacob Carse"
__copyright__ = "Copyright 2020, Multi-Directional Contrastive Predictive Coding for Histology"
__credits__ = ["Jacob Carse", "Stephen McKenna"]
__license__ = "MIT"
__version__ = "0.0.1"
__maintainer__ = "Jacob Carse"
__email__ = "j.carse@dundee.ac.uk"
__status__ = "Development"
def train_cpc(arguments, device):
"""
Function used to train the Multi-Directional Contrastive Predictive Coding model.
:param arguments: Dictionary containing arguments.
:param device: PyTorch device object.
:return: Returns lists of training and validation losses and an integer for the best performing epoch.
"""
# Loads a TensorBoard Summary Writer.
if arguments["tensorboard"]:
writer = SummaryWriter(os.path.join("TensorBoard", arguments["task"], arguments["experiment"]))
# Loads the training data and splits it into a validation data.
train_data = Dataset(arguments, "train")
validation_data = train_data.get_validation_set()
# Creates the data loaders for the training and validation data.
training_data_loader = DataLoader(train_data, batch_size=arguments["batch_size"],
shuffle=True, num_workers=arguments["data_workers"],
pin_memory=False, drop_last=True)
validation_data_loader = DataLoader(validation_data, batch_size=arguments["batch_size"],
shuffle=False, num_workers=arguments["data_workers"],
pin_memory=False, drop_last=True)
# Shuffles the training and validation data.
random_train_data = train_data.shuffle()
random_validation_data = validation_data.shuffle()
# Creates the random data loaders for the training and validation data.
random_training_loader = DataLoader(random_train_data, batch_size=arguments["cpc_random_patches"],
shuffle=True, num_workers=arguments["data_workers"],
pin_memory=False, drop_last=True)
random_validation_loader = DataLoader(random_validation_data, batch_size=arguments["cpc_random_patches"],
shuffle=True, num_workers=arguments["data_workers"],
pin_memory=False, drop_last=True)
# Calculates the number of patches across each dimension.
in_patches = int(((arguments["image_size"] + 2 * 0 - 1 * (arguments["cpc_patch_size"] - 1) - 1)
/ arguments["cpc_patch_stride"]) + 1)
log(arguments, "Loaded Datasets")
# Initialises the encoder and autoregressor.
encoder = Encoder(arguments["cpc_code_size"], arguments["image_size"])
autoregressor = MultiDirectionalPixelCNN(arguments["cpc_code_size"],
multi_directional=arguments["cpc_multi_directional"])
# Sets the models to training mode.
encoder.train()
autoregressor.train()
# Moves the models to the selected device.
encoder.to(device)
autoregressor.to(device)
# Combines the parameters from the two models.
parameters = list(encoder.parameters()) + list(autoregressor.parameters())
# Initialises a optimiser used to optimise the parameters of the models.
optimiser = optim.Adam(params=parameters, lr=arguments["learning_rate"])
# If 16 bit precision is being used change the model and optimisers precision.
if arguments["precision"] == 16:
scaler = amp.GradScaler()
# Checks if precision level is supported and if not defaults to 32.
elif arguments["precision"] != 32:
log(arguments, "Only 16 and 32 bit precision supported. Defaulting to 32 bit precision.")
log(arguments, "Models Initialised")
# Main logging variables declared.
start_time = time.time()
losses, validation_losses = [], []
best_loss, best_epoch, total_batches = 1e10, 0, 0
log(arguments, "Training Timer Started\n")
# The beginning of the main training loop.
for epoch in range(1, arguments["max_epochs"] + 1):
epoch_loss, num_batches = 0, 0
# Loops through the dataset by each batch.
for batch, _ in training_data_loader:
batch_losses = []
if arguments["precision"] == 16:
with amp.autocast():
# Loads the batch into memory and splits the batch into patches.
batch = batch.to(device)
batch = extract_patches(arguments, batch, in_patches)
# Encodes the patches with the encoder.
encoded_batch = encoder.forward(batch)
encoded_batch = encoded_batch.view(arguments["batch_size"], in_patches, in_patches, -1)
encoded_batch = encoded_batch.permute(0, 3, 1, 2)
# Loads the random patches into memory and splits into patches.
random_batch, _ = next(iter(random_training_loader))
random_batch = random_batch.to(device)
random_batch = extract_patches(arguments, random_batch, in_patches)
# Encodes the random patches with the encoder.
random_encoded = encoder.forward(random_batch)
random_encoded = random_encoded.view(arguments["cpc_random_patches"], in_patches, in_patches, -1)
random_encoded = random_encoded.permute(0, 3, 1, 2)
# Clones the encoded batch for masking.
masked_batch = encoded_batch.clone()
# Applies a mask to the encoded batch.
if arguments["cpc_alt_mask"]:
for i in range(1, 6):
for j in range(1, 6):
masked_batch[:, :, i, j] = 0
else:
for i in range(3, 7):
for j in range(0, 7):
masked_batch[:, :, i, j] = 0
# Forward propagates the autoregressor with the masked batch.
predictions = autoregressor.forward(masked_batch)
# Loops through the images in the batch.
for image in range(arguments["batch_size"]):
# Gets the masked elements of the predicted and encoded patches.
if arguments["cpc_alt_mask"]:
predicted_patches = predictions[image, :, 1:6, 1:6].reshape(1, -1)
target_patches = encoded_batch[image, :, 1:6, 1:6].reshape(1, -1)
else:
predicted_patches = predictions[image, :, 3:7, 0:7].reshape(1, -1)
target_patches = encoded_batch[image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the predicted patches.
good_dot_terms = torch.sum(predicted_patches * target_patches, dim=1)
dot_terms = [torch.unsqueeze(good_dot_terms, dim=0)]
# Loops through the random images for each batch.
for random_image in range(arguments["cpc_random_patches"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
random_patches = random_encoded[random_image, :, 1:6, 1:6].reshape(1, -1)
else:
random_patches = random_encoded[random_image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the random patches.
bad_dot_terms = torch.sum(predicted_patches * random_patches, dim=1)
dot_terms.append(torch.unsqueeze(bad_dot_terms, dim=0))
# Calculates the log softmax for all the dot terms.
log_softmax = torch.log_softmax(torch.cat(dot_terms, dim=0), dim=0)
batch_losses.append(-log_softmax[0, ])
# Finds the loss of the batch by combinding the loss for each image in the batch.
loss = torch.mean(torch.cat(batch_losses))
scaler.scale(loss).backward()
# Updates the weights of the model using the optimiser.
scaler.step(optimiser)
scaler.update()
optimiser.zero_grad()
else:
# Loads the batch into memory and splits the batch into patches.
batch = batch.to(device)
batch = extract_patches(arguments, batch, in_patches)
# Encodes the patches with the encoder.
encoded_batch = encoder.forward(batch)
encoded_batch = encoded_batch.view(arguments["batch_size"], in_patches, in_patches, -1)
encoded_batch = encoded_batch.permute(0, 3, 1, 2)
# Loads the random patches into memory and splits into patches.
random_batch, _ = next(iter(random_training_loader))
random_batch = random_batch.to(device)
random_batch = extract_patches(arguments, random_batch, in_patches)
# Encodes the random patches with the encoder.
random_encoded = encoder.forward(random_batch)
random_encoded = random_encoded.view(arguments["cpc_random_patches"], in_patches, in_patches, -1)
random_encoded = random_encoded.permute(0, 3, 1, 2)
# Clones the encoded batch for masking.
masked_batch = encoded_batch.clone()
# Applies a mask to the encoded batch.
if arguments["cpc_alt_mask"]:
for i in range(1, 6):
for j in range(1, 6):
masked_batch[:, :, i, j] = 0
else:
for i in range(3, 7):
for j in range(0, 7):
masked_batch[:, :, i, j] = 0
# Forward propagates the autoregressor with the masked batch.
predictions = autoregressor.forward(masked_batch)
# Loops through the images in the batch.
for image in range(arguments["batch_size"]):
# Gets the masked elements of the predicted and encoded patches.
if arguments["cpc_alt_mask"]:
predicted_patches = predictions[image, :, 1:6, 1:6].reshape(1, -1)
target_patches = encoded_batch[image, :, 1:6, 1:6].reshape(1, -1)
else:
predicted_patches = predictions[image, :, 3:7, 0:7].reshape(1, -1)
target_patches = encoded_batch[image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the predicted patches.
good_dot_terms = torch.sum(predicted_patches * target_patches, dim=1)
dot_terms = [torch.unsqueeze(good_dot_terms, dim=0)]
# Loops through the random images for each batch.
for random_image in range(arguments["cpc_random_patches"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
random_patches = random_encoded[random_image, :, 1:6, 1:6].reshape(1, -1)
else:
random_patches = random_encoded[random_image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the random patches.
bad_dot_terms = torch.sum(predicted_patches * random_patches, dim=1)
dot_terms.append(torch.unsqueeze(bad_dot_terms, dim=0))
# Calculates the log softmax for all the dot terms.
log_softmax = torch.log_softmax(torch.cat(dot_terms, dim=0), dim=0)
batch_losses.append(-log_softmax[0, ])
# Finds the loss of the batch by combinding the loss for each image in the batch.
loss = torch.mean(torch.cat(batch_losses))
loss.backward()
# Updates the weights of the model using the optimiser.
optimiser.step()
optimiser.zero_grad()
# Adds the batch loss to the epoch loss and updates the number of batches.
num_batches += 1
epoch_loss += loss.item()
# Writes the batch loss to TensorBoard.
if arguments["tensorboard"]:
writer.add_scalar("Loss/batch", loss.item(), num_batches + total_batches)
# Logs the details of the training batch.
if num_batches % arguments["log_intervals"] == 0:
log(arguments, "Time: {}s\tTrain Epoch: {} [{}/{}] ({:.0f}%)]\tLoss: {:.6f}".format(
str(int(time.time() - start_time)).rjust(6, '0'), str(epoch).rjust(2, '0'),
str(num_batches * arguments["batch_size"]).rjust(len(str(len(train_data))), '0'),
len(train_data), 100. * num_batches / (len(train_data) / arguments["batch_size"]),
epoch_loss / num_batches
))
# Stops the epoch early if specified.
if num_batches == arguments["batches_per_epoch"]:
break
# Writes the epoch loss to TensorBoard.
if arguments["tensorboard"]:
writer.add_scalar("Loss/train", epoch_loss / num_batches, epoch)
# Performs a validation epoch with no gradients.
with torch.no_grad():
# Logging metrics for validation epoch.
validation_loss, validation_batches = 0, 0
# Loops through the validation dataset.
for batch, _ in validation_data_loader:
batch_losses = []
if arguments["precision"] == 16:
with amp.autocast():
# Moves the batch to the selected device and splits the images to patches.
batch = batch.to(device)
batch = extract_patches(arguments, batch, in_patches)
# Encodes the patches with the encoder.
encoded_batch = encoder.forward(batch)
encoded_batch = encoded_batch.view(arguments["batch_size"], in_patches, in_patches, -1)
encoded_batch = encoded_batch.permute(0, 3, 1, 2)
# Loads the random patches into memory and splits into patches.
random_batch, _ = next(iter(random_validation_loader))
random_batch = random_batch.to(device)
random_batch = extract_patches(arguments, random_batch, in_patches)
# Encodes the random patches with the encoder.
random_encoded = encoder.forward(random_batch)
random_encoded = random_encoded.view(arguments["cpc_random_patches"], in_patches, in_patches, -1)
random_encoded = random_encoded.permute(0, 3, 1, 2)
# Clones the encoded batch for masking.
masked_batch = encoded_batch.clone()
# Applies a mask to the encoded batch.
if arguments["cpc_alt_mask"]:
for i in range(1, 6):
for j in range(1, 6):
masked_batch[:, :, i, j] = 0
else:
for i in range(3, 7):
for j in range(0, 7):
masked_batch[:, :, i, j] = 0
# Forward propagates the autoregressor with the masked batch.
predictions = autoregressor.forward(masked_batch)
# Loops through the images in the batch.
for image in range(arguments["batch_size"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
predicted_patches = predictions[image, :, 1:6, 1:6].reshape(1, -1)
target_patches = encoded_batch[image, :, 1:6, 1:6].reshape(1, -1)
else:
predicted_patches = predictions[image, :, 3:7, 0:7].reshape(1, -1)
target_patches = encoded_batch[image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the predicted patches.
good_dot_terms = torch.sum(predicted_patches * target_patches, dim=1)
dot_terms = [torch.unsqueeze(good_dot_terms, dim=0)]
# Loops through the random images for each batch.
for random_image in range(arguments["cpc_random_patches"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
random_patches = random_encoded[random_image, :, 1:6, 1:6].reshape(1, -1)
else:
random_patches = random_encoded[random_image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the random patches.
bad_dot_terms = torch.sum(predicted_patches * random_patches, dim=1)
dot_terms.append(torch.unsqueeze(bad_dot_terms, dim=0))
# Calculates the log softmax for all the dot terms.
log_softmax = torch.log_softmax(torch.cat(dot_terms, dim=0), dim=0)
batch_losses.append(-log_softmax[0,])
else:
# Moves the batch to the selected device and splits the images to patches.
batch = batch.to(device)
batch = extract_patches(arguments, batch, in_patches)
# Encodes the patches with the encoder.
encoded_batch = encoder.forward(batch)
encoded_batch = encoded_batch.view(arguments["batch_size"], in_patches, in_patches, -1)
encoded_batch = encoded_batch.permute(0, 3, 1, 2)
# Loads the random patches into memory and splits into patches.
random_batch, _ = next(iter(random_validation_loader))
random_batch = random_batch.to(device)
random_batch = extract_patches(arguments, random_batch, in_patches)
# Encodes the random patches with the encoder.
random_encoded = encoder.forward(random_batch)
random_encoded = random_encoded.view(arguments["cpc_random_patches"], in_patches, in_patches, -1)
random_encoded = random_encoded.permute(0, 3, 1, 2)
# Clones the encoded batch for masking.
masked_batch = encoded_batch.clone()
# Applies a mask to the encoded batch.
if arguments["cpc_alt_mask"]:
for i in range(1, 6):
for j in range(1, 6):
masked_batch[:, :, i, j] = 0
else:
for i in range(3, 7):
for j in range(0, 7):
masked_batch[:, :, i, j] = 0
# Forward propagates the autoregressor with the masked batch.
predictions = autoregressor.forward(masked_batch)
# Loops through the images in the batch.
for image in range(arguments["batch_size"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
predicted_patches = predictions[image, :, 1:6, 1:6].reshape(1, -1)
target_patches = encoded_batch[image, :, 1:6, 1:6].reshape(1, -1)
else:
predicted_patches = predictions[image, :, 3:7, 0:7].reshape(1, -1)
target_patches = encoded_batch[image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the predicted patches.
good_dot_terms = torch.sum(predicted_patches * target_patches, dim=1)
dot_terms = [torch.unsqueeze(good_dot_terms, dim=0)]
# Loops through the random images for each batch.
for random_image in range(arguments["cpc_random_patches"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
random_patches = random_encoded[random_image, :, 1:6, 1:6].reshape(1, -1)
else:
random_patches = random_encoded[random_image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the random patches.
bad_dot_terms = torch.sum(predicted_patches * random_patches, dim=1)
dot_terms.append(torch.unsqueeze(bad_dot_terms, dim=0))
# Calculates the log softmax for all the dot terms.
log_softmax = torch.log_softmax(torch.cat(dot_terms, dim=0), dim=0)
batch_losses.append(-log_softmax[0,])
# Combines the loss for each image into a batch loss
validation_batches += 1
validation_loss += torch.mean(torch.cat(batch_losses))
# Stops the epoch early if specified.
if validation_batches == arguments["batches_per_epoch"]:
break
# Writes the validation loss to TensorBoard
if arguments["tensorboard"]:
writer.add_scalar("Loss/validation", validation_loss / validation_batches, epoch)
# Adds the epoch and validation loss to a list of losses.
losses.append(epoch_loss / num_batches)
validation_losses.append(validation_loss / validation_batches)
# Logs the epoch information.
log(arguments, "\nEpoch: {}\tLoss: {:.6f}\tValidation Loss: {:.6f}\n\n".
format(epoch, losses[-1], validation_losses[-1]))
# Adds the total number of batches.
total_batches += num_batches
# Checks if the validation loss is the best achieved loss and saves the model.
if validation_losses[-1] < best_loss:
best_loss = validation_losses[-1]
best_epoch = epoch
encoder.save_model(arguments["model_dir"], arguments["experiment"], "best")
autoregressor.save_model(arguments["model_dir"], arguments["experiment"], "best")
# Saves the models with reference to the current epoch.
#encoder.save_model(arguments["model_dir"], arguments["experiment"], epoch)
#autoregressor.save_model(arguments["model_dir"], arguments["experiment"], epoch)
# Checks if training has performed the minimum number of epochs.
if epoch >= arguments["min_epochs"]:
# Calculates the generalised validation loss.
g_loss = 100 * ((validation_losses[-1] / min(validation_losses[:-1])) - 1)
# Calculates the training progress using a window over the training losses.
t_progress = 1000 * ((sum(losses[-(arguments["window"] + 1): - 1]) /
(arguments["window"] * min(losses[-(arguments["window"] + 1): - 1]))) - 1)
# Compares the generalised loss and training progress against a selected target value.
if g_loss / t_progress > arguments["target"]:
break
# Logs that the training has finished.
log(arguments, f"\n\nTraining Finished after {epoch} epochs in {int(time.time() - start_time)}s")
# Returns the loss values from the training.
return losses, validation_losses, best_epoch
def test_cpc(arguments, device):
"""
Function used to test a trained Multi-Directional Contrastive Predictive Coding model.
:param arguments: Dictionary containing arguments.
:param device: PyTorch device object.
:return: Returns the loss for the testing data on the model.
"""
# Loads the testing dataset.
test_data = Dataset(arguments, "test")
# Creates the data loader for the testing data.
test_data_loader = DataLoader(test_data, batch_size=arguments["batch_size"],
shuffle=False, num_workers=arguments["data_workers"],
pin_memory=False, drop_last=True)
# Shuffles a copy of the testing data.
random_test_data = test_data.shuffle()
# Creates the data loader for the random testing data.
random_test_loader = DataLoader(random_test_data, batch_size=arguments["cpc_random_patches"],
shuffle=True, num_workers=arguments["data_workers"],
pin_memory=False, drop_last=True)
# Calculates the number of patches across each dimension.
in_patches = int(((arguments["image_size"] + 2 * 0 - 1 * (arguments["cpc_patch_size"] - 1) - 1)
/ arguments["cpc_patch_stride"]) + 1)
log(arguments, "Loaded Testing Data")
# Initialises the encoder and autoregressor.
encoder = Encoder(arguments["cpc_code_size"], arguments["image_size"])
autoregressor = MultiDirectionalPixelCNN(arguments["cpc_code_size"],
multi_directional=arguments["cpc_multi_directional"])
# Loads the trained weights of the encoder and autoregressor.
encoder.load_state_dict(torch.load(os.path.join(arguments["model_dir"], arguments["experiment"] +
"_encoder_best.pt"), map_location=device))
autoregressor.load_state_dict(torch.load(os.path.join(arguments["model_dir"], arguments["experiment"] +
"_autoregressor_best.pt"), map_location=device))
# Sets the models to evaluation mode.
encoder.eval()
autoregressor.eval()
# Moves the models to the selected device.
encoder.to(device)
autoregressor.to(device)
log(arguments, "Models Initialised")
# Performs a testing epoch with no gradients.
with torch.no_grad():
# Logging metrics for the testing epoch.
loss, num_batches = 0, 0
# Loops through the testing dataset.
for batch, _ in test_data_loader:
batch_losses = []
if arguments["precision"] == 16:
with amp.autocast:
# Moves the batch to the selected device and splits the images to patches.
batch = batch.to(device)
batch = extract_patches(arguments, batch, in_patches)
# Encodes the patches with the encoder.
encoded_batch = encoder.forward(batch)
encoded_batch = encoded_batch.view(arguments["batch_size"], in_patches, in_patches, -1)
encoded_batch = encoded_batch.permute(0, 3, 1, 2)
# Loads the random patches into memory and splits into patches.
random_batch, _ = next(iter(random_test_loader))
random_batch = random_batch.to(device)
random_batch = extract_patches(arguments, random_batch, in_patches)
# Encodes the random patches with the encoder.
random_encoded = encoder.forward(random_batch)
random_encoded = random_encoded.view(arguments["cpc_random_patches"], in_patches, in_patches, -1)
random_encoded = random_encoded.permute(0, 3, 1, 2)
# Clones the encoded batch for masking.
masked_batch = encoded_batch.clone()
# Applies a mask to the encoded batch.
if arguments["cpc_alt_mask"]:
for i in range(1, 6):
for j in range(1, 6):
masked_batch[:, :, i, j] = 0
else:
for i in range(3, 7):
for j in range(0, 7):
masked_batch[:, :, i, j] = 0
# Forward propagates the autoregressor with the masked batch.
predictions = autoregressor.forward(masked_batch)
# Loops through the images in the batch.
for image in range(arguments["batch_size"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
predicted_patches = predictions[image, :, 1:6, 1:6].reshape(1, -1)
target_patches = encoded_batch[image, :, 1:6, 1:6].reshape(1, -1)
else:
predicted_patches = predictions[image, :, 3:7, 0:7].reshape(1, -1)
target_patches = encoded_batch[image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the predicted patches.
good_dot_terms = torch.sum(predicted_patches * target_patches, dim=1)
dot_terms = [torch.unsqueeze(good_dot_terms, dim=0)]
# Loops through the random images for each batch.
for random_image in range(arguments["cpc_random_patches"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
random_patches = random_encoded[random_image, :, 1:6, 1:6].reshape(1, -1)
else:
random_patches = random_encoded[random_image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the random patches.
bad_dot_terms = torch.sum(predicted_patches * random_patches, dim=1)
dot_terms.append(torch.unsqueeze(bad_dot_terms, dim=0))
# Calculates the log softmax for all the dot terms.
log_softmax = torch.log_softmax(torch.cat(dot_terms, dim=0), dim=0)
batch_losses.append(-log_softmax[0,])
else:
# Moves the batch to the selected device and splits the images to patches.
batch = batch.to(device)
batch = extract_patches(arguments, batch, in_patches)
# Encodes the patches with the encoder.
encoded_batch = encoder.forward(batch)
encoded_batch = encoded_batch.view(arguments["batch_size"], in_patches, in_patches, -1)
encoded_batch = encoded_batch.permute(0, 3, 1, 2)
# Loads the random patches into memory and splits into patches.
random_batch, _ = next(iter(random_test_loader))
random_batch = random_batch.to(device)
random_batch = extract_patches(arguments, random_batch, in_patches)
# Encodes the random patches with the encoder.
random_encoded = encoder.forward(random_batch)
random_encoded = random_encoded.view(arguments["cpc_random_patches"], in_patches, in_patches, -1)
random_encoded = random_encoded.permute(0, 3, 1, 2)
# Clones the encoded batch for masking.
masked_batch = encoded_batch.clone()
# Applies a mask to the encoded batch.
if arguments["cpc_alt_mask"]:
for i in range(1, 6):
for j in range(1, 6):
masked_batch[:, :, i, j] = 0
else:
for i in range(3, 7):
for j in range(0, 7):
masked_batch[:, :, i, j] = 0
# Forward propagates the autoregressor with the masked batch.
predictions = autoregressor.forward(masked_batch)
# Loops through the images in the batch.
for image in range(arguments["batch_size"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
predicted_patches = predictions[image, :, 1:6, 1:6].reshape(1, -1)
target_patches = encoded_batch[image, :, 1:6, 1:6].reshape(1, -1)
else:
predicted_patches = predictions[image, :, 3:7, 0:7].reshape(1, -1)
target_patches = encoded_batch[image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the predicted patches.
good_dot_terms = torch.sum(predicted_patches * target_patches, dim=1)
dot_terms = [torch.unsqueeze(good_dot_terms, dim=0)]
# Loops through the random images for each batch.
for random_image in range(arguments["cpc_random_patches"]):
# Gets the masked elements for the random patches.
if arguments["cpc_alt_mask"]:
random_patches = random_encoded[random_image, :, 1:6, 1:6].reshape(1, -1)
else:
random_patches = random_encoded[random_image, :, 3:7, 0:7].reshape(1, -1)
# Calculates the dot terms for the random patches.
bad_dot_terms = torch.sum(predicted_patches * random_patches, dim=1)
dot_terms.append(torch.unsqueeze(bad_dot_terms, dim=0))
# Calculates the log softmax for all the dot terms.
log_softmax = torch.log_softmax(torch.cat(dot_terms, dim=0), dim=0)
batch_losses.append(-log_softmax[0,])
# Combines the loss for each image into a batch loss.
num_batches += 1
loss += torch.mean(torch.cat(batch_losses))
# Stops the epoch early if specified.
if num_batches == arguments["batches_per_epoch"]:
break
# Gets the testing loss from the batch losses.
loss /= num_batches
log(arguments, "\nTesting Loss: {:.6f}".format(loss))
# Returns the testing loss.
return loss