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
13.94k stars 1.62k forks source link

HueSaturationValue: Invalid number of channels in input image #499

Open Dok11 opened 4 years ago

Dok11 commented 4 years ago

🐛 Bug

To Reproduce

Steps to reproduce the behavior:

for image_index in range(len(images_1)):
    augmentation = aug(p=0.9)
    data = {'image': images_1[image_index]}
    augmented = augmentation(**data)

def aug(p=0.5):
    return Compose([HueSaturationValue(p=0.3)], p=p)

Where images_1[image_index] is `ndarray (64, 64, 1)

Environment

Additional context

Very similar to https://github.com/albumentations-team/albumentations/issues/180

Dipet commented 4 years ago

I am cannot reproduce this error. Please add code for image generation and error traceback.

import numpy as np
import albumentations as A

def aug(p=0.5):
    return A.Compose([A.HueSaturationValue(p=0.3)], p=p)

print(A.__version__) # 0.4.3
np.random.seed(0)
images_1 = [np.random.randint(0, 256, [64, 64, 1], dtype=np.uint8)]

for image_index in range(len(images_1)):
    augmentation = aug(p=0.9)
    data = {'image': images_1[image_index]}
    augmented = augmentation(**data)
Dok11 commented 4 years ago
  File "C:\Users\User\Anaconda3\envs\DLDM\lib\site-packages\albumentations\core\composition.py", line 176, in __call__
    data = t(force_apply=force_apply, **data)
  File "C:\Users\User\Anaconda3\envs\DLDM\lib\site-packages\albumentations\core\transforms_interface.py", line 87, in __call__
    return self.apply_with_params(params, **kwargs)
  File "C:\Users\User\Anaconda3\envs\DLDM\lib\site-packages\albumentations\core\transforms_interface.py", line 100, in apply_with_params
    res[key] = target_function(arg, **dict(params, **target_dependencies))
  File "C:\Users\User\Anaconda3\envs\DLDM\lib\site-packages\albumentations\augmentations\transforms.py", line 2110, in apply
    return F.shift_hsv(image, hue_shift, sat_shift, val_shift)
  File "C:\Users\User\Anaconda3\envs\DLDM\lib\site-packages\albumentations\augmentations\functional.py", line 387, in shift_hsv
    return _shift_hsv_non_uint8(img, hue_shift, sat_shift, val_shift)
  File "C:\Users\User\Anaconda3\envs\DLDM\lib\site-packages\albumentations\augmentations\functional.py", line 369, in _shift_hsv_non_uint8
    img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
cv2.error: OpenCV(4.1.2) c:\projects\opencv-python\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__cdecl cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0x3b52564f::Set<3,-1,-1>,struct cv::impl::A0x3b52564f::Set<0,5,-1>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 1

Maybe point is in float64? image

Dok11 commented 4 years ago

Very strange. After restart PyCharm I run this code again and all works fine, but all other runs is failed with traced as above. I try restart again and it works in every runs.

Dipet commented 4 years ago

ok, may fail. This transform is newer executed, need set p=1

By default not all transforms supports 1-channel images. Use RGB image, all transforms supports them.

Dok11 commented 4 years ago

need set p=1

p=1 for what? HueSaturationValue?

Use RGB image, all transforms supports them.

When all transforms will be support 1-channel images? Is it in the plans?

Dipet commented 4 years ago

It is need only for check this error.

def aug():
    return A.Compose([A.HueSaturationValue(p=1)], p=1)

Support of 1-channel images for all transforms is not a prior thing, so we can not tell when it will be added.

In addition use transforms like HueSaturationValue for 1-channel images is strange, because it has the meaning only for RGB images.

Dok11 commented 4 years ago

In addition use transforms like HueSaturationValue for 1-channel images is strange, because it has the meaning only for RGB images.

Hmm, yes, now I found transform for brightness, it's ok. Could you advice easy way (or lib, or func) to convert (64, 64, 1) to (64, 64, 3)?

Support of 1-channel images for all transforms is not a prior thing, so we can not tell when it will be added.

I got you. But 1-channel images use in NN very often in my cases. I choose it when think what human would can detect on b/w image, if it possible task then I use 1-channel. Maybe it's wrong way, but working for me.