LazyObject is used to lazily initialize the imported module during parsing the configuration file. During parsing process, the syntax like: Examples: >>> import torch.nn as nn >>> from mmdet.models import RetinaNet >>> import mmcls.models >>> import mmcl
| 441 | return node |
| 442 | |
| 443 | class LazyObject: |
| 444 | """LazyObject is used to lazily initialize the imported module during |
| 445 | parsing the configuration file. |
| 446 | |
| 447 | During parsing process, the syntax like: |
| 448 | |
| 449 | Examples: |
| 450 | >>> import torch.nn as nn |
| 451 | >>> from mmdet.models import RetinaNet |
| 452 | >>> import mmcls.models |
| 453 | >>> import mmcls.datasets |
| 454 | >>> import mmcls |
| 455 | |
| 456 | Will be parsed as: |
| 457 | |
| 458 | Examples: |
| 459 | >>> # import torch.nn as nn |
| 460 | >>> nn = lazyObject('torch.nn') |
| 461 | >>> # from mmdet.models import RetinaNet |
| 462 | >>> RetinaNet = lazyObject('mmdet.models', 'RetinaNet') |
| 463 | >>> # import mmcls.models; import mmcls.datasets; import mmcls |
| 464 | >>> mmcls = lazyObject(['mmcls', 'mmcls.datasets', 'mmcls.models']) |
| 465 | |
| 466 | ``LazyObject`` records all module information and will be further |
| 467 | referenced by the configuration file. |
| 468 | |
| 469 | Args: |
| 470 | module (str or list or tuple): The module name to be imported. |
| 471 | imported (str, optional): The imported module name. Defaults to None. |
| 472 | location (str, optional): The filename and line number of the imported |
| 473 | module statement happened. |
| 474 | """ |
| 475 | |
| 476 | def __init__(self, |
| 477 | module: Union[str, list, tuple], |
| 478 | imported: Optional[str] = None, |
| 479 | location: Optional[str] = None): |
| 480 | if not isinstance(module, str) and not is_seq_of(module, str): |
| 481 | raise TypeError('module should be `str`, `list`, or `tuple`' |
| 482 | f'but got {type(module)}, this might be ' |
| 483 | 'a bug of MMEngine, please report it to ' |
| 484 | 'https://github.com/open-mmlab/mmengine/issues') |
| 485 | self._module: Union[str, list, tuple] = module |
| 486 | |
| 487 | if not isinstance(imported, str) and imported is not None: |
| 488 | raise TypeError('imported should be `str` or None, but got ' |
| 489 | f'{type(imported)}, this might be ' |
| 490 | 'a bug of MMEngine, please report it to ' |
| 491 | 'https://github.com/open-mmlab/mmengine/issues') |
| 492 | self._imported = imported |
| 493 | self.location = location |
| 494 | |
| 495 | def build(self) -> Any: |
| 496 | """Return imported object. |
| 497 | |
| 498 | Returns: |
| 499 | Any: Imported object |
| 500 | """ |