-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpolate.py
145 lines (117 loc) · 5.02 KB
/
interpolate.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
import torch
import torch.nn as nn
import numpy as np
from utils import norm_coord
from pykdtree.kdtree import KDTree
class TreeMultiQuad(nn.Module):
'''
Set up for a
R G
G B
Bayer array'''
def __init__(self, sz=512, k=4):
super().__init__()
self.k = k
self.sz = sz
def _find_idx(self, mask):
'''
Given a mask, find the idxs that are the closest
'''
img = torch.ones((self.sz, self.sz)).cuda()
holed_img = img * mask
filled_idx = (holed_img != 0).nonzero()
unfilled_idx = (holed_img == 0).nonzero()
# normalize coordinates to [-1, 1]
filled_idx_n = norm_coord(filled_idx, self.sz) # size: num_coords, 2
unfilled_idx_n = norm_coord(unfilled_idx, self.sz) # size: num_coords, 2
# define tree
tree = KDTree(filled_idx_n)
# for all the idxs you need to fill up, find out the closest filled idxs
dist, idx = tree.query(unfilled_idx_n, k=self.k)
# dist = [unfilled_idx num_coords, k]
# idx = [unfilled_idx num_coords, k]
# idx is of a flattened img with values from [0, sz * sz]
idx = idx.astype(np.int32)
dist = torch.from_numpy(dist).cuda()
return idx, dist, filled_idx, unfilled_idx
def _fill(self, holed_img, params):
''' Given idxs that need to be filled, fill them in, weighted by distance '''
idx, dist, filled_idx, unfilled_idx = params
vals = 0
for i in range(self.k):
# find coords of the points which are knn
idx_select = filled_idx[idx[:, i]] # size: num_coords, 2
# add value of those coords, weighted by their distance
vals += holed_img[idx_select[:, 0], idx_select[:, 1]] * (1.0 / dist[:, i])
vals /= torch.sum((1.0 / dist), dim=1)
holed_img[unfilled_idx[:, 0], unfilled_idx[:, 1]] = vals
return holed_img
def forward(self, coded):
sz = self.sz
# red exposure
mask = torch.zeros((sz, sz)).cuda()
mask[::2, ::2] = 1
holed_img = coded[0, 0, :, :] * mask
params = self._find_idx(mask)
red = self._fill(holed_img, params)
# blue exposure
mask = torch.zeros((sz, sz)).cuda()
mask[1::2, 1::2] = 1
holed_img = coded[0, 0, :, :] * mask
params = self._find_idx(mask)
blue = self._fill(holed_img, params)
# green exposure
mask = torch.zeros((sz, sz)).cuda()
mask[0::2, 1::2] = 1
mask[1::2, 0::2] = 1
holed_img = coded[0, 0, :, :] * mask
params = self._find_idx(mask)
green = self._fill(holed_img, params)
stacked = torch.stack((red, green, blue), dim=0).unsqueeze(0)
return stacked
class TreeMultiRandom(nn.Module):
'''
Use for irregularly interspaced data to be interpolated into full resolution
'''
def __init__(self, sz=512, k=3, num_channels=8):
super().__init__()
self.k = k # number of neighboring points to search
self.sz = sz
self.num_channels = num_channels # num channels to produce from interpolation
# lookup_table should be (sz, sz) size tensor with an integer value for each
# pixel, indicating which pixel should be interpolated for which channel from 1-NUM_CHANNELS
# e.g. if the pixel has value 4, then it will be used to interpolate the 4th channel only
self.lookup_table = None
def _find_idx(self, mask):
img = torch.ones((self.sz, self.sz)).cuda()
holed_img = img * mask
filled_idx = (holed_img != 0).nonzero()
unfilled_idx = (holed_img == 0).nonzero()
filled_idx_n = norm_coord(filled_idx, self.sz) # size: num_coords, 2
unfilled_idx_n = norm_coord(unfilled_idx, self.sz) # size: num_coords, 2
tree = KDTree(filled_idx_n)
dist, idx = tree.query(unfilled_idx_n, k=self.k)
idx = idx.astype(np.int32)
dist = torch.from_numpy(dist).cuda()
return idx, dist, filled_idx, unfilled_idx
def _fill(self, holed_img, params):
idx, dist, filled_idx, unfilled_idx = params
vals = 0
for i in range(self.k):
# find coords of the points which are knn
idx_select = filled_idx[idx[:, i]] # size: num_coords, 2
# add value of those coords, weighted by their distance
vals += holed_img[idx_select[:, 0], idx_select[:, 1]] * (1.0 / dist[:, i])
vals /= torch.sum((1.0 / dist), dim=1)
holed_img[unfilled_idx[:, 0], unfilled_idx[:, 1]] = vals
return holed_img
def forward(self, coded, lookup_table):
self.lookup_table = lookup_table
stacked = torch.zeros((self.num_channels, self.sz, self.sz)).cuda()
for i in range(0, self.num_channels):
mask = (self.lookup_table == i)
holed_img = coded[0, 0, :, :] * mask
params = self._find_idx(mask)
filled_img = self._fill(holed_img, params)
stacked[i-1, :, :] = filled_img
return stacked.unsqueeze(0)