otx.algo.utils.mmengine_utils#

This implementation replaces the functionality of mmengine utils.

Functions

get_dist_info()

Get distributed information of the given process group.

is_seq_of(seq, expected_type[, seq_type])

Check whether it is a sequence of some type.

is_tuple_of(seq, expected_type)

Check whether it is a tuple of some type.

load_checkpoint(model, checkpoint[, ...])

Load state dict from path of checkpoint and dump to model.

load_checkpoint_to_model(model, checkpoint)

Loads a checkpoint dictionary into a PyTorch model.

load_from_http(filename[, map_location, ...])

Loads a checkpoint from an HTTP URL.

load_state_dict(module, state_dict[, strict])

Load state_dict to a module.

Classes

InstanceData(*[, metainfo])

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:
  • metainfo (dict, optional) – A dict contains the meta information of single image, such as dict(img_shape=(512, 512, 3), scale_factor=(1, 1, 1, 1)). Defaults to None.

  • kwargs (dict, optional) – A dict contains annotations of single image or model predictions. Defaults to None.

all_items() Iterator[tuple[str, Any]][source]#

Returns iterator object whose element is (key, value) tuple pairs for metainfo and data.

all_keys() list[source]#

Returns list contains all keys in metainfo and data.

all_values() list[source]#

Returns list contains all values in metainfo and data.

clone() InstanceData[source]#

Deep copy the current data element.

Returns:

The copy of current data element.

Return type:

InstanceData

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.

keys() list[source]#

Returns lits contains all keys in data_fields.

metainfo_items() Iterator[tuple[str, Any]][source]#

Returns iterator object whose element is (key, value) tuple pairs for metainfo.

metainfo_keys() list[source]#

Returns list contains all keys in metainfo_fields.

metainfo_values() list[source]#

Returns list contains all values in metainfo.

new(*, metainfo: dict | None = None, **kwargs) InstanceData[source]#

Return a new data element with same type.

If metainfo and data 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:
  • metainfo (dict, optional) – A dict contains the meta information of image, such as img_shape, scale_factor, etc. Defaults to None.

  • kwargs (dict) – A dict contains annotations of image or model predictions.

Returns:

A new data element with same type.

Return type:

InstanceData

numpy() InstanceData[source]#

Convert all tensors to np.ndarray in data.

pop(*args) Any[source]#

Pop property in data and metainfo as the same as python.

set_data(data: dict) None[source]#

Set or change key-value pairs in data_field by parameter data.

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 parameter metainfo.

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_dict() dict[source]#

Convert InstanceData to dict.

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.

values() list[source]#

Returns list contains all values in data.

property metainfo: dict#

A dict contains metainfo of current data element.

Type:

dict

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

Returns:

Return a tuple containing the rank and world_size.

Return type:

tuple[int, int]

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:
  • seq (Sequence) – The sequence to be checked.

  • expected_type (type or tuple) – Expected type of sequence items.

  • seq_type (type, optional) – Expected sequence type. Defaults to None.

Returns:

Return True if seq is valid else False.

Return type:

bool

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:
  • model (nn.Module) – The PyTorch model to load the checkpoint into.

  • checkpoint (dict) – The checkpoint dictionary containing the model’s state_dict.

  • strict (bool, optional) – Whether to strictly enforce that the keys in the checkpoint match the keys in the model’s state_dict. Defaults to False.

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:

dict[str, Any]

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 for strict is set to False 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’s state_dict() function. Defaults to False.