albumentations-team / albumentations

Fast and flexible image augmentation library. Paper about the library: https://www.mdpi.com/2078-2489/11/2/125
https://albumentations.ai
MIT License
14.2k stars 1.65k forks source link

Some augmentations from domain_adaptation don't accept numpy arrays as reference_images #1483

Open domef opened 1 year ago

domef commented 1 year ago

🐛 Bug

Some augmentations from domain_adaptation (i.e. HistogramMatching, FDA and PixelDistributionAdaptation) crash when a list of numpy array is passed to reference_images.

To Reproduce

import albumentations as A
import numpy as np

reference_image = np.random.randint(0, 255, (256, 256, 3), dtype=np.uint8)
aug = A.Compose([A.HistogramMatching(reference_images=[reference_image])])
image = np.random.randint(0, 255, (256, 256, 3), dtype=np.uint8)
image2 = aug(image=image)["image"]
Traceback (most recent call last):
  File "bug.py", line 9, in <module>
    image2 = aug(image=image)["image"]
  File "/home/myvenv/lib/python3.8/site-packages/albumentations/core/composition.py", line 205, in __call__
    data = t(**data)
  File "/home/myvenv/lib/python3.8/site-packages/albumentations/core/transforms_interface.py", line 102, in __call__
    params = self.get_params()
  File "/home/myvenv/lib/python3.8/site-packages/albumentations/augmentations/domain_adaptation.py", line 170, in get_params
    "reference_image": self.read_fn(random.choice(self.reference_images)),
  File "/home/myvenv/lib/python3.8/site-packages/albumentations/augmentations/utils.py", line 59, in read_rgb_image
    image = cv2.imread(path, cv2.IMREAD_COLOR)
TypeError: Can't convert object to 'str' for 'filename'

The same error is raised with A.FDA and A.PixelDistributionAdaptation.

Expected behavior

Since the doc says reference_images (List[str] or List(np.ndarray)): List of file paths for reference images or list of reference images. and also the type hinting is reference_images: List[Union[str, np.ndarray]] it shouldn't crash when a numpy array is passed

Environment

domef commented 1 year ago

I see in the albumentations tests that lambda x: x is passed to read_fn but I think that it is not very clear, also reference_images could contain any object as long read_fn returns a np.array. Maybe only the documentation and type hinting should be updated.