Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

optimized check image using aminmax (#3270)

Sidde779c63d54ac
diff --git a/kornia/core/check.py b/kornia/core/check.py
index 91abe55d2..99c0619c8 100644
--- a/kornia/core/check.py
+++ b/kornia/core/check.py
@@ -21,6 +21,7 @@ from __future__ import annotations
from typing import Any, Optional, Sequence, TypeVar, cast
+import torch
from torch import float16, float32, float64
from typing_extensions import TypeGuard
@@ -396,14 +397,17 @@ def KORNIA_CHECK_IS_IMAGE(x: Tensor, msg: Optional[str] = None, raises: bool = T
if not raises and not KORNIA_CHECK_IS_COLOR_OR_GRAY(x, msg, raises):
return False
- min_val, max_val = x.min(), x.max()
- if x.dtype in [float16, float32, float64]:
- if min_val < 0.0 or max_val > 1.0:
- return _handle_invalid_range(msg, raises, min_val, max_val)
+ amin, amax = torch.aminmax(x)
+
+ if x.dtype in (float16, float32, float64):
+ invalid = (amin < 0) | (amax > 1)
else:
- max_int_value = 2**bits - 1
- if min_val < 0 or max_val > max_int_value:
- return _handle_invalid_range(msg, raises, min_val, max_val)
+ max_int_value = (1 << bits) - 1
+ invalid = (amin < 0) | (amax > max_int_value)
+
+ if invalid.item():
+ return _handle_invalid_range(msg, raises, amin, amax)
+
return True
diff --git a/tests/core/test_check.py b/tests/core/test_check.py
index ade2013bc..bbb01e410 100644
--- a/tests/core/test_check.py
+++ b/tests/core/test_check.py
@@ -24,6 +24,7 @@ from kornia.core.check import (
KORNIA_CHECK_IS_COLOR,
KORNIA_CHECK_IS_COLOR_OR_GRAY,
KORNIA_CHECK_IS_GRAY,
+ KORNIA_CHECK_IS_IMAGE,
KORNIA_CHECK_IS_LIST_OF_TENSOR,
KORNIA_CHECK_IS_TENSOR,
KORNIA_CHECK_LAF,
@@ -281,3 +282,38 @@ class TestCheckLaf:
def test_invalid_raises_false(self):
assert KORNIA_CHECK_LAF(torch.rand(4, 2, 2), raises=False) is False
+
+
+class TestCheckIsImage:
+ def test_valid_float(self):
+ assert KORNIA_CHECK_IS_IMAGE(torch.rand(3, 4, 4)) is True
+ assert KORNIA_CHECK_IS_IMAGE(torch.rand(2, 3, 4, 4)) is True
+ assert KORNIA_CHECK_IS_IMAGE(torch.rand(1, 1, 4, 4)) is True
+
+ def test_valid_int(self):
+ x = torch.randint(0, 256, (3, 4, 4), dtype=torch.uint8)
+ assert KORNIA_CHECK_IS_IMAGE(x) is True
+ y = torch.randint(0, 256, (2, 3, 4, 4), dtype=torch.uint8)
+ assert KORNIA_CHECK_IS_IMAGE(y) is True
+
+ def test_invalid_float_range(self):
+ with pytest.raises(Exception):
+ KORNIA_CHECK_IS_IMAGE(torch.tensor([[[-0.5, 1.2]]], dtype=torch.float32))
+
+ def test_invalid_int_range(self):
+ bad = torch.tensor([[[300]]], dtype=torch.int32)
+ with pytest.raises(Exception):
+ KORNIA_CHECK_IS_IMAGE(bad)
+
+ def test_invalid_shape(self):
+ x = torch.rand(1, 4, 4, 4)
+ assert KORNIA_CHECK_IS_IMAGE(x) is True
+
+ def test_invalid_range_no_raise(self):
+ bad = torch.tensor([[[-0.1, 2.0]]], dtype=torch.float32)
+ assert KORNIA_CHECK_IS_IMAGE(bad, raises=False) is False
+
+ def test_invalid_shape_no_raise(self):
+ # When raises=False, shape check is actually enforced
+ bad = torch.rand(1, 4, 4, 4)
+ assert KORNIA_CHECK_IS_IMAGE(bad, raises=False) is False