-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.py
264 lines (233 loc) · 11 KB
/
helper.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
import os, time
import shutil
import torch
import csv
import vis_utils
from metrics import Result
fieldnames = [
'epoch', 'rmse', 'photo', 'mae', 'irmse', 'imae', 'mse', 'absrel', 'lg10',
'silog', 'squared_rel', 'delta1', 'delta2', 'delta3', 'data_time',
'gpu_time'
]
class logger:
def __init__(self, args, prepare=True):
self.args = args
output_directory = get_folder_name(args)
self.output_directory = output_directory
self.best_result = Result()
self.best_result.set_to_worst()
if not prepare:
return
if not os.path.exists(output_directory):
os.makedirs(output_directory)
self.train_csv = os.path.join(output_directory, 'train.csv')
self.val_csv = os.path.join(output_directory, 'val.csv')
self.best_txt = os.path.join(output_directory, 'best.txt')
# backup the source code
if args.resume == '':
print("=> creating source code backup ...")
backup_directory = os.path.join(output_directory, "code_backup")
self.backup_directory = backup_directory
backup_source_code(backup_directory)
# create new csv files with only header
with open(self.train_csv, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
with open(self.val_csv, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
print("=> finished creating source code backup.")
def conditional_print(self, split, i, epoch, total_epochs, lr, n_set, blk_avg_meter,
avg_meter):
if (i + 1) % self.args.print_freq == 0:
avg = avg_meter.average()
blk_avg = blk_avg_meter.average()
print('=> output: {}'.format(self.output_directory))
print(
'{split} Epoch: {}/{} [Batch {}/{}]\tlr={lr} '
't_Data={blk_avg.data_time:.3f}({average.data_time:.3f}) '
't_GPU={blk_avg.gpu_time:.3f}({average.gpu_time:.3f})\n\t'
'RMSE={blk_avg.rmse:.2f}({average.rmse:.2f}) '
'MAE={blk_avg.mae:.2f}({average.mae:.2f}) '
'iRMSE={blk_avg.irmse:.2f}({average.irmse:.2f}) '
'iMAE={blk_avg.imae:.2f}({average.imae:.2f})\n\t'
'silog={blk_avg.silog:.2f}({average.silog:.2f}) '
'squared_rel={blk_avg.squared_rel:.2f}({average.squared_rel:.2f}) '
'Delta1={blk_avg.delta1:.3f}({average.delta1:.3f}) '
'Delta2={blk_avg.delta2:.3f}({average.delta2:.3f}) '
'REL={blk_avg.absrel:.3f}({average.absrel:.3f})\n\t'
'Lg10={blk_avg.lg10:.3f}({average.lg10:.3f}) '
'Photometric={blk_avg.photometric:.3f}({average.photometric:.3f}) '
.format(epoch+1,
total_epochs,
i + 1,
n_set,
lr=lr,
blk_avg=blk_avg,
average=avg,
split=split.capitalize()))
blk_avg_meter.reset()
def conditional_save_info(self, split, average_meter, epoch):
avg = average_meter.average()
if split == "train":
csvfile_name = self.train_csv
elif split == "val":
csvfile_name = self.val_csv
elif split == "eval":
eval_filename = os.path.join(self.output_directory, 'eval.txt')
self.save_single_txt(eval_filename, avg, epoch)
return avg
elif "test" in split:
return avg
else:
raise ValueError("wrong split provided to logger")
with open(csvfile_name, 'a') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({
'epoch': epoch+1,
'rmse': avg.rmse,
'photo': avg.photometric,
'mae': avg.mae,
'irmse': avg.irmse,
'imae': avg.imae,
'mse': avg.mse,
'silog': avg.silog,
'squared_rel': avg.squared_rel,
'absrel': avg.absrel,
'lg10': avg.lg10,
'delta1': avg.delta1,
'delta2': avg.delta2,
'delta3': avg.delta3,
'gpu_time': avg.gpu_time,
'data_time': avg.data_time
})
return avg
def save_single_txt(self, filename, result, epoch, total_epochs):
with open(filename, 'w') as txtfile:
txtfile.write(
("rank_metric={}\n" + "epoch={}/{}\n" + "rmse={:.3f}\n" +
"mae={:.3f}\n" + "silog={:.3f}\n" + "squared_rel={:.3f}\n" +
"irmse={:.3f}\n" + "imae={:.3f}\n" + "mse={:.3f}\n" +
"absrel={:.3f}\n" + "lg10={:.3f}\n" + "delta1={:.3f}\n" + "delta2={:.3f}\n" +
"t_gpu={:.4f}").format(self.args.rank_metric, epoch+1, total_epochs,
result.rmse, result.mae, result.silog,
result.squared_rel, result.irmse,
result.imae, result.mse, result.absrel,
result.lg10, result.delta1, result.delta2,
result.gpu_time))
def save_best_txt(self, result, epoch, total_epochs):
self.save_single_txt(self.best_txt, result, epoch, total_epochs)
def _get_img_comparison_name(self, mode, epoch, is_best=False):
if mode == 'eval':
return self.output_directory + '/comparison_eval.png'
if mode == 'val':
if is_best:
return self.output_directory + '/comparison_best.png'
else:
return self.output_directory + '/comparison_' + str(
epoch) + '.png'
def conditional_save_img_comparison(self, mode, i, ele, pred, epoch):
# save 8 images for visualization
if mode == 'val' or mode == 'eval':
skip = 100
if i == 0:
self.img_merge = vis_utils.merge_into_row(ele, pred)
elif i % skip == 0 and i < 8 * skip:
row = vis_utils.merge_into_row(ele, pred)
self.img_merge = vis_utils.add_row(self.img_merge, row)
elif i == 8 * skip:
filename = self._get_img_comparison_name(mode, epoch)
vis_utils.save_image(self.img_merge, filename)
def save_img_comparison_as_best(self, mode, epoch):
if mode == 'val':
filename = self._get_img_comparison_name(mode, epoch, is_best=True)
vis_utils.save_image(self.img_merge, filename)
def get_ranking_error(self, result):
return getattr(result, self.args.rank_metric)
def rank_conditional_save_best(self, mode, result, epoch, total_epochs):
error = self.get_ranking_error(result)
best_error = self.get_ranking_error(self.best_result)
is_best = error < best_error
if is_best and mode == "val":
self.old_best_result = self.best_result
self.best_result = result
self.save_best_txt(result, epoch, total_epochs)
return is_best
def conditional_save_pred(self, mode, i, pred, epoch):
if ("test" in mode or mode == "eval") and self.args.save_pred:
# save images for visualization/ testing
image_folder = os.path.join(self.output_directory,
mode + "_output")
if not os.path.exists(image_folder):
os.makedirs(image_folder)
img = torch.squeeze(pred.data.cpu()).numpy()
filename = os.path.join(image_folder, '{0:010d}.png'.format(i))
vis_utils.save_depth_as_uint16png(img, filename)
def conditional_summarize(self, mode, avg, is_best):
print("\n*\nSummary of", mode, "round:")
print(''
'RMSE={average.rmse:.3f}\n'
'MAE={average.mae:.3f}\n'
'Photo={average.photometric:.3f}\n'
'iRMSE={average.irmse:.3f}\n'
'iMAE={average.imae:.3f}\n'
'squared_rel={average.squared_rel}\n'
'silog={average.silog}\n'
'Delta1={average.delta1:.3f}\n'
'Delta2={average.delta2:.3f}\n'
'REL={average.absrel:.3f}\n'
'Lg10={average.lg10:.3f}\n'
't_GPU={time:.3f}'.format(average=avg, time=avg.gpu_time))
if is_best and mode == "val":
print("New best model by %s (was %.3f)" %
(self.args.rank_metric,
self.get_ranking_error(self.old_best_result)))
elif mode == "val":
print("(best %s is %.3f)" %
(self.args.rank_metric,
self.get_ranking_error(self.best_result)))
print("*\n")
ignore_hidden = shutil.ignore_patterns(".", "..", ".git*", "*pycache*",
"*build", "*.fuse*", "*_drive_*")
def backup_source_code(backup_directory):
if os.path.exists(backup_directory):
shutil.rmtree(backup_directory)
shutil.copytree('.', backup_directory, ignore=ignore_hidden)
def adjust_learning_rate(lr_init, optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every 5 epochs"""
lr = lr_init * (0.1**(epoch // 5))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def save_checkpoint(state, is_best, epoch, output_directory):
checkpoint_filename = os.path.join(output_directory,
'checkpoint-' + str(epoch) + '.pth.tar')
torch.save(state, checkpoint_filename)
if is_best:
best_filename = os.path.join(output_directory, 'model_best.pth.tar')
shutil.copyfile(checkpoint_filename, best_filename)
if epoch > 0:
prev_checkpoint_filename = os.path.join(
output_directory, 'checkpoint-' + str(epoch - 1) + '.pth.tar')
if os.path.exists(prev_checkpoint_filename):
os.remove(prev_checkpoint_filename)
def get_folder_name(args):
current_time = time.strftime('%Y-%m-%d@%H-%M')
if args.use_pose:
prefix = "var.mode={}.w1={}.w2={}.".format(args.train_mode, args.w1, args.w2)
elif args.sample_method != '':
prefix = "var.mode={}.budget={}.samp.method={}.".format(args.train_mode, args.budget, args.sample_method)
else:
prefix = "var.mode={}.budget={}.".format(args.train_mode, args.budget)
return os.path.join(args.result,
prefix + 'input={}.resnet{}.bs={}.pretrained={}.time={}'.
format(args.input, args.layers, args.batch_size, args.pretrained, current_time
))
avgpool = torch.nn.AvgPool2d(kernel_size=2, stride=2).cuda()
def multiscale(img):
img1 = avgpool(img)
img2 = avgpool(img1)
img3 = avgpool(img2)
img4 = avgpool(img3)
img5 = avgpool(img4)
return img5, img4, img3, img2, img1