π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
[TEST][DO NOT MERGE] update to pytorch v1.5.0 (#533)
Edgar Riba78275f54c788
diff --git a/conftest.py b/conftest.pyindex bd0e0ad29..6f5a59314 100644--- a/conftest.py+++ b/conftest.py@@ -3,3 +3,4 @@ import pytestdef pytest_addoption(parser):parser.addoption('--typetest', action="store", default="cpu")+ parser.addoption('--dtypetest', action="store", default="float32")diff --git a/kornia/feature/laf.py b/kornia/feature/laf.pyindex 55b29c7c6..53a5a4771 100644--- a/kornia/feature/laf.py+++ b/kornia/feature/laf.py@@ -311,12 +311,10 @@ def denormalize_laf(LAF: torch.Tensor, images: torch.Tensor) -> torch.Tensor:"""raise_error_if_laf_is_not_valid(LAF)n, ch, h, w = images.size()- w = float(w)- h = float(h)- min_size = min(h, w)+ min_size: float = 1. * min(h, w)coef = torch.ones(1, 1, 2, 3).to(LAF.dtype).to(LAF.device) * min_size- coef[0, 0, 0, 2] = w- coef[0, 0, 1, 2] = h+ coef[0, 0, 0, 2] = float(w)+ coef[0, 0, 1, 2] = float(h)return coef.expand_as(LAF) * LAF@@ -343,9 +341,7 @@ def normalize_laf(LAF: torch.Tensor, images: torch.Tensor) -> torch.Tensor:"""raise_error_if_laf_is_not_valid(LAF)n, ch, h, w = images.size()- w = float(w)- h = float(h)- min_size = min(h, w)+ min_size: float = 1. * min(h, w)coef = torch.ones(1, 1, 2, 3).to(LAF.dtype).to(LAF.device) / min_sizecoef[0, 0, 0, 2] = 1.0 / wcoef[0, 0, 1, 2] = 1.0 / hdiff --git a/kornia/feature/orientation.py b/kornia/feature/orientation.pyindex 3f25c4b99..7ea0094aa 100644--- a/kornia/feature/orientation.py+++ b/kornia/feature/orientation.py@@ -43,7 +43,7 @@ class PatchDominantGradientOrientation(nn.Module):self.num_ang_bins = num_angular_binsself.gradient = SpatialGradient('sobel', 1)self.eps = eps- self.angular_smooth = nn.Conv1d(1, 1, kernel_size=3, padding=2, bias=False, padding_mode="circular")+ self.angular_smooth = nn.Conv1d(1, 1, kernel_size=3, padding=1, bias=False, padding_mode="circular")with torch.no_grad():self.angular_smooth.weight[:] = torch.tensor([[[0.33, 0.34, 0.33]]])sigma: float = float(self.patch_size) / math.sqrt(2.0)diff --git a/packaging/README.rst b/packaging/README.rstindex c3bedf077..ac5f4fbee 100644--- a/packaging/README.rst+++ b/packaging/README.rst@@ -7,11 +7,11 @@ WheelsLinux#####-In order to generate wheels define ```KORNIA_BUILD_VERSION```+In order to generate wheels define ```KORNIA_BUILD_VERSION``` and ```PYTORCH_VERSION```... code:: bash- KORNIA_BUILD_VERSION=0.x.x ./build_wheel.sh+ KORNIA_BUILD_VERSION=0.x.x PYTORCH_VERSION=1.x.x ./build_wheel.shThis will produce a ```*whl``` file under ```OUT_DIR```, by default to ```/tmp/remote```.You can upload the wheels usng `Twine <https://pypi.org/project/twine/>`_diff --git a/packaging/build_wheel.sh b/packaging/build_wheel.shindex 2aa8ecff8..9cf399b8d 100755--- a/packaging/build_wheel.sh+++ b/packaging/build_wheel.sh@@ -1,7 +1,8 @@#!/bin/bash -ex## NOTE: define KORNIA_BUILD_VERSION each release-## KORNIA_BUILD_VERSION=0.1.4+## KORNIA_BUILD_VERSION=0.3.0+## PYTORCH_VERSION=1.5.0export OUT_DIR="/tmp/remote"diff --git a/requirements.txt b/requirements.txtindex 3ac1b6f7f..e1537059f 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,12 +1,12 @@# core dependencies-pytorch==1.4.0-cudatoolkit==10.1.243+pytorch==1.5.0+cudatoolkit==10.2.89# packagesetuptools==41.4.0# utils-torchvision==0.5.0+torchvision==0.6.0opencv==4.1.1ipython==7.8.0jupyter==1.0.0diff --git a/setup.py b/setup.pyindex b6662377b..566f1a25f 100644--- a/setup.py+++ b/setup.py@@ -17,7 +17,7 @@ import distutils.command.clean# NOTE(maintainers): modify this variable each time you do a releaseversion = '0.3.0'-pytorch = 'torch>=1.0.0, <=1.4.0'+#################################sha = 'Unknown'@@ -74,6 +74,16 @@ class clean(distutils.command.clean.clean):subprocess.call(['rm -rf dist/ build/ kornia.egg*'], shell=True)+pytorch_dep = 'torch'+if os.getenv('PYTORCH_VERSION'):+ pytorch_dep += "==" + os.getenv('PYTORCH_VERSION')++requirements = [+ 'numpy',+ pytorch_dep,+]++if __name__ == '__main__':write_version_file()setup(@@ -96,9 +106,7 @@ if __name__ == '__main__':packages=find_packages(exclude=('docs', 'test', 'examples',)),zip_safe=True,- install_requires=[- pytorch,- ],+ install_requires=requirements,classifiers=['Intended Audience :: Developers','Intended Audience :: Education',diff --git a/test/color/test_gray.py b/test/color/test_gray.pyindex bb17d7ef7..6bb1d6be2 100644--- a/test/color/test_gray.py+++ b/test/color/test_gray.py@@ -2,7 +2,7 @@ import pytestimport korniaimport kornia.testing as utils # test utils-from test.common import device+from test.common import device, dtypeimport torchfrom torch.autograd import gradcheck@@ -10,18 +10,18 @@ from torch.testing import assert_allcloseclass TestRgbToGrayscale:- def test_rgb_to_grayscale(self, device):+ def test_rgb_to_grayscale(self, device, dtype):channels, height, width = 3, 4, 5- img = torch.ones(channels, height, width).to(device)+ img = torch.ones(channels, height, width).to(device, dtype)assert kornia.rgb_to_grayscale(img).shape == (1, height, width)- def test_rgb_to_grayscale_batch(self, device):+ def test_rgb_to_grayscale_batch(self, device, dtype):batch_size, channels, height, width = 2, 3, 4, 5- img = torch.ones(batch_size, channels, height, width).to(device)+ img = torch.ones(batch_size, channels, height, width).to(device, dtype)assert kornia.rgb_to_grayscale(img).shape == \(batch_size, 1, height, width)- def test_opencv(self, device):+ def test_opencv(self, device, dtype):data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],[0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],[0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],@@ -39,7 +39,7 @@ class TestRgbToGrayscale:[0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],[0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],[0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])- data = data.to(device)+ data = data.to(device, dtype)# Output data generated with OpenCV 4.1.1: cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY)expected = torch.tensor([[0.4684734, 0.8954562, 0.6064363, 0.5236061, 0.6106016],@@ -47,48 +47,46 @@ class TestRgbToGrayscale:[0.5279005, 0.6092287, 0.3034387, 0.5333768, 0.6064113],[0.3503858, 0.5720159, 0.7052018, 0.4558409, 0.3261529],[0.6988886, 0.5897652, 0.6532392, 0.7234108, 0.7218805]])- expected = expected.to(device)+ expected = expected.to(device, dtype)img_gray = kornia.rgb_to_grayscale(data)assert_allclose(img_gray, expected)- def test_gradcheck(self, device):+ def test_gradcheck(self, device, dtype):batch_size, channels, height, width = 2, 3, 4, 5- img = torch.ones(batch_size, channels, height, width).to(device)+ img = torch.ones(batch_size, channels, height, width).to(device, dtype)img = utils.tensor_to_gradcheck_var(img) # to varassert gradcheck(kornia.rgb_to_grayscale, (img,), raise_exception=True)- @pytest.mark.skip()- def test_jit(self, device):+ def test_jit(self, device, dtype):batch_size, channels, height, width = 2, 3, 64, 64- img = torch.ones(batch_size, channels, height, width).to(device)+ img = torch.ones(batch_size, channels, height, width).to(device, dtype)op = kornia.rgb_to_grayscaleop_jit = kornia.jit.rgb_to_grayscaleassert_allclose(op(img), op_jit(img))- @pytest.mark.skip()- def test_jit_trace(self, device):+ def test_jit_trace(self, device, dtype):batch_size, channels, height, width = 2, 3, 64, 64- img = torch.ones(batch_size, channels, height, width).to(device)+ img = torch.ones(batch_size, channels, height, width).to(device, dtype)gray = kornia.color.RgbToGrayscale()gray_traced = torch.jit.trace(kornia.color.RgbToGrayscale(), img)assert_allclose(gray(img), gray_traced(img))class TestBgrToGrayscale:- def test_bgr_to_grayscale(self, device):+ def test_bgr_to_grayscale(self, device, dtype):channels, height, width = 3, 4, 5- img = torch.ones(channels, height, width).to(device)+ img = torch.ones(channels, height, width).to(device, dtype)assert kornia.bgr_to_grayscale(img).shape == (1, height, width)- def test_bgr_to_grayscale_batch(self, device):+ def test_bgr_to_grayscale_batch(self, device, dtype):batch_size, channels, height, width = 2, 3, 4, 5- img = torch.ones(batch_size, channels, height, width).to(device)+ img = torch.ones(batch_size, channels, height, width).to(device, dtype)assert kornia.bgr_to_grayscale(img).shape == \(batch_size, 1, height, width)# Output data generated with OpenCV 4.1.1: cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)- def test_opencv(self, device):+ def test_opencv(self, device, dtype):data = torch.tensor([[[0.3944633, 0.8597369, 0.1670904, 0.2825457, 0.0953912],[0.1251704, 0.8020709, 0.8933256, 0.9170977, 0.1497008],[0.2711633, 0.1111478, 0.0783281, 0.2771807, 0.5487481],@@ -106,25 +104,25 @@ class TestBgrToGrayscale:[0.7415742, 0.6115875, 0.3317572, 0.0379378, 0.1315770],[0.8692724, 0.0809556, 0.7767404, 0.8742208, 0.1522012],[0.7708948, 0.4509611, 0.0481175, 0.2358997, 0.6900532]]])- data = data.to(device)+ data = data.to(device, dtype)expected = torch.tensor([[0.4485849, 0.8233618, 0.6262833, 0.6218331, 0.6341921],[0.3200093, 0.4340172, 0.7107211, 0.5454938, 0.2801398],[0.6149265, 0.7018101, 0.3503231, 0.4891168, 0.5292346],[0.5096100, 0.4336508, 0.6704276, 0.4525143, 0.2134447],[0.7878902, 0.6494595, 0.5211386, 0.6623823, 0.6660464]])- expected = expected.to(device)+ expected = expected.to(device, dtype)img_gray = kornia.bgr_to_grayscale(data)assert_allclose(img_gray, expected)- def test_gradcheck(self, device):+ def test_gradcheck(self, device, dtype):batch_size, channels, height, width = 2, 3, 4, 5- img = torch.ones(batch_size, channels, height, width).to(device)+ img = torch.ones(batch_size, channels, height, width).to(device, dtype)img = utils.tensor_to_gradcheck_var(img) # to varassert gradcheck(kornia.bgr_to_grayscale, (img,), raise_exception=True)- def test_module(self, device):+ def test_module(self, device, dtype):data = torch.tensor([[[[100., 73.],[200., 22.]],@@ -134,14 +132,14 @@ class TestBgrToGrayscale:[[225., 255.],[48., 8.]]]])- data = data.to(device)+ data = data.to(device, dtype)assert_allclose(kornia.bgr_to_grayscale(data / 255), kornia.color.BgrToGrayscale()(data / 255))@pytest.mark.skip(reason="turn off all jit for a while")- def test_jit(self, device):+ def test_jit(self, device, dtype):batch_size, channels, height, width = 2, 3, 64, 64- img = torch.ones(batch_size, channels, height, width).to(device)+ img = torch.ones(batch_size, channels, height, width).to(device, dtype)gray = kornia.color.BgrToGrayscale()gray_traced = torch.jit.trace(kornia.color.BgrToGrayscale(), img)assert_allclose(gray(img), gray_traced(img))diff --git a/test/common.py b/test/common.pyindex 4e10fcfc7..32e758313 100644--- a/test/common.py+++ b/test/common.py@@ -18,12 +18,32 @@ def get_test_devices() -> Dict[str, torch.device]:return devices+def get_test_dtypes() -> Dict[str, torch.device]:+ """Creates a dictionary with the dtypes the source code.++ Return:+ dict(str, torch.dtype): list with dtype names.+ """+ dtypes: Dict[str, torch.dtype] = {}+ dtypes["float16"] = torch.float16+ dtypes["float32"] = torch.float32+ dtypes["float64"] = torch.float64+ return dtypes++# setup the devices to test the source codeTEST_DEVICES: Dict[str, torch.device] = get_test_devices()+TEST_DTYPES: Dict[str, torch.dtype] = get_test_dtypes()@pytest.fixture()def device(request) -> torch.device:_device_type: str = request.config.getoption('--typetest')return TEST_DEVICES[_device_type]+++def dtype(request) -> torch.device:+ _dtype_name: str = request.config.getoption('--dtypetest')+ return TEST_DTYPES[_dtype_name]diff --git a/test/geometry/test_conversions.py b/test/geometry/test_conversions.pyindex 55ef4db7d..c23d893b8 100644--- a/test/geometry/test_conversions.py+++ b/test/geometry/test_conversions.py@@ -5,7 +5,7 @@ import numpy as npimport korniafrom kornia.testing import tensor_to_gradcheck_var, create_eye_batch-from test.common import device+from test.common import device, dtypeimport torchfrom torch.autograd import gradcheck@@ -34,17 +34,17 @@ class TestAngleAxisToQuaternion:quaternion = kornia.angle_axis_to_quaternion(angle_axis)assert_allclose(quaternion, expected)- def test_small_angle(self, device):+ def test_small_angle(self, device, dtype):theta = 1e-2- angle_axis = torch.tensor([theta, 0., 0.]).to(device)- expected = torch.tensor([np.cos(theta / 2), np.sin(theta / 2), 0., 0.]).to(device)+ angle_axis = torch.tensor([theta, 0., 0.]).to(device, dtype)+ expected = torch.tensor([np.cos(theta / 2), np.sin(theta / 2), 0., 0.]).to(device, dtype)quaternion = kornia.angle_axis_to_quaternion(angle_axis)assert_allclose(quaternion, expected)- def test_x_rotation(self, device):+ def test_x_rotation(self, device, dtype):half_sqrt2 = 0.5 * np.sqrt(2)- angle_axis = torch.tensor([kornia.pi / 2, 0., 0.]).to(device)- expected = torch.tensor([half_sqrt2, half_sqrt2, 0., 0.]).to(device)+ angle_axis = torch.tensor([kornia.pi / 2, 0., 0.]).to(device, dtype)+ expected = torch.tensor([half_sqrt2, half_sqrt2, 0., 0.]).to(device, dtype)quaternion = kornia.angle_axis_to_quaternion(angle_axis)assert_allclose(quaternion, expected)@@ -307,16 +307,16 @@ class TestQuaternionToAngleAxis:angle_axis = kornia.quaternion_to_angle_axis(quaternion)assert_allclose(angle_axis, expected)- def test_z_rotation(self, device):- quaternion = torch.tensor([np.sqrt(3) / 2, 0., 0., 0.5]).to(device)- expected = torch.tensor([0., 0., kornia.pi / 3]).to(device)+ def test_z_rotation(self, device, dtype):+ quaternion = torch.tensor([np.sqrt(3) / 2, 0., 0., 0.5]).to(device, dtype)+ expected = torch.tensor([0., 0., kornia.pi / 3]).to(device, dtype)angle_axis = kornia.quaternion_to_angle_axis(quaternion)assert_allclose(angle_axis, expected)- def test_small_angle(self, device):+ def test_small_angle(self, device, dtype):theta = 1e-2- quaternion = torch.tensor([np.cos(theta / 2), np.sin(theta / 2), 0., 0.]).to(device)- expected = torch.tensor([theta, 0., 0.]).to(device)+ quaternion = torch.tensor([np.cos(theta / 2), np.sin(theta / 2), 0., 0.]).to(device, dtype)+ expected = torch.tensor([theta, 0., 0.]).to(device, dtype)angle_axis = kornia.quaternion_to_angle_axis(quaternion)assert_allclose(angle_axis, expected)diff --git a/test/geometry/test_spatial_softargmax.py b/test/geometry/test_spatial_softargmax.pyindex 85fc30827..76ed62073 100644--- a/test/geometry/test_spatial_softargmax.py+++ b/test/geometry/test_spatial_softargmax.py@@ -219,7 +219,9 @@ class TestDSNT:expected_loss1 = torch.as_tensor([[0.0, 1.0]]).to(device)assert_allclose(loss1, expected_loss1)- target_hm = kornia.geometry.dsnt.render_gaussian_2d(target, std, input.shape[-2:])+ target_hm = kornia.geometry.dsnt.render_gaussian_2d(+ target, std, input.shape[-2:]).contiguous()+loss2 = kornia.losses.js_div_loss_2d(hm, target_hm, reduction='none')expected_loss2 = torch.as_tensor([[0.0087, 0.0818]]).to(device)assert_allclose(loss2, expected_loss2, rtol=0, atol=1e-3)@@ -500,8 +502,6 @@ class TestConvQuadInterp3d:assert val.shape == (2, 3, 3, 4, 4)def test_gradcheck(self, device):- if "cuda" in str(device):- pytest.skip("Extremely unstable using cuda device.")input = torch.rand(1, 2, 3, 5, 5).to(device)input = utils.tensor_to_gradcheck_var(input) # to varassert gradcheck(kornia.geometry.ConvQuadInterp3d(),