Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_gradcheck(self, device):
eps = 1e-12
quaternion = torch.tensor([1., 0., 0., 0.]).to(device) + eps
quaternion = tensor_to_gradcheck_var(quaternion)
# evaluate function gradient
assert gradcheck(kornia.quaternion_to_angle_axis, (quaternion,),
raise_exception=True)
def test_gradcheck(self):
laf = torch.rand(5, 3, 2, 3)
img = torch.rand(5, 3, 10, 10)
PS = 3
from kornia.feature.laf import generate_patch_grid_from_normalized_LAF
img = utils.tensor_to_gradcheck_var(img) # to var
laf = utils.tensor_to_gradcheck_var(laf) # to var
assert gradcheck(generate_patch_grid_from_normalized_LAF,
(img, laf, PS,),
raise_exception=True)
patch_warped_inv = kornia.warp_perspective(
patch_warped, torch.inverse(M), dsize=(height, width))
# generate mask to compute error
mask = torch.ones_like(patch)
mask_warped_inv = kornia.warp_perspective(
kornia.warp_perspective(patch, M, dsize=(height, width)),
torch.inverse(M),
dsize=(height, width))
assert_allclose(mask_warped_inv * patch,
mask_warped_inv * patch_warped_inv)
# evaluate function gradient
patch = utils.tensor_to_gradcheck_var(patch) # to var
M = utils.tensor_to_gradcheck_var(M, requires_grad=False) # to var
assert gradcheck(
kornia.warp_perspective, (patch, M, (
height,
width,
)),
raise_exception=True)
def test_gradcheck(self, device):
points_2d = torch.zeros(1, 2).to(device)
depth = torch.ones(1, 1).to(device)
camera_matrix = torch.eye(3).expand(1, -1, -1).to(device)
# evaluate function gradient
points_2d = tensor_to_gradcheck_var(points_2d)
depth = tensor_to_gradcheck_var(depth)
camera_matrix = tensor_to_gradcheck_var(camera_matrix)
assert gradcheck(kornia.unproject_points,
(points_2d, depth, camera_matrix),
raise_exception=True)
def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 1, 31, 21
patches = torch.rand(batch_size, channels, height, width, device=device)
patches = utils.tensor_to_gradcheck_var(patches) # to var
assert gradcheck(ScaleSpaceDetector(2).to(device), patches,
raise_exception=True, nondet_tol=1e-4)
def test_gradcheck(self):
data = torch.tensor([[[[21., 22.],
[22., 22.]],
[[13., 14.],
[14., 14.]],
[[8., 8.],
[8., 8.]]]]) # 3x2x2
data = utils.tensor_to_gradcheck_var(data) # to var
assert gradcheck(kornia.color.RgbToHls(), (data,),
raise_exception=True)
def test_gradcheck(self):
batch_size, channels, height, width = 1, 1, 21, 21
patches = torch.rand(batch_size, channels, height, width).float()
patches = utils.tensor_to_gradcheck_var(patches) # to var
laf = torch.ones(batch_size, 2, 2, 3).float()
laf[:, :, 0, 1] = 0
laf[:, :, 1, 0] = 0
laf = utils.tensor_to_gradcheck_var(laf) # to var
assert gradcheck(LAFOrienter(8), (laf, patches),
raise_exception=True, rtol=1e-3, atol=1e-3)
def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
img = torch.rand(batch_size, channels, height, width).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.filters.median_blur, (img, (5, 3),),
raise_exception=True)
points_src = torch.zeros_like(norm)
points_src[:, 1, 0] = h
points_src[:, 2, 1] = w
points_src[:, 3, 0] = h
points_src[:, 3, 1] = w
points_dst = points_src + norm
# compute transform from source to target
dst_homo_src = kornia.get_perspective_transform(points_src, points_dst)
assert_allclose(
kornia.transform_points(dst_homo_src, points_src), points_dst)
# compute gradient check
points_src = utils.tensor_to_gradcheck_var(points_src) # to var
points_dst = utils.tensor_to_gradcheck_var(points_dst) # to var
assert gradcheck(
kornia.get_perspective_transform, (
points_src,
points_dst,
),
raise_exception=True)