-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsr_train.py
198 lines (169 loc) · 7.38 KB
/
sr_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
import torch, os
import numpy as np
import scipy.stats
import matplotlib
matplotlib.use('Agg')
from torch.utils.data import DataLoader
from torch.optim import lr_scheduler
import random, sys, pickle
import argparse
from meta import Meta
from dataloader import dataloader as dl
import utility as util
from skimage.io import imsave, imread
import errors
import time
def mean_confidence_interval(accs, confidence=0.95):
n = accs.shape[0]
m, se = np.mean(accs), scipy.stats.sem(accs)
h = se * scipy.stats.t._ppf((1 + confidence) / 2, n - 1)
return m, h
def prepare(l, volatile=False):
device = torch.device('cuda')
def _prepare(tensor):
#if self.args.precision == 'half': tensor = tensor.half()
return tensor.to(device, dtype=torch.float)
return [_prepare(_l) for _l in l]
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
def main():
ck = util.checkpoint(args)
seed = args.seed
random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.manual_seed(222)
torch.cuda.manual_seed_all(222)
np.random.seed(222)
ck.write_log(str(args))
# t = str(int(time.time()))
# t = args.save_name
# os.mkdir('./{}'.format(t))
# (ch_out, ch_in, k, k, stride, padding)
config = [
('conv2d', [32, 16, 3, 3, 1, 1]),
('relu', [True]),
('conv2d', [32, 32, 3, 3, 1, 1]),
('relu', [True]),
('conv2d', [32, 32, 3, 3, 1, 1]),
('relu', [True]),
('conv2d', [32, 32, 3, 3, 1, 1]),
('relu', [True]),
('+1', [True]),
('conv2d', [3, 32, 3, 3, 1, 1])
]
device = torch.device('cuda')
maml = Meta(args, config).to(device)
# (Dataset) calculate the number of trainable tensors
tmp = filter(lambda x: x.requires_grad, maml.parameters())
num = sum(map(lambda x: np.prod(x.shape), tmp))
ck.write_log(str(maml))
ck.write_log('Total trainable tensors: {}'.format(num))
# (Dataset) batchsz here means total episode number
DL_MSI = dl.StereoMSIDatasetLoader(args)
db = DL_MSI.train_loader
dv = DL_MSI.valid_loader
psnr = []
l1_loss = []
psnr_valid = []
for epoch, (spt_ms, spt_rgb, qry_ms, qry_rgb) in enumerate(db):
if epoch // args.epoch: break
spt_ms, spt_rgb, qry_ms, qry_rgb = (spt_ms.to(device),
spt_rgb.to(device), qry_ms.to(device), qry_rgb.to(device))
# optimization is carried out inside meta_learner class, maml.
accs, train_loss = maml(spt_ms, spt_rgb, qry_ms, qry_rgb, epoch)
maml.scheduler.step()
if epoch % args.print_every == 0:
log_epoch = 'epoch: {} \ttraining acc: {}'.format(epoch, accs)
ck.write_log(log_epoch)
psnr.append(accs)
l1_loss.append(train_loss)
ck.plot_loss(psnr, l1_loss, epoch, args.print_every)
if epoch % args.save_every == 0:
with torch.no_grad():
ck.save(maml.net, maml.meta_optim, epoch)
eval_psnr = 0 # psnr loss
for idx, (valid_ms, valid_rgb) in enumerate(dv):
#print('idx', idx)
valid_ms, valid_rgb = prepare([valid_ms, valid_rgb])
sr_rgb = maml.net(valid_ms)
sr_rgb = torch.clamp(sr_rgb, 0, 1)
eval_psnr += errors.find_psnr(valid_rgb, sr_rgb)
############## plot PSNR here you idiot! ###########
psnr_valid.append(eval_psnr/25)
ck.plot_psnr(psnr_valid, epoch, args.save_every)
ck.write_log('Max PSNR is: {}'.format(max(psnr_valid)))
imsave('./{}/validation/img_{}.png'.format(
ck.dir, epoch), np.uint8(sr_rgb[0,:,:,:].permute(
1, 2, 0).cpu().detach().numpy() * 255))
ck.done()
if __name__ == '__main__':
argparser = argparse.ArgumentParser()
argparser.add_argument('--seed', type=int, default=1, help='random seed')
argparser.add_argument('--k', type=int, default=0,
help='kth fold, values are from 0 to 10')
argparser.add_argument('--res_layer', type=int, default=1,
help='residual branch taken from res_layerth')
argparser.add_argument('--epoch', type=int, help='epoch number',
default=60000)
argparser.add_argument('--num_workers', type=int, help='number of workers',
default=1)
argparser.add_argument('--pin_memory', type=bool, help='k shot for support set',
default=True)
argparser.add_argument('--shuffle', type=bool, help='shuffle', default=True)
argparser.add_argument('--batch_size', type=int, help='batch_size',
default=1)
argparser.add_argument('--batchsz', type=int, help='batchsz',
default=100000)
argparser.add_argument('--crop_size', type=int, help='imgsz', default=120)
argparser.add_argument('--imgc', type=int, help='imgc', default=3)
#argparser.add_argument('--imgsz', type=int, help='imgsz', default=84)
argparser.add_argument('--task_num', type=int,
help='meta batch size, namely task num', default=5)
argparser.add_argument('--meta_lr',
type=float, help='meta-level outer learning rate',
default=1e-3)
argparser.add_argument('--update_lr', type=float,
help='task-level inner update learning rate', default=0.01)
argparser.add_argument('--update_step', type=int,
help='task-level inner update steps', default=5)
argparser.add_argument('--update_step_test', type=int,
help='update steps for finetunning', default=10)
argparser.add_argument('--save_every', type=int,
help='task-level inner update learning rate', default=20)
argparser.add_argument('--print_every', type=int,
help='task-level inner update learning rate', default=5)
argparser.add_argument('--folder_name', type=str,
help='folder name', default='')
argparser.add_argument('--load', type=str,
help='load a model to continue training or for testing',
default='.')
argparser.add_argument('--save_name', type=str,
help='save_name', default='.')
argparser.add_argument('--test_only', type=bool,
help='test_only', default=False)
argparser.add_argument('--root', type=str,
help='data', default='/flush5/sho092/Robust_learning/')
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
argparser.add_argument('--data_dist_same', type=str2bool, nargs='?',
help=r'data distribution of the learning and meta learning are '
r'the same ==> True', default=False, const=True)
argparser.add_argument('--data_dist_shuffle', type=str2bool, nargs='?',
help='data distribution shuffle in each batch',
default=False, const=True)
args = argparser.parse_args()
print(args)
main()