-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
351 lines (252 loc) · 9.16 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
#!/usr/bin/env python
'''
Miscellaneous utilities that are extremely helpful but cannot be clubbed
into other modules.
'''
# Scientific computing
import numpy as np
import scipy as sp
import scipy.linalg as lin
import scipy.ndimage as ndim
from scipy import io
from scipy.sparse.linalg import svds
from scipy import signal
import scipy.io.wavfile as wavfile
import torch
# Plotting
import cv2
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
class AudioFile(torch.utils.data.Dataset):
def __init__(self, filename):
self.rate, self.data = wavfile.read(filename)
self.data = self.data.astype(np.float32)
self.timepoints = get_mgrid(len(self.data), 1)
def get_num_samples(self):
return self.timepoints.shape[0]
def __len__(self):
return 1
def __getitem__(self, idx):
amplitude = self.data
scale = np.max(np.abs(amplitude))
amplitude = (amplitude / scale)
amplitude = torch.Tensor(amplitude).view(-1, 1)
return self.rate, self.timepoints, amplitude
# This is for inpainting
# From https://github.com/dalmia/siren
def build_train_data(img_ground_truth, sampled_pc):
H, W, C = img_ground_truth.shape
img_mask_x = torch.from_numpy(
np.random.randint(0, H, sampled_pc))
img_mask_y = torch.from_numpy(
np.random.randint(0, W, sampled_pc))
img_train = img_ground_truth[img_mask_x, img_mask_y]
img_mask_x = img_mask_x.float() / H
img_mask_y = img_mask_y.float() / W
img_mask = torch.stack([img_mask_x, img_mask_y], dim=-1)
return img_mask, img_train
# This is for inpainting
# From https://github.com/dalmia/siren
def build_eval_data(img_ground_truth):
H, W, C = img_ground_truth.shape
img_mask_x = np.arange(0, H)
img_mask_y = np.arange(0, W)
img_mask_x, img_mask_y = np.meshgrid(img_mask_x, img_mask_y, indexing='ij')
img_mask_x = torch.from_numpy(img_mask_x)
img_mask_y = torch.from_numpy(img_mask_y)
img_mask_x = img_mask_x.float() / H
img_mask_y = img_mask_y.float() / W
img_mask = torch.stack([img_mask_x, img_mask_y], dim=-1)
img_mask = img_mask.reshape(-1, 2)
img_eval = img_ground_truth.reshape(-1, 3)
return img_mask, img_eval
def normalize(x, fullnormalize=False):
'''
Normalize input to lie between 0, 1.
Inputs:
x: Input signal
fullnormalize: If True, normalize such that minimum is 0 and
maximum is 1. Else, normalize such that maximum is 1 alone.
Outputs:
xnormalized: Normalized x.
'''
if x.sum() == 0:
return x
xmax = x.max()
if fullnormalize:
xmin = x.min()
else:
xmin = 0
xnormalized = (x - xmin)/(xmax - xmin)
return xnormalized
def rsnr(x, xhat):
'''
Compute reconstruction SNR for a given signal and its reconstruction.
Inputs:
x: Ground truth signal (ndarray)
xhat: Approximation of x
Outputs:
rsnr_val: RSNR = 20log10(||x||/||x-xhat||)
'''
xn = lin.norm(x.reshape(-1))
en = lin.norm((x-xhat).reshape(-1))
rsnr_val = 20*np.log10(xn/en)
return rsnr_val
def psnr(x, xhat):
''' Compute Peak Signal to Noise Ratio in dB
Inputs:
x: Ground truth signal
xhat: Reconstructed signal
Outputs:
snrval: PSNR in dB
'''
err = x - xhat
denom = np.mean(pow(err, 2))
snrval = 10*np.log10(np.max(x)/denom)
return snrval
def measure(x, noise_snr=40, tau=100):
''' Realistic sensor measurement with readout and photon noise
Inputs:
noise_snr: Readout noise in electron count
tau: Integration time. Poisson noise is created for x*tau.
(Default is 100)
Outputs:
x_meas: x with added noise
'''
x_meas = np.copy(x)
noise = np.random.randn(x_meas.size).reshape(x_meas.shape)*noise_snr
# First add photon noise, provided it is not infinity
if tau != float('Inf'):
x_meas = x_meas*tau
x_meas[x > 0] = np.random.poisson(x_meas[x > 0])
x_meas[x <= 0] = -np.random.poisson(-x_meas[x <= 0])
x_meas = (x_meas + noise)/tau
else:
x_meas = x_meas + noise
return x_meas
def build_montage(images):
'''
Build a montage out of images
'''
nimg, H, W = images.shape
nrows = int(np.ceil(np.sqrt(nimg)))
ncols = int(np.ceil(nimg/nrows))
montage_im = np.zeros((H*nrows, W*ncols), dtype=np.float32)
cnt = 0
for r in range(nrows):
for c in range(ncols):
h1 = r*H
h2 = (r+1)*H
w1 = c*W
w2 = (c+1)*W
if cnt == nimg:
break
montage_im[h1:h2, w1:w2] = normalize(images[cnt, ...], True)
cnt += 1
return montage_im
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def get_mgrid(sidelen, dim=2):
'''Generates a flattened grid of (x,y,...) coordinates in a range of -1 to 1.
sidelen: int
dim: int'''
tensors = tuple(dim * [torch.linspace(-1, 1, steps=sidelen)])
mgrid = torch.stack(torch.meshgrid(*tensors, indexing='ij'), dim=-1)
mgrid = mgrid.reshape(-1, dim)
return mgrid
def get_coords(H, W, T=None, dim=2):
'''
Get 2D/3D coordinates
'''
if dim == 2:
X, Y = np.meshgrid(np.linspace(-1, 1, W), np.linspace(-1, 1, H))
coords = np.hstack((X.reshape(-1, 1), Y.reshape(-1, 1)))
if dim == 3:
X, Y, Z = np.meshgrid(np.linspace(-1, 1, W),
np.linspace(-1, 1, H),
np.linspace(-1, 1, T))
coords = np.hstack((X.reshape(-1, 1),
Y.reshape(-1, 1),
Z.reshape(-1, 1)))
return torch.tensor(coords.astype(np.float32))
def resize(cube, scale):
'''
Resize a multi-channel image
Inputs:
cube: (H, W, nchan) image stack
scale: Scaling
'''
H, W, nchan = cube.shape
im0_lr = cv2.resize(cube[..., 0], None, fx=scale, fy=scale)
Hl, Wl = im0_lr.shape
cube_lr = np.zeros((Hl, Wl, nchan), dtype=cube.dtype)
for idx in range(nchan):
cube_lr[..., idx] = cv2.resize(cube[..., idx], None,
fx=scale, fy=scale,
interpolation=cv2.INTER_AREA)
return cube_lr
def get_inpainting_mask(imsize, mask_type='random2d', mask_frac=0.5):
'''
Get a 2D mask for image inpainting
Inputs:
imsize: Image size
mask_type: one of 'random2d', 'random1d'
mask_frac: Fraction of non-zeros in the mask
Outputs:
mask: A 2D mask image
'''
H, W = imsize
if mask_type == 'random2d':
mask = np.random.rand(H, W) < mask_frac
elif mask_type == 'random1d':
mask_row = np.random.rand(1, W) < mask_frac
mask = np.ones((H, 1)).dot(mask_row)
elif mask_type == 'bayer':
mask = np.zeros((H, W))
mask[::2, ::2] = 1
return mask.astype(np.float32)
@torch.no_grad()
def get_layer_outputs(model, coords, imsize,
nfilters_vis=16,
get_imag=False):
'''
get activation images after each layer
Inputs:
model: INR model
coords: 2D coordinates
imsize: Size of the image
nfilters_vis: Number of filters to visualize
get_imag: If True, get imaginary component of the outputs
Outputs:
atoms_montages: A list of 2d grid of outputs
'''
H, W = imsize
if model.pos_encode:
coords = model.positional_encoding(coords)
atom_montages = []
for idx in range(len(model.net)-1):
layer_output = model.net[idx](coords)
layer_images = layer_output.reshape(1, H, W, -1)[0]
if nfilters_vis != 'all':
layer_images = layer_images[..., :nfilters_vis]
if get_imag:
atoms = layer_images.detach().cpu().numpy().imag
else:
atoms = layer_images.detach().cpu().numpy().real
atoms_min = atoms.min(0, keepdims=True).min(1, keepdims=True)
atoms_max = atoms.max(0, keepdims=True).max(1, keepdims=True)
signs = (abs(atoms_min) > abs(atoms_max))
atoms = (1 - 2*signs)*atoms
# Arrange them by variance
atoms_std = atoms.std((0,1))
std_indices = np.argsort(atoms_std)
atoms = atoms[..., std_indices]
atoms_min = atoms.min(0, keepdims=True).min(1, keepdims=True)
atoms_max = atoms.max(0, keepdims=True).max(1, keepdims=True)
atoms = (atoms - atoms_min)/np.maximum(1e-14, atoms_max - atoms_min)
atoms[:, [0, -1], :] = 1
atoms[[0, -1], :, :] = 1
atoms_montage = build_montage(np.transpose(atoms, [2, 0, 1]))
atom_montages.append(atoms_montage)
coords = layer_output
return atom_montages