open-mmlab/mmdetection · error · TypeError
type must be a str or valid type, but got {type(obj_type)}
Error message
type must be a str or valid type, but got {type(obj_type)} What it means
Albu.albu_builder requires each transform entry's 'type' to be either a string (albumentations transform name) or a class. Any other type (int, dict, list, None) raises TypeError.
Source
Thrown at mmdet/datasets/transforms/transforms.py:1665
Args:
cfg (dict): Config dict. It should at least contain the key "type".
Returns:
obj: The constructed object.
"""
assert isinstance(cfg, dict) and 'type' in cfg
args = cfg.copy()
obj_type = args.pop('type')
if is_str(obj_type):
if albumentations is None:
raise RuntimeError('albumentations is not installed')
obj_cls = getattr(albumentations, obj_type)
elif inspect.isclass(obj_type):
obj_cls = obj_type
else:
raise TypeError(
f'type must be a str or valid type, but got {type(obj_type)}')
if 'transforms' in args:
args['transforms'] = [
self.albu_builder(transform)
for transform in args['transforms']
]
return obj_cls(**args)
@staticmethod
def mapper(d: dict, keymap: dict) -> dict:
"""Dictionary mapper. Renames keys according to keymap provided.
Args:
d (dict): old dict
keymap (dict): {'old_key':'new_key'}
Returns:View on GitHub (pinned to cfd5d3a985)
Solutions
- Set 'type' to a valid albumentations transform name string, e.g. 'HorizontalFlip'
- Or pass the actual class object: dict(type=albumentations.HorizontalFlip)
- Check YAML quoting of the type value
Example fix
# before dict(type=Albu, transforms=[dict(type=dict(name='Blur'), p=0.1)]) # after dict(type='Albu', transforms=[dict(type='Blur', p=0.1)])
Defensive patterns
Strategy: type-guard
Validate before calling
for t in albu_cfg['transforms']:
assert isinstance(t['type'], str) or inspect.isclass(t['type']), f"bad type: {t['type']!r}" Type guard
import inspect
def valid_albu_type(v):
return isinstance(v, str) or inspect.isclass(v) Try / catch
try:
Albu(transforms=cfg)
except TypeError as e:
if 'type must be a str' in str(e):
fix_and_retry() Prevention
- Validate config schema before building
- Use string names exactly matching albumentations transforms
When it happens
Trigger: Writing dict(type=3), type=None, type=[...], or a nested dict in the 'type' field of an entry inside Albu transforms or keymap-driven builder input.
Common situations: Hand-editing Albu configs and putting a dict/list into 'type'; YAML quoting issues turning a name into another structure.
Related errors
- sampler should be an instance of ``Sampler``, but got {sampl
- The type of frame_range must be int or list.
- Scale must be a number or tuple of int, but got {type(scale)
- albumentations is not installed
- Albu only supports horizontal boxes now
AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27).
Data as JSON: /api/errors/fc3cdcbff5775345.
Report an issue: GitHub.