-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiff_func.py
355 lines (241 loc) · 12.7 KB
/
diff_func.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
import torch
import torch.nn as nn
from neighbors_finder import get_neighbors_fixed_field_fast
import numpy as np
from torch.autograd import Function
from torch.autograd import Variable
import torch.optim as optim
from datetime import datetime
import os
from utils.poisson_disk import generate_possion_dis
from data_importer import normalization
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.set_default_tensor_type('torch.FloatTensor')
class DiffFunc(Function):
@staticmethod
def forward(ctx, input, target_points, neighborhood_size=0.2, res=128, kernel_sigma=2):
target,_ = d_func(target_points, neighborhood_size=neighborhood_size,
res=res, kernel_sigma=kernel_sigma)
df_input, np = d_func(input, neighborhood_size=neighborhood_size,
res=res, kernel_sigma=kernel_sigma)
ctx.np = np
ctx.neighborhood_size = neighborhood_size
ctx.res = res
ctx.kernel_sigma = kernel_sigma
ctx.save_for_backward(input, target_points, target, df_input)
output = (target - df_input).pow(2).sum()
return output
@staticmethod
def backward(ctx, grad_output):
input, target_points, target, df_input = ctx.saved_tensors
kernel_size = 6 * ctx.kernel_sigma + 1
d_gaussian_kernel_x = d_kernel(ctx.kernel_sigma,res=ctx.res,neighborhood_size=ctx.neighborhood_size)[:,:,0]
d_gaussian_kernel_x = d_gaussian_kernel_x.view(1, 1, kernel_size, kernel_size)
gaussian_filter_x = nn.Conv2d(in_channels=1, out_channels=1,
kernel_size=kernel_size,padding=kernel_size//2, groups=1, bias=False).to(device).float()
gaussian_filter_x.weight.requires_grad = False
gaussian_filter_x.weight.data = d_gaussian_kernel_x.to(device).float()
d_gaussian_kernel_y = d_kernel(ctx.kernel_sigma,res=ctx.res,neighborhood_size=ctx.neighborhood_size)[:,:,1]
d_gaussian_kernel_y = d_gaussian_kernel_y.view(1, 1, kernel_size, kernel_size)
gaussian_filter_y = nn.Conv2d(in_channels=1, out_channels=1,
kernel_size=kernel_size,padding=kernel_size//2, groups=1, bias=False).to(device).float()
gaussian_filter_y.weight.requires_grad = False
gaussian_filter_y.weight.data = d_gaussian_kernel_y.to(device).float()
dis_x = gaussian_filter_x(df_input - target)
dis_y = gaussian_filter_y(df_input - target)
output_x = torch.zeros(input.size()[0])
output_y = torch.zeros(input.size()[0])
for i in range(input.size()[0]):
neighbors_index = ctx.np[i]
if len(neighbors_index)!=0:
indices = torch.floor((input_points[neighbors_index, :] - input_points[i, :] + ctx.neighborhood_size) / (
2 * ctx.neighborhood_size) * ctx.res).reshape(-1, 2)
x = dis_x[0,0,indices[:, 0].long(), indices[:, 1].long()]
y = dis_y[0,0,indices[:, 0].long(), indices[:, 1].long()]
output_x[i] = 4*x.sum()/input.size()[0]
output_y[i] = 4*y.sum()/input.size()[0]
import pdb
pdb.set_trace()
grad_input = torch.cat((output_x.unsqueeze(1),output_y.unsqueeze(1)),1)
import pdb
pdb.set_trace()
return grad_output * grad_input, None, None, None, None
# used in calculting derivatives
def d_kernel(kernel_sigma):
kernel_size = round(6 * kernel_sigma + 1)
kernel_size = kernel_size+1 if (6 * kernel_sigma + 1)%2 == 0 else kernel_size
mean = (kernel_size - 1) / 2
x_cord = torch.arange(kernel_size).float()
x_grid = x_cord.repeat(kernel_size).view(kernel_size, kernel_size)
y_grid = x_grid.t()
xy_grid = torch.stack([x_grid, y_grid], dim=-1)
variance_space = kernel_sigma**2
# Calculate the 2-dimensional gaussian kernel which is
# the product of two gaussian distributions for two different
# variables (in this case called x and y)
d_gaussian_kernel = (2*((xy_grid-mean))/variance_space) * \
torch.exp(
-torch.sum(((xy_grid - mean)) ** 2., dim=-1) / \
(variance_space)
).repeat(2,1,1).permute(1,2,0)
return d_gaussian_kernel
def gaussian_kernel(kernel_sigma):
kernel_size = round(6 * kernel_sigma + 1)
mean = (kernel_size - 1) / 2
x_cord = torch.arange(kernel_size).float()
x_grid = x_cord.repeat(kernel_size).view(kernel_size, kernel_size)
y_grid = x_grid.t()
xy_grid = torch.stack([x_grid, y_grid], dim=-1)
variance = (kernel_sigma) ** 2.
# Calculate the 2-dimensional gaussian kernel which is
# the product of two gaussian distributions for two different
# variables (in this case called x and y)
gaussian_kernel = torch.exp(
-torch.sum(((xy_grid - mean)) ** 2., dim=-1) / \
(variance)
)
return gaussian_kernel,kernel_size
class diff_func(Function):
@staticmethod
def forward(ctx, input, neighborhood_size=0.1, res=128, kernel_sigma=2):
ctx.neighborhood_size = neighborhood_size
ctx.res = res
ctx.kernel_sigma = kernel_sigma
ctx.num_points = input.size()[0]
ctx.input = input
hist = torch.zeros(res, res).to(device)
np = get_neighbors_fixed_field_fast(input, 2, neighborhood_size)
kernel_size = 6 * kernel_sigma + 1
mean = (kernel_size - 1) / 2
x_cord = torch.arange(kernel_size).float()
x_grid = x_cord.repeat(kernel_size).view(kernel_size, kernel_size)
y_grid = x_grid.t()
xy_grid = torch.stack([x_grid, y_grid], dim=-1)
variance = kernel_sigma ** 2.
# Calculate the 2-dimensional gaussian kernel which is
# the product of two gaussian distributions for two different
# variables (in this case called x and y)
gaussian_kernel = torch.exp(
-torch.sum(((xy_grid - mean)) ** 2., dim=-1) / \
(variance)
)
# Reshape to 2d depthwise convolutional weight
gaussian_kernel = gaussian_kernel.view(1, 1, kernel_size, kernel_size)
gaussian_kernel = gaussian_kernel.repeat(1, 1, 1, 1)
gaussian_filter = nn.Conv2d(in_channels=1, out_channels=1,
kernel_size=kernel_size, padding=kernel_size // 2, groups=1, bias=False).to(device)
gaussian_filter.weight.data = gaussian_kernel.to(device).float()
gaussian_filter.weight.requires_grad = False
for i in range(input.size()[0]):
neighbors_index = np[i]
neighbors_index.remove(i)
if len(neighbors_index) != 0:
indices = torch.floor((input[neighbors_index, :] - input[i, :] + neighborhood_size) / (
2 * neighborhood_size) * res).reshape(-1, 2)
hist[indices[:, 0].long(), indices[:, 1].long()] += 1
ctx.np = np
output = gaussian_filter(hist.reshape(1, 1, res, -1)) / input.size()[0]
return output
@staticmethod
def backward(ctx, gradoutput):
kernel_size = 6 * ctx.kernel_sigma + 1
d_gaussian_kernel_x = d_kernel(ctx.kernel_sigma)[:,:,1]
d_gaussian_kernel_x = d_gaussian_kernel_x.view(1, 1, kernel_size, kernel_size)
gaussian_filter_x = nn.Conv2d(in_channels=1, out_channels=1,
kernel_size=kernel_size,padding=kernel_size//2, groups=1, bias=False).to(device).float()
gaussian_filter_x.weight.requires_grad = False
gaussian_filter_x.weight.data = d_gaussian_kernel_x.to(device).float()
d_gaussian_kernel_y = d_kernel(ctx.kernel_sigma)[:,:,0]
d_gaussian_kernel_y = d_gaussian_kernel_y.view(1, 1, kernel_size, kernel_size)
gaussian_filter_y = nn.Conv2d(in_channels=1, out_channels=1,
kernel_size=kernel_size,padding=kernel_size//2, groups=1, bias=False).to(device).float()
gaussian_filter_y.weight.requires_grad = False
gaussian_filter_y.weight.data = d_gaussian_kernel_y.to(device).float()
gradinput_x = torch.zeros(ctx.num_points, ctx.res, ctx.res)
gradinput_y = torch.zeros(ctx.num_points, ctx.res, ctx.res)
for i in range(ctx.num_points):
hist = torch.zeros(ctx.res, ctx.res).to(device)
neighbors_index = ctx.np[i]
if len(neighbors_index) == 0:
continue
indices_forward = torch.floor((ctx.input [neighbors_index, :] - ctx.input [i, :] + ctx.neighborhood_size) / (
2 * ctx.neighborhood_size) * ctx.res).reshape(-1, 2)
indices_backward = torch.floor((ctx.input [i, :] - ctx.input [neighbors_index, :] + ctx.neighborhood_size) / (
2 * ctx.neighborhood_size) * ctx.res).reshape(-1, 2)
hist[indices_forward[:, 0].long(), indices_forward[:, 1].long()] += 1
hist[indices_backward[:, 0].long(), indices_backward[:, 1].long()] += (-1)
gradinput_xi = gaussian_filter_x(hist.reshape(1, 1, ctx.res, ctx.res))
gradinput_yi = gaussian_filter_y(hist.reshape(1, 1, ctx.res, ctx.res))
gradinput_x[i, :, :] = gradinput_xi.squeeze()
gradinput_y[i, :, :] = gradinput_yi.squeeze()
gradinput = torch.cat((gradinput_x.unsqueeze(1),gradinput_y.unsqueeze(1)), 1).to(device)
gradinput = gradinput.reshape(ctx.num_points*2,ctx.res**2).to(device)
return torch.mm(gradinput,gradoutput.reshape(-1,1)).reshape(ctx.num_points,2), None, None, None
def d_func(input_points, neighborhood_size=0.1, res=128, kernel_sigma=2):
hist = torch.zeros(res, res).to(device)
np = get_neighbors_fixed_field_fast(input_points, 2, neighborhood_size)
kernel_size = 6 * kernel_sigma + 1
mean = (kernel_size - 1) / 2
x_cord = torch.arange(kernel_size).float()
x_grid = x_cord.repeat(kernel_size).view(kernel_size, kernel_size)
y_grid = x_grid.t()
xy_grid = torch.stack([x_grid, y_grid], dim=-1)
variance = kernel_sigma ** 2.
# Calculate the 2-dimensional gaussian kernel which is
# the product of two gaussian distributions for two different
# variables (in this case called x and y)
gaussian_kernel = torch.exp(
-torch.sum(((xy_grid - mean)) ** 2., dim=-1) / \
(variance)
)
# Reshape to 2d depthwise convolutional weight
gaussian_kernel = gaussian_kernel.view(1, 1, kernel_size, kernel_size)
gaussian_kernel = gaussian_kernel.repeat(1, 1, 1, 1)
gaussian_filter = nn.Conv2d(in_channels=1, out_channels=1,
kernel_size=kernel_size,padding=kernel_size//2, groups=1, bias=False).to(device)
gaussian_filter.weight.data = gaussian_kernel.to(device).float()
gaussian_filter.weight.requires_grad = False
for i in range(input_points.size()[0]):
neighbors_index = np[i]
neighbors_index.remove(i)
if len(neighbors_index) != 0:
indices = torch.floor((input_points[neighbors_index, :] - input_points[i, :] + neighborhood_size) / (
2 * neighborhood_size) * res).reshape(-1, 2)
hist[indices[:, 0].long(), indices[:, 1].long()] += 1
out = gaussian_filter(hist.reshape(1, 1, res, -1))/input_points.size()[0]
return out, np
if __name__ == "__main__":
#test the module
datestr = datetime.now().strftime("%Y%m%d_%H%M%S")
os.makedirs(datestr)
num_steps = 10000
x = np.linspace(0, 0.99, 20)
y = np.linspace(0, 0.99, 20)
xv, yv = np.meshgrid(x, y)
xc = torch.from_numpy(xv.reshape((-1,1)))
yc = torch.from_numpy(yv.reshape((-1,1)))
target_points = Variable(torch.cat((xc,yc),1).to(device).float(),requires_grad=False)
normalization(target_points, edge=0.01)
np.savetxt(datestr + '/target.txt', target_points.cpu().data.numpy())
tmp = torch.from_numpy(generate_possion_dis(1600))
input_points = Variable(tmp.float().to(
device=device),requires_grad=True)
optimizer = optim.Adam([input_points.requires_grad_()])
run = [0]
while run[0] <= num_steps:
def closure():
input_points.data.clamp_(0, 1)
optimizer.zero_grad()
df_input = diff_func.apply(input_points, 0.4/2)
df_target = diff_func.apply(target_points, 0.4)
loss = (df_input-df_target).pow(2).sum()
loss.backward()
run[0] += 1
if run[0] % 5 == 0:
print("run {}:".format(run))
print('Loss : {:4f}'.format(loss.item(), 0))
np.savetxt(datestr + '/out' + str(run[0]) + '.txt', input_points.cpu().data.numpy())
print()
return loss
optimizer.step(closure)
input_points.data.clamp_(0, 1)