-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
166 lines (122 loc) · 6.38 KB
/
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
import numpy as np
import torch
import torch.nn.functional as F
from timeit import default_timer
from torch.optim import Adam
import os
import matplotlib.pyplot as plt
from glimpse import glimpse
from utils import *
from data_loader import *
from results import evaluator
import config
torch.manual_seed(0)
np.random.seed(0)
enable_cuda = True
device = torch.device('cuda:' + str(config.gpu_num) if torch.cuda.is_available() and enable_cuda else 'cpu')
# experiment path
exp_path = 'experiments/' \
+ str(config.image_size) + '_' + str(config.n_angles) + '_' + config.exp_desc
os.makedirs(exp_path, exist_ok=True)
step_size = 50
gamma = 0.5
myloss = F.mse_loss
# myloss = F.l1_loss
num_batch_pixels = 3 # The number of iterations over each batch
batch_pixels = 512 # Number of pixels to optimize in each iteration
# Print the experiment setup:
print('Experiment setup:')
print('---> num epochs: {}'.format(config.n_epochs))
print('---> batch_size: {}'.format(config.batch_size))
print('---> Learning rate: {}'.format(config.learning_rate))
print('---> experiment path: {}'.format(exp_path))
print('---> image size: {}'.format(config.image_size))
# Dataset:
train_dataset = CT_images(config.train_path, image_size = config.image_size,
noise_snr = config.noise_snr, theta_actual = config.theta_actual,
theta_init = config.theta_init, subset = 'train')
test_dataset = CT_images(config.test_path, image_size = config.image_size,
noise_snr = config.noise_snr, theta_actual = config.theta_actual,
theta_init = config.theta_init, subset = 'test')
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config.batch_size, num_workers=24, shuffle = True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=config.batch_size, num_workers=24, shuffle = False)
ntrain = len(train_loader.dataset)
n_test = len(test_loader.dataset)
n_ood = 0
if config.ood_analysis:
ood_dataset = CT_images(config.ood_path, image_size = config.image_size,
noise_snr = config.noise_snr, theta_actual = config.theta_actual,
theta_init = config.theta_init, subset = 'ood')
ood_loader = torch.utils.data.DataLoader(ood_dataset, batch_size=config.batch_size, num_workers=24, shuffle = False)
n_ood= len(ood_loader.dataset)
print('---> Number of training, test and ood samples: {}, {}, {}'.format(ntrain,n_test, n_ood))
# Loading model
plot_per_num_epoch = 1 if ntrain > 10000 else 30000//ntrain
model = glimpse(image_size = config.image_size, w_size = config.w_size,
theta_init = config.theta_init, lsg = config.lsg,
learnable_filter = config.learnable_filter,
filter_init = config.filter_init).to(device)
# model = torch.nn.DataParallel(model) # Using multiple GPUs
num_param = count_parameters(model)
print('---> Number of trainable parameters: {}'.format(num_param))
optimizer = Adam(model.parameters(), lr=config.learning_rate)
checkpoint_exp_path = os.path.join(exp_path, 'glimpse.pt')
if os.path.exists(checkpoint_exp_path) and config.restore_model:
checkpoint_glimpse = torch.load(checkpoint_exp_path)
model.load_state_dict(checkpoint_glimpse['model_state_dict'])
optimizer.load_state_dict(checkpoint_glimpse['optimizer_state_dict'])
print('glimpse is restored...')
if config.train:
print('Training...')
if plot_per_num_epoch == -1:
plot_per_num_epoch = config.n_epochs + 1 # only plot in the last epoch
loss_plot = np.zeros([config.n_epochs])
for ep in range(config.n_epochs):
model.train()
t1 = default_timer()
loss_epoch = 0
for image, sinogram in train_loader:
batch_size = image.shape[0]
image = image.to(device)
sinogram = sinogram.to(device)
model.train()
for i in range(num_batch_pixels):
coords = get_mgrid(config.image_size).reshape(-1, 2)
coords = torch.unsqueeze(coords, dim = 0)
coords = coords.expand(batch_size , -1, -1).to(device)
optimizer.zero_grad()
pixels = np.random.randint(low = 0, high = config.image_size**2, size = batch_pixels)
batch_coords = coords[:,pixels]
batch_image = image[:,pixels]
out = model(batch_coords, sinogram)
mse_loss = myloss(out.reshape(batch_size, -1) , batch_image.reshape(batch_size, -1) )
total_loss = mse_loss
total_loss.backward()
optimizer.step()
loss_epoch += total_loss.item()
if ep % plot_per_num_epoch == 0 or (ep + 1) == config.n_epochs:
t2 = default_timer()
loss_epoch/= ntrain
loss_plot[ep] = loss_epoch
plt.plot(np.arange(config.n_epochs)[:ep] , loss_plot[:ep], 'o-', linewidth=2)
plt.xlabel('epoch')
plt.ylabel('MSE loss')
plt.savefig(os.path.join(exp_path, 'Loss.jpg'))
np.save(os.path.join(exp_path, 'Loss.npy'), loss_plot[:ep])
plt.close()
torch.save({'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict()}, checkpoint_exp_path)
print('ep: {}/{} | time: {:.0f} | Loss: {:.6f} | GPU: {:.0f}'.format(ep, config.n_epochs, t2-t1,
loss_epoch, config.gpu_num))
with open(os.path.join(exp_path, 'results.txt'), 'a') as file:
file.write('ep: {}/{} | time: {:.0f} | Loss: {:.6f} | gpu: {:.0f}'.format(ep, config.n_epochs, t2-t1,
loss_epoch, config.gpu_num))
file.write('\n')
evaluator(ep = ep, subset = 'test', data_loader = test_loader,
model = model, exp_path = exp_path)
if config.ood_analysis:
evaluator(ep = ep, subset = 'ood', data_loader = ood_loader,
model = model, exp_path = exp_path)
evaluator(ep = -1, subset = 'test', data_loader = test_loader, model = model, exp_path = exp_path)
if config.ood_analysis:
evaluator(ep = -1, subset = 'ood', data_loader = ood_loader, model = model, exp_path = exp_path)