-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
134 lines (102 loc) · 3.4 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
from skimage.metrics import peak_signal_noise_ratio as psnr
from skimage.metrics import structural_similarity as ssim
import numpy as np
import os
import torch
from collections import OrderedDict
import warnings
warnings.filterwarnings("ignore")
import time
class Timer():
def __init__(self) -> None:
self.device: str = None
self.start = None
self.end = None
def record(self) -> None:
self.reset()
if self.device == "cuda":
self.start.record()
else:
self.start = time.time_ns()
def stop(self) -> None:
if self.device == "cuda":
self.end.record()
else:
self.end = time.time_ns()
def sync(self) -> None:
if self.device == "cuda":
torch.cuda.current_stream().synchronize()
def get_elapsed_time(self):
if self.device == "cuda":
return self.start.elapsed_time(self.end)
else:
return (self.end - self.start)/1e6
def to(self, device) -> None:
self.device = device
def reset(self) -> None:
if self.device == "cuda":
self.start = torch.cuda.Event(enable_timing=True)
else:
self.start = 0
if self.device == "cuda":
self.end = torch.cuda.Event(enable_timing=True)
else:
self.end = 0
def time_text(t):
if t >= 3600:
return '{:.1f}h'.format(t / 3600)
elif t >= 60:
return '{:.1f}m'.format(t / 60)
else:
return '{:.1f}s'.format(t)
def compute_psnr(im1, im2):
p = psnr(im1, im2)
return p
def compute_ssim(im1, im2):
isRGB = len(im1.shape) == 3 and im1.shape[-1] == 3
s = ssim(im1, im2, K1=0.01, K2=0.03, gaussian_weights=True, sigma=1.5, use_sample_covariance=False,
multichannel=isRGB)
return s
def shave(im, border):
border = [border, border]
im = im[border[0]:-border[0], border[1]:-border[1], ...]
return im
def modcrop(im, modulo):
sz = im.shape
h = np.int32(sz[0] / modulo) * modulo
w = np.int32(sz[1] / modulo) * modulo
ims = im[0:h, 0:w, ...]
return ims
def get_list(path, ext):
return [os.path.join(path, f) for f in os.listdir(path) if f.endswith(ext)]
def convert_shape(img):
img = np.transpose((img * 255.0).round(), (1, 2, 0))
img = np.uint8(np.clip(img, 0, 255))
return img
def quantize(img):
return img.clip(0, 255).round().astype(np.uint8)
def tensor2np(tensor, out_type=np.uint8, min_max=(0, 1)):
tensor = tensor.float().cpu().clamp_(*min_max)
tensor = (tensor - min_max[0]) / (min_max[1] - min_max[0]) # to range [0, 1]
img_np = tensor.numpy()
img_np = np.transpose(img_np, (1, 2, 0))
if out_type == np.uint8:
img_np = (img_np * 255.0).round()
return img_np.astype(out_type)
def convert2np(tensor):
return tensor.cpu().mul(255).clamp(0, 255).byte().squeeze().permute(1, 2, 0).numpy()
def adjust_learning_rate(optimizer, epoch, step_size, lr_init, gamma):
factor = epoch // step_size
lr = lr_init * (gamma ** factor)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def load_state_dict(path):
state_dict = torch.load(path)
new_state_dcit = OrderedDict()
for k, v in state_dict.items():
if 'module' in k:
name = k[7:]
else:
name = k
new_state_dcit[name] = v
return new_state_dcit