-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·296 lines (238 loc) · 8.33 KB
/
utils.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
""" Utilities """
import os
import math
import logging
import shutil
import torch
import numpy as np
import time
from skimage.metrics import peak_signal_noise_ratio, structural_similarity
import shutil
from PIL import Image
def print_gen(gen):
out = ""
for i in gen._fields:
out += f"{i} = "
if type(getattr(gen, i)[0]) == list:
out += "\n\t" + "\n\t".join([str(s) for s in getattr(gen, i)]) + "\n"
else:
out += str(getattr(gen, i)) + "\n"
print(out)
def get_run_path(base_dir, run_name):
run_dir = "{}-{}".format(run_name, time.strftime("%Y-%m-%d-%H"))
run_dir = os.path.join(base_dir, run_dir)
os.makedirs(run_dir, exist_ok=True)
return run_dir
def save_scripts(run_path):
dest = os.path.join(run_path, "code_copy/")
if os.path.exists(dest):
shutil.rmtree(dest)
shutil.copytree("./", dest)
class LogHandler:
def __init__(self, file_path):
self.file_path = file_path
def create(self):
logger = logging.getLogger(self.file_path.split("/")[-1])
log_format = "%(asctime)s | %(message)s"
formatter = logging.Formatter(log_format, datefmt="%m/%d %I:%M:%S %p")
self.file_handler = logging.FileHandler(self.file_path)
self.file_handler.setFormatter(formatter)
self.stream_handler = logging.StreamHandler()
self.stream_handler.setFormatter(formatter)
logger.addHandler(self.file_handler)
logger.addHandler(self.stream_handler)
logger.setLevel(logging.INFO)
self.logger = logger
return self.logger
def close(self):
self.logger.removeHandler(self.file_handler)
self.logger.removeHandler(self.stream_handler)
def get_logger(file_path):
"""Make python logger"""
# [!] Since tensorboardX use default logger (e.g. logging.info()), we should use custom logger
logger = logging.getLogger(file_path.split("/")[-1])
log_format = "%(asctime)s | %(message)s"
formatter = logging.Formatter(log_format, datefmt="%m/%d %I:%M:%S %p")
file_handler = logging.FileHandler(file_path)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
logger.setLevel(logging.INFO)
return logger
def grad_norm(model):
total_norm = 0
for p in model.parameters():
if p.grad is not None:
param_norm = p.grad.detach().data.norm(2)
total_norm += param_norm.item() ** 2
total_norm = total_norm ** 0.5
return total_norm
def param_size(model):
"""Compute parameter size in MB"""
n_params = sum(
np.prod(v.size())
for k, v in model.named_parameters()
if not k.startswith("aux_head")
)
return n_params / 1024.0 / 1024.0
class AverageMeter:
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
"""Reset all statistics"""
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
"""Update statistics"""
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def save_checkpoint(model, ckpt_dir, is_best=False):
filename = os.path.join(ckpt_dir, "checkpoint.pth.tar")
torch.save(model.state_dict(), filename)
if is_best:
best_filename = os.path.join(ckpt_dir, "best.pth.tar")
shutil.copyfile(filename, best_filename)
def compute_psnr(img1, img2):
img1 = tensor2img_np(img1)
img2 = tensor2img_np(img2)
img1 = rgb2y(img1[4:-4, 4:-4, :])
img2 = rgb2y(img2[4:-4, 4:-4, :])
return psnr(img1, img2)
def psnr(img1, img2):
assert img1.dtype == img2.dtype == np.uint8
img1 = img1.astype(np.float64)
img2 = img2.astype(np.float64)
mse = np.mean((img1 - img2) ** 2)
if mse == 0:
return float("inf")
return 20 * math.log10(255.0 / math.sqrt(mse))
def compute_ssim(img1, img2):
img1 = tensor2img_np(img1)
img2 = tensor2img_np(img2)
img1 = rgb2y(img1[4:-4, 4:-4, :])
img2 = rgb2y(img2[4:-4, 4:-4, :])
return structural_similarity(img1, img2, data_range=255.)
def tensor2img_np(tensor, out_type=np.uint8, min_max=(0, 1)):
tensor = tensor.squeeze(0)
tensor = tensor.float().cpu().clamp_(*min_max) # Clamp is for on hard_tanh
tensor = (tensor - min_max[0]) / (min_max[1] - min_max[0])
n_dim = tensor.dim()
if n_dim == 3:
img_np = tensor.numpy()
img_np = np.transpose(img_np, (1, 2, 0))
elif n_dim == 2:
img_np = tensor.numpy()
else:
raise TypeError(
"Only support 4D, 3D and 2D tensor. But receieved tensor with dimension = %d"
% n_dim
)
if out_type == np.uint8:
img_np = (
img_np * 255.0
).round() # This is important. Unlike matlab, numpy.unit8() WILL NOT round by default.
return img_np.astype(out_type)
def rgb2y(img):
assert img.dtype == np.uint8
in_img_type = img.dtype
img.astype(np.float64)
img_y = (
(np.dot(img[..., :3], [65.481, 128.553, 24.966])) / 255.0 + 16.0
).round()
return img_y.astype(in_img_type)
def min_max(m):
mx = m.max()
mn = m.min()
return (m - m.min()) / (mx - mn)
def prepare_images(path_input, path_target, out):
out = out.permute(1, 2, 0)
out = min_max(out)
if path_input is not None:
input_img = Image.open(path_input)
else:
input_img = None
if path_target is not None:
target_img = Image.open(path_target)
else:
path_target = None
out_image = out.mul(255.0).cpu().numpy()
out_image = np.clip(out_image, 0.0, 255.0).astype(np.uint8)
if out_image.shape[-1] == 1:
out_image = out_image.squeeze(-1)
out_image = np.stack([out_image, out_image, out_image], axis=2)
out_image = Image.fromarray(out_image)
return target_img, input_img, out_image
def save_images(
results_dir, path_input, path_target, out, cur_iter, logger=None
):
cur_iter = round(cur_iter, 3)
results_dir = os.path.join(results_dir, "images")
if not os.path.exists(results_dir):
os.makedirs(results_dir)
target, input_img, out_image = prepare_images(path_input, path_target, out)
if logger is not None:
if not target is None:
logger.add_image(
tag=f"target",
img_tensor=np.array(target),
dataformats="HWC",
global_step=cur_iter,
)
if not input_img is None:
logger.add_image(
tag=f"input_img",
img_tensor=np.array(input_img),
dataformats="HWC",
global_step=cur_iter,
)
logger.add_image(
tag=f"out_image",
img_tensor=np.array(out_image),
dataformats="HWC",
global_step=cur_iter,
)
target.save(f"{results_dir}/taret_step_{cur_iter}.png")
input_img.save(f"{results_dir}/input_step_{cur_iter}.png")
out_image.save(f"{results_dir}/out_image_step_{cur_iter}.png")
class FlopsScheduler:
def __init__(
self, start_reg=0, start_after=0, reg_step=0, step=1, max_reg=1e10
):
self.start_after = start_after
self.cur_reg = start_reg
self.step = step
self.reg_step = reg_step
self.max_reg = max_reg
self.cur_epoch = start_after
self.register = False
def __call__(self, epoch):
if epoch > self.cur_epoch:
self.cur_epoch = epoch + self.step
self.set_reg()
if self.cur_epoch - 1 == epoch:
self.register = True
else:
self.register = False
return self.cur_reg
def set_reg(self):
if self.cur_reg < self.max_reg:
self.cur_reg += self.reg_step
class FlopsLoss:
def __init__(self, n_ops, reduce=4):
self.n_ops = n_ops / reduce
self.norm = 0
def set_norm(self, norm):
self.norm = norm.detach() * self.n_ops
self.min = norm.detach() / self.n_ops
def set_penalty(self, penalty):
self.penalty = float(penalty)
def __call__(self, weighted_flops):
l = (weighted_flops - self.min) / (self.norm - self.min)
return l * self.penalty