Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Filter2d failed with noncontiguous input (view --> reshape)

Paua83df5e57e57
diff --git a/kornia/augmentation/random_erasing.py b/kornia/augmentation/random_erasing.py
index 85c706a56..2a2d6e83e 100644
--- a/kornia/augmentation/random_erasing.py
+++ b/kornia/augmentation/random_erasing.py
@@ -125,13 +125,15 @@ def random_rectangle_erase(
erase_scale_range (Tuple[float, float]): range of proportion of erased area against input image.
aspect_ratio_range (Tuple[float, float]): range of aspect ratio of erased area.
"""
- if not (isinstance(erase_scale_range[0], float) and isinstance(erase_scale_range[1],
- float) and erase_scale_range[0] > 0. and erase_scale_range[1] > 0.):
+ if not (isinstance(erase_scale_range[0], float) and
+ isinstance(erase_scale_range[1], float) and
+ erase_scale_range[0] > 0. and erase_scale_range[1] > 0.):
raise TypeError(
f"'erase_scale_range' must be a Tuple[float, float] with positive values"
)
- if not (isinstance(aspect_ratio_range[0], float) and isinstance(aspect_ratio_range[1],
- float) and aspect_ratio_range[0] > 0. and aspect_ratio_range[1] > 0.):
+ if not (isinstance(aspect_ratio_range[0], float) and
+ isinstance(aspect_ratio_range[1], float) and
+ aspect_ratio_range[0] > 0. and aspect_ratio_range[1] > 0.):
raise TypeError(
f"'aspect_ratio_range' must be a Tuple[float, float] with positive values"
)
diff --git a/kornia/filters/filter.py b/kornia/filters/filter.py
index 84434855f..a7fb6a826 100644
--- a/kornia/filters/filter.py
+++ b/kornia/filters/filter.py
@@ -77,6 +77,6 @@ def filter2D(input: torch.Tensor, kernel: torch.Tensor,
# pad the input tensor
height, width = tmp_kernel.shape[-2:]
padding_shape: List[int] = compute_padding((height, width))
- input_pad: torch.Tensor = F.pad(input.view(b * c, 1, h, w), padding_shape, mode=border_type)
+ input_pad: torch.Tensor = F.pad(input.reshape(b * c, 1, h, w), padding_shape, mode=border_type)
# convolve the tensor with the kernel
return F.conv2d(input_pad, tmp_kernel, padding=0, stride=1).view(b, c, h, w)
diff --git a/kornia/filters/median.py b/kornia/filters/median.py
index ab982d60d..4283f501e 100644
--- a/kornia/filters/median.py
+++ b/kornia/filters/median.py
@@ -48,7 +48,7 @@ class MedianBlur(nn.Module):
kernel: torch.Tensor = self.kernel.to(input.device).to(input.dtype)
# map the local window to single vector
features: torch.Tensor = F.conv2d(
- input.view(b * c, 1, h, w), kernel, padding=self.padding, stride=1)
+ input.reshape(b * c, 1, h, w), kernel, padding=self.padding, stride=1)
features = features.view(b, c, -1, h, w) # BxCx(K_h * K_w)xHxW
# compute the median along the feature axis
diff --git a/test/filters/test_blur.py b/test/filters/test_blur.py
index ab309ebc9..ed7a9cfeb 100644
--- a/test/filters/test_blur.py
+++ b/test/filters/test_blur.py
@@ -66,6 +66,15 @@ class TestBoxBlur:
actual = kornia.filters.box_blur(inp, kernel_size)
assert_allclose(actual[:, 0, 2, 2], expected)
+ def test_noncontiguous(self, device):
+ batch_size = 3
+ inp = torch.rand(3, 5, 5).expand(batch_size, -1, -1, -1).to(device)
+
+ kernel_size = (3, 3)
+ actual = kornia.filters.box_blur(inp, kernel_size)
+ expected = actual
+ assert_allclose(actual, actual)
+
def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
img = torch.rand(batch_size, channels, height, width).to(device)
diff --git a/test/filters/test_filters.py b/test/filters/test_filters.py
index db88bb746..67787bbf0 100644
--- a/test/filters/test_filters.py
+++ b/test/filters/test_filters.py
@@ -95,6 +95,15 @@ class TestFilter2D:
actual = kornia.filter2D(input, kernel)
assert_allclose(actual, expected)
+ def test_noncontiguous(self, device):
+ batch_size = 3
+ inp = torch.rand(3, 5, 5).expand(batch_size, -1, -1, -1).to(device)
+ kernel = torch.ones(1, 2, 2).to(device)
+
+ actual = kornia.filter2D(inp, kernel)
+ expected = actual
+ assert_allclose(actual, actual)
+
def test_gradcheck(self, device):
kernel = torch.rand(1, 3, 3).to(device)
input = torch.ones(1, 1, 7, 8).to(device)
diff --git a/test/filters/test_gaussian.py b/test/filters/test_gaussian.py
index f68c6e11d..c52574700 100644
--- a/test/filters/test_gaussian.py
+++ b/test/filters/test_gaussian.py
@@ -36,6 +36,17 @@ class TestGaussianBlur:
gauss = kornia.filters.GaussianBlur2d(kernel_size, sigma, "replicate")
assert gauss(input).shape == batch_shape
+ def test_noncontiguous(self, device):
+ batch_size = 3
+ inp = torch.rand(3, 5, 5).expand(batch_size, -1, -1, -1).to(device)
+
+ kernel_size = (3, 3)
+ sigma = (1.5, 2.1)
+ gauss = kornia.filters.GaussianBlur2d(kernel_size, sigma, "replicate")
+ actual = gauss(inp)
+ expected = actual
+ assert_allclose(actual, actual)
+
def test_gradcheck(self, device):
# test parameters
batch_shape = (2, 3, 11, 7)
@@ -102,6 +113,16 @@ class TestLaplacian:
laplace = kornia.filters.Laplacian(kernel_size)
assert laplace(input).shape == batch_shape
+ def test_noncontiguous(self, device):
+ batch_size = 3
+ inp = torch.rand(3, 5, 5).expand(batch_size, -1, -1, -1).to(device)
+
+ kernel_size = 3
+ laplace = kornia.filters.Laplacian(kernel_size)
+ actual = laplace(inp)
+ expected = actual
+ assert_allclose(actual, actual)
+
def test_gradcheck(self, device):
# test parameters
batch_shape = (2, 3, 11, 7)
diff --git a/test/filters/test_median.py b/test/filters/test_median.py
index 8987f8a16..c3e590b80 100644
--- a/test/filters/test_median.py
+++ b/test/filters/test_median.py
@@ -42,6 +42,15 @@ class TestMedianBlur:
assert_allclose(actual[0, 0, 2, 2], torch.tensor(3.).to(device))
assert_allclose(actual[0, 1, 1, 1], torch.tensor(14.).to(device))
+ def test_noncontiguous(self, device):
+ batch_size = 3
+ inp = torch.rand(3, 5, 5).expand(batch_size, -1, -1, -1).to(device)
+
+ kernel_size = (3, 3)
+ actual = kornia.filters.median_blur(inp, kernel_size)
+ expected = actual
+ assert_allclose(actual, actual)
+
def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
img = torch.rand(batch_size, channels, height, width).to(device)
diff --git a/test/filters/test_motion.py b/test/filters/test_motion.py
index 6b35f0f1b..18a0235aa 100644
--- a/test/filters/test_motion.py
+++ b/test/filters/test_motion.py
@@ -30,6 +30,17 @@ class TestMotionBlur:
motion = kornia.filters.MotionBlur(ksize, angle, direction)
assert motion(input).shape == batch_shape
+ def test_noncontiguous(self, device):
+ batch_size = 3
+ inp = torch.rand(3, 5, 5).expand(batch_size, -1, -1, -1).to(device)
+
+ kernel_size = 3
+ angle = 200.
+ direction = 0.3
+ actual = kornia.filters.motion_blur(inp, kernel_size, angle, direction)
+ expected = actual
+ assert_allclose(actual, actual)
+
def test_gradcheck(self, device):
batch_shape = (2, 3, 11, 7)
ksize = 9
diff --git a/test/filters/test_sobel.py b/test/filters/test_sobel.py
index 0257f1aae..a9170a374 100644
--- a/test/filters/test_sobel.py
+++ b/test/filters/test_sobel.py
@@ -124,6 +124,14 @@ class TestSpatialGradient:
normalized=True)
assert_allclose(edges, expected)
+ def test_noncontiguous(self, device):
+ batch_size = 3
+ inp = torch.rand(3, 5, 5).expand(batch_size, -1, -1, -1).to(device)
+
+ actual = kornia.filters.spatial_gradient(inp)
+ expected = actual
+ assert_allclose(actual, actual)
+
def test_gradcheck(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
img = torch.rand(batch_size, channels, height, width).to(device)
@@ -173,6 +181,14 @@ class TestSobel:
edges = kornia.filters.sobel(inp, normalized=False)
assert_allclose(edges, expected)
+ def test_noncontiguous(self, device):
+ batch_size = 3
+ inp = torch.rand(3, 5, 5).expand(batch_size, -1, -1, -1).to(device)
+
+ actual = kornia.filters.sobel(inp)
+ expected = actual
+ assert_allclose(actual, actual)
+
def test_gradcheck_unnorm(self, device):
batch_size, channels, height, width = 1, 2, 5, 4
img = torch.rand(batch_size, channels, height, width).to(device)