-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
205 lines (163 loc) · 5.98 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
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
import torch
from torch.nn import functional as F
import numpy as np
import os
import matplotlib.pyplot as plt
from tqdm import tqdm
from matplotlib import gridspec
from model.denomamba_arch import DenoMamba
from data import create_loaders_mix
from options import TrainOptions
np.random.seed(392)
torch.manual_seed(392)
opt = TrainOptions().parse()
full_dose_path = opt.full_dose_path
quarter_dose_path = opt.quarter_dose_path
dataset_ratio = opt.dataset_ratio
train_ratio = opt.train_ratio
batch_size = opt.batch_size
learning_rate = opt.learning_rate
max_epoch = opt.max_epoch
continue_to_train = opt.continue_to_train
ckpt_path = opt.ckpt_path
batch_number = opt.batch_number
validation_freq = opt.validation_freq
save_freq = opt.save_freq
path_to_save = opt.path_to_save
in_ch = opt.in_ch
out_ch = opt.out_ch
dim = opt.dim
num_blocks = opt.num_blocks
n_layer = opt.n_layer
num_refinement_blocks = opt.num_refinement_blocks
if not os.path.exists(path_to_save):
os.makedirs(path_to_save)
def get_pixel_loss(target, prediction):
return F.l1_loss(prediction, target)
def calculate_psnr(original, reconstructed):
mse = F.mse_loss(original, reconstructed)
if mse == 0:
return float('inf')
max_pixel_value = 1
psnr = 20 * torch.log10(max_pixel_value / torch.sqrt(mse))
return psnr.item()
trainloader, validloader = create_loaders_mix(
full_dose_path=full_dose_path,
quarter_dose_path=quarter_dose_path,
dataset_ratio=dataset_ratio,
train_ratio=train_ratio,
batch_size=batch_size
)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
net_model = DenoMamba(
inp_channels=in_ch,
out_channels=out_ch,
dim=dim,
num_blocks=num_blocks,
num_refinement_blocks=num_refinement_blocks,
).to(device)
lr = learning_rate
optimG = torch.optim.Adam(net_model.parameters(), lr=lr)
# Calculate model size
model_size = sum(param.numel() for param in net_model.parameters())
print("Model params: %.2f M" % (model_size / 1e6))
# Load checkpoint if continuing training
if continue_to_train:
ckpt_model = torch.load(ckpt_path)
net_weights = ckpt_model["net_model"]
optimG_weights = ckpt_model["optimG"]
net_model.load_state_dict(net_weights)
optimG.load_state_dict(optimG_weights)
start_epoch = ckpt_model["epoch"] + 1
print(f"Resuming training from epoch {start_epoch}")
else:
start_epoch = 0
train_loss = []
for epoch in range(start_epoch, max_epoch):
with tqdm(trainloader, unit="batch") as tepoch:
tmp_tr_loss = 0
tr_sample = 0
net_model.train()
total_psnr = []
for data, target in tepoch:
tepoch.set_description(f"Epoch {epoch + 1}")
# Move data to device
condition = data.to(device)
x_0 = target.to(device)
# Forward pass
recon_train = net_model(condition)
# Compute loss
optimG.zero_grad()
pixel_loss = get_pixel_loss(recon_train, x_0)
loss_G = pixel_loss
loss_G.backward()
# Gradient clipping and optimizer step
torch.nn.utils.clip_grad_norm_(net_model.parameters(), 1)
optimG.step()
tmp_tr_loss += loss_G.item()
tr_sample += len(data)
tepoch.set_postfix({"Loss": loss_G.item()})
# Calculate PSNR
psnr_score = calculate_psnr(recon_train, x_0)
total_psnr.append(psnr_score)
avg_train_psnr = np.mean(total_psnr)
print("Average PSNR in Train: {:.4f}".format(avg_train_psnr))
train_loss.append(tmp_tr_loss / tr_sample)
# Validation step
if (epoch + 1) % validation_freq == 0:
valid_psnr = []
net_model.eval()
with torch.no_grad():
for batch_idx, (data, target) in enumerate(validloader):
condition = data.to(device)
x_0 = target.to(device)
recon_train = net_model(condition)
psnr_score = calculate_psnr(recon_train, x_0)
valid_psnr.append(psnr_score)
if batch_idx == batch_number:
fig = plt.figure()
fig.set_figheight(8)
fig.set_figwidth(28)
spec = gridspec.GridSpec(
ncols=3,
nrows=1,
width_ratios=[1, 1, 1],
wspace=0.01,
hspace=0.01,
height_ratios=[1],
left=0,
right=1,
top=1,
bottom=0,
)
img = condition[0]
img = img.data.squeeze().cpu()
ax = fig.add_subplot(spec[0])
ax.imshow(img.squeeze(), cmap="gray", vmin=0, vmax=1)
ax.axis("off")
ax.set_title("Input")
img = recon_train[0]
img = img.data.squeeze().cpu()
ax = fig.add_subplot(spec[1])
ax.imshow(img.squeeze(), cmap="gray", vmin=0, vmax=1)
ax.axis("off")
ax.set_title("Reconstructed")
img = target[0]
img = img.data.squeeze().cpu()
ax = fig.add_subplot(spec[2])
ax.imshow(img.squeeze(), cmap="gray", vmin=0, vmax=1)
ax.axis("off")
ax.set_title("Ground Truth")
plt.show()
avg_valid_psnr = np.mean(valid_psnr)
print("Average PSNR in Validation: {:.4f}".format(avg_valid_psnr))
# Save model checkpoint
if (epoch + 1) % save_freq == 0:
ckpt = {
"net_model": net_model.state_dict(),
"optimG": optimG.state_dict(),
"epoch": epoch
}
save_path = os.path.join(path_to_save, f"model_epoch_{epoch + 1}.pkl")
torch.save(ckpt, save_path)
print(f"Model saved at {save_path}")