-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
129 lines (96 loc) · 3.56 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
import torch
import kornia
import numpy as np
import torch.nn.functional as F
from torch.autograd import Function
def get_min(tensor): # :, C, H, W
_min = tensor.view(tensor.size(0), tensor.size(1), -1).min(-1)[0].unsqueeze(-1).unsqueeze(-1)
return _min
def get_max(tensor): # :, C, H, W
_max = tensor.view(tensor.size(0), tensor.size(1), -1).max(-1)[0].unsqueeze(-1).unsqueeze(-1)
return _max
def get_batch_psnr(m1, m2, max_val=255):
assert (m1.shape == m2.shape)
N, C, H, W = m1.shape
mse = ((m1 - m2)**2).view(N, -1).mean(1)
return (10 * torch.log10((max_val**2) / mse)).mean()
def noise(input, scale):
return input + scale*(torch.rand_like(input) - 0.5)
def round_w_offset(input, loc):
diff = STERound.apply(input - loc)
return diff + loc
def quantize(x, mode='noise', offset=None, scale=1):
if mode == 'noise':
return noise(x, scale)
elif mode == 'round':
return STERound.apply(x)
elif mode == 'dequantize':
return round_w_offset(x, offset)
elif mode == 'scaled':
return universal_quantize(x, offset, scale)
else:
raise NotImplementedError
def universal_quantize(x, m, s=1):
return STERound.apply((x - m) / s) * s + m
def gaussian_blur(input, sigma):
kernel_size = round(4 * sigma + 1)
kernel = kornia.get_gaussian_kernel2d((kernel_size, kernel_size), (sigma, sigma)).unsqueeze(0)
return kornia.filter2D(input, kernel)
def gaussian_pyramids(input, base_sigma=1, m=5):
output = [input]
N, C, H, W = input.shape
kernel = kornia.get_gaussian_kernel2d((5, 5), (base_sigma, base_sigma)).unsqueeze(0)
for i in range(m):
input = kornia.filter2D(input, kernel)
if i == 0:
output.append(input)
else:
tmp = input
for j in range(i):
tmp = F.interpolate(tmp, scale_factor=2., mode='bilinear', align_corners=True)
# tmp = kornia.filter2D(tmp, kernel)
output.append(tmp)
input = F.interpolate(input, scale_factor=0.5)
return torch.stack(output, 2)
def var_to_position(var, var_space):
y_axis = np.linspace(-1, 1, len(var_space))
x_axis = var_space
y = []
for i in range(len(var_space) - 1):
slope = (y_axis[i + 1] - y_axis[i]) / (x_axis[i + 1] - x_axis[i])
y.append(slope * (var - x_axis[i]) + y_axis[i])
y, _ = torch.stack(y, -1).min(-1)
return y.clamp(min=-1, max=1.)
class STERound(Function):
@staticmethod
def forward(ctx, x):
return x.round()
@staticmethod
def backward(ctx, g):
return g
class LowerBound(Function):
@staticmethod
def forward(ctx, inputs, bound):
b = torch.ones_like(inputs) * bound
ctx.save_for_backward(inputs, b)
return torch.max(inputs, b)
@staticmethod
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 UpperBound(Function):
@staticmethod
def forward(ctx, inputs, bound):
b = torch.ones_like(inputs) * bound
ctx.save_for_backward(inputs, b)
return torch.min(inputs, b)
@staticmethod
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