-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
478 lines (369 loc) · 13.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# -*- coding: utf-8 -*-
import os
from sys import stdout
import shutil
import random
import torch
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy import misc
from scipy.ndimage.filters import convolve
from scipy import stats
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.models as models
import torch.utils.model_zoo as model_zoo
import torch.utils.data
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Function
def evaluation_criteria(pre, label, label_std=None, cal_or=True):
"""
Evaluation Metrics
"""
pre = np.array(pre)
label = np.array(label)
label_std = np.array(label_std)
srcc = stats.spearmanr(pre, label)[0]
plcc = stats.pearsonr(pre, label)[0]
krcc = stats.stats.kendalltau(pre, label)[0]
rmse = np.sqrt(((pre - label) ** 2).mean())
mae = np.abs((pre - label)).mean()
if cal_or == True:
outlier_ratio = (np.abs(pre - label) > 2 * label_std).mean()
return srcc, plcc, krcc, rmse, mae, outlier_ratio
else:
return srcc, plcc, krcc, rmse, mae
def mos_rescale(mos, min_val, max_val, scale_min=0, scale_max=1):
"""
Rescale MOS value from [min_val, max_val] to [scale_min, scale_max]
"""
mos = scale_min + (mos - min_val) * ((scale_max - scale_min) / (max_val - min_val))
return mos
def ranking_loss(pre, label, loss):
"""
Ranking Loss - Rank every two patch inside a batch
"""
rank_loss = 0.0
rank_id = [(i, j) for i in range(len(pre)) for j in range(len(pre)) if i != j and i <= j]
for i in range(len(rank_id)):
pre_1 = pre[rank_id[i][0]]
pre_2 = pre[rank_id[i][1]]
label_1 = label[rank_id[i][0]]
label_2 = label[rank_id[i][1]]
rank_loss += loss(pre_1 - pre_2, label_1 - label_2)
if len(pre) != 1:
rank_loss /= (len(pre) * (len(pre) - 1) / 2)
return rank_loss
def relative_ranking_loss(pre, label):
"""
Relative Ranking Loss
A re-implementation of a ranking method proposed by
No-Reference Image Quality Assessment via Transformers, Relative Ranking, and Self-Consistency
"""
# Relative Ranking Loss
sort_index = [x for _, x in sorted(zip(pre, list(range(len(pre)))), reverse=True)]
high_pre = pre[sort_index[0]]
second_high_pre = pre[sort_index[1]]
low_pre = pre[sort_index[-1]]
second_low_pre = pre[sort_index[-2]]
high_label = label[sort_index[0]]
second_high_label = label[sort_index[1]]
low_label = label[sort_index[-1]]
second_low_label = label[sort_index[-2]]
margin1 = second_high_label - low_label
margin2 = high_label - second_low_label
triplet_loss_1 = abs(high_pre - second_high_pre) - abs(high_pre - low_pre) + margin1
triplet_loss_2 = abs(second_low_pre - low_pre) - abs(high_pre - low_pre) + margin2
if triplet_loss_1 <= 0:
triplet_loss_1 = 0
if triplet_loss_2 <= 0:
triplet_loss_2 = 0
rank_loss = triplet_loss_1 + triplet_loss_2
return rank_loss
def pseudo_huber_loss(pre, label, delta):
"""
Re-implementation of the Pseudo Huber Loss
"""
# loss = (delta ** 2) * (torch.sqrt(1 + torch.square((pre - label) / (delta + 1e-8))) - 1)
loss = (delta ** 2) * ((1 + ((pre - label) / (delta + 1e-8)) ** 2) ** (1 / 2) - 1)
return loss
# Locally Normalized Image
# by removing the local mean and dividing the local standard deviation
k = np.float32([1, 4, 6, 4, 1])
k = np.outer(k, k)
kern = k / k.sum()
def local_normalize(img, num_ch=1, const=127.0):
if num_ch == 1:
mu = convolve(img[:, :, 0], kern, mode='nearest')
mu_sq = mu * mu
im_sq = img[:, :, 0] * img[:, :, 0]
tmp = convolve(im_sq, kern, mode='nearest') - mu_sq
sigma = np.sqrt(np.abs(tmp))
structdis = (img[:, :, 0] - mu) / (sigma + const)
# Rescale within 0 and 1
# structdis = (structdis + 3) / 6
structdis = 2. * structdis / 3.
norm = structdis[:, :, None]
elif num_ch > 1:
norm = np.zeros(img.shape, dtype='float32')
for ch in range(num_ch):
mu = convolve(img[:, :, ch], kern, mode='nearest')
mu_sq = mu * mu
im_sq = img[:, :, ch] * img[:, :, ch]
tmp = convolve(im_sq, kern, mode='nearest') - mu_sq
sigma = np.sqrt(np.abs(tmp))
structdis = (img[:, :, ch] - mu) / (sigma + const)
# Rescale within 0 and 1
# structdis = (structdis + 3) / 6
structdis = 2. * structdis / 3.
norm[:, :, ch] = structdis
return norm
class LowerBound(Function):
def forward(ctx, inputs, bound):
b = torch.ones(inputs.size()) * bound
b = b.to(inputs.device)
ctx.save_for_backward(inputs, b)
return torch.max(inputs, b)
def backward(ctx, grad_output):
inputs, b = ctx.saved_tensors
pass_through_1 = inputs >= b
pass_through_2 = grad_output < 0
pass_through = pass_through_1 | pass_through_2
return pass_through.type(grad_output.dtype) * grad_output, None
class GDN(nn.Module):
"""Generalized divisive normalization layer.
y[i] = x[i] / sqrt(beta[i] + sum_j(gamma[j, i] * x[j]))
"""
def __init__(self,
ch,
device,
inverse=False,
beta_min=1e-6,
gamma_init=.1,
reparam_offset=2 ** -18):
super(GDN, self).__init__()
self.inverse = inverse
self.beta_min = beta_min
self.gamma_init = gamma_init
self.reparam_offset = torch.FloatTensor([reparam_offset])
self.build(ch, torch.device(device))
def build(self, ch, device):
self.pedestal = self.reparam_offset ** 2
self.beta_bound = (self.beta_min + self.reparam_offset ** 2) ** .5
self.gamma_bound = self.reparam_offset
# Create beta param
beta = torch.sqrt(torch.ones(ch) + self.pedestal)
self.beta = nn.Parameter(beta.to(device))
# Create gamma param
eye = torch.eye(ch)
g = self.gamma_init * eye
g = g + self.pedestal
gamma = torch.sqrt(g)
self.gamma = nn.Parameter(gamma.to(device))
self.pedestal = self.pedestal.to(device)
def forward(self, inputs):
device_id = inputs.device.index
beta = self.beta.to(device_id)
gamma = self.gamma.to(device_id)
pedestal = self.pedestal.to(device_id)
unfold = False
if inputs.dim() == 5:
unfold = True
bs, ch, d, w, h = inputs.size()
inputs = inputs.view(bs, ch, d * w, h)
_, ch, _, _ = inputs.size()
# Beta bound and reparam
beta = LowerBound()(beta, self.beta_bound)
beta = beta ** 2 - pedestal
# Gamma bound and reparam
gamma = LowerBound()(gamma, self.gamma_bound)
gamma = gamma ** 2 - pedestal
gamma = gamma.view(ch, ch, 1, 1)
# Norm pool calc
norm_ = nn.functional.conv2d(inputs ** 2, gamma, beta)
norm_ = torch.sqrt(norm_)
# Apply norm
if self.inverse:
outputs = inputs * norm_
else:
outputs = inputs / norm_
if unfold:
outputs = outputs.view(bs, ch, d, w, h)
return outputs
class L2pooling(nn.Module):
"""
L2 Pooling
"""
def __init__(self, filter_size=5, stride=2, channels=None, pad_off=0):
super(L2pooling, self).__init__()
self.padding = (filter_size - 2) // 2
self.stride = stride
self.channels = channels
a = np.hanning(filter_size)[1:-1]
g = torch.Tensor(a[:, None] * a[None, :])
g = g / torch.sum(g)
self.register_buffer('filter', g[None, None, :, :].repeat((self.channels, 1, 1, 1)))
def forward(self, input):
input = input ** 2
out = F.conv2d(input, self.filter, stride=self.stride, padding=self.padding, groups=input.shape[1])
return (out + 1e-12).sqrt()
class group_norm(torch.nn.Module):
"""
Group Normalization for Graph
"""
def __init__(self, dim_to_norm=None, dim_hidden=16, num_nodes=None, num_groups=None, skip_weight=None, **w):
super(group_norm, self).__init__()
self.num_nodes = num_nodes
self.num_groups = num_groups
self.skip_weight = skip_weight
self.dim_hidden = dim_hidden
self.bn = torch.nn.BatchNorm1d(dim_hidden * self.num_groups * self.num_nodes, momentum=0.3)
self.group_func = torch.nn.Linear(dim_hidden, self.num_groups, bias=True)
def forward(self, x):
if self.num_groups == 1:
x_temp = self.bn(x)
else:
score_cluster = F.softmax(self.group_func(x), dim=2)
x_temp = torch.cat([score_cluster[:, :, group].unsqueeze(dim=2) * x for group in range(self.num_groups)],
dim=2)
# batch, number_nodes, num_groups * dim_hidden
x_temp = self.bn(x_temp.view(-1, self.num_nodes * self.num_groups * self.dim_hidden))
x_temp = x_temp.view(-1, self.num_nodes, self.num_groups, self.dim_hidden).sum(dim=2)
x = x + x_temp * self.skip_weight
return x
def node_norm(x, p=2):
"""
Node Normalization for Graph
:param x: [batch, n_nodes, features]
:return:
"""
std_x = torch.std(x, dim=2, keepdim=True)
x = x / (std_x ** (1 / p) + 1e-5)
return x
def one_zero_normalization(x):
"""
One-zero Normalization
"""
if x.dim() == 4:
dim_0 = x.size(0)
dim_1 = x.size(1)
dim_2 = x.size(2)
dim_3 = x.size(3)
x = x.view(dim_0, -1)
x = x - x.min(dim=1, keepdim=True)[0]
x = x / x.max(dim=1, keepdim=True)[0]
x = x.view(-1, dim_1, dim_2, dim_3)
elif x.dim() == 3:
dim_0 = x.size(0)
dim_1 = x.size(1)
dim_2 = x.size(2)
x = x.view(dim_0, -1)
x = x - x.min(dim=1, keepdim=True)[0]
x = x / x.max(dim=1, keepdim=True)[0]
x = x.view(-1, dim_1, dim_2)
return x
def mean_std_normalization(x):
"""
Mean Normalization
by removing the mean and dividing the standard deviation
"""
if x.dim() == 4:
dim_0 = x.size(0)
dim_1 = x.size(1)
dim_2 = x.size(2)
dim_3 = x.size(3)
x = x.view(dim_0, -1)
x = x - x.mean(dim=1, keepdim=True)
x = x / (x.std(dim=1, keepdim=True) + 1e-12)
x = x.view(-1, dim_1, dim_2, dim_3)
elif x.dim() == 3:
dim_0 = x.size(0)
dim_1 = x.size(1)
dim_2 = x.size(2)
x = x.view(dim_0, -1)
x = x - x.mean(dim=1, keepdim=True)
x = x / (x.std(dim=1, keepdim=True) + 1e-12)
x = x.view(-1, dim_1, dim_2)
return x
def vec_l2_norm(x):
"""
Vector L2 Normalization
:param x: [Batch_size, num_feature]
:return vector after L2 Normalization: [Batch_size, num_feature]
"""
# x: [Batch_size, num_feature]
if x.dim() == 2:
norm = x.norm(p=2, dim=1, keepdim=True) + 1e-8
# x: [Batch_size, num_node, num_feature]
elif x.dim() == 3:
norm = x.norm(p=2, dim=2, keepdim=True) + 1e-8
l2_norm = x.div(norm)
return l2_norm
def bilinear_pool(feature_1, feature_2):
"""
Bilinear Pooling
:param feature_1: [Batch_size, num_feature]
:param feature_2: [Batch_size, num_feature]
:return bilinear pooling vector: [Batch_size, num_feature * num_feature]
"""
num_feature = feature_1.size()[1]
feature_1 = feature_1.unsqueeze(dim=1) # [Batch_size, 1, num_feature]
feature_2 = feature_2.unsqueeze(dim=1) # [Batch_size, 1, num_feature]
# [Batch_size, num_feature, 1] X [Batch_size, 1, num_feature] -> [Batch_size, num_feature, num_feature]
xi = torch.bmm(torch.transpose(feature_1, 1, 2), feature_2)
x = xi.view([-1, num_feature * num_feature]) # [Batch_size, num_feature * num_feature]
y = torch.mul(torch.sign(x), torch.sqrt(torch.abs(x))) # [Batch_size, num_feature * num_feature]
z = vec_l2_norm(y) # [Batch_size, num_feature * num_feature]
return z
def gaussian_prior(mean, scale):
"""
Gaussian Prior
"""
noise = torch.randn(mean.size()).cuda()
mos_pred = mean + noise * scale
return mos_pred
def mkdirs(path):
os.makedirs(path, exist_ok=True)
def setup_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def getFileName(path, suffix):
filename = []
f_list = os.listdir(path)
for i in f_list:
if os.path.splitext(i)[1] == suffix:
filename.append(i)
return filename
def getTIDFileName(path, suffix):
filename = []
f_list = os.listdir(path)
for i in f_list:
if suffix.find(os.path.splitext(i)[1]) != -1:
filename.append(i[1:3])
return filename
def pil_loader(path):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB')
def image_show(path):
image = mpimg.imread(path)
plt.imshow(image)
plt.axis('off')
plt.show()
def image_tensor_show(image_tensor):
for i in range(np.shape(image_tensor)[0]):
temp = image_tensor[i]
temp = np.squeeze(temp, axis=0)
temp = np.transpose(temp, (1, 2, 0))
plt.imshow(temp)
plt.axis('off')
plt.show()