Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement cuda test for flip

edgarriba85b51073b816
diff --git a/test/geometry/transform/test_flip.py b/test/geometry/transform/test_flip.py
index a0aef2c17..10eae36e7 100644
--- a/test/geometry/transform/test_flip.py
+++ b/test/geometry/transform/test_flip.py
@@ -3,48 +3,50 @@ import torch
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
class TestVflip:
- def smoke_test(self):
+ def smoke_test(self, device):
f = kornia.Vflip()
repr = "Vflip()"
assert str(f) == repr
- def test_vflip(self):
+ def test_vflip(self, device):
f = kornia.Vflip()
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
expected = torch.tensor([[0., 1., 1.],
[0., 0., 0.],
- [0., 0., 0.]]) # 3 x 3
+ [0., 0., 0.]]).to(device) # 3 x 3
assert (f(input) == expected).all()
- def test_batch_vflip(self):
+ def test_batch_vflip(self, device):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
- input = input.repeat(2, 1, 1) # 2 x 3 x 3
+ input = input.repeat(2, 1, 1).to(device) # 2 x 3 x 3
f = kornia.Vflip()
expected = torch.tensor([[[0., 1., 1.],
[0., 0., 0.],
- [0., 0., 0.]]]) # 1 x 3 x 3
+ [0., 0., 0.]]]).to(device) # 1 x 3 x 3
expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
assert (f(input) == expected).all()
@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(data: torch.Tensor) -> torch.Tensor:
@@ -52,7 +54,7 @@ class TestVflip:
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input, ))
@@ -60,13 +62,13 @@ class TestVflip:
# Create new inputs
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [5., 5., 0.]]) # 3 x 3
+ [5., 5., 0.]]).to(device) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor([[[5., 5., 0.],
[0., 0., 0.],
- [0., 0., 0.]]]) # 3 x 3
+ [0., 0., 0.]]]).to(device) # 3 x 3
expected = expected.repeat(2, 1, 1)
@@ -74,11 +76,11 @@ class TestVflip:
assert_allclose(actual, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).double() # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var
@@ -87,43 +89,43 @@ class TestVflip:
class TestHflip:
- def smoke_test(self):
+ def smoke_test(self, device):
f = kornia.Hflip()
repr = "Hflip()"
assert str(f) == repr
- def test_hflip(self):
+ def test_hflip(self, device):
f = kornia.Hflip()
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
expected = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [1., 1., 0.]]) # 3 x 3
+ [1., 1., 0.]]).to(device) # 3 x 3
assert (f(input) == expected).all()
- def test_batch_hflip(self):
+ def test_batch_hflip(self, device):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 1 x 3 x 3
+ [0., 1., 1.]]).to(device) # 1 x 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
f = kornia.Hflip()
expected = torch.tensor([[[0., 0., 0.],
[0., 0., 0.],
- [1., 1., 0.]]]) # 3 x 3
+ [1., 1., 0.]]]).to(device) # 3 x 3
expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
assert (f(input) == expected).all()
@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(data: torch.Tensor) -> torch.Tensor:
@@ -131,7 +133,7 @@ class TestHflip:
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input, ))
@@ -139,13 +141,13 @@ class TestHflip:
# Create new inputs
input = torch.tensor([[0., 0., 0.],
[5., 5., 0.],
- [0., 0., 0.]]) # 3 x 3
+ [0., 0., 0.]]).to(device) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor([[[0., 0., 0.],
[0., 5., 5.],
- [0., 0., 0.]]]) # 3 x 3
+ [0., 0., 0.]]]).to(device) # 3 x 3
expected = expected.repeat(2, 1, 1)
@@ -153,11 +155,11 @@ class TestHflip:
assert_allclose(actual, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).double() # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var
@@ -166,43 +168,43 @@ class TestHflip:
class TestRot180:
- def smoke_test(self):
+ def smoke_test(self, device):
f = kornia.Rot180()
repr = "Rot180()"
assert str(f) == repr
- def test_rot180(self):
+ def test_rot180(self, device):
f = kornia.Rot180()
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
expected = torch.tensor([[1., 1., 0.],
[0., 0., 0.],
- [0., 0., 0.]]) # 3 x 3
+ [0., 0., 0.]]).to(device) # 3 x 3
assert (f(input) == expected).all()
- def test_batch_rot180(self):
+ def test_batch_rot180(self, device):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
f = kornia.Rot180()
expected = torch.tensor([[1., 1., 0.],
[0., 0., 0.],
- [0., 0., 0.]]) # 1 x 3 x 3
+ [0., 0., 0.]]).to(device) # 1 x 3 x 3
expected = expected.repeat(2, 1, 1) # 2 x 3 x 3
assert (f(input) == expected).all()
@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(data: torch.Tensor) -> torch.Tensor:
@@ -210,7 +212,7 @@ class TestRot180:
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]) # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input, ))
@@ -218,13 +220,13 @@ class TestRot180:
# Create new inputs
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [5., 5., 0.]]) # 3 x 3
+ [5., 5., 0.]]).to(device) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor([[[0., 5., 5.],
[0., 0., 0.],
- [0., 0., 0.]]]) # 3 x 3
+ [0., 0., 0.]]]).to(device) # 3 x 3
expected = expected.repeat(2, 1, 1)
@@ -232,11 +234,11 @@ class TestRot180:
assert_allclose(actual, expected)
- def test_gradcheck(self):
+ def test_gradcheck(self, device):
input = torch.tensor([[0., 0., 0.],
[0., 0., 0.],
- [0., 1., 1.]]).double() # 3 x 3
+ [0., 1., 1.]]).to(device) # 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var