-
Notifications
You must be signed in to change notification settings - Fork 3
/
kaid.py
367 lines (296 loc) · 16.8 KB
/
kaid.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
import torch
import yaml
from configuration.config import parse_arguments_kaid
from data_io.ixi import IXI
from data_io.brats import BraTS2021
from torch.utils.data import DataLoader
from tools.utilize import *
from model.FT.fourier_transform import *
from model.FT.power_spectrum import *
from metrics.kaid.stats import mask_stats, best_msl_list
import numpy as np
from model.ae.kaid_ae import *
from model.cyclegan.cyclegan import CycleGen
from model.munit.munit import Encoder as MUE
from model.munit.munit import Decoder as MUD
from model.unit.unit import Encoder as UE
from model.unit.unit import Generator as UG
from tools.utilize import create_folders
import os
from loss_function.distance import l1_diff, l2_diff, cosine_similiarity
if __name__ == '__main__':
args = parse_arguments_kaid()
with open('./configuration/kaid/kaid_{}.yaml'.format(args.dataset), 'r') as f:
para_dict = yaml.load(f, Loader=yaml.SafeLoader)
para_dict = merge_config(para_dict, args)
print(para_dict)
file_path = record_path(para_dict)
if para_dict['save_log']:
save_arg(para_dict, file_path)
save_script(__file__, file_path)
with open('./work_dir/log_running.txt'.format(file_path), 'a') as f:
print('---> {}'.format(file_path), file=f)
print(para_dict, file=f)
device, device_ids = parse_device_list(para_dict['gpu_ids'],
int(para_dict['gpu_id']))
seed_everything(para_dict['seed'])
normal_transform = [{'degrees':0, 'translate':[0.00, 0.00],
'scale':[1.00, 1.00],
'size':(para_dict['size'], para_dict['size'])},
{'degrees':0, 'translate':[0.00, 0.00],
'scale':[1.00, 1.00],
'size':(para_dict['size'], para_dict['size'])}]
if para_dict['noise_type'] == 'gaussian':
noise_transform = [{'mu':para_dict['a_mu'], 'sigma':para_dict['a_sigma'],
'size':(para_dict['size'], para_dict['size'])},
{'mu':para_dict['b_mu'], 'sigma':para_dict['b_sigma'],
'size':(para_dict['size'], para_dict['size'])}]
elif para_dict['noise_type'] == 'slight':
noise_transform = [{'degrees': para_dict["a_rotation_degrees"],
'translate': [para_dict['a_trans_lower_limit'], para_dict['a_trans_upper_limit']],
'scale': [para_dict['a_scale_lower_limit'], para_dict['a_scale_upper_limit']],
'size': (para_dict['size'], para_dict['size']),'fillcolor': 0},
{'degrees': para_dict['b_rotation_degrees'],
'translate': [para_dict['b_trans_lower_limit'], para_dict['b_trans_upper_limit']],
'scale': [para_dict['b_scale_lower_limit'], para_dict['b_scale_uppper_limit']],
'size': (para_dict['size'], para_dict['size']),'fillcolor': 0}]
elif para_dict['noise_type'] == 'severe':
noise_transform = [{'degrees':para_dict['severe_rotation'],
'translate':[para_dict['severe_translation'], para_dict['severe_translation']],
'scale':[1-para_dict['severe_scaling'], 1+para_dict['severe_scaling']],
'size':(para_dict['size'], para_dict['size'])},
{'degrees':para_dict['severe_rotation'],
'translate':[para_dict['severe_translation'], para_dict['severe_translation']],
'scale':[1-para_dict['severe_scaling'], 1+para_dict['severe_scaling']],
'size':(para_dict['size'], para_dict['size'])}]
else:
raise NotImplementedError('New Noise Has Not Been Implemented')
#Dataset IO
if para_dict['dataset'] == 'ixi':
assert para_dict['source_domain'] in ['t2', 'pd']
assert para_dict['target_domain'] in ['t2', 'pd']
ixi_normal_dataset = IXI(root=para_dict['data_path'],
modalities=[para_dict['source_domain'], para_dict['target_domain']],
extract_slice=[para_dict['es_lower_limit'], para_dict['es_higher_limit']],
noise_type='normal',
learn_mode='train', #train or test is meaningless if dataset_splited is false
transform_data=normal_transform,
data_mode='paired',
data_num=para_dict['pair_num'],
data_paired_weight=1.0,
client_weights=[1.0],
dataset_splited=False,
data_moda_ratio=1.0,
data_moda_case='case1')
ixi_noise_dataset = IXI(root=para_dict['data_path'],
modalities=[para_dict['source_domain'], para_dict['target_domain']],
extract_slice=[para_dict['es_lower_limit'], para_dict['es_higher_limit']],
noise_type=para_dict['noise_type'],
learn_mode='train', #train or test is meaningless if dataset_splited is false
transform_data=noise_transform,
data_mode='paired',
data_num=para_dict['pair_num'],
data_paired_weight=1.0,
client_weights=[1.0],
dataset_splited=False,
data_moda_ratio=1.0,
data_moda_case='case1')
#TODO: make sure normal and nosiy loader release the same order of dataset
normal_loader = DataLoader(ixi_normal_dataset, num_workers=para_dict['num_workers'],
batch_size=para_dict['batch_size'], shuffle=False)
noisy_loader = DataLoader(ixi_noise_dataset, num_workers=para_dict['num_workers'],
batch_size=para_dict['batch_size'], shuffle=False)
test_loader = DataLoader(ixi_normal_dataset, num_workers=para_dict['num_workers'],
batch_size=1, shuffle=False)
elif para_dict['dataset'] == 'brats2021':
assert para_dict['source_domain'] in ['t1', 't2', 'flair']
assert para_dict['target_domain'] in ['t1', 't2', 'flair']
"""
#TODO: Create a dataset contained the whole part of BraTS 2021, included training and validation
"""
brats_normal_dataset = BraTS2021(root=para_dict['data_path'],
modalities=[para_dict['source_domain'], para_dict['target_domain']],
extract_slice=[para_dict['es_lower_limit'], para_dict['es_higher_limit']],
noise_type='normal',
learn_mode='train', # train or test is meaningless if dataset_spilited is false
transform_data=normal_transform,
data_mode='paired',
data_num=para_dict['pair_num'],
data_paired_weight=1.0,
client_weights=[1.0],
data_moda_ratio=1.0,
data_moda_case='case1')
brats_noise_dataset = BraTS2021(root=para_dict['data_path'],
modalities=[para_dict['source_domain'], para_dict['target_domain']],
noise_type=para_dict['noise_type'],
learn_mode='train',
extract_slice=[para_dict['es_lower_limit'], para_dict['es_higher_limit']],
transform_data=noise_transform,
data_mode='paired',
data_num=para_dict['pair_num'],
client_weights=[1.0],
data_paired_weight=1.0,
data_moda_ratio=1.0,
data_moda_case='case1')
#TODO: make sure normal and nosiy loader release the same order of dataset
normal_loader = DataLoader(brats_normal_dataset, num_workers=para_dict['num_workers'],
batch_size=para_dict['batch_size'], shuffle=False)
noisy_loader = DataLoader(brats_noise_dataset, num_workers=para_dict['num_workers'],
batch_size=para_dict['batch_size'], shuffle=False)
test_loader = DataLoader(brats_normal_dataset, num_workers=para_dict['num_workers'],
batch_size=1, shuffle=False)
else:
raise NotImplementedError("New Data Has Not Been Implemented")
# Debug Mode
if para_dict['debug']:
batch_limit = 2
else:
batch_limit = int(para_dict['pair_num'] / para_dict['batch_size'])
# Model
kaid_ae = KAIDAE().to(device)
# Loss
criterion_recon = torch.nn.L1Loss().to(device)
criterion_high_freq = torch.nn.MSELoss(reduction='mean').to(device)
criterion_low_freq = torch.nn.MSELoss(reduction='mean').to(device)
# Optimizer
optimizer = torch.optim.Adam(kaid_ae.parameters(), lr=para_dict['lr'],
betas=[para_dict['beta1'], para_dict['beta2']])
# Scheduler
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=para_dict['step_size'],
gamma=para_dict['gamma'])
# Mask Statistics
"""
src: source domain
tag: target domain
msl: the half of mask side length
"""
msl_path = os.path.join(para_dict['msl_path'], para_dict['dataset'])
create_folders(msl_path)
if para_dict['msl_stats']:
assert para_dict['msl_assigned'] is False, 'msl_stats and msl_assigned are contradictory'
src_dict, tag_dict = mask_stats(normal_loader, para_dict['source_domain'],
para_dict['target_domain'])
print(f"source_domain: {para_dict['source_domain']}, its_dict: {src_dict}")
print(f"target_domain: {para_dict['target_domain']}, its_dict: {tag_dict}")
src_best_msl_list = best_msl_list(src_dict, para_dict['delta_diff'])
tag_best_msl_list = best_msl_list(tag_dict, para_dict['delta_diff'])
msl_a = src_best_msl_list[0]
msl_b = tag_best_msl_list[0]
np.savez_compressed(os.path.join(msl_path, para_dict['source_domain']), msl=msl_a)
np.savez_compressed(os.path.join(msl_path, para_dict['target_domain']), msl=msl_b)
elif para_dict['msl_assigned']:
assert para_dict['msl_stats'] is False, 'msl_stats and msl_assigned are contradictory'
msl_a = int(para_dict['msl_assigned_value'])
msl_b = int(para_dict['msl_assigned_value'])
else:
msl_a = np.load(os.path.join(msl_path, para_dict['source_domain'])+'.npz')['msl']
msl_b = np.load(os.path.join(msl_path, para_dict['target_domain'])+'.npz')['msl']
print(f"{para_dict['source_domain']} msl: {msl_a}")
print(f"{para_dict['target_domain']} msl: {msl_b}")
# Training
#TODO: Alternative Training for different training loader
for epoch in range(para_dict['num_epochs']):
for i, batch in enumerate(normal_loader):
#TODO: noisy loader
if i > batch_limit:
break
real_a = batch[para_dict['source_domain']]
real_b = batch[para_dict['target_domain']]
# Fourier Transform
real_a_kspace = torch_fft(real_a)
real_b_kspace = torch_fft(real_b)
real_a_hf = torch_high_pass_filter(real_a_kspace, msl_a)
real_b_hf = torch_high_pass_filter(real_b_kspace, msl_b)
real_a_lf = torch_low_pass_filter(real_a_kspace, msl_a)
real_b_lf = torch_low_pass_filter(real_b_kspace, msl_b)
"""
Magnitude: sqrt(re^2 + im^2) tells you the amplitude of the component at the corresponding frequency
Phase: atan2(im, re) tells you the relative phase of that component
"""
real_a_hf_mag = torch.abs(real_a_hf).to(device)
real_a_lf_mag = torch.abs(real_a_lf).to(device)
real_b_hf_mag = torch.abs(real_b_hf).to(device)
real_b_lf_mag = torch.abs(real_b_lf).to(device)
optimizer.zero_grad()
real_a_hf_z, real_a_hf_hat = kaid_ae(real_a_hf_mag)
real_a_lf_z, real_a_lf_hat = kaid_ae(real_a_lf_mag)
real_b_hf_z, real_b_hf_hat = kaid_ae(real_b_hf_mag)
real_b_lf_z, real_b_lf_hat = kaid_ae(real_b_lf_mag)
"""
Reconstruction
"""
loss_recon_real_a_hf = criterion_recon(real_a_hf_mag, real_a_hf_hat)
loss_recon_real_b_hf = criterion_recon(real_b_hf_mag, real_b_hf_hat)
loss_recon_real_a_lf = criterion_recon(real_a_lf_mag, real_a_lf_hat)
loss_recon_real_b_lf = criterion_recon(real_b_lf_mag, real_b_lf_hat)
loss_recon = (loss_recon_real_a_hf + loss_recon_real_b_hf
+ loss_recon_real_a_lf + loss_recon_real_b_lf)
"""
Contrastive Loss
"""
loss_high_frequency = criterion_high_freq(real_a_hf_z, real_b_hf_z)
loss_low_frequency = criterion_low_freq(real_a_lf_z, real_b_lf_z)
contrastive_loss = (para_dict['lambda_hf'] * loss_high_frequency -
para_dict['lambda_lf'] * loss_low_frequency)
loss_total = (para_dict['lambda_contrastive'] * contrastive_loss +
para_dict['lambda_recon'] * loss_recon)
loss_total.backward()
optimizer.step()
lr_scheduler.step()
# Print Log
infor = '\r{}[Batch {}/{}] [Recons loss: {:.4f}] [contrastive loss: {:.4f}]'.format(
'', i, batch_limit, loss_recon.item(), contrastive_loss.item())
print(infor)
# Score Prediction
#TODO: Load GAN Model and KAID
if para_dict['test_model'] == 'cyclegan':
generator_from_a_to_b = CycleGen().to(device)
generator_from_b_to_a = CycleGen().to(device)
elif para_dict['test_model'] == 'munit':
encoder_from_a_to_b = MUE().to(device)
decoder_from_a_to_b = MUD().to(device)
encoder_from_b_to_a = MUE().to(device)
decoder_from_b_to_a = MUD().to(device)
elif para_dict['test_model'] == 'unit':
encoder_from_a_to_b = UE().to(device)
generator_from_a_to_b = UG().to(device)
encoder_from_b_to_a = UE().to(device)
generator_from_b_to_a = UG().to(device)
else:
raise NotImplementedError('GAN Model Has Not Been Implemented Yet')
#TODO: synthesis data loader
# Single Image Quality, batchsize=1
for i, batch in enumerate(test_loader):
if i > batch_limit:
break
real_a = batch[para_dict['source_domain']].to(device)
real_b = batch[para_dict['target_domain']].to(device)
# Synthesize Image
if para_dict['test_model'] == 'cyclegan':
fake_b = generator_from_a_to_b(real_a).to(device)
fake_a = generator_from_b_to_a(real_b).to(device)
elif para_dict['test_model'] == 'munit':
pass
elif para_dict['test_model'] == 'unit':
pass
else:
raise NotImplementedError('Synthesis Model Not Implemented Yet')
#Distance
real_a_z = kaid_ae.encode(real_a)
fake_a_z = kaid_ae.encode(fake_a)
real_b_z = kaid_ae.encode(real_b)
fake_b_z = kaid_ae.encode(fake_b)
if para_dict['diff_method'] == 'l1':
diff_a = l1_diff(real_a_z, fake_a_z)
diff_b = l1_diff(real_b_z, fake_b_z)
elif para_dict['diff_method'] == 'l2':
diff_a = l2_diff(real_a_z, fake_a_z)
diff_b = l2_diff(real_b_z, fake_b_z)
elif para_dict['diff_method'] == 'cos':
diff_a = cosine_similiarity(real_a_z, fake_a_z)
diff_b = cosine_similiarity(real_b_z, fake_b_z)
else:
raise NotImplementedError('The Difference Method Has Not Been Calculated Yet')
print(f"The mean diff of Modality {para_dict['source_domain']} : {torch.mean(diff_a)}")
print(f"The mean diff of Modality {para_dict['target_domain']} : {torch.mean(diff_b)}")
#TODO: Comparision on NIRPS