1

我正在尝试实例化变压器的组合。
在本机Python中,这看起来像这样:

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

输出:

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={})

我正在尝试使用Hydra实例化相同的转换,但由于某种原因,递归实例化失败,并且仅实例化了转换列表,但没有实例化外部 ( A.Compose) 函数。

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))

输出:

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

但实例化失败:

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

输出:

{'_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)]}
4

1 回答 1

3
  1. 此代码无法在 Hydra 1.1 上运行并导致以下错误:
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")

原因是您的第一个节点没有_target_字段,而是有一个_traget_. 一旦你纠正了这个,它就可以正常工作。

于 2021-08-12T15:48:26.617 回答