forked from 2y7c3/Super-Resolution-Neural-Operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
234 lines (192 loc) · 6.63 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
# modified from: https://github.com/yinboc/liif
import os
import time
import shutil
import math
import torch
import numpy as np
from torch.optim import SGD
from tensorboardX import SummaryWriter
from Adam import Adam
import cv2
import matplotlib.pyplot as plt
from torchvision import transforms
from torchvision.transforms import InterpolationMode
import random
import math
def show_feature_map(feature_map,layer,name='rgb',rgb=False):
feature_map = feature_map.squeeze(0)
#if rgb: feature_map = feature_map.permute(1,2,0)*0.5+0.5
feature_map = feature_map.cpu().numpy()
feature_map_num = feature_map.shape[0]
row_num = math.ceil(np.sqrt(feature_map_num))
if rgb:
#plt.figure()
#plt.imshow(feature_map)
#plt.axis('off')
feature_map = cv2.cvtColor(feature_map,cv2.COLOR_BGR2RGB)
cv2.imwrite('data/'+layer+'/'+name+".png",feature_map*255)
#plt.show()
else:
plt.figure()
for index in range(1, feature_map_num+1):
t = (feature_map[index-1]*255).astype(np.uint8)
t = cv2.applyColorMap(t, cv2.COLORMAP_TWILIGHT)
plt.subplot(row_num, row_num, index)
plt.imshow(t, cmap='gray')
plt.axis('off')
#ensure_path('data/'+layer)
cv2.imwrite('data/'+layer+'/'+str(name)+'_'+str(index)+".png",t)
#plt.show()
plt.savefig('data/'+layer+'/'+str(name)+".png")
def resize_fn(img, size):
return transforms.ToTensor()(
transforms.Resize(size, InterpolationMode.BICUBIC)(
transforms.ToPILImage()(img)))
def downsample(img, scale_min=1, scale_max=4, inp_size=None, augment=False, epoch=None):
if epoch<20: s = random.randint(scale_min, scale_max)
s = random.uniform(scale_min, scale_max)
#print(s)
if inp_size is None:
h_lr = math.floor(img.shape[-2] / s + 1e-9)
w_lr = math.floor(img.shape[-1] / s + 1e-9)
h_hr = round(h_lr * s)
w_hr = round(w_lr * s)
img = img[:, :, :h_hr, :w_hr]
img_down = torch.stack([resize_fn(x, (h_lr, w_lr)) for x in img], dim=0)
crop_lr, crop_hr = img_down, img
else:
h_lr = inp_size
w_lr = inp_size
h_hr = round(h_lr * s)
w_hr = round(w_lr * s)
x0 = random.randint(0, img.shape[-2] - w_hr)
y0 = random.randint(0, img.shape[-1] - w_hr)
crop_hr = img[:, :, x0: x0 + w_hr, y0: y0 + w_hr]
crop_lr = torch.stack([resize_fn(x, w_lr) for x in crop_hr], dim=0)
if augment == True:
hflip = random.random() < 0.5
vflip = random.random() < 0.5
dflip = random.random() < 0.5
def augment(x):
if hflip: x = x.flip(-2)
if vflip: x = x.flip(-1)
if dflip: x = x.transpose(-2, -1)
return x
crop_lr = augment(crop_lr)
crop_hr = augment(crop_hr)
coord = make_coord([h_hr, w_hr], flatten=False)
coord = coord.unsqueeze(0).expand(img.shape[0], *coord.shape[:2], 2)
cell = torch.tensor([2 / crop_hr.shape[-2], 2 / crop_hr.shape[-1]], dtype=torch.float32).unsqueeze(0).expand(img.shape[0], 2)
return {
'inp': crop_lr.contiguous(),
'coord': coord.contiguous(),
'cell': cell.contiguous(),
'gt': crop_hr.contiguous()
}
class Averager():
def __init__(self):
self.n = 0.0
self.v = 0.0
def add(self, v, n=1.0):
self.v = (self.v * self.n + v * n) / (self.n + n)
self.n += n
def item(self):
return self.v
class Timer():
def __init__(self):
self.v = time.time()
def s(self):
self.v = time.time()
def t(self):
return time.time() - self.v
def time_text(t):
if t >= 3600:
return '{:.3f}h'.format(t / 3600)
elif t >= 60:
return '{:.3f}m'.format(t / 60)
else:
return '{:.3f}s'.format(t)
_log_path = None
def set_log_path(path):
global _log_path
_log_path = path
def log(obj, filename='log.txt'):
print(obj)
if _log_path is not None:
with open(os.path.join(_log_path, filename), 'a') as f:
print(obj, file=f)
def ensure_path(path, remove=True):
basename = os.path.basename(path.rstrip('/'))
if os.path.exists(path):
if remove and (basename.startswith('_')
or input('{} exists, remove? (y/[n]): '.format(path)) == 'y'):
shutil.rmtree(path)
os.makedirs(path)
else:
os.makedirs(path)
def set_save_path(save_path, remove=True):
ensure_path(save_path, remove=remove)
set_log_path(save_path)
writer = SummaryWriter(os.path.join(save_path, 'tensorboard'))
return log, writer
def compute_num_params(model, text=False):
tot = int(sum([np.prod(p.shape) for p in model.parameters()]))
if text:
if tot >= 1e6:
return '{:.1f}M'.format(tot / 1e6)
else:
return '{:.1f}K'.format(tot / 1e3)
else:
return tot
def make_optimizer(param_list, optimizer_spec, load_sd=False):
Optimizer = {
'sgd': SGD,
'adam': Adam
}[optimizer_spec['name']]
optimizer = Optimizer(param_list, **optimizer_spec['args'])
if load_sd:
optimizer.load_state_dict(optimizer_spec['sd'])
return optimizer
def make_coord(shape, ranges=None, flatten=True):
""" Make coordinates at grid centers.
"""
coord_seqs = []
for i, n in enumerate(shape):
if ranges is None:
v0, v1 = -1, 1
else:
v0, v1 = ranges[i]
r = (v1 - v0) / (2 * n)
seq = v0 + r + (2 * r) * torch.arange(n).float()
coord_seqs.append(seq)
#ret = torch.stack(torch.meshgrid(*coord_seqs), dim=-1)
ret = torch.stack(torch.meshgrid(*coord_seqs,indexing='ij'), dim=-1)
if flatten:
ret = ret.view(-1, ret.shape[-1])
return ret
def to_pixel_samples(img):
""" Convert the image to coord-RGB pairs.
img: Tensor, (3, H, W)
"""
coord = make_coord(img.shape[-2:])
rgb = img.view(3, -1).permute(1, 0)
return coord, rgb
def calc_psnr(sr, hr, dataset=None, scale=1, rgb_range=1):
diff = (sr - hr) / rgb_range
if dataset is not None:
if dataset == 'benchmark':
shave = scale
if diff.size(1) > 1:
gray_coeffs = [65.738, 129.057, 25.064]
convert = diff.new_tensor(gray_coeffs).view(1, 3, 1, 1) / 256
diff = diff.mul(convert).sum(dim=1)
elif dataset == 'div2k':
shave = scale + 6
else:
raise NotImplementedError
valid = diff[..., shave:-shave, shave:-shave]
else:
valid = diff
mse = valid.pow(2).mean()
return -10 * torch.log10(mse)