Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fixed RandomJigsaw #2494 (#2504)

Jian Shi88a7bbb3f79a
diff --git a/docs/generate_examples.py b/docs/generate_examples.py
index 7ae4758c1..0e50b138f 100644
--- a/docs/generate_examples.py
+++ b/docs/generate_examples.py
@@ -157,6 +157,7 @@ def main():
"RandomSolarize": ((0.2, 0.2), 2, 2019),
"RandomVerticalFlip": ((), 1, 2020),
"RandomThinPlateSpline": ((), 1, 2020),
+ "RandomJigsaw": ((), 2, 2020),
}
# ITERATE OVER THE TRANSFORMS
@@ -171,6 +172,8 @@ def main():
# set seed
torch.manual_seed(seed)
+ if aug_name == "RandomJigsaw": # make sure the image is dividable
+ img_in = K.geometry.resize(img_in, (1020, 500))
# apply the augmentation to the image and concat
out = aug(img_in)
diff --git a/kornia/augmentation/_2d/mix/jigsaw.py b/kornia/augmentation/_2d/mix/jigsaw.py
index d43ad4c45..8cc3423af 100644
--- a/kornia/augmentation/_2d/mix/jigsaw.py
+++ b/kornia/augmentation/_2d/mix/jigsaw.py
@@ -13,7 +13,7 @@ __all__ = ["RandomJigsaw"]
class RandomJigsaw(MixAugmentationBaseV2):
r"""RandomJigsaw augmentation.
- .. image:: https://raw.githubusercontent.com/kornia/data/main/random_jigsaw.png
+ .. image:: _static/img/RandomJigsaw.png
Make Jigsaw puzzles for each image individually. To mix with different images in a
batch, referring to :class:`kornia.augmentation.RandomMosic`.
@@ -61,11 +61,14 @@ class RandomJigsaw(MixAugmentationBaseV2):
b, c, h, w = input.shape
perm = params["permutation"]
+ # Note: with a 100x100 image and a grid size of 3x3, it could work if
+ # we make h = piece_size_h * self.flags["grid"][0] with one pixel loss, then resize to 100 x 100.
+ # Probably worth to check if we should tolerate such "errorness" or to raise it as an error.
piece_size_h, piece_size_w = input.shape[-2] // self.flags["grid"][0], input.shape[-1] // self.flags["grid"][1]
# Convert to C BxN H' W'
input = (
- input.unfold(2, piece_size_h, piece_size_w)
- .unfold(3, piece_size_h, piece_size_w)
+ input.unfold(2, piece_size_h, piece_size_h)
+ .unfold(3, piece_size_w, piece_size_w)
.reshape(b, c, -1, piece_size_h, piece_size_w)
.permute(1, 0, 2, 3, 4)
.reshape(c, -1, piece_size_h, piece_size_w)
@@ -75,7 +78,7 @@ class RandomJigsaw(MixAugmentationBaseV2):
input = (
input.reshape(-1, b, self.flags["grid"][1], h, piece_size_w)
.permute(0, 1, 2, 4, 3)
- .reshape(-1, b, h, w)
+ .reshape(-1, b, w, h)
.permute(0, 1, 3, 2)
.permute(1, 0, 2, 3)
)
diff --git a/test/augmentation/test_augmentation_mix.py b/test/augmentation/test_augmentation_mix.py
index 29afd7f2b..41c07ec1c 100644
--- a/test/augmentation/test_augmentation_mix.py
+++ b/test/augmentation/test_augmentation_mix.py
@@ -338,11 +338,20 @@ class TestRandomMosaic:
class TestRandomJigsaw:
- def test_smoke(self):
+ def test_smoke(self, device, dtype):
f = RandomJigsaw(data_keys=["input"])
repr = "RandomJigsaw(grid=(4, 4), p=0.5, p_batch=1.0, same_on_batch=False, grid=(4, 4))"
assert str(f) == repr
+ # Test square and non-square images.
+ f = RandomJigsaw(grid=(2, 2), p=1.0, data_keys=["input"])
+ input = torch.arange(64, device=device, dtype=dtype).reshape(2, 1, 4, 8)
+ f(input)
+ input = torch.arange(64, device=device, dtype=dtype).reshape(2, 1, 8, 4)
+ f(input)
+ input = torch.arange(32, device=device, dtype=dtype).reshape(2, 1, 4, 4)
+ f(input)
+
def test_numerical(self, device, dtype):
torch.manual_seed(22)
f = RandomJigsaw(grid=(2, 2), p=1.0, data_keys=["input"])