-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathestimate_gaussian_possion_noise.py
195 lines (139 loc) · 5.9 KB
/
estimate_gaussian_possion_noise.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
'''
estimate the noise variance for general signal-dependent noise
This is the python implementation of the method presented in:
Xinhao Liu, Masayuki Tanaka, and Masatoshi Okutomi.
Practical signal-dependent noise parameter estimation from
a single noisy image. IEEE Transactions on Image Process-
ing, 23(10):4361–4371, 2014.
#
We note that this method is unreliable when local patches of the test image are pure white (very large intensity)
'''
import numpy as np
import torch
import torch.nn.functional as F
from scipy.stats import gamma
from scipy.optimize import minimize as minimize_scipy
def conv2mat(kernel, m, n):
'''
transform convolution operation to matrix multiplication
'''
H, W = kernel.shape
HH = m-H+1; WW = n-W+1
T = torch.zeros(HH*WW, m*n)
k = 0
for i in range(HH):
for j in range(WW):
for p in range(H):
# print((i+p)*n+j+W)
T[k, (i+p)*n+j:(i+p)*n+j+W] = kernel[p, :]
k = k + 1
return T
def im2col(img, block_size, step=1):
'''
image to column. we use tensor.unfold to implement this operation
'''
B, C, H, W = img.shape
pH, pW = block_size
# img = F.pad(img, [pH//2, pH//2, pW//2, pW//2], mode='replicate')
img = img.unfold(2, pH, step=step).unfold(3, pW, step=step) # (H//pH, W//pW, pH, pW)
img = img.reshape(B, C, -1, pH*pW).permute(0, 1, 3, 2) # (pH*pW, H//pH*W//pW)
return img
def im2col_2dim(img, block_size, step=1):
'''
image to column. we use tensor.unfold to implement this operation
'''
H, W = img.shape
pH, pW = block_size
# img = F.pad(img, [pH//2, pH//2, pW//2, pW//2], mode='replicate')
img = img.unfold(0, pH, step=step).unfold(1, pW, step=step) # (H//pH, W//pW, pH, pW)
img = img.permute(1,0,3,2).reshape(-1, pH*pW).permute(1, 0) # (pH*pW, H//pH*W//pW)
return img
def mle_loss(params, localMean, localVar):
y = GSGNoiseModel(params[..., 0:1], params[..., 1:2], params[..., 2:3], localMean)
loglik = localVar/(2*y) + torch.log(y)/2
loglik = torch.sum(loglik)
return loglik
def GSGNoiseModel(sigw, sigu, gamma, X):
# print(sigu.shape, sigw.shape, X.shape)
return X.pow(gamma*2) * sigu**2 + sigw**2
def mle_loss_np(params, localMean, localVar):
y = GSGNoiseModel_np(params[0], params[1], params[2], localMean)
loglik = localVar/(2*y) + np.log(y)/2
loglik = np.sum(loglik)
return loglik
def GSGNoiseModel_np(sigw, sigu, gamma, X):
# print(sigu.shape, sigw.shape, X.shape)
return np.power(X, gamma*2) * sigu**2 + sigw**2
def estimate_possion_gaussian_scipy(img, patchsize=7, conf=1-1e-6, itr=3):
B, C, H, W = img.shape
# img = (img - img.min(dim=1, keepdim=True)[0]) / (img.max(dim=1, keepdim=True)[0] - img.min(dim=1, keepdim=True)[0])
kh = torch.tensor([[-0.5, 0, 0.5]], dtype=torch.float64)
kv = kh.transpose(1,0)
kh_4dim = kh[None, None, ...].repeat(C, 1, 1, 1)
kv_4dim = kv[None, None, ...].repeat(C, 1, 1, 1)
imgh = F.conv2d(img, kh_4dim, groups=C).pow(2)
imgv = F.conv2d(img, kv_4dim, groups=C).pow(2)
# conv to mat
Dh = conv2mat(kh, patchsize, patchsize)
Dv = conv2mat(kv, patchsize, patchsize)
# print(Dh.shape, Dv.shape, Dh)
DD = Dh.transpose(1, 0) @ Dh + Dv.transpose(1, 0) @ Dv
r = torch.linalg.matrix_rank(DD).numpy()
Dtr = torch.trace(DD).numpy()
alpha0 = r / 2; scale0 = 2.0 * Dtr / r
tau0 = gamma.ppf(conf, alpha0, scale=scale0)
# print(r, Dtr, tau0)
p_final = np.zeros((C, 3))
Var_final = []
for chn in range(C):
X = im2col_2dim(img[0, chn], [patchsize, patchsize]) # B, C, pH*pW, H//pH*W//pW
Xh = im2col_2dim(imgh[0, chn], [patchsize, patchsize-2])
Xv = im2col_2dim(imgv[0, chn], [patchsize-2, patchsize])
Xtr = torch.cat([Xh, Xv], dim=0).sum(dim=0, keepdim=True)
Xave = X.mean(dim=0, keepdim=True) # B, C, H//pH*W//pW
# noise axis estimation
cov = X @ X.permute(1, 0)/(X.shape[1]-1)
_, V = torch.linalg.eigh(cov)
# print(eigvalue, V[...,0:1])
Xn = V[..., 0:1].permute(1, 0) @ X
Xn = Xn ** 2
# noise level estimation %%%%%
fun = lambda x: mle_loss_np(x, Xave.squeeze().numpy(), Xn.squeeze().numpy())
res = minimize_scipy(fun, (1e-6,0,0.0), bounds=((0, None), (0, None), (0, None))
, method='Powell')
sigw = res.x[0]
sigu = res.x[1]
gam = res.x[2]
for i in range(itr):
# weak texture selectioin
tau = tau0 * GSGNoiseModel(sigw, sigu, gam, Xave)
p = Xtr<tau
chosen_col = p.nonzero()[:, 1]
XX = torch.index_select(X, dim=1, index=chosen_col)
XXave = torch.index_select(Xave, dim=1, index=chosen_col)
# print(XX.shape, XXave.shape)
# noise axis estimation
cov = XX @ XX.permute(1,0) /(XX.shape[1]-1)
_, V = torch.linalg.eigh(cov)
XXn = V[..., 0:1].permute(1, 0) @ XX
XXn = XXn ** 2
# Xn = Xn.squeeze(dim=2)
# noise level estimation
fun = lambda x: mle_loss_np(x, XXave.squeeze().numpy(), XXn.squeeze().numpy())
res = minimize_scipy(fun, (1e-6,0,0.0), bounds=((0, 100), (0, 100), (0, 100))
, method='Powell')
# if not res.success:
# print('solution error!')
sigw = res.x[0]
sigu = res.x[1]
gam = res.x[2]
p_final[chn, :] = res.x
Var_final.append(XXn)
# print(res.x.exp())
return p_final, Var_final
def estimate_noise_variance(img):
_, Var_list = estimate_possion_gaussian_scipy(img)
var = np.zeros(img.shape[1])
for i in range(len(Var_list)):
var[i] = torch.nan_to_num(Var_list[i].mean(), nan=0.0, posinf=1.0, neginf=1e-4).float().numpy()
return var