File size: 7,138 Bytes
36c95ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
import pytest
import torch
from torch.autograd import gradcheck
import kornia
import kornia.testing as utils # test utils
from kornia.testing import assert_close
class TestVflip:
def smoke_test(self, device, dtype):
f = kornia.geometry.transform.Vflip()
repr = "Vflip()"
assert str(f) == repr
def test_vflip(self, device, dtype):
f = kornia.geometry.transform.Vflip()
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
expected = torch.tensor(
[[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], device=device, dtype=dtype
) # 3 x 3
assert (f(input) == expected).all()
def test_batch_vflip(self, device, dtype):
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
f = kornia.geometry.transform.Vflip()
expected = torch.tensor(
[[[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]], device=device, dtype=dtype
) # 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, device, dtype):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
return kornia.geometry.transform.vflip(data)
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input,))
# Create new inputs
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [5.0, 5.0, 0.0]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor(
[[[5.0, 5.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]], device=device, dtype=dtype
) # 3 x 3
expected = expected.repeat(2, 1, 1)
actual = op_trace(input)
assert_close(actual, expected)
def test_gradcheck(self, device, dtype):
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.geometry.transform.Vflip(), (input,), raise_exception=True)
class TestHflip:
def smoke_test(self, device, dtype):
f = kornia.geometry.transform.Hflip()
repr = "Hflip()"
assert str(f) == repr
def test_hflip(self, device, dtype):
f = kornia.geometry.transform.Hflip()
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
expected = torch.tensor(
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 1.0, 0.0]], device=device, dtype=dtype
) # 3 x 3
assert (f(input) == expected).all()
def test_batch_hflip(self, device, dtype):
input = torch.tensor(
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype
) # 1 x 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
f = kornia.geometry.transform.Hflip()
expected = torch.tensor(
[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 1.0, 0.0]]], device=device, dtype=dtype
) # 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, device, dtype):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
return kornia.geometry.transform.hflip(data)
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input,))
# Create new inputs
input = torch.tensor([[0.0, 0.0, 0.0], [5.0, 5.0, 0.0], [0.0, 0.0, 0.0]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor(
[[[0.0, 0.0, 0.0], [0.0, 5.0, 5.0], [0.0, 0.0, 0.0]]], device=device, dtype=dtype
) # 3 x 3
expected = expected.repeat(2, 1, 1)
actual = op_trace(input)
assert_close(actual, expected)
def test_gradcheck(self, device, dtype):
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.geometry.transform.Hflip(), (input,), raise_exception=True)
class TestRot180:
def smoke_test(self, device, dtype):
f = kornia.geometry.transform.Rot180()
repr = "Rot180()"
assert str(f) == repr
def test_rot180(self, device, dtype):
f = kornia.geometry.transform.Rot180()
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
expected = torch.tensor(
[[1.0, 1.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], device=device, dtype=dtype
) # 3 x 3
assert (f(input) == expected).all()
def test_batch_rot180(self, device, dtype):
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
f = kornia.geometry.transform.Rot180()
expected = torch.tensor(
[[1.0, 1.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]], device=device, dtype=dtype
) # 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, device, dtype):
@torch.jit.script
def op_script(data: torch.Tensor) -> torch.Tensor:
return kornia.geometry.transform.rot180(data)
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
# Build jit trace
op_trace = torch.jit.trace(op_script, (input,))
# Create new inputs
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [5.0, 5.0, 0.0]], device=device, dtype=dtype) # 3 x 3
input = input.repeat(2, 1, 1) # 2 x 3 x 3
expected = torch.tensor(
[[[0.0, 5.0, 5.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]], device=device, dtype=dtype
) # 3 x 3
expected = expected.repeat(2, 1, 1)
actual = op_trace(input)
assert_close(actual, expected)
def test_gradcheck(self, device, dtype):
input = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]], device=device, dtype=dtype) # 3 x 3
input = utils.tensor_to_gradcheck_var(input) # to var
assert gradcheck(kornia.geometry.transform.Rot180(), (input,), raise_exception=True)
|