Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

added gpu tests for cuda stuff

Dmytro Mishkin1606c7b51d36
diff --git a/test/feature/test_affine_shape_estimator.py b/test/feature/test_affine_shape_estimator.py
index b904c4ac5..acb32cb0f 100644
--- a/test/feature/test_affine_shape_estimator.py
+++ b/test/feature/test_affine_shape_estimator.py
@@ -1,6 +1,7 @@
import pytest
import kornia.testing as utils # test utils
import kornia
+from test.common import device
from torch.testing import assert_allclose
from torch.autograd import gradcheck
@@ -8,72 +9,72 @@ from kornia.feature.affine_shape import *
class TestPatchAffineShapeEstimator:
- def test_shape(self):
- inp = torch.rand(1, 1, 32, 32)
- ori = PatchAffineShapeEstimator(32)
+ def test_shape(self, device):
+ inp = torch.rand(1, 1, 32, 32, device=device)
+ ori = PatchAffineShapeEstimator(32).to(device)
ang = ori(inp)
assert ang.shape == torch.Size([1, 1, 3])
- def test_shape_batch(self):
- inp = torch.rand(2, 1, 32, 32)
- ori = PatchAffineShapeEstimator(32)
+ def test_shape_batch(self, device):
+ inp = torch.rand(2, 1, 32, 32, device=device)
+ ori = PatchAffineShapeEstimator(32).to(device)
ang = ori(inp)
assert ang.shape == torch.Size([2, 1, 3])
- def test_print(self):
+ def test_print(self, device):
sift = PatchAffineShapeEstimator(32)
sift.__repr__()
- def test_toy(self):
- aff = PatchAffineShapeEstimator(19)
- inp = torch.zeros(1, 1, 19, 19)
+ def test_toy(self, device):
+ aff = PatchAffineShapeEstimator(19).to(device)
+ inp = torch.zeros(1, 1, 19, 19, device=device)
inp[:, :, 5:-5, 1:-1] = 1
abc = aff(inp)
- expected = torch.tensor([[[0.4146, 0.0000, 1.0000]]])
+ expected = torch.tensor([[[0.4146, 0.0000, 1.0000]]], device=device)
assert_allclose(abc, expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 1, 13, 13
- ori = PatchAffineShapeEstimator(width)
- patches = torch.rand(batch_size, channels, height, width)
+ ori = PatchAffineShapeEstimator(width).to(device)
+ patches = torch.rand(batch_size, channels, height, width, device=device)
patches = utils.tensor_to_gradcheck_var(patches) # to var
assert gradcheck(ori, (patches, ),
- raise_exception=True)
+ raise_exception=True, nondet_tol=1e-4)
class TestLAFAffineShapeEstimator:
- def test_shape(self):
- inp = torch.rand(1, 1, 32, 32)
- laf = torch.rand(1, 1, 2, 3)
- ori = LAFAffineShapeEstimator()
+ def test_shape(self, device):
+ inp = torch.rand(1, 1, 32, 32, device=device)
+ laf = torch.rand(1, 1, 2, 3, device=device)
+ ori = LAFAffineShapeEstimator().to(device)
out = ori(laf, inp)
assert out.shape == laf.shape
- def test_shape_batch(self):
- inp = torch.rand(2, 1, 32, 32)
- laf = torch.rand(2, 34, 2, 3)
- ori = LAFAffineShapeEstimator()
+ def test_shape_batch(self, device):
+ inp = torch.rand(2, 1, 32, 32, device=device)
+ laf = torch.rand(2, 34, 2, 3, device=device)
+ ori = LAFAffineShapeEstimator().to(device)
out = ori(laf, inp)
assert out.shape == laf.shape
- def test_print(self):
+ def test_print(self, device):
sift = LAFAffineShapeEstimator()
sift.__repr__()
- def test_toy(self):
- aff = LAFAffineShapeEstimator(32)
- inp = torch.zeros(1, 1, 32, 32)
+ def test_toy(self, device):
+ aff = LAFAffineShapeEstimator(32).to(device)
+ inp = torch.zeros(1, 1, 32, 32, device=device)
inp[:, :, 15:-15, 9:-9] = 1
- laf = torch.tensor([[[[20., 0., 16.], [0., 20., 16.]]]])
+ laf = torch.tensor([[[[20., 0., 16.], [0., 20., 16.]]]], device=device)
new_laf = aff(laf, inp)
- expected = torch.tensor([[[[36.643, 0., 16.], [0., 10.916, 16.]]]])
+ expected = torch.tensor([[[[36.643, 0., 16.], [0., 10.916, 16.]]]], device=device)
assert_allclose(new_laf, expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 1, 40, 40
- patches = torch.rand(batch_size, channels, height, width)
+ patches = torch.rand(batch_size, channels, height, width, device=device)
patches = utils.tensor_to_gradcheck_var(patches) # to var
- laf = torch.tensor([[[[5., 0., 26.], [0., 5., 26.]]]])
+ laf = torch.tensor([[[[5., 0., 26.], [0., 5., 26.]]]], device=device)
laf = utils.tensor_to_gradcheck_var(laf) # to var
- assert gradcheck(LAFAffineShapeEstimator(11), (laf, patches),
- raise_exception=True, rtol=1e-3, atol=1e-3)
+ assert gradcheck(LAFAffineShapeEstimator(11).to(device), (laf, patches),
+ raise_exception=True, rtol=1e-3, atol=1e-3, nondet_tol=1e-4)
diff --git a/test/feature/test_laf.py b/test/feature/test_laf.py
index d535f59b3..2939dacbe 100644
--- a/test/feature/test_laf.py
+++ b/test/feature/test_laf.py
@@ -3,6 +3,7 @@ import pytest
import kornia as kornia
import kornia.geometry.transform.imgwarp
import kornia.testing as utils # test utils
+from test.common import device
import torch
from torch.testing import assert_allclose
@@ -10,21 +11,21 @@ from torch.autograd import gradcheck
class TestAngleToRotationMatrix:
- def test_shape(self):
- inp = torch.ones(1, 3, 4, 4)
+ def test_shape(self, device):
+ inp = torch.ones(1, 3, 4, 4).to(device)
rotmat = kornia.geometry.transform.imgwarp.angle_to_rotation_matrix(inp)
assert rotmat.shape == (1, 3, 4, 4, 2, 2)
- def test_angles(self):
- ang_deg = torch.tensor([0, 90.])
+ def test_angles(self, device):
+ ang_deg = torch.tensor([0, 90.], device=device)
expected = torch.tensor([[[1.0, 0.], [0., 1.0]],
- [[0, 1.0], [-1.0, 0]]])
+ [[0, 1.0], [-1.0, 0]]], device=device)
rotmat = kornia.geometry.transform.imgwarp.angle_to_rotation_matrix(ang_deg)
assert_allclose(rotmat, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width, device=device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.geometry.transform.imgwarp.angle_to_rotation_matrix,
(img,),
@@ -32,21 +33,21 @@ class TestAngleToRotationMatrix:
class TestGetLAFScale:
- def test_shape(self):
- inp = torch.ones(1, 3, 2, 3)
+ def test_shape(self, device):
+ inp = torch.ones(1, 3, 2, 3, device=device)
rotmat = kornia.feature.get_laf_scale(inp)
assert rotmat.shape == (1, 3, 1, 1)
- def test_scale(self):
- inp = torch.tensor([[5., 1, 0], [1, 1, 0]]).float()
+ def test_scale(self, device):
+ inp = torch.tensor([[5., 1, 0], [1, 1, 0]], device=device).float()
inp = inp.view(1, 1, 2, 3)
- expected = torch.tensor([[[[2]]]]).float()
+ expected = torch.tensor([[[[2]]]], device=device).float()
rotmat = kornia.feature.get_laf_scale(inp)
assert_allclose(rotmat, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 2, 3
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width, device=device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.feature.get_laf_scale,
(img,),
@@ -54,19 +55,19 @@ class TestGetLAFScale:
class TestGetLAFCenter:
- def test_shape(self):
- inp = torch.ones(1, 3, 2, 3)
+ def test_shape(self, device):
+ inp = torch.ones(1, 3, 2, 3, device=device)
xy = kornia.feature.get_laf_center(inp)
assert xy.shape == (1, 3, 2)
- def test_center(self):
- inp = torch.tensor([[5., 1, 2], [1, 1, 3]]).float()
+ def test_center(self, device):
+ inp = torch.tensor([[5., 1, 2], [1, 1, 3]], device=device).float()
inp = inp.view(1, 1, 2, 3)
- expected = torch.tensor([[[2, 3]]]).float()
+ expected = torch.tensor([[[2, 3]]], device=device).float()
xy = kornia.feature.get_laf_center(inp)
assert_allclose(xy, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 2, 3
img = torch.rand(batch_size, channels, height, width)
img = utils.tensor_to_gradcheck_var(img) # to var
@@ -76,21 +77,21 @@ class TestGetLAFCenter:
class TestGetLAFOri:
- def test_shape(self):
- inp = torch.ones(1, 3, 2, 3)
+ def test_shape(self, device):
+ inp = torch.ones(1, 3, 2, 3, device=device)
ori = kornia.feature.get_laf_orientation(inp)
assert ori.shape == (1, 3, 1)
- def test_ori(self):
- inp = torch.tensor([[1, 1, 2], [1, 1, 3]]).float()
+ def test_ori(self, device):
+ inp = torch.tensor([[1, 1, 2], [1, 1, 3]], device=device).float()
inp = inp.view(1, 1, 2, 3)
- expected = torch.tensor([[[45.]]]).float()
+ expected = torch.tensor([[[45.]]], device=device).float()
angle = kornia.feature.get_laf_orientation(inp)
assert_allclose(angle, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 2, 3
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width, device=device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.feature.get_laf_orientation,
(img,),
@@ -98,28 +99,28 @@ class TestGetLAFOri:
class TestScaleLAF:
- def test_shape_float(self):
- inp = torch.ones(7, 3, 2, 3).float()
+ def test_shape_float(self, device):
+ inp = torch.ones(7, 3, 2, 3, device=device).float()
scale = 23.
assert kornia.feature.scale_laf(inp, scale).shape == inp.shape
- def test_shape_tensor(self):
- inp = torch.ones(7, 3, 2, 3).float()
- scale = torch.zeros(7, 1, 1, 1).float()
+ def test_shape_tensor(self, device):
+ inp = torch.ones(7, 3, 2, 3, device=device).float()
+ scale = torch.zeros(7, 1, 1, 1, device=device).float()
assert kornia.feature.scale_laf(inp, scale).shape == inp.shape
- def test_scale(self):
- inp = torch.tensor([[5., 1, 0.8], [1, 1, -4.]]).float()
+ def test_scale(self, device):
+ inp = torch.tensor([[5., 1, 0.8], [1, 1, -4.]], device=device).float()
inp = inp.view(1, 1, 2, 3)
- scale = torch.tensor([[[[2.]]]]).float()
+ scale = torch.tensor([[[[2.]]]], device=device).float()
out = kornia.feature.scale_laf(inp, scale)
- expected = torch.tensor([[[[10., 2, 0.8], [2, 2, -4.]]]]).float()
+ expected = torch.tensor([[[[10., 2, 0.8], [2, 2, -4.]]]], device=device).float()
assert_allclose(out, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 2, 3
- laf = torch.rand(batch_size, channels, height, width)
- scale = torch.rand(batch_size)
+ laf = torch.rand(batch_size, channels, height, width, device=device)
+ scale = torch.rand(batch_size, device=device)
scale = utils.tensor_to_gradcheck_var(scale) # to var
laf = utils.tensor_to_gradcheck_var(laf) # to var
assert gradcheck(kornia.feature.scale_laf,
@@ -128,34 +129,34 @@ class TestScaleLAF:
class TestMakeUpright:
- def test_shape(self):
- inp = torch.ones(5, 3, 2, 3)
+ def test_shape(self, device):
+ inp = torch.ones(5, 3, 2, 3, device=device)
rotmat = kornia.feature.make_upright(inp)
assert rotmat.shape == (5, 3, 2, 3)
- def test_do_nothing(self):
- inp = torch.tensor([[1, 0, 0], [0, 1, 0]]).float()
+ def test_do_nothing(self, device):
+ inp = torch.tensor([[1, 0, 0], [0, 1, 0]], device=device).float()
inp = inp.view(1, 1, 2, 3)
- expected = torch.tensor([[1, 0, 0], [0, 1, 0]]).float()
+ expected = torch.tensor([[1, 0, 0], [0, 1, 0]], device=device).float()
laf = kornia.feature.make_upright(inp)
assert_allclose(laf, expected)
- def test_do_nothing_with_scalea(self):
- inp = torch.tensor([[2, 0, 0], [0, 2, 0]]).float()
+ def test_do_nothing_with_scalea(self, device):
+ inp = torch.tensor([[2, 0, 0], [0, 2, 0]], device=device).float()
inp = inp.view(1, 1, 2, 3)
- expected = torch.tensor([[2, 0, 0], [0, 2, 0]]).float()
+ expected = torch.tensor([[2, 0, 0], [0, 2, 0]], device=device).float()
laf = kornia.feature.make_upright(inp)
assert_allclose(laf, expected)
- def test_check_zeros(self):
- inp = torch.rand(4, 5, 2, 3)
+ def test_check_zeros(self, device):
+ inp = torch.rand(4, 5, 2, 3, device=device)
laf = kornia.feature.make_upright(inp)
must_be_zeros = laf[:, :, 0, 1]
assert_allclose(must_be_zeros, torch.zeros_like(must_be_zeros))
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 14, 2, 2, 3
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width, device=device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.feature.make_upright,
(img,),
@@ -163,23 +164,23 @@ class TestMakeUpright:
class TestELL2LAF:
- def test_shape(self):
- inp = torch.ones(5, 3, 5)
+ def test_shape(self, device):
+ inp = torch.ones(5, 3, 5, device=device)
inp[:, :, 3] = 0
rotmat = kornia.feature.ellipse_to_laf(inp)
assert rotmat.shape == (5, 3, 2, 3)
- def test_conversion(self):
- inp = torch.tensor([[10, -20, 0.01, 0, 0.01]]).float()
+ def test_conversion(self, device):
+ inp = torch.tensor([[10, -20, 0.01, 0, 0.01]], device=device).float()
inp = inp.view(1, 1, 5)
- expected = torch.tensor([[10, 0, 10.], [0, 10, -20]]).float()
+ expected = torch.tensor([[10, 0, 10.], [0, 10, -20]], device=device).float()
expected = expected.view(1, 1, 2, 3)
laf = kornia.feature.ellipse_to_laf(inp)
assert_allclose(laf, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height = 1, 2, 5
- img = torch.rand(batch_size, channels, height).abs()
+ img = torch.rand(batch_size, channels, height, device=device).abs()
img[:, :, 2] = img[:, :, 3].abs() + 0.3
img[:, :, 4] += 1.
# assure it is positive definite
@@ -190,12 +191,12 @@ class TestELL2LAF:
class TestNormalizeLAF:
- def test_shape(self):
+ def test_shape(self, device):
inp = torch.rand(5, 3, 2, 3)
img = torch.rand(5, 3, 10, 10)
assert inp.shape == kornia.feature.normalize_laf(inp, img).shape
- def test_conversion(self):
+ def test_conversion(self, device):
w, h = 10, 5
laf = torch.tensor([[1, 0, 1], [0, 1, 1]]).float()
laf = laf.view(1, 1, 2, 3)
@@ -204,7 +205,7 @@ class TestNormalizeLAF:
lafn = kornia.feature.normalize_laf(laf, img)
assert_allclose(lafn, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 2, 3
laf = torch.rand(batch_size, channels, height, width)
@@ -217,13 +218,13 @@ class TestNormalizeLAF:
class TestLAF2pts:
- def test_shape(self):
- inp = torch.rand(5, 3, 2, 3)
+ def test_shape(self, device):
+ inp = torch.rand(5, 3, 2, 3, device=device)
n_pts = 13
assert kornia.feature.laf_to_boundary_points(inp, n_pts).shape == (5, 3, n_pts, 2)
- def test_conversion(self):
- laf = torch.tensor([[1, 0, 1], [0, 1, 1]]).float()
+ def test_conversion(self, device):
+ laf = torch.tensor([[1, 0, 1], [0, 1, 1]], device=device).float()
laf = laf.view(1, 1, 2, 3)
n_pts = 6
expected = torch.tensor([[[[1, 1],
@@ -231,13 +232,13 @@ class TestLAF2pts:
[2, 1],
[1, 0],
[0, 1],
- [1, 2]]]]).float()
+ [1, 2]]]], device=device).float()
pts = kornia.feature.laf_to_boundary_points(laf, n_pts)
assert_allclose(pts, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 3, 2, 2, 3
- laf = torch.rand(batch_size, channels, height, width)
+ laf = torch.rand(batch_size, channels, height, width, device=device)
laf = utils.tensor_to_gradcheck_var(laf) # to var
assert gradcheck(kornia.feature.laf_to_boundary_points,
(laf),
@@ -245,25 +246,25 @@ class TestLAF2pts:
class TestDenormalizeLAF:
- def test_shape(self):
- inp = torch.rand(5, 3, 2, 3)
- img = torch.rand(5, 3, 10, 10)
+ def test_shape(self, device):
+ inp = torch.rand(5, 3, 2, 3, device=device)
+ img = torch.rand(5, 3, 10, 10, device=device)
assert inp.shape == kornia.feature.denormalize_laf(inp, img).shape
- def test_conversion(self):
+ def test_conversion(self, device):
w, h = 10, 5
- expected = torch.tensor([[1, 0, 1], [0, 1, 1]]).float()
+ expected = torch.tensor([[1, 0, 1], [0, 1, 1]], device=device).float()
expected = expected.view(1, 1, 2, 3)
- img = torch.rand(1, 3, h, w)
- lafn = torch.tensor([[0.2, 0, 0.1], [0, 0.2, 0.2]]).float()
+ img = torch.rand(1, 3, h, w, device=device)
+ lafn = torch.tensor([[0.2, 0, 0.1], [0, 0.2, 0.2]], device=device).float()
laf = kornia.feature.denormalize_laf(lafn.view(1, 1, 2, 3), img)
assert_allclose(laf, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 2, 3
- laf = torch.rand(batch_size, channels, height, width)
- img = torch.rand(batch_size, 3, 10, 32)
+ laf = torch.rand(batch_size, channels, height, width, device=device)
+ img = torch.rand(batch_size, 3, 10, 32, device=device)
img = utils.tensor_to_gradcheck_var(img) # to var
laf = utils.tensor_to_gradcheck_var(laf) # to var
assert gradcheck(kornia.feature.denormalize_laf,
@@ -272,17 +273,17 @@ class TestDenormalizeLAF:
class TestGenPatchGrid:
- def test_shape(self):
- laf = torch.rand(5, 3, 2, 3)
- img = torch.rand(5, 3, 10, 10)
+ def test_shape(self, device):
+ laf = torch.rand(5, 3, 2, 3, device=device)
+ img = torch.rand(5, 3, 10, 10, device=device)
PS = 3
from kornia.feature.laf import generate_patch_grid_from_normalized_LAF
grid = generate_patch_grid_from_normalized_LAF(img, laf, PS)
assert grid.shape == (15, 3, 3, 2)
- def test_gradcheck(self):
- laf = torch.rand(5, 3, 2, 3)
- img = torch.rand(5, 3, 10, 10)
+ def test_gradcheck(self, device):
+ laf = torch.rand(5, 3, 2, 3, device=device)
+ img = torch.rand(5, 3, 10, 10, device=device)
PS = 3
from kornia.feature.laf import generate_patch_grid_from_normalized_LAF
img = utils.tensor_to_gradcheck_var(img) # to var
@@ -293,19 +294,19 @@ class TestGenPatchGrid:
class TestExtractPatchesSimple:
- def test_shape(self):
- laf = torch.rand(5, 4, 2, 3)
- img = torch.rand(5, 3, 100, 30)
+ def test_shape(self, device):
+ laf = torch.rand(5, 4, 2, 3, device=device)
+ img = torch.rand(5, 3, 100, 30, device=device)
PS = 10
patches = kornia.feature.extract_patches_simple(img, laf, PS)
assert patches.shape == (5, 4, 3, PS, PS)
# TODO: check what to do to improve timing
- @pytest.mark.skip("The test takes too long to finish.")
- def test_gradcheck(self):
- nlaf = torch.tensor([[0.1, 0.001, 0.5], [0, 0.1, 0.5]]).float()
+ # @pytest.mark.skip("The test takes too long to finish.")
+ def test_gradcheck(self, device):
+ nlaf = torch.tensor([[0.1, 0.001, 0.5], [0, 0.1, 0.5]], device=device).float()
nlaf = nlaf.view(1, 1, 2, 3)
- img = torch.rand(1, 3, 100, 120)
+ img = torch.rand(1, 3, 20, 30, device=device)
PS = 11
img = utils.tensor_to_gradcheck_var(img) # to var
nlaf = utils.tensor_to_gradcheck_var(nlaf) # to var
@@ -315,20 +316,20 @@ class TestExtractPatchesSimple:
class TestExtractPatchesPyr:
- def test_shape(self):
- laf = torch.rand(5, 4, 2, 3)
- img = torch.rand(5, 3, 100, 30)
+ def test_shape(self, device):
+ laf = torch.rand(5, 4, 2, 3, device=device)
+ img = torch.rand(5, 3, 100, 30, device=device)
PS = 10
patches = kornia.feature.extract_patches_from_pyramid(img, laf, PS)
assert patches.shape == (5, 4, 3, PS, PS)
# TODO: check what to do to improve timing
- @pytest.mark.skip("The test takes too long to finish.")
- def test_gradcheck(self):
- nlaf = torch.tensor([[0.1, 0.001, 0.5], [0, 0.1, 0.5]]).float()
+ # @pytest.mark.skip("The test takes too long to finish.")
+ def test_gradcheck(self, device):
+ nlaf = torch.tensor([[0.1, 0.001, 0.5], [0, 0.1, 0.5]], device=device).float()
nlaf = nlaf.view(1, 1, 2, 3)
- img = torch.rand(1, 3, 20, 15)
- PS = 9
+ img = torch.rand(1, 3, 20, 30, device=device)
+ PS = 11
img = utils.tensor_to_gradcheck_var(img) # to var
nlaf = utils.tensor_to_gradcheck_var(nlaf) # to var
assert gradcheck(kornia.feature.extract_patches_from_pyramid,
@@ -337,41 +338,41 @@ class TestExtractPatchesPyr:
class TestLAFIsTouchingBoundary:
- def test_shape(self):
- inp = torch.rand(5, 3, 2, 3)
- img = torch.rand(5, 3, 10, 10)
+ def test_shape(self, device):
+ inp = torch.rand(5, 3, 2, 3, device=device)
+ img = torch.rand(5, 3, 10, 10, device=device)
assert (5, 3) == kornia.feature.laf_is_inside_image(inp, img).shape
- def test_touch(self):
+ def test_touch(self, device):
w, h = 10, 5
- img = torch.rand(1, 3, h, w)
+ img = torch.rand(1, 3, h, w, device=device)
laf = torch.tensor([[[[10, 0, 3], [0, 10, 3]],
- [[1, 0, 5], [0, 1, 2]]]]).float()
- expected = torch.tensor([[False, True]])
+ [[1, 0, 5], [0, 1, 2]]]], device=device).float()
+ expected = torch.tensor([[False, True]], device=device)
assert torch.all(kornia.feature.laf_is_inside_image(laf, img) == expected).item()
class TestGetCreateLAF:
- def test_shape(self):
- xy = torch.ones(1, 3, 2)
- ori = torch.ones(1, 3, 1)
- scale = torch.ones(1, 3, 1, 1)
+ def test_shape(self, device):
+ xy = torch.ones(1, 3, 2, device=device)
+ ori = torch.ones(1, 3, 1, device=device)
+ scale = torch.ones(1, 3, 1, 1, device=device)
laf = kornia.feature.laf_from_center_scale_ori(xy, scale, ori)
assert laf.shape == (1, 3, 2, 3)
- def test_laf(self):
- xy = torch.ones(1, 1, 2)
- ori = torch.zeros(1, 1, 1)
- scale = 5 * torch.ones(1, 1, 1, 1)
- expected = torch.tensor([[[[5, 0, 1], [0, 5, 1]]]]).float()
+ def test_laf(self, device):
+ xy = torch.ones(1, 1, 2, device=device)
+ ori = torch.zeros(1, 1, 1, device=device)
+ scale = 5 * torch.ones(1, 1, 1, 1, device=device)
+ expected = torch.tensor([[[[5, 0, 1], [0, 5, 1]]]], device=device).float()
laf = kornia.feature.laf_from_center_scale_ori(xy, scale, ori)
assert_allclose(laf, expected)
- def test_cross_consistency(self):
+ def test_cross_consistency(self, device):
batch_size, channels = 3, 2
- xy = torch.rand(batch_size, channels, 2)
- ori = torch.rand(batch_size, channels, 1)
- scale = torch.abs(torch.rand(batch_size, channels, 1, 1))
+ xy = torch.rand(batch_size, channels, 2, device=device)
+ ori = torch.rand(batch_size, channels, 1, device=device)
+ scale = torch.abs(torch.rand(batch_size, channels, 1, 1, device=device))
laf = kornia.feature.laf_from_center_scale_ori(xy, scale, ori)
scale2 = kornia.feature.get_laf_scale(laf)
assert_allclose(scale, scale2)
@@ -380,36 +381,36 @@ class TestGetCreateLAF:
ori2 = kornia.feature.get_laf_orientation(laf)
assert_allclose(ori2, ori)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels = 3, 2
- xy = utils.tensor_to_gradcheck_var(torch.rand(batch_size, channels, 2))
- ori = utils.tensor_to_gradcheck_var(torch.rand(batch_size, channels, 1))
- scale = utils.tensor_to_gradcheck_var(torch.abs(torch.rand(batch_size, channels, 1, 1)))
+ xy = utils.tensor_to_gradcheck_var(torch.rand(batch_size, channels, 2, device=device))
+ ori = utils.tensor_to_gradcheck_var(torch.rand(batch_size, channels, 1, device=device))
+ scale = utils.tensor_to_gradcheck_var(torch.abs(torch.rand(batch_size, channels, 1, 1, device=device)))
assert gradcheck(kornia.feature.laf_from_center_scale_ori,
(xy, scale, ori,),
raise_exception=True)
class TestGetLAF3pts:
- def test_shape(self):
- inp = torch.ones(1, 3, 2, 3)
+ def test_shape(self, device):
+ inp = torch.ones(1, 3, 2, 3, device=device)
out = kornia.feature.laf_to_three_points(inp)
assert out.shape == inp.shape
- def test_batch_shape(self):
- inp = torch.ones(5, 3, 2, 3)
+ def test_batch_shape(self, device):
+ inp = torch.ones(5, 3, 2, 3, device=device)
out = kornia.feature.laf_to_three_points(inp)
assert out.shape == inp.shape
- def test_conversion(self):
- inp = torch.tensor([[1, 0, 2], [0, 1, 3]]).float().view(1, 1, 2, 3)
- expected = torch.tensor([[3, 2, 2], [3, 4, 3]]).float().view(1, 1, 2, 3)
+ def test_conversion(self, device):
+ inp = torch.tensor([[1, 0, 2], [0, 1, 3]], device=device).float().view(1, 1, 2, 3)
+ expected = torch.tensor([[3, 2, 2], [3, 4, 3]], device=device).float().view(1, 1, 2, 3)
threepts = kornia.feature.laf_to_three_points(inp)
assert_allclose(threepts, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 3, 2, 2, 3
- inp = torch.rand(batch_size, channels, height, width)
+ inp = torch.rand(batch_size, channels, height, width, device=device)
inp = utils.tensor_to_gradcheck_var(inp) # to var
assert gradcheck(kornia.feature.laf_to_three_points,
(inp,),
@@ -417,32 +418,32 @@ class TestGetLAF3pts:
class TestGetLAFFrom3pts:
- def test_shape(self):
- inp = torch.ones(1, 3, 2, 3)
+ def test_shape(self, device):
+ inp = torch.ones(1, 3, 2, 3, device=device)
out = kornia.feature.laf_from_three_points(inp)
assert out.shape == inp.shape
- def test_batch_shape(self):
- inp = torch.ones(5, 3, 2, 3)
+ def test_batch_shape(self, device):
+ inp = torch.ones(5, 3, 2, 3, device=device)
out = kornia.feature.laf_from_three_points(inp)
assert out.shape == inp.shape
- def test_conversion(self):
- expected = torch.tensor([[1, 0, 2], [0, 1, 3]]).float().view(1, 1, 2, 3)
- inp = torch.tensor([[3, 2, 2], [3, 4, 3]]).float().view(1, 1, 2, 3)
+ def test_conversion(self, device):
+ expected = torch.tensor([[1, 0, 2], [0, 1, 3]], device=device).float().view(1, 1, 2, 3)
+ inp = torch.tensor([[3, 2, 2], [3, 4, 3]], device=device).float().view(1, 1, 2, 3)
threepts = kornia.feature.laf_from_three_points(inp)
assert_allclose(threepts, expected)
- def test_cross_consistency(self):
+ def test_cross_consistency(self, device):
batch_size, channels, height, width = 3, 2, 2, 3
- inp = torch.rand(batch_size, channels, height, width)
+ inp = torch.rand(batch_size, channels, height, width, device=device)
inp_2 = kornia.feature.laf_from_three_points(inp)
inp_2 = kornia.feature.laf_to_three_points(inp_2)
assert_allclose(inp_2, inp)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 3, 2, 2, 3
- inp = torch.rand(batch_size, channels, height, width)
+ inp = torch.rand(batch_size, channels, height, width, device=device)
inp = utils.tensor_to_gradcheck_var(inp) # to var
assert gradcheck(kornia.feature.laf_from_three_points,
(inp,),
diff --git a/test/feature/test_local_features_orientation.py b/test/feature/test_local_features_orientation.py
index 959a4da00..9468988d1 100644
--- a/test/feature/test_local_features_orientation.py
+++ b/test/feature/test_local_features_orientation.py
@@ -1,6 +1,7 @@
import pytest
import kornia.testing as utils # test utils
import kornia
+from test.common import device
from torch.testing import assert_allclose
from torch.autograd import gradcheck
@@ -8,109 +9,109 @@ from kornia.feature.orientation import *
class TestPassLAF:
- def test_shape(self):
- inp = torch.rand(1, 1, 32, 32)
- laf = torch.rand(1, 1, 2, 3)
- ori = PassLAF()
+ def test_shape(self, device):
+ inp = torch.rand(1, 1, 32, 32, device=device)
+ laf = torch.rand(1, 1, 2, 3, device=device)
+ ori = PassLAF().to(device)
out = ori(laf, inp)
assert out.shape == laf.shape
- def test_shape_batch(self):
- inp = torch.rand(2, 1, 32, 32)
- laf = torch.rand(2, 34, 2, 3)
- ori = PassLAF()
+ def test_shape_batch(self, device):
+ inp = torch.rand(2, 1, 32, 32, device=device)
+ laf = torch.rand(2, 34, 2, 3, device=device)
+ ori = PassLAF().to(device)
out = ori(laf, inp)
assert out.shape == laf.shape
- def test_print(self):
+ def test_print(self, device):
sift = PassLAF()
sift.__repr__()
- def test_pass(self):
- inp = torch.rand(1, 1, 32, 32)
- laf = torch.rand(1, 1, 2, 3)
- ori = PassLAF()
+ def test_pass(self, device):
+ inp = torch.rand(1, 1, 32, 32, device=device)
+ laf = torch.rand(1, 1, 2, 3, device=device)
+ ori = PassLAF().to(device)
out = ori(laf, inp)
assert_allclose(out, laf)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 1, 21, 21
- patches = torch.rand(batch_size, channels, height, width)
+ patches = torch.rand(batch_size, channels, height, width, device=device)
patches = utils.tensor_to_gradcheck_var(patches) # to var
laf = torch.rand(batch_size, 4, 2, 3)
- assert gradcheck(PassLAF(), (patches, laf),
+ assert gradcheck(PassLAF().to(device), (patches, laf),
raise_exception=True)
class TestPatchDominantGradientOrientation:
- def test_shape(self):
- inp = torch.rand(1, 1, 32, 32)
- ori = PatchDominantGradientOrientation(32)
+ def test_shape(self, device):
+ inp = torch.rand(1, 1, 32, 32, device=device)
+ ori = PatchDominantGradientOrientation(32).to(device)
ang = ori(inp)
assert ang.shape == torch.Size([1])
- def test_shape_batch(self):
- inp = torch.rand(10, 1, 32, 32)
- ori = PatchDominantGradientOrientation(32)
+ def test_shape_batch(self, device):
+ inp = torch.rand(10, 1, 32, 32, device=device)
+ ori = PatchDominantGradientOrientation(32).to(device)
ang = ori(inp)
assert ang.shape == torch.Size([10])
- def test_print(self):
+ def test_print(self, device):
sift = PatchDominantGradientOrientation(32)
sift.__repr__()
- def test_toy(self):
- ori = PatchDominantGradientOrientation(19)
- inp = torch.zeros(1, 1, 19, 19)
+ def test_toy(self, device):
+ ori = PatchDominantGradientOrientation(19).to(device)
+ inp = torch.zeros(1, 1, 19, 19, device=device)
inp[:, :, :10, :] = 1
ang = ori(inp)
- expected = torch.tensor([90.])
+ expected = torch.tensor([90.], device=device)
assert_allclose(kornia.rad2deg(ang), expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 1, 13, 13
- ori = PatchDominantGradientOrientation(width)
- patches = torch.rand(batch_size, channels, height, width)
+ ori = PatchDominantGradientOrientation(width).to(device)
+ patches = torch.rand(batch_size, channels, height, width, device=device)
patches = utils.tensor_to_gradcheck_var(patches) # to var
assert gradcheck(ori, (patches, ),
raise_exception=True)
class TestLAFOrienter:
- def test_shape(self):
- inp = torch.rand(1, 1, 32, 32)
- laf = torch.rand(1, 1, 2, 3)
- ori = LAFOrienter()
+ def test_shape(self, device):
+ inp = torch.rand(1, 1, 32, 32, device=device)
+ laf = torch.rand(1, 1, 2, 3, device=device)
+ ori = LAFOrienter().to(device)
out = ori(laf, inp)
assert out.shape == laf.shape
- def test_shape_batch(self):
- inp = torch.rand(2, 1, 32, 32)
- laf = torch.rand(2, 34, 2, 3)
- ori = LAFOrienter()
+ def test_shape_batch(self, device):
+ inp = torch.rand(2, 1, 32, 32, device=device)
+ laf = torch.rand(2, 34, 2, 3, device=device)
+ ori = LAFOrienter().to(device)
out = ori(laf, inp)
assert out.shape == laf.shape
- def test_print(self):
+ def test_print(self, device):
sift = LAFOrienter()
sift.__repr__()
- def test_toy(self):
- ori = LAFOrienter(32)
- inp = torch.zeros(1, 1, 19, 19)
+ def test_toy(self, device):
+ ori = LAFOrienter(32).to(device)
+ inp = torch.zeros(1, 1, 19, 19, device=device)
inp[:, :, :10, :] = 1
- laf = torch.tensor([[[[0, 5., 8.], [5.0, 0., 8.]]]])
+ laf = torch.tensor([[[[0, 5., 8.], [5.0, 0., 8.]]]], device=device)
new_laf = ori(laf, inp)
- expected = torch.tensor([[[[5., 0., 8.], [0., 5., 8.]]]])
+ expected = torch.tensor([[[[5., 0., 8.], [0., 5., 8.]]]], device=device)
assert_allclose(new_laf, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 1, 21, 21
- patches = torch.rand(batch_size, channels, height, width).float()
+ patches = torch.rand(batch_size, channels, height, width, device=device).float()
patches = utils.tensor_to_gradcheck_var(patches) # to var
- laf = torch.ones(batch_size, 2, 2, 3).float()
+ laf = torch.ones(batch_size, 2, 2, 3, device=device).float()
laf[:, :, 0, 1] = 0
laf[:, :, 1, 0] = 0
laf = utils.tensor_to_gradcheck_var(laf) # to var
- assert gradcheck(LAFOrienter(8), (laf, patches),
+ assert gradcheck(LAFOrienter(8).to(device), (laf, patches),
raise_exception=True, rtol=1e-3, atol=1e-3)
diff --git a/test/feature/test_responces_local_features.py b/test/feature/test_responces_local_features.py
index c8fc9e425..537aacf80 100644
--- a/test/feature/test_responces_local_features.py
+++ b/test/feature/test_responces_local_features.py
@@ -2,6 +2,7 @@ import pytest
import kornia as kornia
import kornia.testing as utils # test utils
+from test.common import device
import torch
from torch.testing import assert_allclose
@@ -9,17 +10,17 @@ from torch.autograd import gradcheck
class TestCornerHarris:
- def test_shape(self):
- inp = torch.ones(1, 3, 4, 4)
- harris = kornia.feature.CornerHarris(k=0.04)
+ def test_shape(self, device):
+ inp = torch.ones(1, 3, 4, 4, device=device)
+ harris = kornia.feature.CornerHarris(k=0.04).to(device)
assert harris(inp).shape == (1, 3, 4, 4)
- def test_shape_batch(self):
- inp = torch.zeros(2, 6, 4, 4)
- harris = kornia.feature.CornerHarris(k=0.04)
+ def test_shape_batch(self, device):
+ inp = torch.zeros(2, 6, 4, 4, device=device)
+ harris = kornia.feature.CornerHarris(k=0.04).to(device)
assert harris(inp).shape == (2, 6, 4, 4)
- def test_corners(self):
+ def test_corners(self, device):
inp = torch.tensor([[[
[0., 0., 0., 0., 0., 0., 0.],
[0., 1., 1., 1., 1., 1., 0.],
@@ -28,7 +29,7 @@ class TestCornerHarris:
[0., 1., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 1., 0.],
[0., 0., 0., 0., 0., 0., 0.],
- ]]]).float()
+ ]]], device=device).float()
expected = torch.tensor([[[
[0.001233, 0.003920, 0.001985, 0.000000, 0.001985, 0.003920, 0.001233],
@@ -37,12 +38,13 @@ class TestCornerHarris:
[0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000],
[0.001985, 0.003976, 0.002943, 0.000000, 0.002943, 0.003976, 0.001985],
[0.003920, 0.006507, 0.003976, 0.000000, 0.003976, 0.006507, 0.003920],
- [0.001233, 0.003920, 0.001985, 0.000000, 0.001985, 0.003920, 0.001233]]]]).float()
- harris = kornia.feature.CornerHarris(k=0.04)
+ [0.001233, 0.003920, 0.001985, 0.000000, 0.001985, 0.003920, 0.001233]]]],
+ device=device).float()
+ harris = kornia.feature.CornerHarris(k=0.04).to(device)
scores = harris(inp)
assert_allclose(scores, expected, atol=1e-4, rtol=1e-3)
- def test_corners_batch(self):
+ def test_corners_batch(self, device):
inp = torch.tensor([[
[0., 0., 0., 0., 0., 0., 0.],
[0., 1., 1., 1., 1., 1., 0.],
@@ -59,7 +61,7 @@ class TestCornerHarris:
[0., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.],
- ]]).repeat(2, 1, 1, 1)
+ ]], device=device).repeat(2, 1, 1, 1)
expected = torch.tensor([[
[0.001233, 0.003920, 0.001985, 0.000000, 0.001985, 0.003920, 0.001233],
[0.003920, 0.006507, 0.003976, 0.000000, 0.003976, 0.006507, 0.003920],
@@ -76,42 +78,42 @@ class TestCornerHarris:
[0.003920, 0.006507, 0.003976, 0.003976, 0.006507, 0.001526, 0.000008],
[0.000589, 0.001526, 0.000542, 0.000542, 0.001526, 0.000277, 0.000000],
[0.000000, 0.000008, 0.000000, 0.000000, 0.000008, 0.000000, 0.000000]
- ]]).repeat(2, 1, 1, 1)
+ ]], device=device).repeat(2, 1, 1, 1)
scores = kornia.feature.harris_response(inp, k=0.04)
assert_allclose(scores, expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
k = 0.04
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width, device=device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.feature.harris_response, (img, k),
- raise_exception=True)
+ raise_exception=True, nondet_tol=1e-4)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
+ def test_jit(self, device):
@torch.jit.script
def op_script(input, k):
return kornia.feature.harris_response(input, k)
k = torch.tensor(0.04)
- img = torch.rand(2, 3, 4, 5)
+ img = torch.rand(2, 3, 4, 5, device=device)
actual = op_script(img, k)
expected = kornia.feature.harris_response(img, k)
assert_allclose(actual, expected)
class TestCornerGFTT:
- def test_shape(self):
- inp = torch.ones(1, 3, 4, 4)
- shi_tomasi = kornia.feature.CornerGFTT()
+ def test_shape(self, device):
+ inp = torch.ones(1, 3, 4, 4, device=device)
+ shi_tomasi = kornia.feature.CornerGFTT().to(device)
assert shi_tomasi(inp).shape == (1, 3, 4, 4)
- def test_shape_batch(self):
- inp = torch.zeros(2, 6, 4, 4)
- shi_tomasi = kornia.feature.CornerGFTT()
+ def test_shape_batch(self, device):
+ inp = torch.zeros(2, 6, 4, 4, device=device)
+ shi_tomasi = kornia.feature.CornerGFTT().to(device)
assert shi_tomasi(inp).shape == (2, 6, 4, 4)
- def test_corners(self):
+ def test_corners(self, device):
inp = torch.tensor([[[
[0., 0., 0., 0., 0., 0., 0.],
[0., 1., 1., 1., 1., 1., 0.],
@@ -120,7 +122,7 @@ class TestCornerGFTT:
[0., 1., 1., 1., 1., 1., 0.],
[0., 1., 1., 1., 1., 1., 0.],
[0., 0., 0., 0., 0., 0., 0.],
- ]]]).float()
+ ]]], device=device).float()
expected = torch.tensor([[[
[0.01548, 0.03340, 0.01944, 0.00000, 0.01944, 0.03340, 0.01548],
@@ -129,12 +131,12 @@ class TestCornerGFTT:
[0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000],
[0.01944, 0.03388, 0.04974, 0.00000, 0.04974, 0.03388, 0.01944],
[0.03340, 0.05748, 0.03388, 0.00000, 0.03388, 0.05748, 0.03340],
- [0.01548, 0.03340, 0.01944, 0.00000, 0.01944, 0.03340, 0.01548]]]]).float()
- shi_tomasi = kornia.feature.CornerGFTT()
+ [0.01548, 0.03340, 0.01944, 0.00000, 0.01944, 0.03340, 0.01548]]]], device=device).float()
+ shi_tomasi = kornia.feature.CornerGFTT().to(device)
scores = shi_tomasi(inp)
assert_allclose(scores, expected, atol=1e-4, rtol=1e-3)
- def test_corners_batch(self):
+ def test_corners_batch(self, device):
inp = torch.tensor([[
[0., 0., 0., 0., 0., 0., 0.],
[0., 1., 1., 1., 1., 1., 0.],
@@ -151,7 +153,7 @@ class TestCornerGFTT:
[0., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.],
- ]]).repeat(2, 1, 1, 1)
+ ]], device=device).repeat(2, 1, 1, 1)
expected = torch.tensor([[
[0.01548, 0.03340, 0.01944, 0.00000, 0.01944, 0.03340, 0.01548],
[0.03340, 0.05748, 0.03388, 0.00000, 0.03388, 0.05748, 0.03340],
@@ -168,41 +170,41 @@ class TestCornerGFTT:
[0.03340, 0.05748, 0.03388, 0.03388, 0.05748, 0.01981, 0.00348],
[0.01090, 0.01981, 0.01070, 0.01070, 0.01981, 0.00774, 0.00121],
[0.00136, 0.00348, 0.00193, 0.00193, 0.00348, 0.00121, 0.00000]
- ]]).repeat(2, 1, 1, 1)
- shi_tomasi = kornia.feature.CornerGFTT()
+ ]], device=device).repeat(2, 1, 1, 1)
+ shi_tomasi = kornia.feature.CornerGFTT().to(device)
scores = shi_tomasi(inp)
assert_allclose(scores, expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width, device=device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.feature.gftt_response, (img),
- raise_exception=True)
+ raise_exception=True, nondet_tol=1e-4)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
+ def test_jit(self, device):
@torch.jit.script
def op_script(input):
return kornia.feature.gftt_response(input)
- img = torch.rand(2, 3, 4, 5)
+ img = torch.rand(2, 3, 4, 5, device=device)
actual = op_script(img)
expected = kornia.feature.gftt_response(img)
assert_allclose(actual, expected)
class TestBlobHessian:
- def test_shape(self):
- inp = torch.ones(1, 3, 4, 4)
- shi_tomasi = kornia.feature.BlobHessian()
+ def test_shape(self, device):
+ inp = torch.ones(1, 3, 4, 4, device=device)
+ shi_tomasi = kornia.feature.BlobHessian().to(device)
assert shi_tomasi(inp).shape == (1, 3, 4, 4)
- def test_shape_batch(self):
- inp = torch.zeros(2, 6, 4, 4)
- shi_tomasi = kornia.feature.BlobHessian()
+ def test_shape_batch(self, device):
+ inp = torch.zeros(2, 6, 4, 4, device=device)
+ shi_tomasi = kornia.feature.BlobHessian().to(device)
assert shi_tomasi(inp).shape == (2, 6, 4, 4)
- def test_blobs_batch(self):
+ def test_blobs_batch(self, device):
inp = torch.tensor([[
[0., 0., 0., 0., 0., 0., 0.],
[0., 1., 1., 1., 0., 0., 0.],
@@ -219,7 +221,7 @@ class TestBlobHessian:
[0., 0., 0., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.],
- ]]).repeat(2, 1, 1, 1)
+ ]], device=device).repeat(2, 1, 1, 1)
expected = torch.tensor([[
[0.0564, 0.0759, 0.0342, 0.0759, 0.0564, 0.0057, 0.0000],
[0.0759, 0.0330, 0.0752, 0.0330, 0.0759, 0.0096, 0.0000],
@@ -236,24 +238,24 @@ class TestBlobHessian:
[0.0057, 0.0033, 0.0123, 0.0688, 0.0688, 0.0522, 0.0080],
[0.0000, 0.0057, 0.0564, 0.0522, 0.0522, 0.0564, 0.0057],
[0.0000, 0.0005, 0.0057, 0.0080, 0.0080, 0.0057, 0.0005]
- ]]).repeat(2, 1, 1, 1)
- shi_tomasi = kornia.feature.BlobHessian()
+ ]], device=device).repeat(2, 1, 1, 1)
+ shi_tomasi = kornia.feature.BlobHessian().to(device)
scores = shi_tomasi(inp)
assert_allclose(scores, expected, atol=1e-4, rtol=1e-4)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.rand(batch_size, channels, height, width)
+ img = torch.rand(batch_size, channels, height, width, device=device)
img = utils.tensor_to_gradcheck_var(img) # to var
assert gradcheck(kornia.feature.hessian_response, (img),
- raise_exception=True)
+ raise_exception=True, nondet_tol=1e-4)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit(self):
+ def test_jit(self, device):
@torch.jit.script
def op_script(input):
return kornia.feature.hessian_response(input)
- img = torch.rand(2, 3, 4, 5)
+ img = torch.rand(2, 3, 4, 5, device=device)
actual = op_script(img)
expected = kornia.feature.hessian_response(img)
assert_allclose(actual, expected)
diff --git a/test/feature/test_scale_space_detector.py b/test/feature/test_scale_space_detector.py
index bce455de8..dce5573ae 100644
--- a/test/feature/test_scale_space_detector.py
+++ b/test/feature/test_scale_space_detector.py
@@ -1,6 +1,7 @@
import pytest
import kornia.testing as utils # test utils
import kornia
+from test.common import device
from torch.testing import assert_allclose
from torch.autograd import gradcheck
@@ -8,42 +9,42 @@ from kornia.feature.scale_space_detector import *
class TestScaleSpaceDetector:
- def test_shape(self):
- inp = torch.rand(1, 1, 32, 32)
+ def test_shape(self, device):
+ inp = torch.rand(1, 1, 32, 32, device=device)
n_feats = 10
- det = ScaleSpaceDetector(n_feats)
+ det = ScaleSpaceDetector(n_feats).to(device)
lafs, resps = det(inp)
assert lafs.shape == torch.Size([1, n_feats, 2, 3])
assert resps.shape == torch.Size([1, n_feats])
- def test_shape_batch(self):
- inp = torch.rand(7, 1, 32, 32)
+ def test_shape_batch(self, device):
+ inp = torch.rand(7, 1, 32, 32, device=device)
n_feats = 10
- det = ScaleSpaceDetector(n_feats)
+ det = ScaleSpaceDetector(n_feats).to(device)
lafs, resps = det(inp)
assert lafs.shape == torch.Size([7, n_feats, 2, 3])
assert resps.shape == torch.Size([7, n_feats])
- def test_print(self):
+ def test_print(self, device):
sift = ScaleSpaceDetector()
sift.__repr__()
- def test_toy(self):
- inp = torch.zeros(1, 1, 33, 33)
+ def test_toy(self, device):
+ inp = torch.zeros(1, 1, 33, 33, device=device)
inp[:, :, 13:-13, 13:-13] = 1.0
n_feats = 1
det = ScaleSpaceDetector(n_feats,
resp_module=kornia.feature.BlobHessian(),
- mr_size=3.0)
+ mr_size=3.0).to(device)
lafs, resps = det(inp)
- expected_laf = torch.tensor([[[[6.0548, 0.0000, 16.0], [0.0, 6.0548, 16.0]]]])
- expected_resp = torch.tensor([[0.0806]])
+ expected_laf = torch.tensor([[[[6.0548, 0.0000, 16.0], [0.0, 6.0548, 16.0]]]], device=device)
+ expected_resp = torch.tensor([[0.0806]], device=device)
assert_allclose(expected_laf, lafs)
assert_allclose(expected_resp, resps)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 1, 31, 21
- patches = torch.rand(batch_size, channels, height, width)
+ patches = torch.rand(batch_size, channels, height, width, device=device)
patches = utils.tensor_to_gradcheck_var(patches) # to var
- assert gradcheck(ScaleSpaceDetector(2), (patches),
- raise_exception=True)
+ assert gradcheck(ScaleSpaceDetector(2).to(device), (patches),
+ raise_exception=True, nondet_tol=1e-4)
diff --git a/test/feature/test_siftdesc.py b/test/feature/test_siftdesc.py
index b44a341e9..3456773d0 100644
--- a/test/feature/test_siftdesc.py
+++ b/test/feature/test_siftdesc.py
@@ -1,5 +1,6 @@
import pytest
import kornia.testing as utils # test utils
+from test.common import device
from torch.testing import assert_allclose
from torch.autograd import gradcheck
@@ -25,43 +26,43 @@ def test_get_sift_bin_ksize_stride_pad(ps,
class TestSIFTDescriptor:
- def test_shape(self):
- inp = torch.ones(1, 1, 32, 32)
- sift = SIFTDescriptor(32)
+ def test_shape(self, device):
+ inp = torch.ones(1, 1, 32, 32, device=device)
+ sift = SIFTDescriptor(32).to(device)
out = sift(inp)
assert out.shape == (1, 128)
- def test_batch_shape(self):
- inp = torch.ones(2, 1, 15, 15)
- sift = SIFTDescriptor(15)
+ def test_batch_shape(self, device):
+ inp = torch.ones(2, 1, 15, 15, device=device)
+ sift = SIFTDescriptor(15).to(device)
out = sift(inp)
assert out.shape == (2, 128)
- def test_batch_shape_non_std(self):
- inp = torch.ones(3, 1, 19, 19)
- sift = SIFTDescriptor(19, 5, 3)
+ def test_batch_shape_non_std(self, device):
+ inp = torch.ones(3, 1, 19, 19, device=device)
+ sift = SIFTDescriptor(19, 5, 3).to(device)
out = sift(inp)
assert out.shape == (3, (3 ** 2) * 5)
- def test_print(self):
+ def test_print(self, device):
sift = SIFTDescriptor(41)
sift.__repr__()
- def test_toy(self):
- patch = torch.ones(1, 1, 6, 6).float()
+ def test_toy(self, device):
+ patch = torch.ones(1, 1, 6, 6, device=device).float()
patch[0, 0, :, 3:] = 0
sift = SIFTDescriptor(6,
num_ang_bins=4,
num_spatial_bins=1,
clipval=0.2,
- rootsift=False)
+ rootsift=False).to(device)
out = sift(patch)
- expected = torch.tensor([[0, 0, 1., 0]])
+ expected = torch.tensor([[0, 0, 1., 0]], device=device)
assert_allclose(out, expected, atol=1e-3, rtol=1e-3)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 1, 13, 13
- patches = torch.rand(batch_size, channels, height, width)
+ patches = torch.rand(batch_size, channels, height, width, device=device)
patches = utils.tensor_to_gradcheck_var(patches) # to var
assert gradcheck(sift_describe, (patches, 13),
- raise_exception=True)
+ raise_exception=True, nondet_tol=1e-4)