From 09f479468b1e9391d19614f40c7b337ebe6f2dba Mon Sep 17 00:00:00 2001 From: Sam-Armstrong Date: Wed, 19 Jun 2024 17:27:56 +0100 Subject: [PATCH 1/3] add integration testing --- integration_tests/README.md | 3 + integration_tests/conftest.py | 33 + integration_tests/helpers.py | 205 ++++ .../kornia/geometry/test_bbox.py | 240 ++++ .../kornia/geometry/test_calibration.py | 158 +++ .../kornia/geometry/test_camera.py | 286 +++++ .../kornia/geometry/test_conversions.py | 880 ++++++++++++++ .../kornia/geometry/test_depth.py | 158 +++ .../kornia/geometry/test_epipolar.py | 627 ++++++++++ .../kornia/geometry/test_homography.py | 208 ++++ .../kornia/geometry/test_linalg.py | 194 ++++ .../kornia/geometry/test_solvers.py | 108 ++ .../kornia/geometry/test_subpix.py | 278 +++++ .../kornia/geometry/test_transform.py | 1014 +++++++++++++++++ integration_tests/kornia/test_color.py | 742 ++++++++++++ integration_tests/kornia/test_contrib.py | 154 +++ integration_tests/kornia/test_enhance.py | 639 +++++++++++ integration_tests/kornia/test_feature.py | 713 ++++++++++++ integration_tests/kornia/test_filters.py | 656 +++++++++++ integration_tests/kornia/test_losses.py | 418 +++++++ integration_tests/kornia/test_metrics.py | 242 ++++ integration_tests/kornia/test_morphology.py | 174 +++ integration_tests/kornia/test_utils.py | 148 +++ 23 files changed, 8278 insertions(+) create mode 100644 integration_tests/README.md create mode 100644 integration_tests/conftest.py create mode 100644 integration_tests/helpers.py create mode 100644 integration_tests/kornia/geometry/test_bbox.py create mode 100644 integration_tests/kornia/geometry/test_calibration.py create mode 100644 integration_tests/kornia/geometry/test_camera.py create mode 100644 integration_tests/kornia/geometry/test_conversions.py create mode 100644 integration_tests/kornia/geometry/test_depth.py create mode 100644 integration_tests/kornia/geometry/test_epipolar.py create mode 100644 integration_tests/kornia/geometry/test_homography.py create mode 100644 integration_tests/kornia/geometry/test_linalg.py create mode 100644 integration_tests/kornia/geometry/test_solvers.py create mode 100644 integration_tests/kornia/geometry/test_subpix.py create mode 100644 integration_tests/kornia/geometry/test_transform.py create mode 100644 integration_tests/kornia/test_color.py create mode 100644 integration_tests/kornia/test_contrib.py create mode 100644 integration_tests/kornia/test_enhance.py create mode 100644 integration_tests/kornia/test_feature.py create mode 100644 integration_tests/kornia/test_filters.py create mode 100644 integration_tests/kornia/test_losses.py create mode 100644 integration_tests/kornia/test_metrics.py create mode 100644 integration_tests/kornia/test_morphology.py create mode 100644 integration_tests/kornia/test_utils.py diff --git a/integration_tests/README.md b/integration_tests/README.md new file mode 100644 index 000000000000..3ac1058b013d --- /dev/null +++ b/integration_tests/README.md @@ -0,0 +1,3 @@ +# Ivy Integration Tests + +This folder is for testing all ivy integrations. Please only use these tests if you're actively working on a fix for an ivy integration, otherwise tests should be in the `ivy_tests/` folder. diff --git a/integration_tests/conftest.py b/integration_tests/conftest.py new file mode 100644 index 000000000000..b8acac7f6202 --- /dev/null +++ b/integration_tests/conftest.py @@ -0,0 +1,33 @@ +import ivy +import pytest + +TARGET_FRAMEWORKS = ["numpy", "jax", "tensorflow", "torch"] +BACKEND_COMPILE = False + + +@pytest.fixture(autouse=True) +def run_around_tests(): + ivy.unset_backend() + + +def pytest_addoption(parser): + parser.addoption( + "--backend-compile", + action="store_true", + help="", + ) + + +def pytest_configure(config): + getopt = config.getoption + + global BACKEND_COMPILE + BACKEND_COMPILE = getopt("--backend-compile") + + +def pytest_generate_tests(metafunc): + configs = list() + for target in TARGET_FRAMEWORKS: + configs.append((target, "transpile", BACKEND_COMPILE)) + configs.append(("torch", "trace", BACKEND_COMPILE)) + metafunc.parametrize("target_framework,mode,backend_compile", configs) diff --git a/integration_tests/helpers.py b/integration_tests/helpers.py new file mode 100644 index 000000000000..d7c2385ed9e1 --- /dev/null +++ b/integration_tests/helpers.py @@ -0,0 +1,205 @@ +import copy +import ivy +import jax +import jax.numpy as jnp +import numpy as np +import pytest +import tensorflow as tf +import time +import torch + +jax.config.update('jax_enable_x64', True) + + +# Helpers # +# ------- # + +def _check_allclose(x, y, tolerance=1e-3): + """ + Checks that all values are close. Any arrays must already be in numpy format, rather than native framework. + """ + + if type(x) != type(y): + assert False, f"mistmatched types: {type(x), type(y)}" + + if isinstance(x, np.ndarray): + assert np.allclose(x, y, atol=tolerance), "numpy array values are not all close" + return + + if isinstance(x, (list, set, tuple)): + all([ + _check_allclose(element_x, element_y, tolerance=tolerance) for element_x, element_y in zip(x, y) + ]) + return + + if isinstance(x, dict): + keys_same = all([key_x == key_y for key_x, key_y in zip(x.keys(), y.keys())]) + values_same = all([ + _check_allclose(element_x, element_y, tolerance=tolerance) + for element_x, element_y in zip(x.values(), y.values()) + ]) + assert keys_same and values_same, "keys or values in dict differ" + return + + if isinstance(x, float): + assert x - y < tolerance, f"float values differ: {x} != {y}" + return + + assert x == y, f"values differ: {x} != {y}" + + +def _native_array_to_numpy(x): + if isinstance(x, (torch.Tensor, tf.Tensor)): + return x.numpy() + if isinstance(x, jnp.ndarray): + return np.asarray(x) + return x + + +def _nest_array_to_numpy( + nest, shallow=True +): + return ivy.nested_map( + lambda x: _native_array_to_numpy(x), + nest, + include_derived=True, + shallow=shallow, + ) + + +def _array_to_new_backend( + x, + target, +): + """ + Converts a torch tensor to an array/tensor in a different framework. + If the input is not a torch tensor, the input if returned without modification. + """ + + if isinstance(x, torch.Tensor): + if target == "torch": return x + y = x.numpy() + if target == "jax": + y = jnp.array(y) + elif target == "tensorflow": + y = tf.convert_to_tensor(y) + return y + else: + return x + + +def _nest_torch_tensor_to_new_framework( + nest, target, shallow=True +): + return ivy.nested_map( + lambda x: _array_to_new_backend(x, target), + nest, + include_derived=True, + shallow=shallow, + ) + + +def _test_trace_function( + fn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + backend_compile, + tolerance=1e-3, +): + graph = ivy.trace_graph( + fn, + to="torch", + args=trace_args, + kwargs=trace_kwargs, + backend_compile=backend_compile, + graph_caching=True, + ) + + graph_args = copy.deepcopy(test_args) + graph_kwargs = copy.deepcopy(test_kwargs) + + orig_out = fn(*test_args, **test_kwargs) + graph_out = graph(*graph_args, **graph_kwargs) + + orig_np = _nest_array_to_numpy(orig_out) + graph_np = _nest_array_to_numpy(graph_out) + + _check_allclose(orig_np, graph_np, tolerance=tolerance) + + +def _test_transpile_function( + fn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target, + backend_compile, + tolerance=1e-3, +): + graph = ivy.transpile( + fn, + source="torch", + to=target, + args=trace_args, + kwargs=trace_kwargs, + backend_compile=backend_compile, + graph_caching=True, + ) + + orig_out = fn(*test_args, **test_kwargs) + graph_args = _nest_torch_tensor_to_new_framework(test_args, target) + graph_kwargs = _nest_torch_tensor_to_new_framework(test_kwargs, target) + graph_out = graph(*graph_args, **graph_kwargs) + + orig_np = _nest_array_to_numpy(orig_out) + graph_np = _nest_array_to_numpy(graph_out) + + _check_allclose(orig_np, graph_np, tolerance=tolerance) + + +def _test_function( + fn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target, + backend_compile, + tolerance=1e-3, + mode="transpile" +): + start_time = time.time() + if mode == "transpile": + print(f"\ntesting {fn.__module__}.{fn.__name__} --> {target}") + if mode == "trace" and target == "torch": + print(f"\ntesting {fn.__module__}.{fn.__name__} --> traced graph") + + if mode == "trace": + if target != "torch": + pytest.skip() + + _test_trace_function( + fn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + backend_compile, + tolerance=tolerance, + ) + else: + _test_transpile_function( + fn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target, + backend_compile, + tolerance=tolerance, + ) + time_taken = round(time.time() - start_time, 2) + print(f"Test Finished in {time_taken} seconds") diff --git a/integration_tests/kornia/geometry/test_bbox.py b/integration_tests/kornia/geometry/test_bbox.py new file mode 100644 index 000000000000..4777f9270648 --- /dev/null +++ b/integration_tests/kornia/geometry/test_bbox.py @@ -0,0 +1,240 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_bbox_generator(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([0, 1]), + torch.tensor([1, 0]), + torch.tensor([5, 3]), + torch.tensor([7, 4]), + ) + trace_kwargs = {} + test_args = ( + torch.tensor([2, 3]), + torch.tensor([4, 5]), + torch.tensor([6, 8]), + torch.tensor([10, 12]), + ) + test_kwargs = {} + _test_function( + kornia.geometry.bbox.bbox_generator, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_bbox_generator3d(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([0, 3]), + torch.tensor([1, 4]), + torch.tensor([2, 5]), + torch.tensor([10, 40]), + torch.tensor([20, 50]), + torch.tensor([30, 60]), + ) + trace_kwargs = {} + test_args = ( + torch.tensor([5, 8]), + torch.tensor([6, 7]), + torch.tensor([9, 11]), + torch.tensor([15, 20]), + torch.tensor([25, 30]), + torch.tensor([35, 45]), + ) + test_kwargs = {} + _test_function( + kornia.geometry.bbox.bbox_generator3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_bbox_to_mask(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[[1., 1.], [3., 1.], [3., 2.], [1., 2.]]]), + 5, + 5, + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[[2., 2.], [4., 2.], [4., 3.], [2., 3.]]]), + 6, + 6, + ) + test_kwargs = {} + _test_function( + kornia.geometry.bbox.bbox_to_mask, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_bbox_to_mask3d(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[ + [1., 1., 1.], [2., 1., 1.], [2., 2., 1.], [1., 2., 1.], + [1., 1., 2.], [2., 1., 2.], [2., 2., 2.], [1., 2., 2.] + ]]), + (4, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[ + [2., 2., 2.], [3., 2., 2.], [3., 3., 2.], [2., 3., 2.], + [2., 2., 3.], [3., 2., 3.], [3., 3., 3.], [2., 3., 3.] + ]]), + (5, 6, 6), + ) + test_kwargs = {} + _test_function( + kornia.geometry.bbox.bbox_to_mask3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_infer_bbox_shape(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[ + [1., 1.], [2., 1.], [2., 2.], [1., 2.] + ]]), + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[ + [2., 2.], [4., 2.], [4., 3.], [2., 3.] + ]]), + ) + test_kwargs = {} + _test_function( + kornia.geometry.bbox.infer_bbox_shape, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_infer_bbox_shape3d(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[ + [0, 1, 2], [10, 1, 2], [10, 21, 2], [0, 21, 2], + [0, 1, 32], [10, 1, 32], [10, 21, 32], [0, 21, 32] + ]]), + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[ + [3, 4, 5], [43, 4, 5], [43, 54, 5], [3, 54, 5], + [3, 4, 65], [43, 4, 65], [43, 54, 65], [3, 54, 65] + ]]), + ) + test_kwargs = {} + _test_function( + kornia.geometry.bbox.infer_bbox_shape3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_nms(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([ + [10., 10., 20., 20.], + [15., 5., 15., 25.], + [100., 100., 200., 200.], + [100., 100., 200., 200.] + ]), + torch.tensor([0.9, 0.8, 0.7, 0.9]), + 0.8, + ) + trace_kwargs = {} + test_args = ( + torch.tensor([ + [5., 5., 15., 15.], + [10., 0., 20., 20.], + [90., 90., 180., 180.], + [90., 90., 180., 180.] + ]), + torch.tensor([0.85, 0.75, 0.65, 0.85]), + 0.7, + ) + test_kwargs = {} + _test_function( + kornia.geometry.bbox.nms, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_transform_bbox(target_framework, mode, backend_compile): + trace_args = ( + torch.eye(3).unsqueeze(0), + torch.tensor([[ + [0., 0.], [2., 0.], [2., 2.], [0., 2.] + ]]), + ) + trace_kwargs = {'mode': 'xyxy'} + test_args = ( + torch.eye(3).unsqueeze(0), + torch.tensor([[ + [1., 1.], [3., 1.], [3., 3.], [1., 3.] + ]]), + ) + test_kwargs = {'mode': 'xyxy'} + _test_function( + kornia.geometry.bbox.transform_bbox, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_calibration.py b/integration_tests/kornia/geometry/test_calibration.py new file mode 100644 index 000000000000..9db55dc49409 --- /dev/null +++ b/integration_tests/kornia/geometry/test_calibration.py @@ -0,0 +1,158 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_undistort_image(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.eye(3)[None], + torch.rand(1, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(1, 3, 10, 10), + torch.eye(3)[None], + torch.rand(1, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.calibration.undistort_image, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_undistort_points(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.eye(3)[None], + torch.rand(1, 4), + ) + trace_kwargs = {'num_iters': 5} + test_args = ( + torch.rand(1, 6, 2), + torch.eye(3)[None], + torch.rand(1, 4), + ) + test_kwargs = {'num_iters': 5} + _test_function( + kornia.geometry.calibration.undistort_points, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_distort_points(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2), + torch.eye(3)[None], + torch.rand(1, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(1, 3, 2), + torch.eye(3)[None], + torch.rand(1, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.calibration.distort_points, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_tilt_projection(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor(0.1), + torch.tensor(0.2), + ) + trace_kwargs = {'return_inverse': False} + test_args = ( + torch.tensor(0.3), + torch.tensor(0.4), + ) + test_kwargs = {'return_inverse': False} + _test_function( + kornia.geometry.calibration.tilt_projection, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_solve_pnp_dlt(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[ + [5.0, -5.0, 0.0], [0.0, 0.0, 1.5], + [2.5, 3.0, 6.0], [9.0, -2.0, 3.0], + [-4.0, 5.0, 2.0], [-5.0, 5.0, 1.0] + ]], dtype=torch.float64), + torch.tensor([[ + [1409.1504, -800.936], [407.0207, -182.1229], + [392.7021, 177.9428], [1016.838, -2.9416], + [-63.1116, 142.9204], [-219.3874, 99.666] + ]], dtype=torch.float64), + torch.tensor([[ + [500.0, 0.0, 250.0], + [0.0, 500.0, 250.0], + [0.0, 0.0, 1.0] + ]], dtype=torch.float64), + ) + trace_kwargs = {'svd_eps': 1e-4} + test_args = ( + torch.tensor([[ + [10.0, -10.0, 0.0], [0.0, 0.0, 3.0], + [5.0, 6.0, 12.0], [18.0, -4.0, 6.0], + [-8.0, 10.0, 4.0], [-10.0, 10.0, 2.0] + ]], dtype=torch.float64), + torch.tensor([[ + [2818.3008, -1601.872], [814.0414, -364.2458], + [785.4042, 355.8856], [2033.676, -5.8832], + [-126.2232, 285.8408], [-438.7748, 199.332] + ]], dtype=torch.float64), + torch.tensor([[ + [1000.0, 0.0, 500.0], + [0.0, 1000.0, 500.0], + [0.0, 0.0, 1.0] + ]], dtype=torch.float64), + ) + test_kwargs = {'svd_eps': 1e-4} + _test_function( + kornia.geometry.calibration.solve_pnp_dlt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_camera.py b/integration_tests/kornia/geometry/test_camera.py new file mode 100644 index 000000000000..d6b9fef1567a --- /dev/null +++ b/integration_tests/kornia/geometry/test_camera.py @@ -0,0 +1,286 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_project_points_z1(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.project_points_z1, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_unproject_points_z1(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2), + torch.rand(1), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2), + torch.rand(1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.unproject_points_z1, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_dx_project_points_z1(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.dx_project_points_z1, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_project_points_orthographic(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.project_points_orthographic, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_unproject_points_orthographic(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 2), + torch.rand(2, 1), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + torch.rand(5, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.unproject_points_orthographic, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_dx_project_points_orthographic(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.dx_project_points_orthographic, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_distort_points_affine(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 2), + torch.rand(2, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + torch.rand(5, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.distort_points_affine, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_undistort_points_affine(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 2), + torch.rand(2, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + torch.rand(5, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.undistort_points_affine, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_dx_distort_points_affine(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 2), + torch.rand(2, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + torch.rand(5, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.dx_distort_points_affine, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_distort_points_kannala_brandt(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 2), + torch.rand(2, 8), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + torch.rand(5, 8), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.distort_points_kannala_brandt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_undistort_points_kannala_brandt(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 2), + torch.rand(2, 8), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + torch.rand(5, 8), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.undistort_points_kannala_brandt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_dx_distort_points_kannala_brandt(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 2), + torch.rand(2, 8), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + torch.rand(5, 8), + ) + test_kwargs = {} + _test_function( + kornia.geometry.camera.dx_distort_points_kannala_brandt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_conversions.py b/integration_tests/kornia/geometry/test_conversions.py new file mode 100644 index 000000000000..130a39552c39 --- /dev/null +++ b/integration_tests/kornia/geometry/test_conversions.py @@ -0,0 +1,880 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_rad2deg(target_framework, mode, backend_compile): + trace_args = (torch.tensor(3.1415926535),) + trace_kwargs = {} + test_args = (torch.rand(5, 3, 3),) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.rad2deg, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_deg2rad(target_framework, mode, backend_compile): + trace_args = (torch.tensor(180.),) + trace_kwargs = {} + test_args = (torch.rand(5, 3, 3),) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.deg2rad, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_pol2cart(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3, 3), torch.rand(1, 3, 3)) + trace_kwargs = {} + test_args = (torch.rand(5, 3, 3), torch.rand(5, 3, 3)) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.pol2cart, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_cart2pol(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3, 3), torch.rand(1, 3, 3)) + trace_kwargs = {'eps': 1.0e-8} + test_args = (torch.rand(5, 3, 3), torch.rand(5, 3, 3)) + test_kwargs = {'eps': 1.0e-8} + _test_function( + kornia.geometry.conversions.cart2pol, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_angle_to_rotation_matrix(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3),) + trace_kwargs = {} + test_args = (torch.rand(5, 3),) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.angle_to_rotation_matrix, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_convert_points_from_homogeneous(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.convert_points_from_homogeneous, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_convert_points_to_homogeneous(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.convert_points_to_homogeneous, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_convert_affinematrix_to_homography(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.convert_affinematrix_to_homography, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_denormalize_pixel_coordinates(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2), + 64, + 64, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + 128, + 128, + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.denormalize_pixel_coordinates, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_normalize_pixel_coordinates(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2), + 64, + 64, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + 128, + 128, + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.normalize_pixel_coordinates, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_denormalize_pixel_coordinates3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3), + 32, + 64, + 64, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3), + 64, + 128, + 128, + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.denormalize_pixel_coordinates3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_normalize_pixel_coordinates3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3), + 32, + 64, + 64, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3), + 64, + 128, + 128, + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.normalize_pixel_coordinates3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_normalize_points_with_intrinsics(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2), + torch.eye(3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + torch.eye(3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.normalize_points_with_intrinsics, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_denormalize_points_with_intrinsics(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2), + torch.eye(3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2), + torch.eye(3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.denormalize_points_with_intrinsics, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_normalize_homography(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 3), + (32, 32), + (64, 64), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 3), + (32, 32), # TODO: changing these values fails the test + (64, 64), + ) + test_kwargs = {} + _test_function( + kornia.geometry.normalize_homography, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_denormalize_homography(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 3), + (64, 64), + (32, 32), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 3), + (64, 64), + (32, 32), + ) + test_kwargs = {} + _test_function( + kornia.geometry.denormalize_homography, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_normalize_homography3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 4), + (8, 32, 32), + (16, 64, 64), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 4), + (8, 32, 32), + (16, 64, 64), + ) + test_kwargs = {} + _test_function( + kornia.geometry.normalize_homography3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_quaternion_to_axis_angle(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 4), + ) + trace_kwargs = {} + test_args = ( + torch.randn(5, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.quaternion_to_axis_angle, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_quaternion_to_rotation_matrix(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 4), + ) + trace_kwargs = {} + test_args = ( + torch.randn(5, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.quaternion_to_rotation_matrix, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_quaternion_log_to_exp(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 3), + ) + trace_kwargs = {'eps': 1.0e-12} + test_args = ( + torch.randn(5, 3), + ) + test_kwargs = {'eps': 1.0e-12} + _test_function( + kornia.geometry.conversions.quaternion_log_to_exp, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_quaternion_exp_to_log(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 4), + ) + trace_kwargs = {'eps': 1.0e-12} + test_args = ( + torch.randn(5, 4), + ) + test_kwargs = {'eps': 1.0e-12} + _test_function( + kornia.geometry.conversions.quaternion_exp_to_log, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_normalize_quaternion(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 4), + ) + trace_kwargs = {'eps': 1.0e-12} + test_args = ( + torch.randn(5, 4), + ) + test_kwargs = {'eps': 1.0e-12} + _test_function( + kornia.geometry.conversions.normalize_quaternion, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_vector_to_skew_symmetric_matrix(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([1.0, 2.0, 3.0]), + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.vector_to_skew_symmetric_matrix, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rotation_matrix_to_axis_angle(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.rotation_matrix_to_axis_angle, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rotation_matrix_to_quaternion(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 3), + ) + trace_kwargs = {'eps': 1e-8} + test_args = ( + torch.rand(5, 3, 3), + ) + test_kwargs = {'eps': 1e-4} + _test_function( + kornia.geometry.conversions.rotation_matrix_to_quaternion, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_axis_angle_to_quaternion(target_framework, mode, backend_compile): + trace_args = ( + torch.rand((1, 3)), + ) + trace_kwargs = {} + test_args = ( + torch.rand((2, 3)), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.axis_angle_to_quaternion, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_axis_angle_to_rotation_matrix(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[0., 0., 0.], [1.5708, 0., 0.]]), + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[0., 0., 0.], [1.5708, 0., 0.]]), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.axis_angle_to_rotation_matrix, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_quaternion_from_euler(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor(0.), + torch.tensor(0.), + torch.tensor(0.), + ) + trace_kwargs = {} + test_args = ( + torch.tensor([0., 1., 2.]), + torch.tensor([2., 1., 0.]), + torch.tensor([1., 2., 0.]), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.quaternion_from_euler, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_euler_from_quaternion(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor(1.), + torch.tensor(0.), + torch.tensor(0.), + torch.tensor(0.), + ) + trace_kwargs = {} + test_args = ( + torch.tensor(2.), + torch.tensor(1.), + torch.tensor(1.), + torch.tensor(1.), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.euler_from_quaternion, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_Rt_to_matrix4x4(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 3, 3), + torch.rand(1, 3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.randn(5, 3, 3), + torch.rand(5, 3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.Rt_to_matrix4x4, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_matrix4x4_to_Rt(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.matrix4x4_to_Rt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_worldtocam_to_camtoworld_Rt(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 3, 3), + torch.rand(1, 3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.randn(5, 3, 3), + torch.rand(5, 3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.worldtocam_to_camtoworld_Rt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_camtoworld_to_worldtocam_Rt(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 3, 3), + torch.rand(1, 3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.randn(5, 3, 3), + torch.rand(5, 3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.camtoworld_to_worldtocam_Rt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_camtoworld_graphics_to_vision_4x4(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.camtoworld_graphics_to_vision_4x4, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_camtoworld_vision_to_graphics_4x4(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.camtoworld_vision_to_graphics_4x4, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_camtoworld_graphics_to_vision_Rt(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 3, 3), + torch.rand(1, 3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.randn(5, 3, 3), + torch.rand(5, 3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.camtoworld_graphics_to_vision_Rt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_camtoworld_vision_to_graphics_Rt(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 3, 3), + torch.rand(1, 3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.randn(5, 3, 3), + torch.rand(5, 3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.camtoworld_vision_to_graphics_Rt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_ARKitQTVecs_to_ColmapQTVecs(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4), + torch.rand(1, 3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4), + torch.rand(5, 3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.conversions.ARKitQTVecs_to_ColmapQTVecs, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_depth.py b/integration_tests/kornia/geometry/test_depth.py new file mode 100644 index 000000000000..41619c6d008d --- /dev/null +++ b/integration_tests/kornia/geometry/test_depth.py @@ -0,0 +1,158 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_depth_from_disparity(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(4, 1, 4, 4), + torch.rand(1), + torch.rand(1), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 5, 5), + torch.rand(1), + torch.rand(1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.depth.depth_from_disparity, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_depth_to_3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 4, 4), + torch.rand(1, 3, 3), + ) + trace_kwargs = {'normalize_points': False} + test_args = ( + torch.rand(5, 1, 5, 5), + torch.rand(5, 3, 3), + ) + test_kwargs = {'normalize_points': False} + _test_function( + kornia.geometry.depth.depth_to_3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_depth_to_3d_v2(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(4, 4), + torch.eye(3), + ) + trace_kwargs = {'normalize_points': False} + test_args = ( + torch.rand(5, 5), + torch.eye(3), + ) + test_kwargs = {'normalize_points': False} + _test_function( + kornia.geometry.depth.depth_to_3d_v2, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_unproject_meshgrid(target_framework, mode, backend_compile): + trace_args = ( + 4, + 4, + torch.eye(3), + ) + trace_kwargs = {'normalize_points': False, 'device': 'cpu', 'dtype': torch.float32} + test_args = ( + 5, + 5, + torch.eye(3), + ) + test_kwargs = {'normalize_points': False, 'device': 'cpu', 'dtype': torch.float32} + _test_function( + kornia.geometry.depth.unproject_meshgrid, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_depth_to_normals(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 4, 4), + torch.eye(3)[None], + ) + trace_kwargs = {'normalize_points': False} + test_args = ( + torch.rand(1, 1, 5, 5), + torch.eye(3)[None], + ) + test_kwargs = {'normalize_points': False} + _test_function( + kornia.geometry.depth.depth_to_normals, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_warp_frame_depth(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + torch.rand(1, 1, 4, 4), + torch.eye(4)[None], + torch.eye(3)[None], + ) + trace_kwargs = {'normalize_points': False} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(5, 1, 5, 5), + torch.eye(4)[None].repeat(5, 1, 1), + torch.eye(3)[None].repeat(5, 1, 1), + ) + test_kwargs = {'normalize_points': False} + _test_function( + kornia.geometry.depth.warp_frame_depth, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_epipolar.py b/integration_tests/kornia/geometry/test_epipolar.py new file mode 100644 index 000000000000..12f027c1bad0 --- /dev/null +++ b/integration_tests/kornia/geometry/test_epipolar.py @@ -0,0 +1,627 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +# NOTE: takes a while to run this test +# def test_find_essential(target_framework, mode, backend_compile): +# trace_args = ( +# torch.rand(1, 8, 2), +# torch.rand(1, 8, 2), +# ) +# trace_kwargs = {'weights': torch.rand(1, 8)} +# test_args = ( +# torch.rand(5, 8, 2), +# torch.rand(5, 8, 2), +# ) +# test_kwargs = {'weights': torch.rand(5, 8)} +# _test_function( +# kornia.geometry.epipolar.find_essential, +# trace_args, +# trace_kwargs, +# test_args, +# test_kwargs, +# target_framework, +# backend_compile, +# tolerance=1e-4, +# mode=mode, +# ) + + +def test_essential_from_fundamental(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3), + torch.rand(3, 3), + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 3), + torch.rand(3, 3), + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.essential_from_fundamental, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_essential_from_Rt(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3), + torch.rand(3, 1), + torch.rand(3, 3), + torch.rand(3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 3), + torch.rand(3, 1), + torch.rand(3, 3), + torch.rand(3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.essential_from_Rt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_decompose_essential_matrix(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.decompose_essential_matrix, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_motion_from_essential(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.motion_from_essential, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_motion_from_essential_choose_solution(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3), + torch.rand(3, 3), + torch.rand(3, 3), + torch.rand(8, 2), + torch.rand(8, 2), + ) + trace_kwargs = {'mask': torch.ones(8).bool()} + test_args = ( + torch.rand(3, 3), + torch.rand(3, 3), + torch.rand(3, 3), + torch.rand(8, 2), + torch.rand(8, 2), + ) + test_kwargs = {'mask': torch.ones(8).bool()} + _test_function( + kornia.geometry.epipolar.motion_from_essential_choose_solution, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_relative_camera_motion(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3), + torch.rand(3, 1), + torch.rand(3, 3), + torch.rand(3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 3), + torch.rand(3, 1), + torch.rand(3, 3), + torch.rand(3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.relative_camera_motion, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_find_fundamental(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 8, 2), + torch.rand(2, 8, 2), + ) + trace_kwargs = {'weights': torch.rand(2, 8), 'method': '8POINT'} + test_args = ( + torch.rand(5, 8, 2), + torch.rand(5, 8, 2), + ) + test_kwargs = {'weights': torch.rand(5, 8), 'method': '8POINT'} + _test_function( + kornia.geometry.epipolar.find_fundamental, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_fundamental_from_essential(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3), + torch.rand(3, 3), + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 3), + torch.rand(3, 3), + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.fundamental_from_essential, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_fundamental_from_projections(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 4), + torch.rand(3, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 4), + torch.rand(3, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.fundamental_from_projections, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_compute_correspond_epilines(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 8, 2), + torch.rand(2, 3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 8, 2), + torch.rand(5, 3, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.compute_correspond_epilines, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_normalize_points(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 8, 2), + ) + trace_kwargs = {'eps': 1e-8} + test_args = ( + torch.rand(5, 8, 2), + ) + test_kwargs = {'eps': 1e-8} + _test_function( + kornia.geometry.epipolar.normalize_points, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_normalize_transformation(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3), + ) + trace_kwargs = {'eps': 1e-8} + test_args = ( + torch.rand(3, 3), + ) + test_kwargs = {'eps': 1e-8} + _test_function( + kornia.geometry.epipolar.normalize_transformation, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_perpendicular(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2, 3), + torch.rand(1, 2, 2), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 5, 3), + torch.rand(2, 5, 2), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.get_perpendicular, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_closest_point_on_epipolar_line(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2, 2), + torch.rand(1, 2, 2), + torch.rand(2, 3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(1, 5, 2), + torch.rand(1, 5, 2), + torch.rand(5, 3, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.get_closest_point_on_epipolar_line, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_sampson_epipolar_distance(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.rand(1, 4, 2), + torch.rand(1, 3, 3), + ) + trace_kwargs = {'squared': True, 'eps': 1e-8} + test_args = ( + torch.rand(5, 4, 2), + torch.rand(5, 4, 2), + torch.rand(5, 3, 3), + ) + test_kwargs = {'squared': True, 'eps': 1e-8} + _test_function( + kornia.geometry.epipolar.sampson_epipolar_distance, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_symmetrical_epipolar_distance(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.rand(1, 4, 2), + torch.rand(1, 3, 3), + ) + trace_kwargs = {'squared': True, 'eps': 1e-8} + test_args = ( + torch.rand(5, 4, 2), + torch.rand(5, 4, 2), + torch.rand(5, 3, 3), + ) + test_kwargs = {'squared': True, 'eps': 1e-8} + _test_function( + kornia.geometry.epipolar.symmetrical_epipolar_distance, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_left_to_right_epipolar_distance(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.rand(1, 4, 2), + torch.rand(1, 3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 2), + torch.rand(5, 4, 2), + torch.rand(5, 3, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.left_to_right_epipolar_distance, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_right_to_left_epipolar_distance(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.rand(1, 4, 2), + torch.rand(1, 3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 2), + torch.rand(5, 4, 2), + torch.rand(5, 3, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.right_to_left_epipolar_distance, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_projection_from_KRt(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3), + torch.eye(3), + torch.rand(3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 3, 3), + torch.stack([torch.eye(3) for _ in range(2)]), + torch.rand(2, 3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.projection_from_KRt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_projections_from_fundamental(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3, 3),) + trace_kwargs = {} + test_args = (torch.rand(2, 3, 3),) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.projections_from_fundamental, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_intrinsics_like(target_framework, mode, backend_compile): + trace_args = (0.5, torch.rand(1, 3, 256, 256)) + trace_kwargs = {} + test_args = (0.8, torch.rand(2, 3, 256, 256)) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.intrinsics_like, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_scale_intrinsics(target_framework, mode, backend_compile): + trace_args = (torch.rand(3, 3), 0.5) + trace_kwargs = {} + test_args = (torch.rand(2, 3, 3), 1.2) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.scale_intrinsics, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_random_intrinsics(target_framework, mode, backend_compile): + trace_args = (0.1, 1.0) + trace_kwargs = {} + test_args = (0.2, 2.0) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.random_intrinsics, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_cross_product_matrix(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3),) + trace_kwargs = {} + test_args = (torch.rand(5, 3),) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.cross_product_matrix, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_triangulate_points(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4), + torch.rand(1, 3, 4), + torch.rand(1, 10, 2), + torch.rand(1, 10, 2), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 3, 4), + torch.rand(2, 3, 4), + torch.rand(2, 20, 2), + torch.rand(2, 20, 2), + ) + test_kwargs = {} + _test_function( + kornia.geometry.epipolar.triangulate_points, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_homography.py b/integration_tests/kornia/geometry/test_homography.py new file mode 100644 index 000000000000..b5d67a38f6ef --- /dev/null +++ b/integration_tests/kornia/geometry/test_homography.py @@ -0,0 +1,208 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_find_homography_dlt(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.rand(1, 4, 2), + ) + trace_kwargs = {'weights': torch.rand(1, 4), 'solver': 'svd'} + test_args = ( + torch.rand(5, 4, 2), + torch.rand(5, 4, 2), + ) + test_kwargs = {'weights': torch.rand(5, 4), 'solver': 'svd'} + _test_function( + kornia.geometry.homography.find_homography_dlt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_find_homography_dlt_iterated(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.rand(1, 4, 2), + torch.rand(1, 4), + ) + trace_kwargs = {'soft_inl_th': 3.0, 'n_iter': 5} + test_args = ( + torch.rand(5, 4, 2), + torch.rand(5, 4, 2), + torch.rand(5, 4), + ) + test_kwargs = {'soft_inl_th': 4.0, 'n_iter': 5} + _test_function( + kornia.geometry.homography.find_homography_dlt_iterated, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_find_homography_lines_dlt(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2, 2), + torch.rand(1, 4, 2, 2), + ) + trace_kwargs = {'weights': torch.rand(1, 4)} + test_args = ( + torch.rand(5, 4, 2, 2), + torch.rand(5, 4, 2, 2), + ) + test_kwargs = {'weights': torch.rand(5, 4)} + _test_function( + kornia.geometry.homography.find_homography_lines_dlt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_find_homography_lines_dlt_iterated(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2, 2), + torch.rand(1, 4, 2, 2), + torch.rand(1, 4), + ) + trace_kwargs = {'soft_inl_th': 4.0, 'n_iter': 5} + test_args = ( + torch.rand(5, 4, 2, 2), + torch.rand(5, 4, 2, 2), + torch.rand(5, 4), + ) + test_kwargs = {'soft_inl_th': 3.0, 'n_iter': 5} + _test_function( + kornia.geometry.homography.find_homography_lines_dlt_iterated, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_line_segment_transfer_error_one_way(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2, 2), + torch.rand(1, 4, 2, 2), + torch.rand(1, 3, 3), + ) + trace_kwargs = {'squared': True} + test_args = ( + torch.rand(5, 4, 2, 2), + torch.rand(5, 4, 2, 2), + torch.rand(5, 3, 3), + ) + test_kwargs = {'squared': True} + _test_function( + kornia.geometry.homography.line_segment_transfer_error_one_way, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_oneway_transfer_error(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.rand(1, 4, 2), + torch.rand(1, 3, 3), + ) + trace_kwargs = {'squared': False, 'eps': 1e-8} + test_args = ( + torch.rand(5, 4, 2), + torch.rand(5, 4, 2), + torch.rand(5, 3, 3), + ) + test_kwargs = {'squared': False, 'eps': 1e-7} + _test_function( + kornia.geometry.homography.oneway_transfer_error, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_sample_is_valid_for_homography(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.rand(1, 4, 2), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 2), + torch.rand(5, 4, 2), + ) + test_kwargs = {} + _test_function( + kornia.geometry.homography.sample_is_valid_for_homography, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_symmetric_transfer_error(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2), + torch.rand(1, 4, 2), + torch.rand(1, 3, 3), + ) + trace_kwargs = {'squared': True, 'eps': 1e-8} + test_args = ( + torch.rand(5, 4, 2), + torch.rand(5, 4, 2), + torch.rand(5, 3, 3), + ) + test_kwargs = {'squared': True, 'eps': 1e-7} + _test_function( + kornia.geometry.homography.symmetric_transfer_error, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_linalg.py b/integration_tests/kornia/geometry/test_linalg.py new file mode 100644 index 000000000000..91391efc09af --- /dev/null +++ b/integration_tests/kornia/geometry/test_linalg.py @@ -0,0 +1,194 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_relative_transformation(target_framework, mode, backend_compile): + trace_args = ( + torch.eye(4), + torch.eye(4), + ) + trace_kwargs = {} + test_args = ( + torch.eye(4).repeat(5, 1, 1), + torch.eye(4).repeat(5, 1, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.linalg.relative_transformation, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_compose_transformations(target_framework, mode, backend_compile): + trace_args = ( + torch.eye(4), + torch.eye(4), + ) + trace_kwargs = {} + test_args = ( + torch.eye(4).repeat(5, 1, 1), + torch.eye(4).repeat(5, 1, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.linalg.compose_transformations, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_inverse_transformation(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.linalg.inverse_transformation, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_transform_points(target_framework, mode, backend_compile): + trace_args = ( + torch.eye(4).view(1, 4, 4), + torch.rand(2, 4, 3), + ) + trace_kwargs = {} + test_args = ( + torch.eye(4).repeat(5, 1, 1), + torch.rand(5, 4, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.linalg.transform_points, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_point_line_distance(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3), + torch.rand(3), + ) + trace_kwargs = {'eps': 1e-9} + test_args = ( + torch.rand(5, 3), + torch.rand(5, 3), + ) + test_kwargs = {'eps': 1e-9} + _test_function( + kornia.geometry.linalg.point_line_distance, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_squared_norm(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(5), + ) + trace_kwargs = {'keepdim': False} + test_args = ( + torch.rand(10, 5), + ) + test_kwargs = {'keepdim': False} + _test_function( + kornia.geometry.linalg.squared_norm, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_batched_dot_product(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 5), + torch.rand(3, 5), + ) + trace_kwargs = {'keepdim': False} + test_args = ( + torch.rand(5, 3, 5), + torch.rand(5, 3, 5), + ) + test_kwargs = {'keepdim': False} + _test_function( + kornia.geometry.linalg.batched_dot_product, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_euclidean_distance(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 5), + torch.rand(3, 5), + ) + trace_kwargs = {'keepdim': False, 'eps': 1e-6} + test_args = ( + torch.rand(5, 3, 5), + torch.rand(5, 3, 5), + ) + test_kwargs = {'keepdim': False, 'eps': 1e-6} + _test_function( + kornia.geometry.linalg.euclidean_distance, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_solvers.py b/integration_tests/kornia/geometry/test_solvers.py new file mode 100644 index 000000000000..05a531972d1f --- /dev/null +++ b/integration_tests/kornia/geometry/test_solvers.py @@ -0,0 +1,108 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_solve_quadratic(target_framework, mode, backend_compile): + trace_args = (torch.tensor([[1., -3., 2.]]),) + trace_kwargs = {} + test_args = (torch.tensor([[1., -2., 1.]]),) + test_kwargs = {} + _test_function( + kornia.geometry.solvers.solve_quadratic, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_solve_cubic(target_framework, mode, backend_compile): + trace_args = (torch.tensor([[1., -6., 11., -6.]]),) + trace_kwargs = {} + test_args = (torch.tensor([[1., -4., 6., -4.]]),) + test_kwargs = {} + _test_function( + kornia.geometry.solvers.solve_cubic, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_multiply_deg_one_poly(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 9), + torch.rand(1, 9), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 9), + torch.rand(3, 9), + ) + test_kwargs = {} + _test_function( + kornia.geometry.solvers.multiply_deg_one_poly, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_multiply_deg_two_one_poly(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 20), + torch.rand(1, 20), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 20), + torch.rand(3, 20), + ) + test_kwargs = {} + _test_function( + kornia.geometry.solvers.multiply_deg_two_one_poly, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_determinant_to_polynomial(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3, 13),) + trace_kwargs = {} + test_args = (torch.rand(5, 3, 13),) + test_kwargs = {} + _test_function( + kornia.geometry.solvers.determinant_to_polynomial, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_subpix.py b/integration_tests/kornia/geometry/test_subpix.py new file mode 100644 index 000000000000..c21fd4d497a7 --- /dev/null +++ b/integration_tests/kornia/geometry/test_subpix.py @@ -0,0 +1,278 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_conv_soft_argmax2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(20, 16, 50, 32), + ) + trace_kwargs = { + 'kernel_size': (3, 3), + 'stride': (1, 1), + 'padding': (1, 1), + 'temperature': torch.tensor(1.0), + 'normalized_coordinates': True, + 'eps': 1e-8, + 'output_value': True, + } + test_args = ( + torch.rand(10, 16, 50, 32), + ) + test_kwargs = { + 'kernel_size': (3, 3), + 'stride': (1, 1), + 'padding': (1, 1), + 'temperature': torch.tensor(0.5), + 'normalized_coordinates': True, + 'eps': 1e-8, + 'output_value': True, + } + _test_function( + kornia.geometry.subpix.conv_soft_argmax2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_conv_soft_argmax3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(20, 16, 3, 50, 32), + ) + trace_kwargs = { + 'kernel_size': (3, 3, 3), + 'stride': (1, 1, 1), + 'padding': (1, 1, 1), + 'temperature': torch.tensor(1.0), + 'normalized_coordinates': False, + 'eps': 1e-8, + 'output_value': True, + 'strict_maxima_bonus': 0.0, + } + test_args = ( + torch.rand(10, 16, 5, 50, 32), + ) + test_kwargs = { + 'kernel_size': (3, 3, 3), + 'stride': (1, 1, 1), + 'padding': (1, 1, 1), + 'temperature': torch.tensor(0.5), + 'normalized_coordinates': False, + 'eps': 1e-8, + 'output_value': True, + 'strict_maxima_bonus': 0.0, + } + _test_function( + kornia.geometry.subpix.conv_soft_argmax3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_conv_quad_interp3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(20, 16, 3, 50, 32), + ) + trace_kwargs = { + 'strict_maxima_bonus': 10.0, + 'eps': 1e-7, + } + test_args = ( + torch.rand(10, 16, 5, 50, 32), + ) + test_kwargs = { + 'strict_maxima_bonus': 5.0, + 'eps': 1e-7, + } + _test_function( + kornia.geometry.subpix.conv_quad_interp3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_spatial_softmax2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + ) + trace_kwargs = { + 'temperature': torch.tensor(1.0), + } + test_args = ( + torch.rand(10, 1, 5, 5), + ) + test_kwargs = { + 'temperature': torch.tensor(0.5), + } + _test_function( + kornia.geometry.subpix.spatial_softmax2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_spatial_expectation2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + ) + trace_kwargs = { + 'normalized_coordinates': False, + } + test_args = ( + torch.rand(10, 1, 5, 5), + ) + test_kwargs = { + 'normalized_coordinates': False, + } + _test_function( + kornia.geometry.subpix.spatial_expectation2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_spatial_soft_argmax2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + ) + trace_kwargs = { + 'temperature': torch.tensor(1.0), + 'normalized_coordinates': True, + } + test_args = ( + torch.rand(10, 1, 5, 5), + ) + test_kwargs = { + 'temperature': torch.tensor(0.5), + 'normalized_coordinates': True, + } + _test_function( + kornia.geometry.subpix.spatial_soft_argmax2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_render_gaussian2d(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[1.0, 1.0]]), + torch.tensor([[1.0, 1.0]]), + (5, 5), + ) + trace_kwargs = { + 'normalized_coordinates': False, + } + test_args = ( + torch.tensor([[2.0, 2.0]]), + torch.tensor([[0.5, 0.5]]), + (10, 10), + ) + test_kwargs = { + 'normalized_coordinates': False, + } + _test_function( + kornia.geometry.subpix.render_gaussian2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_nms2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + (3, 3), + ) + trace_kwargs = { + 'mask_only': False, + } + test_args = ( + torch.rand(10, 1, 5, 5), + (3, 3), + ) + test_kwargs = { + 'mask_only': False, + } + _test_function( + kornia.geometry.subpix.nms2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_nms3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5, 5), + (3, 3, 3), + ) + trace_kwargs = { + 'mask_only': False, + } + test_args = ( + torch.rand(10, 1, 5, 5, 5), + (3, 3, 3), + ) + test_kwargs = { + 'mask_only': False, + } + _test_function( + kornia.geometry.subpix.nms3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/geometry/test_transform.py b/integration_tests/kornia/geometry/test_transform.py new file mode 100644 index 000000000000..17559beac8e0 --- /dev/null +++ b/integration_tests/kornia/geometry/test_transform.py @@ -0,0 +1,1014 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_warp_perspective(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.eye(3).unsqueeze(0), + (4, 4), + ) + trace_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.eye(3).unsqueeze(0), + (4, 4), # TODO: changing this fails the test + ) + test_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + _test_function( + kornia.geometry.transform.warp_perspective, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_warp_perspective3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5, 5), + torch.eye(4).unsqueeze(0), + (4, 4, 4), + ) + trace_kwargs = {'flags': 'bilinear', 'border_mode': 'zeros', 'align_corners': False} + test_args = ( + torch.rand(5, 3, 5, 5, 5), + torch.eye(4).unsqueeze(0), + (4, 4, 4), + ) + test_kwargs = {'flags': 'bilinear', 'border_mode': 'zeros', 'align_corners': False} + _test_function( + kornia.geometry.transform.warp_perspective3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_warp_affine(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.eye(2, 3).unsqueeze(0), + (4, 4), + ) + trace_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.eye(2, 3).unsqueeze(0), + (4, 4), + ) + test_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + _test_function( + kornia.geometry.transform.warp_affine, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_warp_affine3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5, 5), + torch.eye(3, 4).unsqueeze(0), + (4, 4, 4), + ) + trace_kwargs = {'flags': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + test_args = ( + torch.rand(5, 3, 5, 5, 5), + torch.eye(3, 4).unsqueeze(0), + (4, 4, 4), + ) + test_kwargs = {'flags': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + _test_function( + kornia.geometry.transform.warp_affine3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_warp_image_tps(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 32, 32), + torch.rand(1, 5, 2), + torch.rand(1, 5, 2), + torch.rand(1, 3, 2), + ) + trace_kwargs = {'align_corners': False} + test_args = ( + torch.rand(5, 3, 32, 32), + torch.rand(5, 5, 2), + torch.rand(5, 5, 2), + torch.rand(5, 3, 2), + ) + test_kwargs = {'align_corners': False} + _test_function( + kornia.geometry.transform.warp_image_tps, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_warp_points_tps(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2), + torch.rand(1, 5, 2), + torch.rand(1, 5, 2), + torch.rand(1, 3, 2), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 10, 2), + torch.rand(5, 10, 2), + torch.rand(5, 10, 2), + torch.rand(5, 3, 2), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.warp_points_tps, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_warp_grid(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 5, 2), + torch.eye(3).unsqueeze(0), + ) + trace_kwargs = {} + test_args = ( + torch.rand(1, 10, 10, 2), + torch.eye(3).unsqueeze(0), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.warp_grid, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_warp_grid3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 5, 5, 3), + torch.eye(4).unsqueeze(0), + ) + trace_kwargs = {} + test_args = ( + torch.rand(1, 10, 10, 10, 3), + torch.eye(4).unsqueeze(0), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.warp_grid3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_remap(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(1, 5, 5), + torch.rand(1, 5, 5), + ) + trace_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': None, 'normalized_coordinates': False} + test_args = ( + torch.rand(1, 3, 10, 10), + torch.rand(1, 10, 10), + torch.rand(1, 10, 10), + ) + test_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': None, 'normalized_coordinates': False} + _test_function( + kornia.geometry.transform.remap, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_affine(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2, 3, 5), + torch.eye(2, 3).unsqueeze(0), + ) + trace_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + test_args = ( + torch.rand(5, 2, 3, 5), + torch.eye(2, 3).unsqueeze(0).repeat(5, 1, 1), + ) + test_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + _test_function( + kornia.geometry.transform.affine, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rotate(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + torch.tensor([90.]), + ) + trace_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + test_args = ( + torch.rand(5, 3, 4, 4), + torch.tensor([45., 45., 45., 45., 45.]), + ) + test_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + _test_function( + kornia.geometry.transform.rotate, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_translate(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + torch.tensor([[1., 0.]]), + ) + trace_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + test_args = ( + torch.rand(5, 3, 4, 4), + torch.tensor([[1., 0.]]).repeat(5, 1), + ) + test_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + _test_function( + kornia.geometry.transform.translate, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_scale(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + torch.rand(1), + ) + trace_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + test_args = ( + torch.rand(5, 3, 4, 4), + torch.rand(5), + ) + test_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': True} + _test_function( + kornia.geometry.transform.scale, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_shear(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + torch.tensor([[0.5, 0.0]]), + ) + trace_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': False} + test_args = ( + torch.rand(5, 3, 4, 4), + torch.tensor([[0.1, 0.3]]).repeat(5, 1), + ) + test_kwargs = {'mode': 'bilinear', 'padding_mode': 'zeros', 'align_corners': False} + _test_function( + kornia.geometry.transform.shear, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_hflip(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.hflip, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_vflip(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.vflip, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rot180(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.rot180, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_resize(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + (6, 8), + ) + trace_kwargs = {'interpolation': 'bilinear'} + test_args = ( + torch.rand(5, 3, 4, 4), + (12, 16), + ) + test_kwargs = {'interpolation': 'bilinear'} + _test_function( + kornia.geometry.transform.resize, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rescale(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + (2, 3), + ) + trace_kwargs = {'interpolation': 'bilinear'} + test_args = ( + torch.rand(5, 3, 4, 4), + (1.5, 2), + ) + test_kwargs = {'interpolation': 'bilinear'} + _test_function( + kornia.geometry.transform.rescale, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_elastic_transform2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(1, 2, 5, 5), + ) + trace_kwargs = {'kernel_size': (63, 63), 'sigma': (32.0, 32.0), 'alpha': (1.0, 1.0)} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(5, 2, 5, 5), + ) + test_kwargs = {'kernel_size': (31, 31), 'sigma': (16.0, 16.0), 'alpha': (0.5, 0.5)} + _test_function( + kornia.geometry.transform.elastic_transform2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_pyrdown(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 4, 4), + ) + trace_kwargs = {'border_type': 'reflect', 'align_corners': False, 'factor': 2.0} + test_args = ( + torch.rand(5, 1, 8, 8), + ) + test_kwargs = {'border_type': 'reflect', 'align_corners': False, 'factor': 2.0} + _test_function( + kornia.geometry.transform.pyrdown, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_pyrup(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + ) + trace_kwargs = {'border_type': 'reflect', 'align_corners': False} + test_args = ( + torch.rand(5, 1, 4, 4), + ) + test_kwargs = {'border_type': 'reflect', 'align_corners': False} + _test_function( + kornia.geometry.transform.pyrup, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_build_pyramid(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 8, 8), + 3, + ) + trace_kwargs = {'border_type': 'reflect', 'align_corners': False} + test_args = ( + torch.rand(5, 3, 16, 16), + 4, + ) + test_kwargs = {'border_type': 'reflect', 'align_corners': False} + _test_function( + kornia.geometry.transform.build_pyramid, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_build_laplacian_pyramid(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 8, 8), + 3, + ) + trace_kwargs = {'border_type': 'reflect', 'align_corners': False} + test_args = ( + torch.rand(5, 3, 16, 16), + 4, + ) + test_kwargs = {'border_type': 'reflect', 'align_corners': False} + _test_function( + kornia.geometry.transform.build_laplacian_pyramid, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_upscale_double(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 8, 8), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.upscale_double, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_perspective_transform(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[[0., 0.], [1., 0.], [1., 1.], [0., 1.]]]), + torch.tensor([[[1., 0.], [0., 0.], [0., 1.], [1., 1.]]]) + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[[0., 0.], [2., 0.], [2., 2.], [0., 2.]]]), + torch.tensor([[[2., 0.], [0., 0.], [0., 2.], [2., 2.]]]) + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.get_perspective_transform, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_perspective_transform3d(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[[0., 0., 0.], [1., 0., 0.], [1., 1., 0.], [0., 1., 0.]]]), + torch.tensor([[[1., 0., 0.], [0., 0., 0.], [0., 1., 0.], [1., 1., 0.]]]) + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[[0., 0., 0.], [2., 0., 0.], [2., 2., 0.], [0., 2., 0.]]]), + torch.tensor([[[2., 0., 0.], [0., 0., 0.], [0., 2., 0.], [2., 2., 0.]]]) + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.get_perspective_transform3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_projective_transform(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[0., 0., 0.]]), + torch.tensor([[30., 45., 60.]]), + torch.tensor([[1., 1., 1.]]) + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[1., 1., 1.]]), + torch.tensor([[45., 60., 75.]]), + torch.tensor([[1.5, 1.5, 1.5]]) + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.get_projective_transform, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_rotation_matrix2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2), + 45. * torch.ones(1), + torch.rand(1, 2) + ) + trace_kwargs = {} + test_args = ( + torch.rand(1, 2), + 90. * torch.ones(1), + 2.0 * torch.ones(1, 2) + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.get_rotation_matrix2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_shear_matrix2d(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[0., 0.]]), + torch.tensor([1.0]), + torch.tensor([0.5]) + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[1., 1.]]), + torch.tensor([1.5]), + torch.tensor([0.75]) + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.get_shear_matrix2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_shear_matrix3d(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[0., 0., 0.]]), + torch.tensor([1.0]), + torch.tensor([0.5]), + torch.tensor([0.2]), + torch.tensor([0.3]), + torch.tensor([0.4]), + torch.tensor([0.6]) + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[1., 1., 1.]]), + torch.tensor([1.5]), + torch.tensor([0.75]), + torch.tensor([0.4]), + torch.tensor([0.5]), + torch.tensor([0.6]), + torch.tensor([0.8]) + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.get_shear_matrix3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_affine_matrix2d(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[0., 0.]]), + torch.tensor([[0., 0.]]), + torch.ones(1, 2), + 45. * torch.ones(1), + torch.tensor([1.0]), + torch.tensor([0.5]) + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[1., 1.]]), + torch.tensor([[1., 1.]]), + 2.0 * torch.ones(1, 2), + 90. * torch.ones(1), + torch.tensor([1.5]), + torch.tensor([0.75]) + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.get_affine_matrix2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_affine_matrix3d(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[0., 0., 0.]]), + torch.tensor([[0., 0., 0.]]), + torch.ones(1, 3), + torch.tensor([[45., 45., 45.]]), + torch.tensor([1.0]), + torch.tensor([0.5]), + torch.tensor([0.2]), + torch.tensor([0.3]), + torch.tensor([0.4]), + torch.tensor([0.6]) + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[1., 1., 1.]]), + torch.tensor([[1., 1., 1.]]), + 2.0 * torch.ones(1, 3), + torch.tensor([[90., 90., 90.]]), + torch.tensor([1.5]), + torch.tensor([0.75]), + torch.tensor([0.4]), + torch.tensor([0.5]), + torch.tensor([0.6]), + torch.tensor([0.8]) + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.get_affine_matrix3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_invert_affine_transform(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 2, 3), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.invert_affine_transform, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_projection_from_Rt(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 3), + torch.rand(1, 3, 1), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 3), + torch.rand(5, 3, 1), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.projection_from_Rt, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_tps_transform(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2), + torch.rand(1, 5, 2), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 5, 2), + torch.rand(5, 5, 2), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.get_tps_transform, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_crop_by_indices(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 64, 64), + torch.tensor([[[10, 10], [50, 10], [50, 50], [10, 50]]], dtype=torch.float32), + ) + trace_kwargs = {'size': (40, 40), 'interpolation': 'bilinear'} + test_args = ( + torch.rand(5, 3, 64, 64), + torch.tensor([[[10, 10], [50, 10], [50, 50], [10, 50]]]*5, dtype=torch.float32), + ) + test_kwargs = {'size': (40, 40), 'interpolation': 'bilinear'} + _test_function( + kornia.geometry.transform.crop_by_indices, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_crop_by_boxes(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 64, 64), + torch.tensor([[[10, 10], [50, 10], [50, 50], [10, 50]]], dtype=torch.float32), + torch.tensor([[[0, 0], [40, 0], [40, 40], [0, 40]]], dtype=torch.float32), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 64, 64), + torch.tensor([[[10, 10], [50, 10], [50, 50], [10, 50]]]*5, dtype=torch.float32), + torch.tensor([[[0, 0], [40, 0], [40, 40], [0, 40]]]*5, dtype=torch.float32), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.crop_by_boxes, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_center_crop(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 64, 64), + (32, 32), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 64, 64), + (32, 32), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.center_crop, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_crop_and_resize(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 64, 64), + torch.tensor([[[10, 10], [50, 10], [50, 50], [10, 50]]], dtype=torch.float32), + (32, 32), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 64, 64), + torch.tensor([[[10, 10], [50, 10], [50, 50], [10, 50]]]*5, dtype=torch.float32), + (32, 32), + ) + test_kwargs = {} + _test_function( + kornia.geometry.transform.crop_and_resize, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/test_color.py b/integration_tests/kornia/test_color.py new file mode 100644 index 000000000000..9f9f1da6a4fb --- /dev/null +++ b/integration_tests/kornia/test_color.py @@ -0,0 +1,742 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + + +def test_rgb_to_grayscale(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_grayscale, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_bgr_to_grayscale(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.color.bgr_to_grayscale, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_grayscale_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.color.grayscale_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_bgr(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3, 5, 5),) + trace_kwargs = {} + test_args = (torch.rand(5, 3, 5, 5),) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_bgr, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_bgr_to_rgb(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3, 5, 5),) + trace_kwargs = {} + test_args = (torch.rand(5, 3, 5, 5),) + test_kwargs = {} + _test_function( + kornia.color.bgr_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_linear_rgb(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3, 5, 5),) + trace_kwargs = {} + test_args = (torch.rand(5, 3, 5, 5),) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_linear_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_linear_rgb_to_rgb(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 3, 5, 5),) + trace_kwargs = {} + test_args = (torch.rand(5, 3, 5, 5),) + test_kwargs = {} + _test_function( + kornia.color.linear_rgb_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_bgr_to_rgba(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + 1.0, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + 0.5, + ) + test_kwargs = {} + _test_function( + kornia.color.bgr_to_rgba, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_rgba(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + 1.0, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + 0.5, + ) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_rgba, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgba_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.color.rgba_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgba_to_bgr(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 4, 4), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 4, 4), + ) + test_kwargs = {} + _test_function( + kornia.color.rgba_to_bgr, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_hls(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 5), + ) + trace_kwargs = {'eps': 1e-8} + test_args = ( + torch.rand(5, 3, 4, 5), + ) + test_kwargs = {'eps': 1e-8} + _test_function( + kornia.color.rgb_to_hls, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_hls_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 5), + ) + test_kwargs = {} + _test_function( + kornia.color.hls_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_hsv(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {'eps': 1e-8} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {'eps': 1e-2} + _test_function( + kornia.color.rgb_to_hsv, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_hsv_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {} + _test_function( + kornia.color.hsv_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_luv(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = { + 'eps': 1e-12 + } + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = { + 'eps': 1e-12 + } + _test_function( + kornia.color.rgb_to_luv, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_luv_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {'eps': 1e-12} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {'eps': 1e-12} + _test_function( + kornia.color.luv_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_lab(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_lab, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_lab_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {'clip': True} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {'clip': True} + _test_function( + kornia.color.lab_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_ycbcr(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_ycbcr, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_ycbcr_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {} + _test_function( + kornia.color.ycbcr_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_yuv(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_yuv, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_yuv_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {} + _test_function( + kornia.color.yuv_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_yuv420(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 6), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 6), + ) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_yuv420, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_yuv420_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 4, 6), + torch.rand(1, 2, 2, 3) * 2.0 - 0.5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 4, 6), + torch.rand(5, 2, 2, 3) * 2.0 - 0.5, + ) + test_kwargs = {} + _test_function( + kornia.color.yuv420_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_yuv422(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 6, 8), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 6, 8), + ) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_yuv422, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_yuv422_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 6, 6), + torch.rand(1, 2, 3, 3) - 0.5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 6, 6), + torch.rand(5, 2, 3, 3) - 0.5, + ) + test_kwargs = {} + _test_function( + kornia.color.yuv422_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_xyz(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_xyz, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_xyz_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + ) + test_kwargs = {} + _test_function( + kornia.color.xyz_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rgb_to_raw(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + kornia.color.CFA.BG, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + kornia.color.CFA.BG, + ) + test_kwargs = {} + _test_function( + kornia.color.rgb_to_raw, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_raw_to_rgb(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 4, 6), + kornia.color.CFA.RG, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 4, 6), + kornia.color.CFA.RG, + ) + test_kwargs = {} + _test_function( + kornia.color.raw_to_rgb, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_raw_to_rgb_2x2_downscaled(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 4, 6), + kornia.color.CFA.RG, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 4, 6), + kornia.color.CFA.RG, + ) + test_kwargs = {} + _test_function( + kornia.color.raw_to_rgb_2x2_downscaled, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_sepia(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = { + 'rescale': True, + 'eps': 1e-6, + } + test_args = ( + torch.rand(5, 3, 4, 4), + ) + test_kwargs = { + 'rescale': True, # TODO: changing this to False fails + 'eps': 1e-6, + } + _test_function( + kornia.color.sepia, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + diff --git a/integration_tests/kornia/test_contrib.py b/integration_tests/kornia/test_contrib.py new file mode 100644 index 000000000000..3cbebde25bb4 --- /dev/null +++ b/integration_tests/kornia/test_contrib.py @@ -0,0 +1,154 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_compute_padding(target_framework, mode, backend_compile): + trace_args = ( + (4, 3), + (3, 3), + ) + trace_kwargs = {} + test_args = ( + (8, 5), + (4, 4), + ) + test_kwargs = {} + _test_function( + kornia.contrib.compute_padding, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_extract_tensor_patches(target_framework, mode, backend_compile): + trace_args = ( + torch.arange(16).view(1, 1, 4, 4), + ) + trace_kwargs = { + 'window_size': (2, 2), + 'stride': (2, 2), + } + test_args = ( + torch.flip(torch.arange(32), (0,)).view(2, 1, 4, 4), + ) + test_kwargs = { + 'window_size': (2, 2), + 'stride': (2, 2), + } + _test_function( + kornia.contrib.extract_tensor_patches, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_combine_tensor_patches(target_framework, mode, backend_compile): + trace_args = ( + kornia.contrib.extract_tensor_patches( + torch.arange(16).view(1, 1, 4, 4), + window_size=(2, 2), + stride=(2, 2), + ), + ) + trace_kwargs = { + 'original_size': (4, 4), + 'window_size': (2, 2), + 'stride': (2, 2), + } + test_args = ( + kornia.contrib.extract_tensor_patches( + torch.flip(torch.arange(32), (0,)).view(2, 1, 4, 4), + window_size=(2, 2), + stride=(2, 2), + ), + ) + test_kwargs = { + 'original_size': (4, 4), + 'window_size': (2, 2), + 'stride': (2, 2), + } + _test_function( + kornia.contrib.combine_tensor_patches, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_distance_transform(target_framework, mode, backend_compile): + trace_args = ( + torch.randn(1, 1, 5, 5), + ) + trace_kwargs = { + 'kernel_size': 3, + 'h': 0.35 + } + test_args = ( + torch.randn(5, 1, 5, 5), + ) + test_kwargs = { + 'kernel_size': 3, + 'h': 0.5 + } + _test_function( + kornia.contrib.distance_transform, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_diamond_square(target_framework, mode, backend_compile): + trace_args = ( + (1, 1, 8, 8), + ) + trace_kwargs = { + 'roughness': 0.5, + 'random_scale': 1.0, + 'normalize_range': (0, 1), + } + test_args = ( + (5, 1, 8, 8), + ) + test_kwargs = { + 'roughness': 0.7, + 'random_scale': 0.9, + 'normalize_range': (-1, 1), + } + _test_function( + kornia.contrib.diamond_square, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/test_enhance.py b/integration_tests/kornia/test_enhance.py new file mode 100644 index 000000000000..7b6f28c60028 --- /dev/null +++ b/integration_tests/kornia/test_enhance.py @@ -0,0 +1,639 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_add_weighted(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + 0.5, + torch.rand(1, 1, 5, 5), + 0.5, + 1.0, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 5, 5), + 0.7, + torch.rand(5, 1, 5, 5), + 0.8, + 0.8, + ) + test_kwargs = {} + _test_function( + kornia.enhance.add_weighted, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_adjust_brightness(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + 1.0, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 2, 2), + 1.3, + ) + test_kwargs = {} + _test_function( + kornia.enhance.adjust_brightness, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_adjust_contrast(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + 2.0, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 2, 2), + 0.5, + ) + test_kwargs = {} + _test_function( + kornia.enhance.adjust_contrast, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_adjust_contrast_with_mean_subtraction(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + 2.0, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 2, 2), + 0.5, + ) + test_kwargs = {} + _test_function( + kornia.enhance.adjust_contrast_with_mean_subtraction, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_adjust_gamma(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + 2.2, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 2, 2), + 0.4, + ) + test_kwargs = {} + _test_function( + kornia.enhance.adjust_gamma, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_adjust_hue(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 2, 2), + 0.5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 2, 2), + -0.2, + ) + test_kwargs = {} + _test_function( + kornia.enhance.adjust_hue, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_adjust_saturation(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 2, 2), + 0.5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 2, 2), + 1.5, + ) + test_kwargs = {} + _test_function( + kornia.enhance.adjust_saturation, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_adjust_sigmoid(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + 0.5, + 0.1, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 2, 2), + 0.7, + 0.05, + ) + test_kwargs = {} + _test_function( + kornia.enhance.adjust_sigmoid, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_adjust_log(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + 0.5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 2, 2), + 1.2, + ) + test_kwargs = {} + _test_function( + kornia.enhance.adjust_log, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_invert(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 2, 2), + ) + test_kwargs = {} + _test_function( + kornia.enhance.invert, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_posterize(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + 3, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 2, 2), + 4, + ) + test_kwargs = {} + _test_function( + kornia.enhance.posterize, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_sharpness(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + 0.5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 5, 5), + 1.0, + ) + test_kwargs = {} + _test_function( + kornia.enhance.sharpness, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_solarize(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 2, 2), + 0.5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 2, 2), + 0.7, + ) + test_kwargs = {} + _test_function( + kornia.enhance.solarize, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_equalize(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 2, 3, 3),) + trace_kwargs = {} + test_args = (torch.rand(5, 2, 3, 3),) + test_kwargs = {} + _test_function( + kornia.enhance.equalize, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_equalize_clahe(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 10, 20),) + trace_kwargs = {'clip_limit': 40.0, 'grid_size': (8, 8), 'slow_and_differentiable': False} + test_args = (torch.rand(2, 3, 10, 20),) + test_kwargs = {'clip_limit': 20.0, 'grid_size': (4, 4), 'slow_and_differentiable': False} + _test_function( + kornia.enhance.equalize_clahe, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_equalize3d(target_framework, mode, backend_compile): + trace_args = (torch.rand(1, 2, 3, 3, 3),) + trace_kwargs = {} + test_args = (torch.rand(5, 2, 3, 3, 3),) + test_kwargs = {} + _test_function( + kornia.enhance.equalize3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + + +def test_histogram(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 10), + torch.linspace(0, 255, 128), + torch.tensor(0.9), + ) + trace_kwargs = {'epsilon': 1e-10} + test_args = ( + torch.rand(5, 10), + torch.linspace(0, 255, 128), + torch.tensor(0.9), + ) + test_kwargs = {'epsilon': 1e-10} + _test_function( + kornia.enhance.histogram, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_histogram2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 32), + torch.rand(2, 32), + torch.linspace(0, 255, 128), + torch.tensor(0.9), + ) + trace_kwargs = {'epsilon': 1e-10} + test_args = ( + torch.rand(5, 32), + torch.rand(5, 32), + torch.linspace(0, 255, 128), + torch.tensor(0.9), + ) + test_kwargs = {'epsilon': 1e-10} + _test_function( + kornia.enhance.histogram2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_image_histogram2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 10, 10), + ) + trace_kwargs = { + 'min': 0.0, + 'max': 255.0, + 'n_bins': 256, + 'bandwidth': None, + 'centers': None, + 'return_pdf': False, + 'kernel': 'triangular', + 'eps': 1e-10 + } + test_args = ( + torch.rand(5, 1, 10, 10), + ) + test_kwargs = { + 'min': 0.0, + 'max': 255.0, + 'n_bins': 256, + 'bandwidth': None, + 'centers': None, + 'return_pdf': False, + 'kernel': 'triangular', + 'eps': 1e-10 + } + _test_function( + kornia.enhance.image_histogram2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_normalize(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + torch.tensor([0.5, 0.5, 0.5]), + torch.tensor([0.5, 0.5, 0.5]) + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + torch.tensor([0.4, 0.4, 0.4]), + torch.tensor([0.6, 0.6, 0.6]) + ) + test_kwargs = {} + _test_function( + kornia.enhance.normalize, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + +def test_normalize_min_max(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + 0.0, + 1.0 + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + -1.0, + 1.0 + ) + test_kwargs = {} + _test_function( + kornia.enhance.normalize_min_max, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + +def test_denormalize(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + torch.tensor([0.5, 0.5, 0.5]), + torch.tensor([0.5, 0.5, 0.5]) + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 4, 4), + torch.tensor([0.4, 0.4, 0.4]), + torch.tensor([0.6, 0.6, 0.6]) + ) + test_kwargs = {} + _test_function( + kornia.enhance.denormalize, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + +def test_zca_mean(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(10, 20), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 10, 20), + ) + test_kwargs = {} + _test_function( + kornia.enhance.zca_mean, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + +def test_zca_whiten(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(10, 20), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 10, 20), + ) + test_kwargs = {} + _test_function( + kornia.enhance.zca_whiten, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + +def test_linear_transform(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(10, 3, 4, 5), + torch.eye(10 * 3 * 4), + torch.zeros(1, 10 * 3 * 4) + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 10, 3, 4, 5), + torch.eye(10 * 3 * 4), + torch.zeros(1, 10 * 3 * 4) + ) + test_kwargs = {} + _test_function( + kornia.enhance.linear_transform, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + +def test_jpeg_codec_differentiable(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 3, 64, 64), + torch.tensor([99.0]) + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 3, 64, 64), + torch.tensor([50.0]) + ) + test_kwargs = {} + _test_function( + kornia.enhance.jpeg_codec_differentiable, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/test_feature.py b/integration_tests/kornia/test_feature.py new file mode 100644 index 000000000000..037a2c9e1b98 --- /dev/null +++ b/integration_tests/kornia/test_feature.py @@ -0,0 +1,713 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_gftt_response(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 7, 7), + ) + trace_kwargs = {'grads_mode': 'sobel', 'sigmas': None} + test_args = ( + torch.rand(5, 1, 7, 7), + ) + test_kwargs = {'grads_mode': 'sobel', 'sigmas': None} + _test_function( + kornia.feature.gftt_response, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_harris_response(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 7, 7), + ) + trace_kwargs = {'k': 0.04, 'grads_mode': 'sobel', 'sigmas': None} + test_args = ( + torch.rand(5, 1, 7, 7), + ) + test_kwargs = {'k': 0.04, 'grads_mode': 'sobel', 'sigmas': None} + _test_function( + kornia.feature.harris_response, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_hessian_response(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 7, 7), + ) + trace_kwargs = {'grads_mode': 'sobel', 'sigmas': None} + test_args = ( + torch.rand(5, 1, 7, 7), + ) + test_kwargs = {'grads_mode': 'sobel', 'sigmas': None} + _test_function( + kornia.feature.hessian_response, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_dog_response(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 5, 5, 5), + ) + test_kwargs = {} + _test_function( + kornia.feature.dog_response, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_dog_response_single(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + ) + trace_kwargs = {'sigma1': 1.0, 'sigma2': 1.6} + test_args = ( + torch.rand(5, 1, 5, 5), + ) + test_kwargs = {'sigma1': 0.5, 'sigma2': 1.2} + _test_function( + kornia.feature.dog_response_single, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-3, + mode=mode, + ) + + +def test_get_laf_descriptors(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 32, 32), + torch.rand(1, 3, 2, 2), + kornia.feature.HardNet8(True), + ) + trace_kwargs = {'patch_size': 32, 'grayscale_descriptor': True} + test_args = ( + torch.rand(5, 1, 32, 32), + torch.rand(5, 3, 2, 2), + kornia.feature.HardNet8(True), + ) + test_kwargs = {'patch_size': 32, 'grayscale_descriptor': True} + _test_function( + kornia.feature.get_laf_descriptors, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_match_nn(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 128), + torch.rand(1, 128), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 128), + torch.rand(5, 128), + ) + test_kwargs = {} + _test_function( + kornia.feature.match_nn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_match_mnn(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 128), + torch.rand(1, 128), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 128), + torch.rand(5, 128), + ) + test_kwargs = {} + _test_function( + kornia.feature.match_mnn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_match_snn(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 128), + torch.rand(1, 128), + ) + trace_kwargs = {'th': 0.8} + test_args = ( + torch.rand(5, 128), + torch.rand(5, 128), + ) + test_kwargs = {'th': 0.8} + _test_function( + kornia.feature.match_snn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_match_smnn(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 128), + torch.rand(1, 128), + ) + trace_kwargs = {'th': 0.95} + test_args = ( + torch.rand(5, 128), + torch.rand(5, 128), + ) + test_kwargs = {'th': 0.95} + _test_function( + kornia.feature.match_smnn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_match_fginn(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 128), + torch.rand(3, 128), + torch.rand(1, 3, 2, 3), + torch.rand(1, 3, 2, 3), + ) + trace_kwargs = {'th': 0.8, 'spatial_th': 10.0, 'mutual': False} + test_args = ( + torch.rand(5, 128), + torch.rand(5, 128), + torch.rand(1, 5, 2, 3), + torch.rand(1, 5, 2, 3), + ) + test_kwargs = {'th': 0.8, 'spatial_th': 10.0, 'mutual': False} + _test_function( + kornia.feature.match_fginn, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_match_adalam(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(3, 128), + torch.rand(3, 128), + torch.rand(1, 3, 2, 3), + torch.rand(1, 3, 2, 3), + ) + trace_kwargs = {'config': None, 'hw1': None, 'hw2': None} + test_args = ( + torch.rand(5, 128), + torch.rand(5, 128), + torch.rand(1, 5, 2, 3), + torch.rand(1, 5, 2, 3), + ) + test_kwargs = {'config': None, 'hw1': None, 'hw2': None} + _test_function( + kornia.feature.match_adalam, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_extract_patches_from_pyramid(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 32, 32), + torch.rand(1, 5, 2, 3), + ) + trace_kwargs = {'PS': 32} + test_args = ( + torch.rand(1, 3, 64, 64), # TODO: changing the batch size of these causes the trace_graph test to fail + torch.rand(1, 5, 2, 3), + ) + test_kwargs = {'PS': 16} + _test_function( + kornia.feature.extract_patches_from_pyramid, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +# NOTE: this test can take a while to run (10+ mins) +# def test_extract_patches_simple(target_framework, mode, backend_compile): +# trace_args = ( +# torch.rand(1, 3, 32, 32), +# torch.rand(1, 5, 2, 3), +# ) +# trace_kwargs = {'PS': 32, 'normalize_lafs_before_extraction': True} +# test_args = ( +# torch.rand(2, 3, 64, 64), +# torch.rand(2, 5, 2, 3), +# ) +# test_kwargs = {'PS': 16, 'normalize_lafs_before_extraction': False} +# _test_function( +# kornia.feature.extract_patches_simple, +# trace_args, +# trace_kwargs, +# test_args, +# test_kwargs, +# target_framework, +# backend_compile, +# tolerance=1e-4, +# mode=mode, +# ) + + +def test_normalize_laf(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2, 3), + torch.rand(1, 3, 32, 32), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 5, 2, 3), + torch.rand(2, 3, 64, 64), + ) + test_kwargs = {} + _test_function( + kornia.feature.normalize_laf, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_denormalize_laf(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2, 3), + torch.rand(1, 3, 32, 32), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 5, 2, 3), + torch.rand(2, 3, 64, 64), + ) + test_kwargs = {} + _test_function( + kornia.feature.denormalize_laf, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_laf_to_boundary_points(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2, 3), + ) + trace_kwargs = {'n_pts': 50} + test_args = ( + torch.rand(2, 5, 2, 3), + ) + test_kwargs = {'n_pts': 100} + _test_function( + kornia.feature.laf_to_boundary_points, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_ellipse_to_laf(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 10, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 10, 5), + ) + test_kwargs = {} + _test_function( + kornia.feature.ellipse_to_laf, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_make_upright(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2, 3), + ) + trace_kwargs = {'eps': 1e-9} + test_args = ( + torch.rand(2, 5, 2, 3), + ) + test_kwargs = {'eps': 1e-6} + _test_function( + kornia.feature.make_upright, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_scale_laf(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2, 3), + 0.5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 5, 2, 3), + 2.0, + ) + test_kwargs = {} + _test_function( + kornia.feature.scale_laf, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_laf_scale(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 10, 2, 3), + ) + test_kwargs = {} + _test_function( + kornia.feature.get_laf_scale, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_laf_center(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 10, 2, 3), + ) + test_kwargs = {} + _test_function( + kornia.feature.get_laf_center, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_rotate_laf(target_framework, mode, backend_compile): + trace_args = ( + torch.randn((1, 5, 2, 3)), + torch.randn((1, 5, 1)), + ) + trace_kwargs = {} + test_args = ( + torch.randn((2, 10, 2, 3)), + torch.randn((2, 10, 1)), + ) + test_kwargs = {} + _test_function( + kornia.feature.rotate_laf, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_laf_orientation(target_framework, mode, backend_compile): + trace_args = ( + torch.randn((1, 5, 2, 3)), + ) + trace_kwargs = {} + test_args = ( + torch.randn((2, 10, 2, 3)), + ) + test_kwargs = {} + _test_function( + kornia.feature.get_laf_orientation, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_set_laf_orientation(target_framework, mode, backend_compile): + trace_args = ( + torch.randn((1, 5, 2, 3)), + torch.randn((1, 5, 1)), + ) + trace_kwargs = {} + test_args = ( + torch.randn((2, 10, 2, 3)), + torch.randn((2, 10, 1)), + ) + test_kwargs = {} + _test_function( + kornia.feature.set_laf_orientation, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_laf_from_center_scale_ori(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2), + torch.randn(1, 5, 1, 1), + torch.randn(1, 5, 1), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 10, 2), + torch.randn(5, 10, 1, 1), + torch.randn(5, 10, 1), + ) + test_kwargs = {} + _test_function( + kornia.feature.laf_from_center_scale_ori, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_laf_is_inside_image(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2, 3), + torch.rand(1, 1, 32, 32), + ) + trace_kwargs = {'border': 0} + test_args = ( + torch.rand(2, 10, 2, 3), + torch.rand(2, 1, 64, 64), + ) + test_kwargs = {'border': 1} + _test_function( + kornia.feature.laf_is_inside_image, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_laf_to_three_points(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 10, 2, 3), + ) + test_kwargs = {} + _test_function( + kornia.feature.laf_to_three_points, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_laf_from_three_points(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 5, 6), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 10, 6), + ) + test_kwargs = {} + _test_function( + kornia.feature.laf_from_three_points, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_perspective_transform_lafs(target_framework, mode, backend_compile): + trace_args = ( + torch.eye(3).repeat(1, 1, 1), + torch.rand(1, 5, 2, 3), + ) + trace_kwargs = {} + test_args = ( + torch.eye(3).repeat(2, 1, 1), + torch.rand(2, 10, 2, 3), + ) + test_kwargs = {} + _test_function( + kornia.feature.perspective_transform_lafs, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/test_filters.py b/integration_tests/kornia/test_filters.py new file mode 100644 index 000000000000..1c934c6a8799 --- /dev/null +++ b/integration_tests/kornia/test_filters.py @@ -0,0 +1,656 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_bilateral_blur(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + (3, 3), + 0.1, + (1.5, 1.5), + ) + trace_kwargs = {'border_type': 'reflect', 'color_distance_type': 'l1'} + test_args = ( + torch.rand(5, 3, 5, 5), + (5, 5), + 0.2, + (2.0, 2.0), + ) + test_kwargs = {'border_type': 'reflect', 'color_distance_type': 'l1'} + _test_function( + kornia.filters.bilateral_blur, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_blur_pool2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + 3, + ) + trace_kwargs = {'stride': 2} + test_args = ( + torch.rand(5, 3, 8, 8), + 3, # NOTE: changing this kernel size fails the test; also true for some of the other tests in this file + ) + test_kwargs = {'stride': 2} + _test_function( + kornia.filters.blur_pool2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_box_blur(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + (3, 3), + ) + trace_kwargs = {'border_type': 'reflect', 'separable': False} + test_args = ( + torch.rand(5, 3, 5, 5), + (3, 3), + ) + test_kwargs = {'border_type': 'reflect', 'separable': False} + _test_function( + kornia.filters.box_blur, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_gaussian_blur2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + (3, 3), + (1.5, 1.5), + ) + trace_kwargs = {'border_type': 'reflect', 'separable': True} + test_args = ( + torch.rand(5, 3, 5, 5), + (3, 3), + (1.5, 1.5), + ) + test_kwargs = {'border_type': 'reflect', 'separable': True} + _test_function( + kornia.filters.gaussian_blur2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_guided_blur(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(1, 3, 5, 5), + (3, 3), + 0.1, + ) + trace_kwargs = {'border_type': 'reflect', 'subsample': 1} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(5, 3, 5, 5), + (3, 3), + 0.1, + ) + test_kwargs = {'border_type': 'reflect', 'subsample': 1} + _test_function( + kornia.filters.guided_blur, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_joint_bilateral_blur(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(1, 3, 5, 5), + (3, 3), + 0.1, + (1.5, 1.5), + ) + trace_kwargs = {'border_type': 'reflect', 'color_distance_type': 'l1'} + test_args = ( + torch.rand(4, 3, 5, 5), + torch.rand(4, 3, 5, 5), + (5, 5), + 0.2, + (2.0, 2.0), + ) + test_kwargs = {'border_type': 'reflect', 'color_distance_type': 'l1'} + _test_function( + kornia.filters.joint_bilateral_blur, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_max_blur_pool2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + 3, + ) + trace_kwargs = {'stride': 2, 'max_pool_size': 2, 'ceil_mode': False} + test_args = ( + torch.rand(5, 3, 8, 8), + 3, + ) + test_kwargs = {'stride': 2, 'max_pool_size': 2, 'ceil_mode': False} + _test_function( + kornia.filters.max_blur_pool2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_median_blur(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + (3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + (5, 5), + ) + test_kwargs = {} + _test_function( + kornia.filters.median_blur, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_motion_blur(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + 5, + 45.0, + 1.0, + ) + trace_kwargs = {'border_type': 'constant', 'mode': 'nearest'} + test_args = ( + torch.rand(5, 3, 5, 5), + 5, + 90.0, + 0.5, + ) + test_kwargs = {'border_type': 'constant', 'mode': 'nearest'} + _test_function( + kornia.filters.motion_blur, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_unsharp_mask(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + (3, 3), + (1.5, 1.5), + ) + trace_kwargs = {'border_type': 'reflect'} + test_args = ( + torch.rand(5, 3, 5, 5), + (5, 5), + (2.0, 2.0), + ) + test_kwargs = {'border_type': 'reflect'} + _test_function( + kornia.filters.unsharp_mask, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_canny(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = { + 'low_threshold': 0.1, + 'high_threshold': 0.2, + 'kernel_size': (5, 5), + 'sigma': (1, 1), + 'hysteresis': True, + 'eps': 1e-6, + } + test_args = ( + torch.rand(5, 3, 4, 4), + ) + test_kwargs = { + 'low_threshold': 0.2, + 'high_threshold': 0.3, + 'kernel_size': (5, 5), + 'sigma': (1, 1), + 'hysteresis': True, + 'eps': 1e-6, + } + _test_function( + kornia.filters.canny, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-3, + mode=mode, + ) + + +def test_laplacian(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 4, 5, 5), + 3, + ) + trace_kwargs = { + 'border_type': 'reflect', + 'normalized': True, + } + test_args = ( + torch.rand(5, 4, 5, 5), + 3, + ) + test_kwargs = { + 'border_type': 'reflect', + 'normalized': True, + } + _test_function( + kornia.filters.laplacian, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_sobel(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = { + 'normalized': True, + 'eps': 1e-6, + } + test_args = ( + torch.rand(5, 3, 4, 4), + ) + test_kwargs = { + 'normalized': True, + 'eps': 1e-5, + } + _test_function( + kornia.filters.sobel, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_spatial_gradient(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 4, 4), + ) + trace_kwargs = { + 'mode': 'sobel', + 'order': 1, + 'normalized': True, + } + test_args = ( + torch.rand(5, 3, 4, 4), + ) + test_kwargs = { + 'mode': 'sobel', + 'order': 1, + 'normalized': True, + } + _test_function( + kornia.filters.spatial_gradient, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_spatial_gradient3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 2, 4, 4), + ) + trace_kwargs = { + 'mode': 'diff', + 'order': 1, + } + test_args = ( + torch.rand(5, 4, 2, 4, 4), + ) + test_kwargs = { + 'mode': 'diff', + 'order': 1, + } + _test_function( + kornia.filters.spatial_gradient3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_filter2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + torch.rand(1, 3, 3), + ) + trace_kwargs = {'padding': 'same'} + test_args = ( + torch.rand(2, 1, 5, 5), + torch.rand(1, 3, 3), + ) + test_kwargs = {'padding': 'same'} + _test_function( + kornia.filters.filter2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_filter2d_separable(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + torch.rand(1, 3), + torch.rand(1, 3), + ) + trace_kwargs = {'padding': 'same'} + test_args = ( + torch.rand(2, 1, 5, 5), + torch.rand(1, 3), + torch.rand(1, 3), + ) + test_kwargs = {'padding': 'same'} + _test_function( + kornia.filters.filter2d_separable, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_filter3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5, 5), + torch.rand(1, 3, 3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 1, 5, 5, 5), + torch.rand(1, 3, 3, 3), + ) + test_kwargs = {} + _test_function( + kornia.filters.filter3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_gaussian_kernel1d(target_framework, mode, backend_compile): + trace_args = (3, 2.5) + trace_kwargs = {} + test_args = (5, 1.5) + test_kwargs = {} + _test_function( + kornia.filters.get_gaussian_kernel1d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_gaussian_erf_kernel1d(target_framework, mode, backend_compile): + trace_args = (3, 2.5) + trace_kwargs = {} + test_args = (5, 1.5) + test_kwargs = {} + _test_function( + kornia.filters.get_gaussian_erf_kernel1d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_gaussian_discrete_kernel1d(target_framework, mode, backend_compile): + trace_args = (3, 2.5) + trace_kwargs = {} + test_args = (5, 1.5) + test_kwargs = {} + _test_function( + kornia.filters.get_gaussian_discrete_kernel1d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_gaussian_kernel2d(target_framework, mode, backend_compile): + trace_args = ((5, 5), (1.5, 1.5)) + trace_kwargs = {} + test_args = ((3, 5), (1.5, 1.5)) + test_kwargs = {} + _test_function( + kornia.filters.get_gaussian_kernel2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_hanning_kernel1d(target_framework, mode, backend_compile): + trace_args = (4,) + trace_kwargs = {} + test_args = (8,) + test_kwargs = {} + _test_function( + kornia.filters.get_hanning_kernel1d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_hanning_kernel2d(target_framework, mode, backend_compile): + trace_args = ((4, 4),) + trace_kwargs = {} + test_args = ((8, 8),) + test_kwargs = {} + _test_function( + kornia.filters.get_hanning_kernel2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_laplacian_kernel1d(target_framework, mode, backend_compile): + trace_args = (3,) + trace_kwargs = {} + test_args = (5,) + test_kwargs = {} + _test_function( + kornia.filters.get_laplacian_kernel1d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_laplacian_kernel2d(target_framework, mode, backend_compile): + trace_args = (3,) + trace_kwargs = {} + test_args = (5,) + test_kwargs = {} + _test_function( + kornia.filters.get_laplacian_kernel2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_get_motion_kernel2d(target_framework, mode, backend_compile): + trace_args = (5, 0.0) + trace_kwargs = {'direction': 0.0, 'mode': 'nearest'} + test_args = (3, 215.0) + test_kwargs = {'direction': -0.5, 'mode': 'nearest'} + _test_function( + kornia.filters.get_motion_kernel2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/test_losses.py b/integration_tests/kornia/test_losses.py new file mode 100644 index 000000000000..c64b3eb7dfbd --- /dev/null +++ b/integration_tests/kornia/test_losses.py @@ -0,0 +1,418 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_ssim_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 5, 5), + torch.rand(1, 4, 5, 5), + 5, + ) + trace_kwargs = {'max_val': 1.0, 'eps': 1e-12, 'reduction': 'mean', 'padding': 'same'} + test_args = ( + torch.rand(5, 4, 5, 5), + torch.rand(5, 4, 5, 5), + 7, + ) + test_kwargs = {'max_val': 1.0, 'eps': 1e-12, 'reduction': 'mean', 'padding': 'same'} + _test_function( + kornia.losses.ssim_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_ssim3d_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 4, 5, 5, 5), + torch.rand(1, 4, 5, 5, 5), + 5, + ) + trace_kwargs = {'max_val': 1.0, 'eps': 1e-12, 'reduction': 'mean', 'padding': 'same'} + test_args = ( + torch.rand(5, 4, 5, 5, 5), + torch.rand(5, 4, 5, 5, 5), + 7, + ) + test_kwargs = {'max_val': 1.0, 'eps': 1e-12, 'reduction': 'mean', 'padding': 'same'} + _test_function( + kornia.losses.ssim3d_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_psnr_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 32, 32), + torch.rand(1, 3, 32, 32), + 1.0, + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 32, 32), + torch.rand(5, 3, 32, 32), + 1.0, + ) + test_kwargs = {} + _test_function( + kornia.losses.psnr_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_total_variation(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 32, 32), + ) + trace_kwargs = {'reduction': 'sum'} + test_args = ( + torch.rand(5, 3, 32, 32), + ) + test_kwargs = {'reduction': 'sum'} + _test_function( + kornia.losses.total_variation, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_inverse_depth_smoothness_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 4, 5), + torch.rand(1, 3, 4, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 1, 4, 5), + torch.rand(5, 3, 4, 5), + ) + test_kwargs = {} + _test_function( + kornia.losses.inverse_depth_smoothness_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_charbonnier_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 32, 32), + torch.rand(1, 3, 32, 32), + ) + trace_kwargs = {'reduction': 'none'} + test_args = ( + torch.rand(5, 3, 32, 32), + torch.rand(5, 3, 32, 32), + ) + test_kwargs = {'reduction': 'none'} + _test_function( + kornia.losses.charbonnier_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_welsch_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 32, 32), + torch.rand(1, 3, 32, 32), + ) + trace_kwargs = {'reduction': 'none'} + test_args = ( + torch.rand(5, 3, 32, 32), + torch.rand(5, 3, 32, 32), + ) + test_kwargs = {'reduction': 'none'} + _test_function( + kornia.losses.welsch_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_cauchy_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 32, 32), + torch.rand(1, 3, 32, 32), + ) + trace_kwargs = {'reduction': 'none'} + test_args = ( + torch.rand(5, 3, 32, 32), + torch.rand(5, 3, 32, 32), + ) + test_kwargs = {'reduction': 'none'} + _test_function( + kornia.losses.cauchy_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_geman_mcclure_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 32, 32), + torch.rand(1, 3, 32, 32), + ) + trace_kwargs = {'reduction': 'none'} + test_args = ( + torch.rand(5, 3, 32, 32), + torch.rand(5, 3, 32, 32), + ) + test_kwargs = {'reduction': 'none'} + _test_function( + kornia.losses.geman_mcclure_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_binary_focal_loss_with_logits(target_framework, mode, backend_compile): + trace_args = ( + torch.randn((1, 3, 5)), + torch.randint(2, (1, 3, 5)), + ) + trace_kwargs = {"alpha": 0.25, "gamma": 2.0, "reduction": 'mean'} + test_args = ( + torch.randn((5, 3, 5)), + torch.randint(2, (5, 3, 5)), + ) + test_kwargs = {"alpha": 0.5, "gamma": 3.1, "reduction": 'mean'} + _test_function( + kornia.losses.binary_focal_loss_with_logits, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_focal_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.randn((1, 5, 3, 5)), + torch.randint(5, (1, 3, 5)), + ) + trace_kwargs = {"alpha": 0.5, "gamma": 2.0, "reduction": 'mean'} + test_args = ( + torch.randn((5, 5, 3, 5)), + torch.randint(5, (5, 3, 5)), + ) + test_kwargs = {"alpha": 0.7, "gamma": 2.5, "reduction": 'mean'} + _test_function( + kornia.losses.focal_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_dice_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.randn((1, 5, 3, 5)), + torch.empty(1, 3, 5, dtype=torch.long).random_(5), + ) + trace_kwargs = {"average": "micro", "eps": 1e-8} + test_args = ( + torch.randn((5, 5, 3, 5)), + torch.empty(5, 3, 5, dtype=torch.long).random_(5), + ) + test_kwargs = {"average": "micro", "eps": 1e-8} + _test_function( + kornia.losses.dice_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_tversky_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.randn((1, 5, 3, 5)), + torch.empty(1, 3, 5, dtype=torch.long).random_(5), + ) + trace_kwargs = {"alpha": 0.5, "beta": 0.5, "eps": 1e-8} + test_args = ( + torch.randn(5, 5, 3, 5), + torch.empty(5, 3, 5, dtype=torch.long).random_(5), + ) + test_kwargs = {"alpha": 0.5, "beta": 0.5, "eps": 1e-8} + _test_function( + kornia.losses.tversky_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_lovasz_hinge_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.randn((1, 1, 3, 5)), + torch.empty(1, 3, 5, dtype=torch.long).random_(1), + ) + trace_kwargs = {} + test_args = ( + torch.randn((5, 1, 3, 5)), + torch.empty(5, 3, 5, dtype=torch.long).random_(1), + ) + test_kwargs = {} + _test_function( + kornia.losses.lovasz_hinge_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_lovasz_softmax_loss(target_framework, mode, backend_compile): + trace_args = ( + torch.randn((1, 5, 3, 5)), + torch.empty(1, 3, 5, dtype=torch.long).random_(5), + ) + trace_kwargs = {} + test_args = ( + torch.randn((5, 5, 3, 5)), + torch.empty(5, 3, 5, dtype=torch.long).random_(5), + ) + test_kwargs = {} + _test_function( + kornia.losses.lovasz_softmax_loss, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_js_div_loss_2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand((1, 1, 2, 4)), + torch.rand((1, 1, 2, 4)), + ) + trace_kwargs = {"reduction": "mean"} + test_args = ( + torch.rand((5, 1, 2, 4)), + torch.rand((5, 1, 2, 4)), + ) + test_kwargs = {"reduction": "mean"} + _test_function( + kornia.losses.js_div_loss_2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_kl_div_loss_2d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand((1, 1, 2, 4)), + torch.rand((1, 1, 2, 4)), + ) + trace_kwargs = {"reduction": "mean"} + test_args = ( + torch.rand((5, 1, 2, 4)), + torch.rand((5, 1, 2, 4)), + ) + test_kwargs = {"reduction": "mean"} + _test_function( + kornia.losses.kl_div_loss_2d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/test_metrics.py b/integration_tests/kornia/test_metrics.py new file mode 100644 index 000000000000..308cec35ddf9 --- /dev/null +++ b/integration_tests/kornia/test_metrics.py @@ -0,0 +1,242 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_accuracy(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[0, 1, 0]]), + torch.tensor([[1]]), + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[0, 0.8, 0.2], [0, 0.4, 0.6]]), + torch.tensor([1, 2]), + ) + test_kwargs = {} + _test_function( + kornia.metrics.accuracy, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_confusion_matrix(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([0, 1, 0]), + torch.tensor([0, 1, 0]), + 3, + ) + trace_kwargs = {} + test_args = ( + torch.tensor([0, 1, 2]), + torch.tensor([0, 2, 1]), + 3, + ) + test_kwargs = {} + _test_function( + kornia.metrics.confusion_matrix, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_mean_iou(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([0, 1, 0]), + torch.tensor([0, 1, 0]), + 3, + ) + trace_kwargs = {} + test_args = ( + torch.tensor([0, 1, 2]), + torch.tensor([0, 2, 1]), + 3, + ) + test_kwargs = {} + _test_function( + kornia.metrics.mean_iou, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_mean_average_precision(target_framework, mode, backend_compile): + trace_args = ( + [torch.tensor([[100, 50, 150, 100.]])], + [torch.tensor([1.])], + [torch.tensor([0.7])], + [torch.tensor([[100, 50, 150, 100.]])], + [torch.tensor([1.])], + 2, + ) + trace_kwargs = {} + test_args = ( + [torch.tensor([[50, 25, 75, 50], [100, 50, 150, 100.]])], + [torch.tensor([1, 2.])], + [torch.tensor([0.6, 0.8])], + [torch.tensor([[50, 25, 75, 50], [100, 50, 150, 100.]])], + [torch.tensor([1, 2.])], + 3, + ) + kornia.metrics.mean_average_precision(*trace_args) + kornia.metrics.mean_average_precision(*test_args) + test_kwargs = {} + _test_function( + kornia.metrics.mean_average_precision, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_mean_iou_bbox(target_framework, mode, backend_compile): + trace_args = ( + torch.tensor([[40, 40, 60, 60], [30, 40, 50, 60]]), + torch.tensor([[40, 50, 60, 70], [30, 40, 40, 50]]) + ) + trace_kwargs = {} + test_args = ( + torch.tensor([[20, 20, 40, 40], [10, 30, 30, 50]]), + torch.tensor([[20, 30, 40, 50], [10, 20, 20, 30]]) + ) + test_kwargs = {} + _test_function( + kornia.metrics.mean_iou_bbox, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_psnr(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 4, 4), + 1.2 * torch.rand(1, 1, 4, 4), + 2.0, + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 3, 4, 4), + 1.1 * torch.rand(2, 3, 4, 4), + 2.0, + ) + test_kwargs = {} + _test_function( + kornia.metrics.psnr, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_ssim(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5), + torch.rand(1, 1, 5, 5), + 5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 3, 5, 5), + torch.rand(2, 3, 5, 5), + 5, + ) + test_kwargs = {} + _test_function( + kornia.metrics.ssim, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_ssim3d(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 1, 5, 5, 5), + torch.rand(1, 1, 5, 5, 5), + 5, + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 3, 5, 5, 5), + torch.rand(2, 3, 5, 5, 5), + 5, + ) + test_kwargs = {} + _test_function( + kornia.metrics.ssim3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_aepe(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(4, 4, 2), + 1.2 * torch.rand(4, 4, 2), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 4, 4, 2), + torch.rand(5, 4, 4, 2), + ) + test_kwargs = {} + _test_function( + kornia.metrics.aepe, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/test_morphology.py b/integration_tests/kornia/test_morphology.py new file mode 100644 index 000000000000..4c78d52110c7 --- /dev/null +++ b/integration_tests/kornia/test_morphology.py @@ -0,0 +1,174 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_dilation(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.morphology.dilation, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_erosion(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(5, 5), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(5, 5), + ) + test_kwargs = {} + _test_function( + kornia.morphology.erosion, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_opening(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.morphology.opening, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_closing(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.morphology.closing, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_gradient(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.morphology.gradient, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_top_hat(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.morphology.top_hat, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_bottom_hat(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 5, 5), + torch.rand(3, 3), + ) + trace_kwargs = {} + test_args = ( + torch.rand(5, 3, 5, 5), + torch.rand(3, 3), + ) + test_kwargs = {} + _test_function( + kornia.morphology.bottom_hat, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) diff --git a/integration_tests/kornia/test_utils.py b/integration_tests/kornia/test_utils.py new file mode 100644 index 000000000000..f401708afb8f --- /dev/null +++ b/integration_tests/kornia/test_utils.py @@ -0,0 +1,148 @@ +from helpers import _test_function +import kornia +import torch + + +# Tests # +# ----- # + +def test_draw_line(target_framework, mode, backend_compile): + trace_args = ( + torch.zeros((1, 8, 8)), + torch.tensor([6, 4]), + torch.tensor([1, 4]), + torch.tensor([255]), + ) + trace_kwargs = {} + test_args = ( + torch.zeros((1, 8, 8)), + torch.tensor([0, 2]), + torch.tensor([5, 1]), + torch.tensor([255]), + ) + test_kwargs = {} + _test_function( + kornia.utils.draw_line, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_draw_rectangle(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(2, 3, 10, 12), + torch.tensor([[[0, 0, 4, 4]], [[4, 4, 10, 10]]]), + ) + trace_kwargs = {} + test_args = ( + torch.rand(3, 3, 10, 12), + torch.tensor([[[0, 0, 4, 4]], [[4, 4, 10, 10]], [[2, 2, 6, 6]]]), + ) + test_kwargs = {} + _test_function( + kornia.utils.draw_rectangle, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_draw_convex_polygon(target_framework, mode, backend_compile): + trace_args = ( + torch.rand(1, 3, 12, 16), + torch.tensor([[[4, 4], [12, 4], [12, 8], [4, 8]]]), + torch.tensor([0.5, 0.5, 0.5]), + ) + trace_kwargs = {} + test_args = ( + torch.rand(2, 3, 12, 16), + torch.tensor([[[4, 4], [12, 4], [12, 8], [4, 8]], [[3, 3], [10, 3], [10, 7], [3, 7]]]), + torch.tensor([0.5, 0.5, 0.5]), + ) + test_kwargs = {} + _test_function( + kornia.utils.draw_convex_polygon, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-3, + mode=mode, + ) + + +def test_create_meshgrid(target_framework, mode, backend_compile): + trace_args = (2, 2) + trace_kwargs = {} + test_args = (4, 4) + test_kwargs = {} + _test_function( + kornia.utils.create_meshgrid, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_create_meshgrid3d(target_framework, mode, backend_compile): + trace_args = (2, 2, 2) + trace_kwargs = {} + test_args = (4, 4, 4) + test_kwargs = {} + _test_function( + kornia.utils.create_meshgrid3d, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) + + +def test_one_hot(target_framework, mode, backend_compile): + trace_args = ( + torch.LongTensor([[[0, 1], [2, 0]]]), + 3, + torch.device('cpu'), + torch.int64, + ) + trace_kwargs = {} + test_args = ( + torch.LongTensor([[[1, 2], [0, 1]]]), + 5, + torch.device('cpu'), + torch.int64, + ) + test_kwargs = {} + _test_function( + kornia.utils.one_hot, + trace_args, + trace_kwargs, + test_args, + test_kwargs, + target_framework, + backend_compile, + tolerance=1e-4, + mode=mode, + ) From cb77283669d142fa8cd9133fcdac98f842978bb7 Mon Sep 17 00:00:00 2001 From: Sam-Armstrong Date: Thu, 20 Jun 2024 05:45:54 +0100 Subject: [PATCH 2/3] fix: tracing ivy.set_item with Ellipsis/unbound slices --- ivy/functional/ivy/general.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ivy/functional/ivy/general.py b/ivy/functional/ivy/general.py index fceb0fc4eca7..735a37a68cd4 100644 --- a/ivy/functional/ivy/general.py +++ b/ivy/functional/ivy/general.py @@ -2879,6 +2879,21 @@ def set_item( ivy.array([[ 0, -1, 20], [10, 10, 10]]) """ + if ( + isinstance(query, (list, tuple)) and + any([ + q is Ellipsis or ( + isinstance(q, slice) and q.stop is None + ) for q in query + ]) + ): + # use numpy for item setting when an ellipsis or unbounded slice is present, + # as they would otherwise cause static dim sizes to be traced into the graph + # NOTE: this does however cause tf.function to be incompatible + np_array = x.numpy() + np_array[query] = np.asarray(val) + return ivy.array(np_array) + if copy: x = ivy.copy_array(x) if not ivy.is_array(val): From d36ae2c74ec9930cfaf98d6f46ebb574cf29f1a4 Mon Sep 17 00:00:00 2001 From: Sam-Armstrong Date: Mon, 24 Jun 2024 12:38:57 +0100 Subject: [PATCH 3/3] add todo note to the added logic --- ivy/functional/ivy/general.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ivy/functional/ivy/general.py b/ivy/functional/ivy/general.py index 735a37a68cd4..e59ea2ed5725 100644 --- a/ivy/functional/ivy/general.py +++ b/ivy/functional/ivy/general.py @@ -2879,6 +2879,8 @@ def set_item( ivy.array([[ 0, -1, 20], [10, 10, 10]]) """ + + # TODO: we may be able to remove this logic by instead tracing _parse_query as a node in the graph?? if ( isinstance(query, (list, tuple)) and any([