pythonfb-hydraalbumentations

recursive Instantiating of objects- hydra


I'm trying to instantiate a composition of transformers.
In native Python this looks like this:

import albumentations as A
transforms = A.Compose([
   A.Rotate(limit=10), 
    A.ToGray(p=1),
])
print(transforms)

output:

Compose([
  Rotate(always_apply=False, p=0.5, limit=(-10, 10), interpolation=1, border_mode=4, value=None, mask_value=None),
  ToGray(always_apply=False, p=1),
], p=1.0, bbox_params=None, keypoint_params=None, additional_targets={})

I'm trying to instantiate the same transformation with Hydra, but for some reason, the recursive instantiation is failed and only the list of transforms is instantiated but not the outer (A.Compose) function.

from omegaconf import OmegaConf
import hydra
conf = OmegaConf.create({"compose": {"_traget_": "albumentations.Compose", "_recursive_": True, "transforms": [{"_target_": "albumentations.Rotate", "limit": 10}, {"_target_": "albumentations.ToGray", "p": 1}]}})
print(OmegaConf.to_yaml(conf))

output:

compose:
  _traget_: albumentations.Compose
  _recursive_: true
  transforms:
  - _target_: albumentations.Rotate
    limit: 10
  - _target_: albumentations.ToGray
    p: 1

But instantiation is failed:

print(hydra.utils.instantiate(conf.compose))

output:

{'_traget_': 'albumentations.Compose', 'transforms': [Rotate(always_apply=False, p=0.5, limit=(-10, 10), interpolation=1, border_mode=4, value=None, mask_value=None), ToGray(always_apply=False, p=1)]}

Solution

    1. This code does not run on Hydra 1.1 and results in the following error:
    Traceback (most recent call last):
      File "1.py", line 30, in <module>
        print(hydra.utils.instantiate(conf.compose))
      File "/home/omry/dev/hydra/hydra/utils.py", line 95, in instantiate
        target = _get_target_type(config, kwargs)
      File "/home/omry/dev/hydra/hydra/_internal/utils.py", line 640, in _get_target_type
        raise InstantiationException("Unable to determine target")
    

    The reason is that your first node does not have a _target_ field, instead it has a _traget_. Once you correct this, it works fine.