forked from Vandermode/QRNN3D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hsi_setup.py
445 lines (353 loc) · 16.7 KB
/
hsi_setup.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import torch
import torch.optim as optim
import models
import os
import argparse
from os.path import join
from utility import *
from utility.ssim import SSIMLoss
model_names = sorted(name for name in models.__dict__
if name.islower() and not name.startswith("__")
and callable(models.__dict__[name]))
class MultipleLoss(nn.Module):
def __init__(self, losses, weight=None):
super(MultipleLoss, self).__init__()
self.losses = nn.ModuleList(losses)
self.weight = weight or [1/len(self.losses)] * len(self.losses)
def forward(self, predict, target):
total_loss = 0
for weight, loss in zip(self.weight, self.losses):
total_loss += loss(predict, target) * weight
return total_loss
def extra_repr(self):
return 'weight={}'.format(self.weight)
def train_options(parser):
def _parse_str_args(args):
str_args = args.split(',')
parsed_args = []
for str_arg in str_args:
arg = int(str_arg)
if arg >= 0:
parsed_args.append(arg)
return parsed_args
parser.add_argument('--prefix', '-p', type=str, default='denoise',
help='prefix')
parser.add_argument('--arch', '-a', metavar='ARCH', required=True,
choices=model_names,
help='model architecture: ' +
' | '.join(model_names))
parser.add_argument('--batchSize', '-b', type=int,
default=16, help='training batch size. default=16')
parser.add_argument('--lr', type=float, default=1e-3,
help='learning rate. default=1e-3.')
parser.add_argument('--wd', type=float, default=0,
help='weight decay. default=0')
parser.add_argument('--loss', type=str, default='l2',
help='which loss to choose.', choices=['l1', 'l2', 'smooth_l1', 'ssim', 'l2_ssim'])
parser.add_argument('--init', type=str, default='kn',
help='which init scheme to choose.', choices=['kn', 'ku', 'xn', 'xu', 'edsr'])
parser.add_argument('--no-cuda', action='store_true', help='disable cuda?')
parser.add_argument('--no-log', action='store_true',
help='disable logger?')
parser.add_argument('--threads', type=int, default=8,
help='number of threads for data loader to use')
parser.add_argument('--seed', type=int, default=2018,
help='random seed to use. default=2018')
parser.add_argument('--resume', '-r', action='store_true',
help='resume from checkpoint')
parser.add_argument('--no-ropt', '-nro', action='store_true',
help='not resume optimizer')
parser.add_argument('--chop', action='store_true',
help='forward chop')
parser.add_argument('--resumePath', '-rp', type=str,
default=None, help='checkpoint to use.')
parser.add_argument('--dataroot', '-d', type=str,
default='/data/weikaixuan/hsi/data/ICVL64_31.db', help='data root')
parser.add_argument('--clip', type=float, default=1e6)
parser.add_argument('--gpu-ids', type=str, default='0', help='gpu ids')
opt = parser.parse_args()
opt.gpu_ids = _parse_str_args(opt.gpu_ids)
return opt
def make_dataset(opt, train_transform, target_transform, common_transform, batch_size=None, repeat=1):
dataset = LMDBDataset(opt.dataroot, repeat=repeat)
# dataset.length -= 1000
# dataset.length = size or dataset.length
"""Split patches dataset into training, validation parts"""
dataset = TransformDataset(dataset, common_transform)
train_dataset = ImageTransformDataset(dataset, train_transform, target_transform)
train_loader = DataLoader(train_dataset,
batch_size=batch_size or opt.batchSize, shuffle=True,
num_workers=opt.threads, pin_memory=not opt.no_cuda, worker_init_fn=worker_init_fn)
return train_loader
class Engine(object):
def __init__(self, opt):
self.prefix = opt.prefix
self.opt = opt
self.net = None
self.optimizer = None
self.criterion = None
self.basedir = None
self.iteration = None
self.epoch = None
self.best_psnr = None
self.best_loss = None
self.writer = None
self.__setup()
def __setup(self):
self.basedir = join('checkpoints', self.opt.arch)
if not os.path.exists(self.basedir):
os.makedirs(self.basedir)
self.best_psnr = 0
self.best_loss = 1e6
self.epoch = 0 # start from epoch 0 or last checkpoint epoch
self.iteration = 0
cuda = not self.opt.no_cuda
self.device = 'cuda' if cuda else 'cpu'
print('Cuda Acess: %d' % cuda)
if cuda and not torch.cuda.is_available():
raise Exception("No GPU found, please run without --cuda")
torch.manual_seed(self.opt.seed)
if cuda:
torch.cuda.manual_seed(self.opt.seed)
"""Model"""
print("=> creating model '{}'".format(self.opt.arch))
self.net = models.__dict__[self.opt.arch]()
# initialize parameters
init_params(self.net, init_type=self.opt.init) # disable for default initialization
if len(self.opt.gpu_ids) > 1:
from models.sync_batchnorm import DataParallelWithCallback
self.net = DataParallelWithCallback(self.net, device_ids=self.opt.gpu_ids)
if self.opt.loss == 'l2':
self.criterion = nn.MSELoss()
if self.opt.loss == 'l1':
self.criterion = nn.L1Loss()
if self.opt.loss == 'smooth_l1':
self.criterion = nn.SmoothL1Loss()
if self.opt.loss == 'ssim':
self.criterion = SSIMLoss(data_range=1, channel=31)
if self.opt.loss == 'l2_ssim':
self.criterion = MultipleLoss([nn.MSELoss(), SSIMLoss(data_range=1, channel=31)], weight=[1, 2.5e-3])
print(self.criterion)
if cuda:
self.net.to(self.device)
self.criterion = self.criterion.to(self.device)
"""Logger Setup"""
log = not self.opt.no_log
if log:
self.writer = get_summary_writer(os.path.join(self.basedir, 'logs'), self.opt.prefix)
"""Optimization Setup"""
self.optimizer = optim.Adam(
self.net.parameters(), lr=self.opt.lr, weight_decay=self.opt.wd, amsgrad=False)
"""Resume previous model"""
if self.opt.resume:
# Load checkpoint.
self.load(self.opt.resumePath, not self.opt.no_ropt)
else:
print('==> Building model..')
print(self.net)
def forward(self, inputs):
if self.opt.chop:
output = self.forward_chop(inputs)
else:
output = self.net(inputs)
return output
def forward_chop(self, x, base=16):
n, c, b, h, w = x.size()
h_half, w_half = h // 2, w // 2
shave_h = np.ceil(h_half / base) * base - h_half
shave_w = np.ceil(w_half / base) * base - w_half
shave_h = shave_h if shave_h >= 10 else shave_h + base
shave_w = shave_w if shave_w >= 10 else shave_w + base
h_size, w_size = int(h_half + shave_h), int(w_half + shave_w)
inputs = [
x[..., 0:h_size, 0:w_size],
x[..., 0:h_size, (w - w_size):w],
x[..., (h - h_size):h, 0:w_size],
x[..., (h - h_size):h, (w - w_size):w]
]
outputs = [self.net(input_i) for input_i in inputs]
output = torch.zeros_like(x)
output_w = torch.zeros_like(x)
output[..., 0:h_half, 0:w_half] += outputs[0][..., 0:h_half, 0:w_half]
output_w[..., 0:h_half, 0:w_half] += 1
output[..., 0:h_half, w_half:w] += outputs[1][..., 0:h_half, (w_size - w + w_half):w_size]
output_w[..., 0:h_half, w_half:w] += 1
output[..., h_half:h, 0:w_half] += outputs[2][..., (h_size - h + h_half):h_size, 0:w_half]
output_w[..., h_half:h, 0:w_half] += 1
output[..., h_half:h, w_half:w] += outputs[3][..., (h_size - h + h_half):h_size, (w_size - w + w_half):w_size]
output_w[..., h_half:h, w_half:w] += 1
output /= output_w
return output
def __step(self, train, inputs, targets):
if train:
self.optimizer.zero_grad()
loss_data = 0
total_norm = None
if self.get_net().bandwise:
O = []
for time, (i, t) in enumerate(zip(inputs.split(1, 1), targets.split(1, 1))):
o = self.net(i)
O.append(o)
loss = self.criterion(o, t)
if train:
loss.backward()
loss_data += loss.item()
outputs = torch.cat(O, dim=1)
else:
outputs = self.net(inputs)
# outputs = torch.clamp(self.net(inputs), 0, 1)
# loss = self.criterion(outputs, targets)
# if outputs.ndimension() == 5:
# loss = self.criterion(outputs[:,0,...], torch.clamp(targets[:,0,...], 0, 1))
# else:
# loss = self.criterion(outputs, torch.clamp(targets, 0, 1))
loss = self.criterion(outputs, targets)
if train:
loss.backward()
loss_data += loss.item()
if train:
total_norm = nn.utils.clip_grad_norm_(self.net.parameters(), self.opt.clip)
self.optimizer.step()
return outputs, loss_data, total_norm
def load(self, resumePath=None, load_opt=True):
model_best_path = join(self.basedir, self.prefix, 'model_latest.pth')
if os.path.exists(model_best_path):
best_model = torch.load(model_best_path)
print('==> Resuming from checkpoint %s..' % resumePath)
assert os.path.isdir('checkpoints'), 'Error: no checkpoint directory found!'
checkpoint = torch.load(resumePath or model_best_path)
#### comment when using memnet
self.epoch = checkpoint['epoch']
self.iteration = checkpoint['iteration']
if load_opt:
self.optimizer.load_state_dict(checkpoint['optimizer'])
####
self.get_net().load_state_dict(checkpoint['net'])
def train(self, train_loader):
print('\nEpoch: %d' % self.epoch)
self.net.train()
train_loss = 0
for batch_idx, (inputs, targets) in enumerate(train_loader):
if not self.opt.no_cuda:
inputs, targets = inputs.to(self.device), targets.to(self.device)
outputs, loss_data, total_norm = self.__step(True, inputs, targets)
train_loss += loss_data
avg_loss = train_loss / (batch_idx+1)
if not self.opt.no_log:
self.writer.add_scalar(
join(self.prefix, 'train_loss'), loss_data, self.iteration)
self.writer.add_scalar(
join(self.prefix, 'train_avg_loss'), avg_loss, self.iteration)
self.iteration += 1
progress_bar(batch_idx, len(train_loader), 'AvgLoss: %.4e | Loss: %.4e | Norm: %.4e'
% (avg_loss, loss_data, total_norm))
self.epoch += 1
if not self.opt.no_log:
self.writer.add_scalar(
join(self.prefix, 'train_loss_epoch'), avg_loss, self.epoch)
def validate(self, valid_loader, name):
self.net.eval()
validate_loss = 0
total_psnr = 0
print('[i] Eval dataset {}...'.format(name))
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(valid_loader):
if not self.opt.no_cuda:
inputs, targets = inputs.to(self.device), targets.to(self.device)
outputs, loss_data, _ = self.__step(False, inputs, targets)
psnr = np.mean(cal_bwpsnr(outputs, targets))
validate_loss += loss_data
avg_loss = validate_loss / (batch_idx+1)
total_psnr += psnr
avg_psnr = total_psnr / (batch_idx+1)
progress_bar(batch_idx, len(valid_loader), 'Loss: %.4e | PSNR: %.4f'
% (avg_loss, avg_psnr))
if not self.opt.no_log:
self.writer.add_scalar(
join(self.prefix, name, 'val_loss_epoch'), avg_loss, self.epoch)
self.writer.add_scalar(
join(self.prefix, name, 'val_psnr_epoch'), avg_psnr, self.epoch)
return avg_psnr, avg_loss
def save_checkpoint(self, model_out_path=None, **kwargs):
if not model_out_path:
model_out_path = join(self.basedir, self.prefix, "model_epoch_%d_%d.pth" % (
self.epoch, self.iteration))
state = {
'net': self.get_net().state_dict(),
'optimizer': self.optimizer.state_dict(),
'epoch': self.epoch,
'iteration': self.iteration,
}
state.update(kwargs)
if not os.path.isdir(join(self.basedir, self.prefix)):
os.makedirs(join(self.basedir, self.prefix))
torch.save(state, model_out_path)
print("Checkpoint saved to {}".format(model_out_path))
# saving result into disk
def test_develop(self, test_loader, savedir=None, verbose=True):
from scipy.io import savemat
from os.path import basename, exists
def torch2numpy(hsi):
if self.net.use_2dconv:
R_hsi = hsi.data[0].cpu().numpy().transpose((1,2,0))
else:
R_hsi = hsi.data[0].cpu().numpy()[0,...].transpose((1,2,0))
return R_hsi
self.net.eval()
test_loss = 0
total_psnr = 0
dataset = test_loader.dataset.dataset
res_arr = np.zeros((len(test_loader), 3))
input_arr = np.zeros((len(test_loader), 3))
with torch.no_grad():
for batch_idx, (inputs, targets) in enumerate(test_loader):
if not self.opt.no_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
outputs, loss_data, _ = self.__step(False, inputs, targets)
test_loss += loss_data
avg_loss = test_loss / (batch_idx+1)
res_arr[batch_idx, :] = MSIQA(outputs, targets)
input_arr[batch_idx, :] = MSIQA(inputs, targets)
"""Visualization"""
# Visualize3D(inputs.data[0].cpu().numpy())
# Visualize3D(outputs.data[0].cpu().numpy())
psnr = res_arr[batch_idx, 0]
ssim = res_arr[batch_idx, 1]
if verbose:
print(batch_idx, psnr, ssim)
if savedir:
filedir = join(savedir, basename(dataset.filenames[batch_idx]).split('.')[0])
outpath = join(filedir, '{}.mat'.format(self.opt.arch))
if not exists(filedir):
os.mkdir(filedir)
if not exists(outpath):
savemat(outpath, {'R_hsi': torch2numpy(outputs)})
return res_arr, input_arr
def test_real(self, test_loader, savedir=None):
"""Warning: this code is not compatible with bandwise flag"""
from scipy.io import savemat
from os.path import basename
self.net.eval()
dataset = test_loader.dataset.dataset
with torch.no_grad():
for batch_idx, inputs in enumerate(test_loader):
if not self.opt.no_cuda:
inputs = inputs.cuda()
outputs = self.forward(inputs)
"""Visualization"""
input_np = inputs[0].cpu().numpy()
output_np = outputs[0].cpu().numpy()
display = np.concatenate([input_np, output_np], axis=-1)
Visualize3D(display)
# Visualize3D(outputs[0].cpu().numpy())
# Visualize3D((outputs-inputs).data[0].cpu().numpy())
if savedir:
R_hsi = outputs.data[0].cpu().numpy()[0,...].transpose((1,2,0))
savepath = join(savedir, basename(dataset.filenames[batch_idx]).split('.')[0], self.opt.arch + '.mat')
savemat(savepath, {'R_hsi': R_hsi})
return outputs
def get_net(self):
if len(self.opt.gpu_ids) > 1:
return self.net.module
else:
return self.net