Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(filters): validate each in_range Tensor bound and accept the documented bound forms (#5260)

Dmytro Mishkin81f7e7df17d5
diff --git a/changelog.d/5260.fixed.md b/changelog.d/5260.fixed.md
new file mode 100644
index 000000000..d1c0a15c6
--- /dev/null
+++ b/changelog.d/5260.fixed.md
@@ -0,0 +1,13 @@
+`kornia.filters.in_range` and `InRange` now check each Tensor bound on its own. A bound that does not fit the input
+raises a `ValueError` naming it, instead of being accepted whenever the other bound was `(B, C, 1, 1)`. A `(W,)` bound
+used to be applied per column. A bound with the wrong batch, height or width was broadcast into a wrongly shaped mask
+when the image's own dimension was 1, and raised a `RuntimeError` otherwise. A bound with several channels on a
+single-channel image was applied as extra thresholds. A Tensor bound paired with a tuple bound raises a `TypeError`.
+A 1-D bound is read per channel and needs `C` elements (or one), so `(C,)` is accepted. Any other Tensor bound of at
+most four dimensions is aligned with the input `(B, C, H, W)` from its last dimension and each dimension must be 1 or
+the input's. That accepts the documented `(1, C, 1, 1)`. It keeps the shapes that already gave a correct mask next to a
+`(B, C, 1, 1)` bound, such as `(C, 1, 1)`, `(C, H, W)`, `(B, C, H, W)` and a 0-d Tensor, and accepts them for both
+bounds.
+A 1-D bound whose length equals both the channel count and the width used to be applied per column and is now applied
+per channel, without an error.
+The docstring formulas now read `lower ≀ input ≀ upper` with the conjunction over `c = 0 … C βˆ’ 1`.
diff --git a/kornia/filters/in_range.py b/kornia/filters/in_range.py
index 0bfed8d80..29dee3d91 100644
--- a/kornia/filters/in_range.py
+++ b/kornia/filters/in_range.py
@@ -25,6 +25,27 @@ from torch import nn
from kornia.image.utils import perform_keep_shape_image
+def _as_bchw_bound(name: str, bound: torch.Tensor, input_shape: torch.Size) -> torch.Tensor:
+ """Validate a Tensor bound against the ``(B, C, H, W)`` input and return it as a 4-D Tensor.
+
+ A 1-D bound is read per channel: it must have ``C`` elements (or one) and becomes ``(1, C, 1, 1)``. A bound with
+ zero to four other dimensions is aligned with the input from the last dimension, and every dimension must be 1 or
+ the input's own size.
+ """
+ if bound.dim() == 1:
+ if bound.shape[0] in (1, input_shape[1]):
+ return bound.reshape(1, -1, 1, 1)
+ elif bound.dim() <= 4:
+ shape = (1,) * (4 - bound.dim()) + tuple(bound.shape)
+ if all(b in (1, i) for b, i in zip(shape, input_shape)):
+ return bound.reshape(shape)
+ raise ValueError(
+ f"`{name}` as a Tensor must be 0-d, have shape (C,) or (1,), or have two to four dimensions that, aligned "
+ f"with the input from the last dimension, are each 1 or equal to the input's. "
+ f"Got {tuple(bound.shape)} for an input viewed as (B, C, H, W) = {tuple(input_shape)}."
+ )
+
+
@perform_keep_shape_image
def in_range(
input: torch.Tensor,
@@ -39,15 +60,15 @@ def in_range(
The formula applied for single-channel torch.Tensor is:
.. math::
- \text{out}(I) = \text{lower}(I) \leq \text{input}(I) \geq \text{upper}(I)
+ \text{out}(I) = \text{lower}(I) \leq \text{input}(I) \leq \text{upper}(I)
The formula applied for multi-channel torch.Tensor is:
.. math::
- \text{out}(I) = \bigwedge_{c=0}^{C}
- \left( \text{lower}_c(I) \leq \text{input}_c(I) \geq \text{upper}_c(I) \right)
+ \text{out}(I) = \bigwedge_{c=0}^{C-1}
+ \left( \text{lower}_c(I) \leq \text{input}_c(I) \leq \text{upper}_c(I) \right)
- where `C` is the number of channels.
+ where `C` is the number of channels. Both comparisons are inclusive.
Args:
input: The input torch.Tensor to be filtered in the shape of :math:`(*, *, H, W)`.
@@ -60,7 +81,10 @@ def in_range(
or filtered input image :math:`(*, *, H, W)`.
Raises:
- ValueError: If the shape of `lower`, `upper`, and `input` image channels do not match.
+ TypeError: If `lower` or `upper` is neither a tuple nor a torch.Tensor, if one is a tuple and the other a
+ torch.Tensor, or if `return_mask` is not a bool.
+ ValueError: If a tuple bound does not have one element per channel, or a torch.Tensor bound does not fit the
+ input as described in the note.
.. note::
Clarification of `lower` and `upper`:
@@ -72,7 +96,19 @@ def in_range(
The torch.Tensor shape should be (B, C, 1, 1), where B is the batch size and C is
the number of channels.
- - If the torch.Tensor has a 1-D shape, same bound will be applied across all batches.
+ - A 1-D torch.Tensor is read per channel, never per column: its shape is (C,) (or one element, for the
+ same bound in every channel) and the same bound is applied across all batches. A 1-D bound of any other
+ length, such as (W,) when W is neither C nor 1, raises. When W == C, a (W,) bound is the (C,) bound, not a
+ per-column one.
+
+ - Any other torch.Tensor with at most four dimensions is aligned with the input :math:`(B, C, H, W)` from the
+ last dimension, and every dimension must be 1 or the input's. For example (1, C, 1, 1) and (C, 1, 1) apply
+ the same per-channel bound to all batches, (B, C, H, W), (C, H, W) and (H, W) give a different bound at each
+ pixel, and a 0-d torch.Tensor applies one bound everywhere.
+
+ - Each torch.Tensor bound is checked on its own, so a mis-shaped one raises whatever the other bound is.
+ The input is read as :math:`(B, C, H, W)`: a 3-D input is :math:`(1, C, H, W)`, a 2-D input is
+ :math:`(1, 1, H, W)`, and for more than four dimensions the leading dimensions are flattened into B.
Examples:
>>> rng = torch.manual_seed(1)
@@ -130,13 +166,11 @@ def in_range(
)
elif isinstance(lower, torch.Tensor) and isinstance(upper, torch.Tensor):
- valid_tensor_shape = (input_shape[0], input_shape[1], 1, 1)
- if valid_tensor_shape not in (lower.shape, upper.shape):
- raise ValueError(
- "`lower` and `upper` bounds as Tensors must have compatible shapes with the input (B, C, 1, 1)."
- )
- lower = lower.to(input)
- upper = upper.to(input)
+ lower = _as_bchw_bound("lower", lower, input_shape).to(input)
+ upper = _as_bchw_bound("upper", upper, input_shape).to(input)
+
+ else:
+ raise TypeError("Invalid `lower` and `upper` format. Both should be tuples or both torch.Tensor.")
# Apply lower and upper bounds. Combine masks with logical_and.
mask = torch.logical_and(input >= lower, input <= upper)
diff --git a/tests/filters/test_in_range.py b/tests/filters/test_in_range.py
index d6b66a0d0..e390796c7 100644
--- a/tests/filters/test_in_range.py
+++ b/tests/filters/test_in_range.py
@@ -15,8 +15,6 @@
# limitations under the License.
#
-import re
-
import pytest
import torch
@@ -92,14 +90,10 @@ class TestInRange(BaseTester):
):
InRange(lower=(0.2,), upper=(0.2,))(input_tensor)
- lower = torch.tensor([0.2, 0.2, 0.2])
- upper = torch.tensor([0.6, 0.6, 0.6])
- with pytest.raises(
- ValueError,
- match=re.escape(
- "`lower` and `upper` bounds as Tensors must have compatible shapes with the input (B, C, 1, 1)."
- ),
- ):
+ # A 1-D Tensor bound must have one element per channel: C = 3 here.
+ lower = torch.tensor([0.2, 0.2, 0.2, 0.2])
+ upper = torch.tensor([0.6, 0.6, 0.6, 0.6])
+ with pytest.raises(ValueError, match=r"`lower` as a Tensor must be"):
InRange(lower=lower, upper=upper)(input_tensor)
lower = torch.tensor([0.2, 0.2, 0.2])
@@ -107,8 +101,275 @@ class TestInRange(BaseTester):
with pytest.raises(Exception, match=r"Invalid `return_mask` format. Should be boolean."):
InRange(lower=lower, upper=upper, return_mask=2)(input_tensor)
+ @staticmethod
+ def _per_channel_bounds(channels, device, dtype):
+ lower = torch.linspace(0.0, 0.3, channels, device=device, dtype=torch.float32).to(dtype)
+ upper = torch.linspace(0.7, 0.95, channels, device=device, dtype=torch.float32).to(dtype)
+ return lower, upper
+
+ @staticmethod
+ def _bounds_in_form(bound, form, batch_size):
+ # `bound` is the per-channel (C,) vector; build the same bound in every accepted tensor layout.
+ channels = bound.shape[0]
+ if form == "c":
+ return bound
+ if form == "c11":
+ return bound.reshape(channels, 1, 1)
+ if form == "chw":
+ return bound.reshape(channels, 1, 1).repeat(1, 4, 5)
+ if form == "1c11":
+ return bound.reshape(1, channels, 1, 1)
+ if form == "1chw":
+ return bound.reshape(1, channels, 1, 1).repeat(1, 1, 4, 5)
+ if form == "bc11":
+ return bound.reshape(1, channels, 1, 1).repeat(batch_size, 1, 1, 1)
+ if form == "bchw":
+ return bound.reshape(1, channels, 1, 1).repeat(batch_size, 1, 4, 5)
+ raise AssertionError(form)
+
+ @pytest.mark.parametrize("batch_size", [1, 3])
+ @pytest.mark.parametrize("lower_form", ["c", "c11", "chw", "1c11", "1chw", "bc11", "bchw"])
+ @pytest.mark.parametrize("upper_form", ["c", "c11", "chw", "1c11", "1chw", "bc11", "bchw"])
+ @pytest.mark.parametrize("return_mask", [True, False])
+ def test_tensor_bound_forms_match_bchw(self, batch_size, lower_form, upper_form, return_mask, device, dtype):
+ # Every accepted tensor layout of a bound gives the same result as the (B, C, 1, 1) bound, in any pairing.
+ torch.manual_seed(0)
+ channels = 3
+ img = torch.rand(batch_size, channels, 4, 5).to(device=device, dtype=dtype)
+ lower_c, upper_c = self._per_channel_bounds(channels, device, dtype)
+ expected = in_range(
+ img,
+ self._bounds_in_form(lower_c, "bc11", batch_size),
+ self._bounds_in_form(upper_c, "bc11", batch_size),
+ return_mask=return_mask,
+ )
+ lower = self._bounds_in_form(lower_c, lower_form, batch_size)
+ upper = self._bounds_in_form(upper_c, upper_form, batch_size)
+
+ actual = in_range(img, lower, upper, return_mask=return_mask)
+ module_actual = InRange(lower, upper, return_mask=return_mask)(img)
+
+ assert actual.dtype == img.dtype
+ assert actual.device == img.device
+ assert torch.equal(actual, expected)
+ assert torch.equal(module_actual, expected)
+ # Guard against a vacuous comparison: the bounds keep some pixels and reject others.
+ mask = in_range(img, lower, upper, return_mask=True)
+ assert 0 < mask.sum() < mask.numel()
+
+ @staticmethod
+ def _reference_mask(img, lower, upper):
+ """Compute the mask with explicit loops, on the CPU.
+
+ A bound is aligned with (B, C, H, W) from its last dimension, a dimension of size 1 is repeated, and a 1-D
+ bound is read per channel.
+ """
+ batch, channels, height, width = img.shape
+
+ def at(bound, b, c, y, x):
+ if bound.dim() == 1:
+ return float(bound[0 if bound.shape[0] == 1 else c])
+ shape = (1,) * (4 - bound.dim()) + tuple(bound.shape)
+ index = tuple(0 if size == 1 else i for size, i in zip(shape, (b, c, y, x)))
+ return float(bound.reshape(shape)[index])
+
+ mask = torch.zeros(batch, 1, height, width)
+ for b in range(batch):
+ for y in range(height):
+ for x in range(width):
+ keep = all(
+ at(lower, b, c, y, x) <= float(img[b, c, y, x]) <= at(upper, b, c, y, x)
+ for c in range(channels)
+ )
+ mask[b, 0, y, x] = float(keep)
+ return mask
+
+ @pytest.mark.parametrize("batch_size", [1, 2])
+ @pytest.mark.parametrize("pairing", ["lower", "upper", "both"])
+ @pytest.mark.parametrize(
+ "shape",
+ [
+ pytest.param(lambda b: (), id="0d"),
+ pytest.param(lambda b: (1,), id="1d-one-element"),
+ pytest.param(lambda b: (3,), id="c"),
+ pytest.param(lambda b: (3, 1, 1), id="c11"),
+ pytest.param(lambda b: (1, 3, 1, 1), id="1c11"),
+ pytest.param(lambda b: (b, 3, 1, 1), id="bc11"),
+ pytest.param(lambda b: (3, 4, 5), id="chw"),
+ pytest.param(lambda b: (1, 3, 4, 5), id="1chw"),
+ pytest.param(lambda b: (b, 3, 4, 5), id="bchw"),
+ pytest.param(lambda b: (4, 5), id="hw"),
+ pytest.param(lambda b: (1, 1, 4, 5), id="11hw"),
+ pytest.param(lambda b: (b, 1, 1, 1), id="b111"),
+ pytest.param(lambda b: (1, 5), id="1w"),
+ ],
+ )
+ def test_tensor_bound_layouts_match_explicit_loops(self, batch_size, pairing, shape, device, dtype):
+ # H != W != C, so a bound applied along the wrong axis changes the mask. `lower` / `upper` pair the layout
+ # with a (B, C, 1, 1) bound; `both` uses the layout for both bounds.
+ gen = torch.Generator().manual_seed(0)
+ img = torch.rand(batch_size, 3, 4, 5, generator=gen).to(dtype)
+ layout = shape(batch_size)
+ lower_t = (torch.rand(layout, generator=gen) * 0.3).to(dtype)
+ upper_t = (0.7 + torch.rand(layout, generator=gen) * 0.3).to(dtype)
+ lower_b = (torch.rand(batch_size, 3, 1, 1, generator=gen) * 0.3).to(dtype)
+ upper_b = (0.7 + torch.rand(batch_size, 3, 1, 1, generator=gen) * 0.3).to(dtype)
+ lower, upper = {"lower": (lower_t, upper_b), "upper": (lower_b, upper_t), "both": (lower_t, upper_t)}[pairing]
+
+ expected = self._reference_mask(img, lower, upper).to(device=device, dtype=dtype)
+ assert 0 < expected.sum() < expected.numel() # not a vacuous comparison
+
+ actual = in_range(img.to(device), lower.to(device), upper.to(device), return_mask=True)
+ assert actual.dtype == dtype
+ assert actual.device == img.to(device).device
+ assert torch.equal(actual, expected)
+ assert torch.equal(InRange(lower.to(device), upper.to(device), return_mask=True)(img.to(device)), expected)
+
+ def test_one_dim_bound_is_per_channel_not_per_column(self, device, dtype):
+ # (C,) must apply along the channel axis: a bound of 0 on channel 2 only clears the pixels through channel 2.
+ img = torch.full((2, 3, 4, 5), 0.5, device=device, dtype=dtype)
+ lower = torch.tensor([0.0, 0.0, 0.0], device=device, dtype=dtype)
+ upper = torch.tensor([1.0, 1.0, 0.0], device=device, dtype=dtype)
+ assert not in_range(img, lower, upper, return_mask=True).any()
+ upper = torch.tensor([1.0, 1.0, 1.0], device=device, dtype=dtype)
+ assert in_range(img, lower, upper, return_mask=True).all()
+
+ def test_one_dim_bound_as_long_as_channels_and_width_is_per_channel(self, device, dtype):
+ # C == W == 5: the shape cannot tell a per-channel bound from a per-column one, and a 1-D bound is per channel.
+ # Channel 4 has an upper bound of 0, so every pixel is cleared; a per-column reading would keep columns 0-3.
+ img = torch.full((2, 5, 4, 5), 0.5, device=device, dtype=dtype)
+ lower = torch.zeros(5, device=device, dtype=dtype)
+ upper = torch.tensor([1.0, 1.0, 1.0, 1.0, 0.0], device=device, dtype=dtype)
+ assert not in_range(img, lower, upper, return_mask=True).any()
+ assert not InRange(lower, upper, return_mask=True)(img).any()
+ assert not in_range(img, lower, upper, return_mask=False).any()
+
+ @pytest.mark.parametrize("input_shape", [(3, 4, 5), (2, 3, 4, 5), (2, 2, 3, 4, 5)])
+ def test_one_dim_bound_leading_dims(self, input_shape, device, dtype):
+ # `in_range` flattens leading dims into the batch; a (C,) bound is shared by every flattened image.
+ torch.manual_seed(0)
+ img = torch.rand(input_shape, device=device, dtype=dtype)
+ lower_c, upper_c = self._per_channel_bounds(3, device, dtype)
+ expected = in_range(img, tuple(lower_c.tolist()), tuple(upper_c.tolist()), return_mask=True)
+ actual = in_range(img, lower_c, upper_c, return_mask=True)
+ assert actual.shape == expected.shape
+ self.assert_close(actual, expected)
+
+ def test_bounds_are_inclusive(self, device, dtype):
+ # lower <= input <= upper: values equal to a bound are kept (these values are exact in every dtype).
+ img = torch.tensor([0.125, 0.25, 0.5, 0.75, 0.875], device=device, dtype=dtype).reshape(1, 1, 1, 5)
+ expected = torch.tensor([0.0, 1.0, 1.0, 1.0, 0.0], device=device, dtype=dtype).reshape(1, 1, 1, 5)
+ by_tuple = in_range(img, (0.25,), (0.75,), return_mask=True)
+ by_tensor = in_range(
+ img, torch.tensor([0.25]).reshape(1, 1, 1, 1), torch.tensor([0.75]).reshape(1, 1, 1, 1), return_mask=True
+ )
+ self.assert_close(by_tuple, expected)
+ self.assert_close(by_tensor, expected)
+
+ def test_multi_channel_mask_requires_every_channel(self, device, dtype):
+ # The conjunction runs over the C channels: one channel out of range clears the pixel.
+ img = torch.tensor([0.5, 0.5, 0.5, 0.5, 0.5, 0.95], device=device, dtype=dtype).reshape(1, 3, 1, 2)
+ mask = in_range(img, (0.0, 0.0, 0.0), (1.0, 1.0, 0.9), return_mask=True)
+ self.assert_close(mask, torch.tensor([[[[1.0, 0.0]]]], device=device, dtype=dtype))
+
+ @pytest.mark.parametrize("batch_size", [1, 2])
+ @pytest.mark.parametrize(
+ "bad_shape",
+ [
+ pytest.param(lambda b: (4,), id="1d-C+1"),
+ pytest.param(lambda b: (5,), id="1d-W"), # a 1-D bound is per channel, so W elements fit no axis
+ pytest.param(lambda b: (3, 1), id="2d-C1"),
+ pytest.param(lambda b: (5, 4), id="2d-WH"),
+ pytest.param(lambda b: (4, 4, 5), id="3d-C+1HW"),
+ pytest.param(lambda b: (3, 5, 4), id="3d-CWH"),
+ pytest.param(lambda b: (1, b, 3, 1, 1), id="5d"),
+ pytest.param(lambda b: (1, 1, 1, 1, 1), id="5d-all-ones"), # every dimension fits, only the rank is wrong
+ pytest.param(lambda b: (b, 4, 1, 1), id="4d-C+1"),
+ pytest.param(lambda b: (b, 1, 3, 1), id="4d-channels-on-height-axis"),
+ pytest.param(lambda b: (b + 1, 3, 1, 1), id="4d-B+1"),
+ pytest.param(lambda b: (b, 3, 7, 1), id="4d-height-neither-1-nor-H"),
+ ],
+ )
+ @pytest.mark.parametrize("bad_bound", ["lower", "upper"])
+ def test_mis_shaped_tensor_bound_raises(self, batch_size, bad_shape, bad_bound, device, dtype):
+ # Every bound is validated on its own: a mis-shaped one raises, whatever the other bound is.
+ img = torch.rand(batch_size, 3, 4, 5, device=device, dtype=dtype)
+ bad = torch.full(bad_shape(batch_size), 0.5, device=device, dtype=dtype)
+ match = rf"`{bad_bound}` as a Tensor must be"
+
+ # The other bound is a well-formed (B, C, 1, 1) Tensor, then a well-formed (C,) Tensor.
+ for good_shape in [(batch_size, 3, 1, 1), (3,)]:
+ good = torch.full(good_shape, 0.5, device=device, dtype=dtype)
+ lower, upper = (bad, good) if bad_bound == "lower" else (good, bad)
+ with pytest.raises(ValueError, match=match):
+ in_range(img, lower, upper)
+ with pytest.raises(ValueError, match=match):
+ InRange(lower, upper)(img)
+
+ @pytest.mark.parametrize(
+ "input_shape, input_view",
+ [
+ ((2, 3, 4, 5), (2, 3, 4, 5)),
+ ((3, 4, 5), (1, 3, 4, 5)),
+ ((4, 5), (1, 1, 4, 5)),
+ ((2, 2, 3, 4, 5), (4, 3, 4, 5)),
+ ],
+ )
+ @pytest.mark.parametrize("bad_bound", ["lower", "upper"])
+ def test_error_message_quotes_the_bound_shape_and_the_input_view(
+ self, input_shape, input_view, bad_bound, device, dtype
+ ):
+ # The error quotes the shape of the offending bound and the (B, C, H, W) view of the input it was checked
+ # against.
+ img = torch.rand(input_shape, device=device, dtype=dtype)
+ bad = torch.zeros(7, device=device, dtype=dtype)
+ good = torch.zeros(input_view[1], device=device, dtype=dtype)
+ lower, upper = (bad, good) if bad_bound == "lower" else (good, bad)
+ with pytest.raises(ValueError) as error:
+ in_range(img, lower, upper)
+ message = str(error.value)
+ assert message.startswith(f"`{bad_bound}` as a Tensor must be")
+ assert f"Got (7,) for an input viewed as (B, C, H, W) = {input_view}." in message
+
+ @pytest.mark.parametrize("tensor_bound", ["lower", "upper"])
+ def test_tensor_bound_with_tuple_bound_raises(self, tensor_bound, device, dtype):
+ # A Tensor bound paired with a tuple bound raises a TypeError that names the rule, whatever the Tensor's shape.
+ img = torch.rand(2, 3, 4, 5, device=device, dtype=dtype)
+ for shape in [(5,), (3,), (2, 3, 1, 1)]:
+ bound = torch.full(shape, 0.5, device=device, dtype=dtype)
+ lower, upper = (bound, (0.9, 0.9, 0.9)) if tensor_bound == "lower" else ((0.1, 0.1, 0.1), bound)
+ with pytest.raises(TypeError, match=r"Both should be tuples or both torch\.Tensor"):
+ in_range(img, lower, upper)
+ with pytest.raises(TypeError, match=r"Both should be tuples or both torch\.Tensor"):
+ InRange(lower, upper)(img)
+
+ def test_tensor_bounds_follow_input_dtype_and_device(self, device, dtype):
+ img = torch.rand(2, 3, 4, 5, device=device, dtype=dtype)
+ lower = torch.tensor([0.1, 0.2, 0.3], dtype=torch.float32) # created on the CPU, not on `device`
+ upper = torch.tensor([0.9, 0.8, 0.7], dtype=torch.float32)
+ out = in_range(img, lower, upper, return_mask=False)
+ mask = in_range(img, lower, upper, return_mask=True)
+ assert out.dtype == dtype
+ assert out.device == img.device
+ assert mask.dtype == dtype
+ assert mask.device == img.device
+
+ @pytest.mark.parametrize("bound_shape", [(1,), (1, 1, 1, 1)])
+ def test_tensor_bounds_are_cast_to_the_input_dtype(self, bound_shape, device, dtype):
+ # 0.1 is not exact in any dtype. The input holds 0.1 rounded to `dtype`; a bound of a wider dtype holds a
+ # different number and only equals the input once it is cast to the input's dtype, so the pixel is kept only
+ # if it is. The bound is created on the CPU, which also covers devices without float64.
+ bound_dtype = torch.float32 if dtype in (torch.float16, torch.bfloat16) else torch.float64
+ img = torch.full((1, 1, 1, 1), 0.1, dtype=torch.float64).to(dtype).to(device)
+ bound = torch.full(bound_shape, 0.1, dtype=bound_dtype)
+ assert bound.item() != img.item() or dtype == torch.float64 # the bound really is a different number
+ mask = in_range(img, bound, bound, return_mask=True)
+ assert mask.dtype == dtype
+ assert mask.all()
+ self.assert_close(in_range(img, bound, bound, return_mask=False), img)
+
def test_tensor_bounds_return_masked_input(self, device, dtype):
- # Exercises the Tensor-bounds branch (lines 132-139) with return_mask=False (line 148)
+ # Exercises the Tensor-bounds branch with return_mask=False
inp = torch.ones(1, 3, 4, 4, device=device, dtype=dtype) * 0.5
lower = torch.tensor([0.2, 0.2, 0.2], device=device, dtype=dtype).reshape(1, 3, 1, 1)
upper = torch.tensor([0.8, 0.8, 0.8], device=device, dtype=dtype).reshape(1, 3, 1, 1)
@@ -149,5 +410,18 @@ class TestInRange(BaseTester):
pytest.skip("Failing to compile on CPU see pytorch/pytorch#126619")
data = torch.rand(batch_size, 3, 5, 5, device=device, dtype=dtype)
op = InRange(lower=(0.2, 0.2, 0.2), upper=(0.6, 0.6, 0.6), return_mask=True)
- op_optimized = torch_optimizer(op)
+ op_optimized = torch_optimizer(op, fullgraph=True)
+ self.assert_close(op(data), op_optimized(data))
+
+ @pytest.mark.parametrize("batch_size", [1, 2])
+ @pytest.mark.parametrize("form", ["c", "c11", "1c11", "chw", "bc11"])
+ def test_dynamo_tensor_bounds(self, batch_size, form, device, dtype, torch_optimizer):
+ if device == torch.device("cpu") and torch_version() in {"2.3.0", "2.3.1"}:
+ pytest.skip("Failing to compile on CPU see pytorch/pytorch#126619")
+ data = torch.rand(batch_size, 3, 4, 5, device=device, dtype=dtype)
+ lower_c, upper_c = self._per_channel_bounds(3, device, dtype)
+ lower = self._bounds_in_form(lower_c, form, batch_size)
+ upper = self._bounds_in_form(upper_c, form, batch_size)
+ op = InRange(lower=lower, upper=upper, return_mask=True)
+ op_optimized = torch_optimizer(op, fullgraph=True)
self.assert_close(op(data), op_optimized(data))