Cannot instantiate augmentations with tuple parameters from Hydra config
See original GitHub issue🐛 Bug
I’d like to use Hydra to configure and instantiate augmentations. It works nicely, unless I try to use a tuple as a parameter value. Hydra translates lists and tuples to an internal type that can be casted to a tuple, but Albumentations checks the parameter types and won’t try to cast it if it’s not a list.
To Reproduce
It’s convenient to define augmentations in a Hydra configuration like this:
augmentations:
- _target_: albumentations.HorizontalFlip
p: 0.5
- _target_: albumentations.HueSaturationValue
sat_shift_limit:
- -10
- 20
One can then instantiate the augmentations like this:
from omegaconf import DictConfig
import hydra
from hydra.utils import instantiate
@hydra.main(config_path='conf', config_name='config')
def my_app(cfg: DictConfig) -> None:
augmentations = [instantiate(a) for a in cfg.augmentations]
if __name__ == "__main__":
my_app()
If you run the above code, it will correctly instantiate the HorizontalFlip
augmentation, but fails to instantiate the HueSaturationValue
. The reason is that instantiate()
translates the sat_shift_limit
value [-10, 20] to an omegaconf.ListConfig
. It would be possible to cast omegaconf.ListConfig
to a tuple, but to_tuple() checks that the type is either a list or a tuple before casting. It’s not, so instead of casting it throws an exception:
ValueError: Argument param must be either scalar (int, float) or tuple
Expected behavior
to_tuple()
should cast the omegaconf.ListConfig
to a tuple, so that instantiate()
would instantiate the HueSaturationValue
transform.
Environment
- Albumentations version (e.g., 0.1.8): 0.5.2
- Python version (e.g., 3.7): 3.8.3
- OS (e.g., Linux): Linux
- How you installed albumentations (
conda
,pip
, source): conda
Additional context
I understand that it’s impossible to check for omegaconf.ListConfig
and all other possible cases that can be casted to a tuple. Actually it’s common to use “duck typing” in Python, meaning that anything that acts like a list can be provided in place of a list, so it’s problematic to check the exact type of parameters.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
It seems hydra (Tested on 1.1)
instantiate
has additional parameter_convert_
:So adding
_convert_
should do the job:It looks like we can replace check
isinstance(param, (list, tuple))
withisinstance(param, Sequence)
and everything will work fine.Sequence
- is a standard type from typing module