-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
75 lines (63 loc) · 2.51 KB
/
main.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
## Main test
from datasets import OASISDataset
from plotimg import plotimg
from model.configurations import get_VitBase_config
from model.RegistrationNetworks import RegTransformer
import torch
import torch.utils.data as data
import time
from train_val_test import train_epoch, validate_epoch, test_model, plot_test, test_initial
from plotOutput import plotOutput
from plotSample import plotSample
#This is a test
torch.cuda.empty_cache()
torch.backends.cudnn.benchmark = True
learning_rate = 1e-9 # Tune this hyperparameter
batch_size = 1
epochs = 2
device = 'cuda'
#%% data prep
dataset = OASISDataset()
train_set, val_set, test_set = data.random_split(dataset,[0.7,0.1,0.2], generator=torch.Generator().manual_seed(42))
train_loader = torch.utils.data.DataLoader(train_set, batch_size=batch_size, shuffle=True,num_workers=0,pin_memory=True)
val_loader = torch.utils.data.DataLoader(val_set, batch_size=batch_size, shuffle=True,num_workers=0,pin_memory=True)
test_loader = torch.utils.data.DataLoader(test_set, batch_size=batch_size, shuffle=False,num_workers=0)
config = get_VitBase_config(img_size=tuple(dataset.inshape))
model = RegTransformer(config)
model = model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
#%%
""" TRAINING """
print('\n----- Training -----')
epoch = 1
start = time.time()
while epoch <= epochs:
print(f'\n[epoch {epoch} / {epochs}]')
train_ncc, train_MSEt , train_dice, mse_train = train_epoch(model, train_loader, dataset, optimizer, device)
# train_NCC_list.append(train_ncc)
# train_MSEt_list.append(train_MSEt)
# train_dsc_list.append(train_dice)
# train_mse_list.append(mse_train)
#train_hd95_list.append(hd95_train)
val_ncc, val_MSEt, val_dice, mse_val = validate_epoch(model, val_loader, dataset, device)
# val_NCC_list.append(val_ncc)
# val_MSEt_list.append(val_MSEt)
# val_dsc_list.append(val_dice)
# val_mse_list.append(mse_val)
# #val_hd95_list.append(hd95_val)
epoch += 1
end = time.time()
traintime = round(end - start)
print('Total time training: ', traintime, ' seconds')
#%% Output is a dictionary with keys: ['ncc','MSE_T','MSE_img','dice','hd95']
""" TESTING """
output = test_model(model, test_loader, dataset, device)
#%% Copy from GPU to CPU
for key, values in output.items():
for i, value in enumerate(values):
if isinstance(value, torch.Tensor):
values[i] = value.item()
#%% Plot graphs of scores
plotOutput(output)
#%% Plot sample
plotSample(model, dataset, test_set, 5, 40, device)