Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement cuda test for crop

edgarribabd20a6b6e2ba
diff --git a/test/geometry/transform/test_crop.py b/test/geometry/transform/test_crop.py
index 63cf670bb..a6d7d90e3 100644
--- a/test/geometry/transform/test_crop.py
+++ b/test/geometry/transform/test_crop.py
@@ -4,6 +4,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
@@ -11,31 +12,31 @@ from torch.autograd import gradcheck
class TestCropAndResize:
- def test_crop(self):
+ def test_crop(self, device):
inp = torch.tensor([[
[1., 2., 3., 4.],
[5., 6., 7., 8.],
[9., 10., 11., 12.],
[13., 14., 15., 16.],
- ]])
+ ]]).to(device)
height, width = 2, 3
expected = torch.tensor([[
[6., 6.5, 7.],
[10., 10.5, 11.],
- ]])
+ ]]).to(device)
boxes = torch.tensor([[
[1., 1.],
[2., 1.],
[2., 2.],
[1., 2.],
- ]]) # 1x4x2
+ ]]).to(device) # 1x4x2
patches = kornia.crop_and_resize(inp, boxes, (height, width))
assert_allclose(patches, expected)
- def test_crop_batch(self):
+ def test_crop_batch(self, device):
inp = torch.tensor([[[
[1., 2., 3., 4.],
[5., 6., 7., 8.],
@@ -46,7 +47,7 @@ class TestCropAndResize:
[2., 6., 10., 14.],
[3., 7., 11., 15.],
[4., 8., 12., 16.],
- ]]])
+ ]]]).to(device)
height, width = 2, 2
expected = torch.tensor([[[
@@ -55,7 +56,7 @@ class TestCropAndResize:
]], [[
[7., 15.],
[8., 16.],
- ]]])
+ ]]]).to(device)
boxes = torch.tensor([[
[1., 1.],
@@ -67,12 +68,12 @@ class TestCropAndResize:
[3., 2.],
[3., 3.],
[1., 3.],
- ]]) # 2x4x2
+ ]]).to(device) # 2x4x2
patches = kornia.crop_and_resize(inp, boxes, (height, width))
assert_allclose(patches, expected)
- def test_crop_batch_broadcast(self):
+ def test_crop_batch_broadcast(self, device):
inp = torch.tensor([[[
[1., 2., 3., 4.],
[5., 6., 7., 8.],
@@ -83,7 +84,7 @@ class TestCropAndResize:
[2., 6., 10., 14.],
[3., 7., 11., 15.],
[4., 8., 12., 16.],
- ]]])
+ ]]]).to(device)
height, width = 2, 2
expected = torch.tensor([[[
@@ -92,21 +93,21 @@ class TestCropAndResize:
]], [[
[6., 10.],
[7., 11.],
- ]]])
+ ]]]).to(device)
boxes = torch.tensor([[
[1., 1.],
[2., 1.],
[2., 2.],
[1., 2.],
- ]]) # 1x4x2
+ ]]).to(device) # 1x4x2
patches = kornia.crop_and_resize(inp, boxes, (height, width))
assert_allclose(patches, 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).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
boxes = torch.tensor([[
@@ -114,7 +115,7 @@ class TestCropAndResize:
[2., 1.],
[2., 2.],
[1., 2.],
- ]]) # 1x4x2
+ ]]).to(device) # 1x4x2
boxes = utils.tensor_to_gradcheck_var(
boxes, requires_grad=False) # to var
@@ -124,7 +125,7 @@ class TestCropAndResize:
raise_exception=True)
@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: torch.Tensor,
boxes: torch.Tensor,
@@ -135,13 +136,13 @@ class TestCropAndResize:
[5., 6., 7., 8.],
[9., 10., 11., 12.],
[13., 14., 15., 16.],
- ]])
+ ]]).to(device)
boxes = torch.tensor([[
[1., 1.],
[2., 1.],
[2., 2.],
[1., 2.],
- ]]) # 1x4x2
+ ]]).to(device) # 1x4x2
crop_height, crop_width = 4, 2
actual = op_script(img, boxes, (crop_height, crop_width))
@@ -150,30 +151,30 @@ class TestCropAndResize:
class TestCenterCrop:
- def test_center_crop_h2_w4(self):
+ def test_center_crop_h2_w4(self, device):
inp = torch.tensor([[
[1., 2., 3., 4.],
[5., 6., 7., 8.],
[9., 10., 11., 12.],
[13., 14., 15., 16.],
- ]])
+ ]]).to(device)
height, width = 2, 4
expected = torch.tensor([[
[5., 6., 7., 8.],
[9., 10., 11., 12.],
- ]])
+ ]]).to(device)
out_crop = kornia.center_crop(inp, (height, width))
assert_allclose(out_crop, expected)
- def test_center_crop_h4_w2(self):
+ def test_center_crop_h4_w2(self, device):
inp = torch.tensor([[
[1., 2., 3., 4.],
[5., 6., 7., 8.],
[9., 10., 11., 12.],
[13., 14., 15., 16.],
- ]])
+ ]]).to(device)
height, width = 4, 2
expected = torch.tensor([[
@@ -181,12 +182,12 @@ class TestCenterCrop:
[6., 7.],
[10., 11.],
[14., 15.],
- ]])
+ ]]).to(device)
out_crop = kornia.center_crop(inp, (height, width))
assert_allclose(out_crop, expected)
- def test_center_crop_h4_w2_batch(self):
+ def test_center_crop_h4_w2_batch(self, device):
inp = torch.tensor([[
[1., 2., 3., 4.],
[5., 6., 7., 8.],
@@ -197,7 +198,7 @@ class TestCenterCrop:
[2., 6., 10., 14.],
[3., 7., 11., 15.],
[4., 8., 12., 16.],
- ]])
+ ]]).to(device)
height, width = 4, 2
expected = torch.tensor([[
@@ -210,14 +211,14 @@ class TestCenterCrop:
[6., 10.],
[7., 11.],
[8., 12.],
- ]])
+ ]]).to(device)
out_crop = kornia.center_crop(inp, (height, width))
assert_allclose(out_crop, 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).to(device)
img = utils.tensor_to_gradcheck_var(img) # to var
crop_height, crop_width = 4, 2
@@ -225,13 +226,13 @@ class TestCenterCrop:
raise_exception=True)
@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: torch.Tensor,
size: Tuple[int, int]) -> torch.Tensor:
return kornia.center_crop(input, size)
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.ones(batch_size, channels, height, width)
+ img = torch.ones(batch_size, channels, height, width).to(device)
crop_height, crop_width = 4, 2
actual = op_script(img, (crop_height, crop_width))
@@ -239,14 +240,14 @@ class TestCenterCrop:
assert_allclose(actual, expected)
@pytest.mark.skip(reason="turn off all jit for a while")
- def test_jit_trace(self):
+ def test_jit_trace(self, device):
@torch.jit.script
def op_script(input: torch.Tensor,
size: Tuple[torch.Tensor, torch.Tensor]) -> torch.Tensor:
return kornia.center_crop(input, size)
# 1. Trace op
batch_size, channels, height, width = 1, 2, 5, 4
- img = torch.ones(batch_size, channels, height, width)
+ img = torch.ones(batch_size, channels, height, width).to(device)
crop_height, crop_width = 4, 2
op_trace = torch.jit.trace(
@@ -255,7 +256,7 @@ class TestCenterCrop:
# 2. Generate new input
batch_size, channels, height, width = 2, 1, 6, 3
- img = torch.ones(batch_size, channels, height, width)
+ img = torch.ones(batch_size, channels, height, width).to(device)
# 3. Evaluate
crop_height, crop_width = 2, 3