Arulkumar03/Wheat_HEAD_Detection_Counting_ComputerVision_Model
0
1# Copyright (c) Facebook, Inc. and its affiliates.2 3from typing import Any4import pydoc5from fvcore.common.registry import Registry # for backward compatibility.6 7"""8``Registry`` and `locate` provide ways to map a string (typically found9in config files) to callable objects.10"""11 12__all__ = ["Registry", "locate"]13 14 15def _convert_target_to_string(t: Any) -> str:16 """17 Inverse of ``locate()``.18 19 Args:20 t: any object with ``__module__`` and ``__qualname__``21 """22 module, qualname = t.__module__, t.__qualname__23 24 # Compress the path to this object, e.g. ``module.submodule._impl.class``25 # may become ``module.submodule.class``, if the later also resolves to the same26 # object. This simplifies the string, and also is less affected by moving the27 # class implementation.28 module_parts = module.split(".")29 for k in range(1, len(module_parts)):30 prefix = ".".join(module_parts[:k])31 candidate = f"{prefix}.{qualname}"32 try:33 if locate(candidate) is t:34 return candidate35 except ImportError:36 pass37 return f"{module}.{qualname}"38 39 40def locate(name: str) -> Any:41 """42 Locate and return an object ``x`` using an input string ``{x.__module__}.{x.__qualname__}``,43 such as "module.submodule.class_name".44 45 Raise Exception if it cannot be found.46 """47 obj = pydoc.locate(name)48 49 # Some cases (e.g. torch.optim.sgd.SGD) not handled correctly50 # by pydoc.locate. Try a private function from hydra.51 if obj is None:52 try:53 # from hydra.utils import get_method - will print many errors54 from hydra.utils import _locate55 except ImportError as e:56 raise ImportError(f"Cannot dynamically locate object {name}!") from e57 else:58 obj = _locate(name) # it raises if fails59 60 return obj61 