Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

[Fix] Missing device initializations (#672)

Adrian749bd0254e07
diff --git a/Makefile b/Makefile
index 7a21ad601..92ecd2188 100644
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,9 @@
.PHONY: test test-cpu test-cuda lint mypy build-docs install uninstall FORCE
test: mypy lint build-docs test-all
-
+# TODO: Add cuda-float16 when #649 is solved
test-all: FORCE
- pytest -v --device all --dtype all --cov=kornia test/ --flake8 --mypy
+ pytest -v --device all --dtype float32,float64 --cov=kornia test/ --flake8 --mypy
test-cpu: FORCE
pytest -v --device cpu --dtype all --cov=kornia test/ --flake8 --mypy
diff --git a/kornia/enhance/adjust.py b/kornia/enhance/adjust.py
index 35d1718d3..de9680492 100644
--- a/kornia/enhance/adjust.py
+++ b/kornia/enhance/adjust.py
@@ -445,7 +445,7 @@ def _scale_channel(im):
# and then normalization by step.
lut = (torch.cumsum(histo, 0) + (step // 2)) // step
# Shift lut, prepending with 0.
- lut = torch.cat([torch.zeros(1), lut[:-1]])
+ lut = torch.cat([torch.zeros(1, device=lut.device), lut[:-1]])
# Clip the counts to be in range. This is done
# in the C code for image.point.
return torch.clamp(lut, 0, 255)
diff --git a/kornia/geometry/transform/affwarp.py b/kornia/geometry/transform/affwarp.py
index e619f97bf..39af9da5d 100644
--- a/kornia/geometry/transform/affwarp.py
+++ b/kornia/geometry/transform/affwarp.py
@@ -101,7 +101,7 @@ def _compute_translation_matrix(translation: torch.Tensor) -> torch.Tensor:
def _compute_scaling_matrix(scale: torch.Tensor,
center: torch.Tensor) -> torch.Tensor:
"""Computes affine matrix for scaling."""
- angle: torch.Tensor = torch.zeros(scale.shape[:1])
+ angle: torch.Tensor = torch.zeros(scale.shape[:1], device=scale.device, dtype=scale.dtype)
matrix: torch.Tensor = get_rotation_matrix2d(center, angle, scale)
return matrix
diff --git a/kornia/geometry/transform/imgwarp.py b/kornia/geometry/transform/imgwarp.py
index 804871562..d9182ca8f 100644
--- a/kornia/geometry/transform/imgwarp.py
+++ b/kornia/geometry/transform/imgwarp.py
@@ -349,9 +349,11 @@ def get_rotation_matrix2d(
.format(center.shape, angle.shape, scale.shape))
# convert angle and apply scale
rotation_matrix: torch.Tensor = angle_to_rotation_matrix(angle)
- scaling_matrix: torch.Tensor = torch.zeros((2, 2)).fill_diagonal_(1).repeat(rotation_matrix.size(0), 1, 1)
+ scaling_matrix: torch.Tensor = torch.zeros(
+ (2, 2), device=rotation_matrix.device, dtype=rotation_matrix.dtype).fill_diagonal_(1).repeat(
+ rotation_matrix.size(0), 1, 1)
scaling_matrix = scaling_matrix * scale.unsqueeze(dim=2).repeat(1, 1, 2)
- scaled_rotation: torch.Tensor = rotation_matrix @ scaling_matrix.to(rotation_matrix)
+ scaled_rotation: torch.Tensor = rotation_matrix @ scaling_matrix
alpha: torch.Tensor = scaled_rotation[:, 0, 0]
beta: torch.Tensor = scaled_rotation[:, 0, 1]
diff --git a/test/augmentation/test_augmentation_mix.py b/test/augmentation/test_augmentation_mix.py
index 51b6951d2..51578a673 100644
--- a/test/augmentation/test_augmentation_mix.py
+++ b/test/augmentation/test_augmentation_mix.py
@@ -79,7 +79,7 @@ class TestRandomMixUp:
assert_allclose(out_image, expected, rtol=1e-4, atol=1e-4)
assert (out_label[:, 0] == label).all()
- assert (out_label[:, 1] == torch.tensor([0, 1])).all()
+ assert (out_label[:, 1] == torch.tensor([0, 1], device=device)).all()
assert_allclose(out_label[:, 2], lam, rtol=1e-4, atol=1e-4)
def test_random_mixup_same_on_batch(self, device, dtype):
@@ -172,8 +172,8 @@ class TestRandomCutMix:
assert_allclose(out_image, expected, rtol=1e-4, atol=1e-4)
assert (out_label[0, :, 0] == label).all()
- assert (out_label[0, :, 1] == torch.tensor([0, 1])).all()
- assert (out_label[0, :, 2] == torch.tensor([0., 0.])).all()
+ assert (out_label[0, :, 1] == torch.tensor([0, 1], device=device)).all()
+ assert (out_label[0, :, 2] == torch.tensor([0., 0.], device=device)).all()
def test_random_mixup_num2(self, device, dtype):
torch.manual_seed(76)
@@ -196,7 +196,7 @@ class TestRandomCutMix:
assert_allclose(out_image, expected, rtol=1e-4, atol=1e-4)
assert (out_label[:, :, 0] == label).all()
- assert (out_label[:, :, 1] == torch.tensor([[1, 0], [1, 0], [1, 0], [1, 0], [0, 1]])).all()
+ assert (out_label[:, :, 1] == torch.tensor([[1, 0], [1, 0], [1, 0], [1, 0], [0, 1]], device=device)).all()
assert_allclose(out_label[:, :, 2], torch.tensor([[0., 0.], [0., 0.], [0., 0.0833], [0., 0.], [0.5, 0.3333]],
device=device, dtype=dtype), rtol=1e-4, atol=1e-4)
diff --git a/test/augmentation/test_perspective.py b/test/augmentation/test_perspective.py
index 8bf6daf6d..c830e7e7d 100644
--- a/test/augmentation/test_perspective.py
+++ b/test/augmentation/test_perspective.py
@@ -13,7 +13,7 @@ class TestPerspective:
def test_smoke(self, device):
x_data = torch.rand(1, 2, 3, 4).to(device)
- batch_prob = torch.rand(1) < 0.5
+ batch_prob = torch.rand(1, device=device) < 0.5
start_points = torch.rand(1, 4, 2).to(device)
end_points = torch.rand(1, 4, 2).to(device)
diff --git a/test/enhance/test_adjust.py b/test/enhance/test_adjust.py
index 56ae43e74..5ef319b11 100644
--- a/test/enhance/test_adjust.py
+++ b/test/enhance/test_adjust.py
@@ -628,7 +628,7 @@ class TestEqualize:
0.0000, 0.07843, 0.15686, 0.2353, 0.3137, 0.3922, 0.4706, 0.5490, 0.6275,
0.7059, 0.7843, 0.8627, 0.9412, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000
- ])
+ ], device=device)
expected = self.build_input(channels, height, width, bs=1, row=row_expected)
expected = expected.to(device)
@@ -646,7 +646,7 @@ class TestEqualize:
0.0000, 0.07843, 0.15686, 0.2353, 0.3137, 0.3922, 0.4706, 0.5490, 0.6275,
0.7059, 0.7843, 0.8627, 0.9412, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000,
1.0000, 1.0000
- ])
+ ], device=device)
expected = self.build_input(channels, height, width, bs, row=row_expected)
expected = expected.to(device)
@@ -695,11 +695,11 @@ class TestEqualize3D:
bs, channels, depth, height, width = 1, 3, 6, 10, 10
inputs3d = self.build_input(channels, depth, height, width).squeeze(dim=0)
- inputs3d.to(device)
+ inputs3d = inputs3d.to(device)
row_expected = torch.tensor([
0.0000, 0.11764, 0.2353, 0.3529, 0.4706, 0.5882, 0.7059, 0.8235, 0.9412, 1.0000
- ])
+ ], device=device)
expected = self.build_input(channels, depth, height, width, bs=1, row=row_expected)
expected = expected.to(device)
@@ -715,7 +715,7 @@ class TestEqualize3D:
row_expected = torch.tensor([
0.0000, 0.11764, 0.2353, 0.3529, 0.4706, 0.5882, 0.7059, 0.8235, 0.9412, 1.0000
- ])
+ ], device=device)
expected = self.build_input(channels, depth, height, width, bs, row=row_expected)
expected = expected.to(device)
diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.py
index 05edbca79..cac86fbb7 100644
--- a/test/geometry/test_conversions.py
+++ b/test/geometry/test_conversions.py
@@ -369,7 +369,7 @@ def test_deg2rad(batch_shape, device):
class TestConvertPointsToHomogeneous:
- def test_convert_points(self, device):
+ def test_convert_points(self, device, dtype):
# generate input data
points_h = torch.tensor([
[1., 2., 1.],
@@ -377,7 +377,7 @@ class TestConvertPointsToHomogeneous:
[2., 1., 0.],
[-1., -2., -1.],
[0., 1., -2.],
- ]).to(device)
+ ], device=device, dtype=dtype)
expected = torch.tensor([
[1., 2., 1., 1.],
@@ -385,13 +385,13 @@ class TestConvertPointsToHomogeneous:
[2., 1., 0., 1.],
[-1., -2., -1., 1.],
[0., 1., -2., 1.],
- ]).to(device)
+ ], device=device, dtype=dtype)
# to euclidean
points = kornia.convert_points_to_homogeneous(points_h)
assert_allclose(points, expected)
- def test_convert_points_batch(self, device):
+ def test_convert_points_batch(self, device, dtype):
# generate input data
points_h = torch.tensor([[
[2., 1., 0.],
@@ -399,7 +399,7 @@ class TestConvertPointsToHomogeneous:
[0., 1., 2.],
], [
[0., 1., -2.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
expected = torch.tensor([[
[2., 1., 0., 1.],
@@ -407,7 +407,7 @@ class TestConvertPointsToHomogeneous:
[0., 1., 2., 1.],
], [
[0., 1., -2., 1.],
- ]]).to(device)
+ ]], device=device, dtype=dtype)
# to euclidean
points = kornia.convert_points_to_homogeneous(points_h)
diff --git a/test/geometry/test_linalg.py b/test/geometry/test_linalg.py
index af9c97ce8..a135448b7 100644
--- a/test/geometry/test_linalg.py
+++ b/test/geometry/test_linalg.py
@@ -79,17 +79,17 @@ class TestTransformPoints:
assert gradcheck(kornia.transform_points, (dst_homo_src, points_src,),
raise_exception=True)
- @pytest.mark.skip(reason="turn off all jit for a while")
def test_jit(self, device):
@torch.jit.script
def op_script(transform, points):
return kornia.transform_points(transform, points)
- points = torch.ones(1, 2, 2).to(device)
- transform = torch.eye(3)[None].to(device)
+ points = torch.ones(1, 2, 2, device=device)
+ transform = kornia.eye_like(3, points)
+ op = kornia.transform_points
+ op_script = torch.jit.script(op)
actual = op_script(transform, points)
- expected = kornia.transform_points(transform, points)
-
+ expected = op(transform, points)
assert_allclose(actual, expected)
@pytest.mark.skip(reason="turn off all jit for a while")
diff --git a/test/geometry/transform/test_affine.py b/test/geometry/transform/test_affine.py
index 4dd0237d2..244453056 100644
--- a/test/geometry/transform/test_affine.py
+++ b/test/geometry/transform/test_affine.py
@@ -470,6 +470,12 @@ class TestAffine2d:
kornia.Affine(angle, translation)
def test_affine_rotate(self, device):
+ # TODO: Remove when #666 is implemented
+ if device.type == 'cuda':
+ pytest.skip(
+ "Currently breaks in CUDA."
+ "See https://github.com/kornia/kornia/issues/666"
+ )
torch.manual_seed(0)
angle = torch.rand(1, device=device) * 90.0
input = torch.rand(1, 2, 3, 4, device=device)
@@ -480,6 +486,12 @@ class TestAffine2d:
assert_allclose(actual, expected)
def test_affine_translate(self, device):
+ # TODO: Remove when #666 is implemented
+ if device.type == 'cuda':
+ pytest.skip(
+ "Currently breaks in CUDA."
+ "See https://github.com/kornia/kornia/issues/666"
+ )
torch.manual_seed(0)
translation = torch.rand(1, 2, device=device) * 2.0
input = torch.rand(1, 2, 3, 4, device=device)
@@ -490,6 +502,12 @@ class TestAffine2d:
assert_allclose(actual, expected)
def test_affine_scale(self, device):
+ # TODO: Remove when #666 is implemented
+ if device.type == 'cuda':
+ pytest.skip(
+ "Currently breaks in CUDA."
+ "See https://github.com/kornia/kornia/issues/666"
+ )
torch.manual_seed(0)
_scale_factor = torch.rand(1, device=device) * 2.0
scale_factor = torch.stack([_scale_factor, _scale_factor], dim=1)
@@ -515,6 +533,12 @@ class TestAffine2d:
assert_allclose(actual, expected)
def test_affine_rotate_translate(self, device):
+ # TODO: Remove when #666 is implemented
+ if device.type == 'cuda':
+ pytest.skip(
+ "Currently breaks in CUDA."
+ "See https://github.com/kornia/kornia/issues/666"
+ )
batch_size = 2
input = torch.tensor(
@@ -522,7 +546,7 @@ class TestAffine2d:
).repeat(batch_size, 1, 1, 1)
angle = torch.tensor(180.0, device=device).repeat(batch_size)
- translation = torch.tensor([1.0, 0.0]).repeat(batch_size, 1)
+ translation = torch.tensor([1.0, 0.0], device=device).repeat(batch_size, 1)
expected = torch.tensor(
[[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 1.0, 0.0, 0.0]]], device=device,