π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): convert dict image inputs (#5855)
Karnajeet Gosavidea03570de57
diff --git a/changelog.d/5855.fixed.md b/changelog.d/5855.fixed.mdnew file mode 100644index 000000000..c3790831f--- /dev/null+++ b/changelog.d/5855.fixed.md@@ -0,0 +1 @@+`AugmentationSequential` called with a dictionary now converts a PIL image or an image file path under an image key (`input`, `image`) as it converts the same value given positionally, instead of failing with `AttributeError`. A path under an image key that does not exist raises `FileNotFoundError`.diff --git a/kornia/augmentation/container/augment.py b/kornia/augmentation/container/augment.pyindex df789d882..2a3615ce0 100644--- a/kornia/augmentation/container/augment.py+++ b/kornia/augmentation/container/augment.py@@ -797,7 +797,8 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):inputs: Inputs to operate on.input_names_to_handle: List of input names to convert.If None, convert every tensor, NumPy array and PIL image argument, and load a string as an image- path only if it is the first positional argument.+ path only if it is the first positional argument, or the value of an ``input`` or ``image`` key+ (``image_0`` too) in a dictionary input.output_type: Desired output type ('pt', 'numpy', or 'pil').kwargs: Additional arguments.@@ -812,7 +813,8 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):keys, data_keys, args, _ = self._preproc_dict_data(inputs[0])converted_dict = dict(inputs[0])for key, arg, data_key in zip(keys, args, data_keys):- if data_key in _IMG_OPTIONS and _is_numpy_array(arg):+ # The key marks a string as an image path, so a missing file raises instead of passing through.+ if data_key in _IMG_OPTIONS and (isinstance(arg, str) or self._is_valid_arg(arg)):converted_dict[key] = self.to_tensor(arg)else:converted_dict[key] = self._convert_non_image(arg, data_key)diff --git a/tests/augmentation/container/test_augmentation_sequential.py b/tests/augmentation/container/test_augmentation_sequential.pyindex 704b4b564..c373bf876 100644--- a/tests/augmentation/container/test_augmentation_sequential.py+++ b/tests/augmentation/container/test_augmentation_sequential.py@@ -1890,6 +1890,28 @@ class TestConventionAugmentationSequential(BaseTester):assert torch.equal(early_output, expected)assert torch.equal(image_first({"image": image, "mask": mask})["mask"], expected)+ @pytest.mark.parametrize("input_kind", ["pil", "path", "numpy"])+ def test_dictionary_image_accepts_pil_path_and_numpy_5852(self, input_kind, tmp_path):+ import numpy as np+ from PIL import Image as PILImage++ image = PILImage.new("RGB", (5, 4), color=(200, 100, 25))+ path = tmp_path / "image.png"+ image.save(path)+ value = {"pil": image, "path": str(path), "numpy": np.array(image)}[input_kind]++ positional = K.AugmentationSequential(K.RandomHorizontalFlip(p=0.0), data_keys=["input"])(value)+ dictionary = K.AugmentationSequential(K.RandomHorizontalFlip(p=0.0), data_keys=None)({"input": value})++ assert torch.equal(dictionary["input"], positional)++ @pytest.mark.parametrize("key", ["input", "image"])+ def test_dictionary_image_missing_path_raises_5852(self, key, tmp_path):+ # The key marks the string as an image path, so a missing file is reported as such.+ aug = K.AugmentationSequential(K.RandomHorizontalFlip(p=0.0), data_keys=None)+ with pytest.raises(FileNotFoundError):+ aug({key: str(tmp_path / "missing.png")})+@pytest.mark.parametrize("mask_dtypes", [(torch.int64, torch.bool), (torch.bool, torch.int64)])def test_convention_each_mask_keeps_its_own_dtype_4478(self, mask_dtypes, device, dtype):# Convention pin for kornia#4478: each mask output comes back in the dtype of its own argument, so labels