-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest2d.py
225 lines (165 loc) · 8.16 KB
/
test2d.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
import numpy as np
import torch
import torch.nn.functional as F
from torch import nn
import cuda_gridsample as cu
import naive_gridsample as nv
from torch.autograd import grad
from functools import partial
import unittest
from torch.testing import assert_close
class CudaGridsampleTest(unittest.TestCase):
def test_naive_constant(self):
image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).reshape(1, 1, 3, 3)
optical = np.array([0.1, 0.1]).reshape(1, 1, 1, 2)
self.cmp_with_naive(image, optical)
def test_naive_oob(self):
image = np.array([[1, 2], [3, 4]]).reshape(1, 1, 2, 2)
optical = np.array([0.1, 1.1]).reshape(1, 1, 1, 2)
self.cmp_with_naive(image, optical)
def test_naive_random(self):
for i in range(10):
input, grid = self.create_random_input(oob=False)
self.cmp_with_naive(input, grid)
def test_gradcheck_constant(self):
image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).reshape(1, 1, 3, 3)
optical = np.array([-2.1, 0.1]).reshape(1, 1, 1, 2)
self.gradcheck(image, optical, padding_mode='zeros')
def test_gradcheck_random(self):
for i in range(10):
input, grid = self.create_random_input(max_dim=5)
self.gradcheck(input, grid)
def test_grad_output(self):
for i in range(10):
input, grid, grad_output = self.create_random_input(oob=True, max_dim=10, with_grad_output=True)
input = torch.DoubleTensor(input)
grid = torch.DoubleTensor(grid)
grad_output = torch.DoubleTensor(grad_output)
input = input.cuda()
grid = grid.cuda()
grad_output = grad_output.cuda()
input.requires_grad = True
grid.requires_grad = True
grad_output.requires_grad = True
torch.autograd.gradcheck(lambda grad_output, input, grid: cu._GridSample2dBackward.apply(grad_output, input, grid), (grad_output, input, grid))
def test_gradcheck_random_oob_zeros(self):
for i in range(10):
input, grid = self.create_random_input(oob=True, max_dim=5)
self.gradcheck(input, grid, padding_mode='zeros')
def test_gradcheck_random_oob_border(self):
for i in range(10):
input, grid = self.create_random_input(oob=True, max_dim=5)
self.gradcheck(input, grid, padding_mode='border')
def test_gradcheck_random_nocorners(self):
for i in range(10):
input, grid = self.create_random_input(oob=True, max_dim=5)
self.gradcheck(input, grid, padding_mode='zeros', align_corners=False)
def test_float(self):
image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).reshape(1, 1, 3, 3)
optical = np.array([0.1, 0.1]).reshape(1, 1, 1, 2)
self.cmp_with_naive(image, optical, double=False)
def test_strided(self):
for i in range(100):
input, grid = self.create_random_input(max_dim=10)
if i % 2 == 0:
bs_stride = np.random.randint(1, input.shape[0] + 1)
c_stride = np.random.randint(1, input.shape[1] + 1)
h_stride = np.random.randint(1, input.shape[2] + 1)
w_stride = np.random.randint(1, input.shape[3] + 1)
hg_stride = np.random.randint(1, grid.shape[1] + 1)
wg_stride = np.random.randint(1, grid.shape[2] + 1)
else:
bs_stride = np.random.randint(1, 3)
c_stride = np.random.randint(1, 3)
h_stride = np.random.randint(1, 3)
w_stride = np.random.randint(1, 3)
hg_stride = np.random.randint(1, 3)
wg_stride = np.random.randint(1, 3)
input = torch.DoubleTensor(input)
grid = torch.DoubleTensor(grid)
input.requires_grad = True
grid.requires_grad = True
image = input.cuda()
optical = grid.cuda()
image = image[::bs_stride, ::c_stride, ::h_stride, ::w_stride]
optical = optical[::bs_stride, ::hg_stride, ::wg_stride]
self.assertTrue(torch.autograd.gradcheck(partial(cu.grid_sample_2d), inputs=(image, optical)))
self.assertTrue(torch.autograd.gradgradcheck(partial(cu.grid_sample_2d), inputs=(image, optical)))
def test_use_case(self):
torch.set_default_dtype(torch.float64)
for i in range(10):
input, grid = self.create_random_input(max_dim=10)
l1 = nn.Conv2d(input.shape[1], input.shape[1], 1)
l2 = nn.Conv2d(input.shape[1], 1, 1)
image = torch.DoubleTensor(input)
optical = torch.DoubleTensor(grid)
image.requires_grad = True
optical.requires_grad = True
image = image.cuda()
optical = optical.cuda()
l1.cuda()
l2.cuda()
def fn(image, optical):
out = l1(image)
out = F.relu(out)
out = cu.grid_sample_2d(out, optical)
out = l2(out)
out = out * out
out = out.sum()
return out
self.assertTrue(torch.autograd.gradcheck(partial(fn), inputs=(image, optical), nondet_tol=1e-05))
self.assertTrue(torch.autograd.gradgradcheck(partial(fn), inputs=(image, optical), nondet_tol=1e-05))
torch.set_default_dtype(torch.float32)
def create_random_input(self, oob=False, max_dim=20, with_grad_output=False):
bs = np.random.randint(1, max_dim)
c = np.random.randint(1, max_dim)
h = np.random.randint(1, max_dim)
w = np.random.randint(1, max_dim)
hg = np.random.randint(1, max_dim)
wg = np.random.randint(1, max_dim)
if oob:
bounds = (-2, 2)
else:
bounds = (-1, 1)
grid = np.random.uniform(bounds[0], bounds[1], size=(bs, hg, wg, 2))
input = np.random.normal(size=(bs, c, h, w))
if not with_grad_output:
return input, grid
else:
grad_output = np.random.normal(size=(bs, c, hg, wg))
return input, grid, grad_output
def cmp_with_naive(self, image, optical, double=True):
if double:
image = torch.DoubleTensor(image)
optical = torch.DoubleTensor(optical)
else:
image = torch.FloatTensor(image)
optical = torch.FloatTensor(optical)
image.requires_grad = True
optical.requires_grad = True
nv_out = nv.grid_sample_2d(image, optical)
nv_out = torch.sum(nv_out ** 2)
nv_grad_image, nv_grad_optical = grad(nv_out, [image, optical], create_graph=True)
nv_grad2_image, nv_grad2_optical = grad(torch.sum(nv_grad_image) + torch.sum(nv_grad_optical), [image, optical])
image = image.cuda()
optical = optical.cuda()
out = cu.grid_sample_2d(image, optical, padding_mode='border', align_corners=True)
out = torch.sum(out ** 2)
grad_image, grad_optical = grad(out, [image, optical], create_graph=True)
grad2_image, grad2_optical = grad(torch.sum(grad_image) + torch.sum(grad_optical), [image, optical])
assert_close(nv_out, out.cpu())
assert_close(nv_grad_image, grad_image.cpu())
assert_close(nv_grad_optical, grad_optical.cpu())
assert_close(nv_grad2_optical, grad2_optical.cpu())
assert_close(nv_grad2_image, grad2_image.cpu())
def gradcheck(self, image, optical, padding_mode='border', align_corners=True):
image = torch.DoubleTensor(image)
optical = torch.DoubleTensor(optical)
image.requires_grad = True
optical.requires_grad = True
image = image.cuda()
optical = optical.cuda()
self.assertTrue(torch.autograd.gradcheck(partial(cu.grid_sample_2d, padding_mode=padding_mode, align_corners=align_corners), inputs=(image, optical)))
self.assertTrue(torch.autograd.gradgradcheck(partial(cu.grid_sample_2d, padding_mode=padding_mode, align_corners=align_corners), inputs=(image, optical)))
if __name__ == "__main__":
unittest.main()