otx.algo.utils.mmengine_utils#
This implementation replaces the functionality of mmengine utils.
Functions
Get distributed information of the given process group. |
|
|
Check whether it is a sequence of some type. |
|
Check whether it is a tuple of some type. |
|
Load state dict from path of checkpoint and dump to model. |
|
Loads a checkpoint dictionary into a PyTorch model. |
|
Loads a checkpoint from an HTTP URL. |
|
Load state_dict to a module. |
Classes
|
A base data interface that supports Tensor-like and dict-like operations. |
- class otx.algo.utils.mmengine_utils.InstanceData(*, metainfo: dict | None = None, **kwargs)[source]#
Bases:
object
A base data interface that supports Tensor-like and dict-like operations.
This class is from open-mmlab/mmengine and slightly modified.
- Parameters:
- all_items() Iterator[tuple[str, Any]] [source]#
Returns iterator object whose element is (key, value) tuple pairs for
metainfo
anddata
.
- clone() InstanceData [source]#
Deep copy the current data element.
- Returns:
The copy of current data element.
- Return type:
- cpu() InstanceData [source]#
Convert all tensors to CPU in data.
- cuda() InstanceData [source]#
Convert all tensors to GPU in data.
- detach() InstanceData [source]#
Detach all tensors in data.
- get(key: str, default: Any | None = None) Any [source]#
Get property in data and metainfo as the same as python.
- items() Iterator[tuple[str, Any]] [source]#
Returns iterator object whose element is (key, value) tuple pairs for
data
.
- metainfo_items() Iterator[tuple[str, Any]] [source]#
Returns iterator object whose element is (key, value) tuple pairs for
metainfo
.
- new(*, metainfo: dict | None = None, **kwargs) InstanceData [source]#
Return a new data element with same type.
If
metainfo
anddata
are None, the new data element will have same metainfo and data. If metainfo or data is not None, the new result will overwrite it with the input value.- Parameters:
- Returns:
A new data element with same type.
- Return type:
- numpy() InstanceData [source]#
Convert all tensors to np.ndarray in data.
- set_data(data: dict) None [source]#
Set or change key-value pairs in
data_field
by parameterdata
.- Parameters:
data (dict) – A dict contains annotations of image or model predictions.
- set_field(value: Any, name: str, dtype: type | tuple[type, ...] | None = None, field_type: str = 'data') None [source]#
Special method for set union field, used as property.setter functions.
- set_metainfo(metainfo: dict) None [source]#
Set or change key-value pairs in
metainfo_field
by parametermetainfo
.- Parameters:
metainfo (dict) – A dict contains the meta information of image, such as
img_shape
,scale_factor
, etc.
- to(*args, **kwargs) InstanceData [source]#
Apply same name function to all tensors in data_fields.
- to_tensor() InstanceData [source]#
Convert all np.ndarray to tensor in data.
- update(instance: InstanceData) None [source]#
The method updates the InstanceData with the elements from another InstanceData object.
- Parameters:
instance (InstanceData) – Another InstanceData object for update the current object.
- otx.algo.utils.mmengine_utils.get_dist_info() tuple[int, int] [source]#
Get distributed information of the given process group.
Note
Calling
get_dist_info
in non-distributed environment will return (0, 1).
- otx.algo.utils.mmengine_utils.is_seq_of(seq: Any, expected_type: type | tuple, seq_type: type | None = None) bool [source]#
Check whether it is a sequence of some type.
Copied from mmengine.utils.misc.is_seq_of
- Parameters:
- Returns:
Return True if
seq
is valid else False.- Return type:
Examples
>>> from mmengine.utils import is_seq_of >>> seq = ['a', 'b', 'c'] >>> is_seq_of(seq, str) True >>> is_seq_of(seq, int) False
- otx.algo.utils.mmengine_utils.is_tuple_of(seq: Any, expected_type: type | tuple) bool [source]#
Check whether it is a tuple of some type.
Copied from mmengine.utils.misc.is_tuple_of
A partial method of
is_seq_of()
.
- otx.algo.utils.mmengine_utils.load_checkpoint(model: Module, checkpoint: str, map_location: str = 'cpu', strict: bool = False, prefix: str = '') None [source]#
Load state dict from path of checkpoint and dump to model.
- otx.algo.utils.mmengine_utils.load_checkpoint_to_model(model: Module, checkpoint: dict, strict: bool = False, prefix: str = '') None [source]#
Loads a checkpoint dictionary into a PyTorch model.
Copy of mmengine.runner.checkpoint._load_checkpoint_to_model.
- Parameters:
- Returns:
None
- otx.algo.utils.mmengine_utils.load_from_http(filename: str, map_location: str | None = None, model_dir: Path | str | None = None, progress: bool = False) dict[str, Any] [source]#
Loads a checkpoint from an HTTP URL.
Copy of mmengine.runner.checkpoint.load_from_http.
- Parameters:
filename (str) – The URL of the checkpoint file.
map_location (str | None, optional) – Specifies where to load the checkpoint onto. Defaults to None.
model_dir (str | None, optional) – The directory to save the downloaded checkpoint. Defaults to None.
progress (bool, optional) – Whether to display a progress bar while downloading the checkpoint. Defaults to True if running in a terminal, otherwise False.
- Returns:
The loaded checkpoint.
- Return type:
- Raises:
None –
- otx.algo.utils.mmengine_utils.load_state_dict(module: Module, state_dict: OrderedDict, strict: bool = False) None [source]#
Load state_dict to a module.
This method is modified from
torch.nn.Module.load_state_dict()
. Default value forstrict
is set toFalse
and the message for param mismatch will be shown even if strict is False.- Parameters:
module (Module) – Module that receives the state_dict.
state_dict (OrderedDict) – Weights.
strict (bool) – whether to strictly enforce that the keys in
state_dict
match the keys returned by this module’sstate_dict()
function. Defaults to False.