-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc_rec_train_and_test_dudornet.py
382 lines (309 loc) · 19.1 KB
/
mc_rec_train_and_test_dudornet.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
import numpy as np
import torch
import torch.nn as nn
import os
from tqdm import tqdm
import torchvision.utils as vutils
from torch.nn.utils import clip_grad_norm
import data.Mc_Rec_Dataloader_Dataset as Dataloader_Dataset
from utils.averagemeter import AverageMeter
from utils.save_image import save_image
from skimage.metrics import structural_similarity
from skimage.metrics import peak_signal_noise_ratio
from skimage.metrics import normalized_root_mse
import matplotlib.pyplot as plt
import csv
n_iter = 0
def train(train_loader, model, loss, optimizer, epoch, train_writer, device, logger, args):
global n_iter
train_losses = AverageMeter()
loss_ = torch.FloatTensor([0.]).to(device)
loss_.requires_grad_(True)
loss_sr = torch.FloatTensor([0.]).to(device)
loss_sr.requires_grad_(True)
loss_rec = torch.FloatTensor([0.]).to(device)
loss_rec.requires_grad_(True)
# ------------------------------------------------------------------------------------------------------------------
# total image numbers
if args.data_name == 'IXI':
total = len(Dataloader_Dataset.IXI_dataset(args, 'train'))
elif args.data_name == 'fastMRI':
total = len(Dataloader_Dataset.fastMRI_dataset(args, 'train'))
model.train()
# ------------------------------------------------------------------------------------------------------------------
logger.info('Epoch [{}/{}]\tLearning Rate: {:.3e}'.format(
epoch, args.n_epochs, optimizer.state_dict()['param_groups'][0]['lr']))
for idx, (name_rec, image_rec, fully, under_sample, mask, name_ref, image_ref, fully_ref) in enumerate(train_loader):
image_rec = image_rec.float().to(device) # (bs, 1, 256, 256)
fully = fully.float().to(device) # (bs, 2, 256, 256) k-space torch.cat([fully_real, fully_imag], dim=0)
under_sample = under_sample.float().to(device) # (bs, 2, 256, 256)
mask = mask.float().to(device) # (bs, 1, 256, 256)
image_ref = image_ref.float().to(device)
fully_ref = fully_ref.float().to(device) # (bs, 2, 256, 256)
# # --------------------------------------------------------------------------------------------------------------
# # 将under_sample变为复数形式,并进行数据截断
# fully_real = torch.unsqueeze(fully[:, 0, :, :], 1)
# fully_imag = torch.unsqueeze(fully[:, 1, :, :], 1)
# fully = torch.complex(fully_real, fully_imag)
# 将under_sample变为复数形式
under_sample_real = torch.unsqueeze(under_sample[:, 0, :, :], 1)
under_sample_imag = torch.unsqueeze(under_sample[:, 1, :, :], 1)
under_sample = torch.complex(under_sample_real, under_sample_imag) # bs ,1, 256, 256
under_image_rec = torch.abs(torch.fft.ifft2(under_sample))
# fully = torch.fft.fftshift(torch.fft.fft2(image_rec)) # [b, 1, H, W]
# under_sample = fully * mask
# under_image_rec = torch.abs(torch.fft.ifft2(torch.fft.ifftshift(under_sample)))
# fully_ref = torch.fft.fftshift(torch.fft.fft2(image_ref)) # [B, 1, H, W]
# plt.subplot(131), plt.imshow(np.array(image_ref[0, 0, :, :].cpu()), 'gray')
# plt.subplot(132), plt.imshow(np.array(image_rec[0, 0, :, :].cpu()), 'gray')
# plt.subplot(133), plt.imshow(np.array(under_image_rec[0, 0, :, :].cpu()), 'gray')
# plt.subplot(133), plt.imshow(np.array(under_image_rec[0, 0, :, :].cpu()), 'gray')
# plt.show()
# plt.savefig('y.png')
# 参考图片,欠采样图片,欠采样k-space,掩码
net, image_Rec = model(under_image_rec, under_sample, mask)
# loss_ = loss(image_Rec, image_rec)
# --------------------------------------------------------------------------------------------------------------
# 更新损失并作记录
train_losses.update(loss_.item(), args.batch_size) # 整个epoch的平均损失
train_writer.add_scalar('Train_loss', loss_.item(), n_iter)
# --------------------------------------------------------------------------------------------------------------
# 计算梯度并更新参数
loss_G_L1 = 0
optimizer.zero_grad()
# criterion= nn.MSELoss()
criterion= nn.L1Loss()
# Image domain
n_recurrent=5
loss_img_dc = 0
for j in range(1, n_recurrent + 1):
loss_img_dc = loss_img_dc + criterion(net['r%d_img_dc_pred' % j], image_rec)
# Kspace domain
loss_kspc = 0
for j in range(1, n_recurrent + 1):
loss_kspc = loss_kspc + criterion(net['r%d_kspc_pred' % j], fully)
loss_ = loss_img_dc + loss_kspc
# loss_G_L1 = loss_G_L1.item()
# loss_img = loss_img_dc.item()
# loss_kspc = loss_kspc.item()
# loss_ = loss_G_L1
loss_.backward()
optimizer.step()
# 梯度裁剪
if args.clip_grad_norm:
clip_grad_norm(model.parameters(), max_norm=10, norm_type=2)
n_iter += 1
# --------------------------------------------------------------------------------------------------------------
# 输出已经处理的数据及相应batch的loss_
# print(loss_) # TODO
if (idx + 1) % 60 == 0:
logger.info(
'Epoch-{}-[{}/{}]\t\tLoss: {:.4f}'.format(epoch, (idx + 1) * args.batch_size, total, loss_.item()))
# ------------------------------------------------------------------------------------------------------------------
# 返回一个周期的平均损失
return train_losses.avg
def validate(valid_loader, model, loss, epoch, output_writers, device, logger, args):
logger.info('\n{}: [{}/{}/X{}] Validation:'.format(args.data_name, args.model, args.modal, args.acceleration))
loss_ = torch.FloatTensor([0.]).to(device)
loss_.requires_grad_(True)
loss_sr = torch.FloatTensor([0.]).to(device)
loss_sr.requires_grad_(True)
loss_rec = torch.FloatTensor([0.]).to(device)
loss_rec.requires_grad_(True)
valid_losses = AverageMeter()
valid_psnr = AverageMeter()
valid_ssim = AverageMeter()
valid_nrmse = AverageMeter()
# ------------------------------------------------------------------------------------------------------------------
# total image numbers
if args.data_name == 'IXI':
total = len(Dataloader_Dataset.IXI_dataset(args, 'valid'))
# print(total) # TODO
elif args.data_name == 'fastMRI':
total = len(Dataloader_Dataset.fastMRI_dataset(args, 'valid'))
model.eval()
# ------------------------------------------------------------------------------------------------------------------
image_recs = []
image_Recs = []
image_refs = []
with torch.no_grad():
for idx, (name_rec, image_rec, fully, under_sample, mask, name_ref, image_ref, fully_ref) in tqdm(enumerate(valid_loader), desc='Batch',
total=len(valid_loader), ncols=80):
image_rec = image_rec.float().to(device) # (bs, 1, 256, 256)
fully = fully.float().to(device) # (bs, 2, 256, 256)
under_sample = under_sample.float().to(device) # (bs, 2, 256, 256)
mask = mask.float().to(device) # (bs, 1, 256, 256)
image_ref = image_ref.float().to(device)
fully_ref = fully_ref.float().to(device) # (bs, 2, 256, 256)
# # --------------------------------------------------------------------------------------------------------------
# # 将under_sample变为复数形式,并进行数据截断
# fully_real = torch.unsqueeze(fully[:, 0, :, :], 1)
# fully_imag = torch.unsqueeze(fully[:, 1, :, :], 1)
# fully = torch.complex(fully_real, fully_imag)
#
# 将under_sample变为复数形式
under_sample_real = torch.unsqueeze(under_sample[:, 0, :, :], 1)
under_sample_imag = torch.unsqueeze(under_sample[:, 1, :, :], 1)
under_sample = torch.complex(under_sample_real, under_sample_imag) # bs ,1, 256, 256
under_image_rec = torch.abs(torch.fft.ifft2(under_sample))
# fully = torch.fft.fftshift(torch.fft.fft2(image_rec)) # [b, 1, H, W]
# under_sample = fully * mask
# under_image_rec = torch.abs(torch.fft.ifft2(torch.fft.ifftshift(under_sample)))
# fully_ref = torch.fft.fftshift(torch.fft.fft2(image_ref)) # [B, 1, H, W]
# plt.subplot(131), plt.imshow(np.array(image_ref[0, 0, :, :].cpu()), 'gray')
# plt.subplot(132), plt.imshow(np.array(image_rec[0, 0, :, :].cpu()), 'gray')
# plt.subplot(133), plt.imshow(np.array(under_image_rec[0, 0, :, :].cpu()), 'gray')
# plt.show()
# 参考图片,欠采样图片,欠采样k-space,掩码
net, image_Rec = model(under_image_rec, under_sample, mask)
loss_ = loss(image_Rec, image_rec)
valid_losses.update(loss_.item(), image_rec.size(0)) # 整个epoch的平均损失
# ----------------------------------------------------------------------------------------------------------
# 计算PSNR,SSIM,NRMSE
psnr = peak_signal_noise_ratio(np.array(image_rec[0, 0, :, :].data.cpu()),
np.array(image_Rec[0, 0, :, :].clamp(0, 1).data.cpu()),
data_range=np.max(np.array(image_rec[0, 0, :, :].data.cpu())))
ssim = structural_similarity(np.array(image_rec[0, 0, :, :].data.cpu()),
np.array(image_Rec[0, 0, :, :].clamp(0, 1).data.cpu()))
nrmse = normalized_root_mse(np.array(image_rec[0, 0, :, :].data.cpu()),
np.array(image_Rec[0, 0, :, :].clamp(0, 1).data.cpu()))
valid_psnr.update(psnr, image_rec.size(0))
valid_ssim.update(ssim, image_rec.size(0))
valid_nrmse.update(nrmse, image_rec.size(0))
# ----------------------------------------------------------------------------------------------------------
# 可视化图片质量-周期数的变化
step = int(total / 8)
image_idx = [x for x in range(0, 8 * step, step)]
if idx in image_idx:
image_recs.append(image_rec)
image_Recs.append(image_Rec)
image_refs.append(image_ref)
image_recs = torch.cat(image_recs, dim=0) # (8, 1, 256, 256)
image_Recs = torch.cat(image_Recs, dim=0)
image_refs = torch.cat(image_refs, dim=0)
image_recs = vutils.make_grid(image_recs, normalize=True, scale_each=True) # (3, 256, 2048)
image_Recs = vutils.make_grid(image_Recs, normalize=True, scale_each=True)
image_refs = vutils.make_grid(image_refs, normalize=True, scale_each=True)
output_writers[0].add_image('ground_truth images', torch.unsqueeze(image_recs[0], dim=0), 0)
output_writers[1].add_image('Rec images ', torch.unsqueeze(image_Recs[0], dim=0), epoch)
output_writers[2].add_image('Ref images ', torch.unsqueeze(image_refs[0], dim=0), 0)
# --------------------------------------------------------------------------------------------------------------
return valid_losses.avg, valid_psnr.avg, valid_ssim.avg, valid_nrmse.avg
def test(test_loader, model, loss, output_writers, device, args):
print("======================== Start testing! ========================")
test_psnr = AverageMeter()
test_ssim = AverageMeter()
test_nrmse = AverageMeter()
test_losses = AverageMeter()
# ------------------------------------------------------------------------------------------------------------------
# total image numbers
if args.data_name == 'IXI':
total = len(Dataloader_Dataset.IXI_dataset(args, 'test'))
elif args.data_name == 'fastMRI':
total = len(Dataloader_Dataset.fastMRI_dataset(args, 'test'))
model.eval()
# ------------------------------------------------------------------------------------------------------------------
# /home/shx/My_Projects/My_Net/experiment/IXI/T2/random/MBMSN/X8/test_reconstruction_images
save_dir = os.path.join(args.root_path, args.save_dir, args.data_name, args.modal,
args.mask,
args.model,
'X{}'.format(args.acceleration), 'test_reconstruction_images')
if not os.path.exists(save_dir):
os.makedirs(save_dir, exist_ok=True)
print("The reconstructed images will be saved in {}".format(save_dir))
save_path = os.path.join(args.root_path, args.save_dir, args.data_name, args.modal,
args.mask,
args.model,
'X{}'.format(args.acceleration))
if not os.path.exists(save_path):
os.makedirs(save_path, exist_ok=True)
csv_filename = 'instance_metrics.csv'
field_names = ['name', 'psnr', 'ssim', 'nrmse']
with open(os.path.join(save_path, csv_filename), 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=field_names, delimiter=',')
writer.writeheader()
# ------------------------------------------------------------------------------------------------------------------
image_recs = []
image_Recs = []
image_refs = []
with torch.no_grad():
# image_rec_128 = None
for idx, (name_rec, image_rec, fully, under_sample, mask, name_ref, image_ref, fully_ref) in tqdm(enumerate(test_loader), desc='Batch',
total=len(test_loader), ncols=80):
image_rec = image_rec.float().to(device) # (bs, 1, 256, 256)
fully = fully.float().to(device) # (bs, 2, 256, 256)
under_sample = under_sample.float().to(device) # (bs, 2, 256, 256)
mask = mask.float().to(device) # (bs, 1, 256, 256)
image_ref = image_ref.float().to(device)
fully_ref = fully_ref.float().to(device) # (bs, 2, 256, 256)
# # --------------------------------------------------------------------------------------------------------------
# # 将under_sample变为复数形式,并进行数据截断
# fully_real = torch.unsqueeze(fully[:, 0, :, :], 1)
# fully_imag = torch.unsqueeze(fully[:, 1, :, :], 1)
# fully = torch.complex(fully_real, fully_imag)
# 将under_sample变为复数形式
under_sample_real = torch.unsqueeze(under_sample[:, 0, :, :], 1)
under_sample_imag = torch.unsqueeze(under_sample[:, 1, :, :], 1)
under_sample = torch.complex(under_sample_real, under_sample_imag) # bs ,1, 256, 256
under_image_rec = torch.abs(torch.fft.ifft2(under_sample))
# fully = torch.fft.fftshift(torch.fft.fft2(image_rec)) # [b, 1, H, W]
# under_sample = fully * mask
# under_image_rec = torch.abs(torch.fft.ifft2(torch.fft.ifftshift(under_sample)))
# fully_ref = torch.fft.fftshift(torch.fft.fft2(image_ref)) # [B, 1, H, W]
# plt.subplot(131), plt.imshow(np.array(image_ref[0, 0, :, :].cpu()), 'gray')
# plt.subplot(132), plt.imshow(np.array(image_rec[0, 0, :, :].cpu()), 'gray')
# plt.subplot(133), plt.imshow(np.array(under_image_rec[0, 0, :, :].cpu()), 'gray')
# plt.show()
# 参考图片,欠采样图片,欠采样k-space,掩码
net, image_Rec = model(under_image_rec, under_sample, mask)
loss_ = loss(image_Rec, image_rec)
test_losses.update(loss_.item(), image_rec.size(0)) # 整个epoch的平均损失
# ----------------------------------------------------------------------------------------------------------
# 计算PSNR,SSIM,NRMSE
psnr = peak_signal_noise_ratio(np.array(image_rec[0, 0, :, :].data.cpu()),
np.array(image_Rec[0, 0, :, :].clamp(0, 1).data.cpu()),
data_range=np.max(np.array(image_rec[0, 0, :, :].data.cpu())))
ssim = structural_similarity(np.array(image_rec[0, 0, :, :].data.cpu()),
np.array(image_Rec[0, 0, :, :].clamp(0, 1).data.cpu()))
nrmse = normalized_root_mse(np.array(image_rec[0, 0, :, :].data.cpu()),
np.array(image_Rec[0, 0, :, :].clamp(0, 1).data.cpu()))
test_psnr.update(psnr, image_rec.size(0))
test_ssim.update(ssim, image_rec.size(0))
test_nrmse.update(nrmse, image_rec.size(0))
# write the metrics into a csv File and visualize
with open(os.path.join(save_path, csv_filename), 'a', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=field_names, delimiter=',')
row_data = [name_rec, psnr, ssim, nrmse]
writer.writerow(dict(zip(field_names, row_data)))
# ----------------------------------------------------------------------------------------------------------
# 可视化图片质量-周期数的变化
step = int(total / 8)
image_idx = [x for x in range(0, 8 * step, step)]
if idx in image_idx:
image_recs.append(image_rec)
image_Recs.append(image_Rec)
image_refs.append(image_ref)
# ----------------------------------------------------------------------------------------------------------
# 保存生成的图像数据
name_rec = name_rec[0].split('.')[0]
if not os.path.exists(os.path.join(save_dir, 'image_under')):
os.makedirs(os.path.join(save_dir, 'image_under'), exist_ok=True)
if not os.path.exists(os.path.join(save_dir, 'image_Rec')):
os.makedirs(os.path.join(save_dir, 'image_Rec'), exist_ok=True)
save_path_under = os.path.join(save_dir, 'image_under', name_rec+'_under.jpg')
save_path_rec = os.path.join(save_dir, 'image_Rec', name_rec+'_Rec.jpg')
image_Rec = np.array(image_Rec[0, 0, :, :].clamp(0, 1).cpu())
image_under = np.array(under_image_rec[0, 0, :, :].cpu())
# np.save(save_path, image_sr_256)
save_image(save_path_under, image_under)
save_image(save_path_rec, image_Rec)
image_recs = torch.cat(image_recs, dim=0) # (8, 1, 256, 256)
image_Recs = torch.cat(image_Recs, dim=0)
image_refs = torch.cat(image_refs, dim=0)
image_recs = vutils.make_grid(image_recs, normalize=True, scale_each=True) # (3, 256, 2048)
image_Recs = vutils.make_grid(image_Recs, normalize=True, scale_each=True)
image_refs = vutils.make_grid(image_refs, normalize=True, scale_each=True)
output_writers[0].add_image('ground_truth images', torch.unsqueeze(image_recs[0], dim=0), 0)
output_writers[1].add_image('Rec images ', torch.unsqueeze(image_Recs[0], dim=0), 0)
output_writers[2].add_image('Ref images ', torch.unsqueeze(image_refs[0], dim=0), 0)
return test_losses.avg, test_psnr.avg, test_ssim.avg, test_nrmse.avg