MCPcopy Create free account
hub / github.com/open-mmlab/mmengine / InstanceData

Class InstanceData

mmengine/structures/instance_data.py:34–311  ·  view source on GitHub ↗

Data structure for instance-level annotations or predictions. Subclass of :class:`BaseDataElement`. All value in `data_fields` should have the same length. This design refer to https://github.com/facebookresearch/detectron2/blob/master/detectron2/structures/instances.py # noqa E501

Source from the content-addressed store, hash-verified

32# Modified from
33# https://github.com/open-mmlab/mmdetection/blob/master/mmdet/core/data_structures/instance_data.py # noqa
34class InstanceData(BaseDataElement):
35 """Data structure for instance-level annotations or predictions.
36
37 Subclass of :class:`BaseDataElement`. All value in `data_fields`
38 should have the same length. This design refer to
39 https://github.com/facebookresearch/detectron2/blob/master/detectron2/structures/instances.py # noqa E501
40 InstanceData also support extra functions: ``index``, ``slice`` and ``cat`` for data field. The type of value
41 in data field can be base data structure such as `torch.Tensor`, `numpy.ndarray`, `list`, `str`, `tuple`,
42 and can be customized data structure that has ``__len__``, ``__getitem__`` and ``cat`` attributes.
43
44 Examples:
45 >>> # custom data structure
46 >>> class TmpObject:
47 ... def __init__(self, tmp) -> None:
48 ... assert isinstance(tmp, list)
49 ... self.tmp = tmp
50 ... def __len__(self):
51 ... return len(self.tmp)
52 ... def __getitem__(self, item):
53 ... if isinstance(item, int):
54 ... if item >= len(self) or item < -len(self): # type:ignore
55 ... raise IndexError(f'Index {item} out of range!')
56 ... else:
57 ... # keep the dimension
58 ... item = slice(item, None, len(self))
59 ... return TmpObject(self.tmp[item])
60 ... @staticmethod
61 ... def cat(tmp_objs):
62 ... assert all(isinstance(results, TmpObject) for results in tmp_objs)
63 ... if len(tmp_objs) == 1:
64 ... return tmp_objs[0]
65 ... tmp_list = [tmp_obj.tmp for tmp_obj in tmp_objs]
66 ... tmp_list = list(itertools.chain(*tmp_list))
67 ... new_data = TmpObject(tmp_list)
68 ... return new_data
69 ... def __repr__(self):
70 ... return str(self.tmp)
71 >>> from mmengine.structures import InstanceData
72 >>> import numpy as np
73 >>> import torch
74 >>> img_meta = dict(img_shape=(800, 1196, 3), pad_shape=(800, 1216, 3))
75 >>> instance_data = InstanceData(metainfo=img_meta)
76 >>> 'img_shape' in instance_data
77 True
78 >>> instance_data.det_labels = torch.LongTensor([2, 3])
79 >>> instance_data["det_scores"] = torch.Tensor([0.8, 0.7])
80 >>> instance_data.bboxes = torch.rand((2, 4))
81 >>> instance_data.polygons = TmpObject([[1, 2, 3, 4], [5, 6, 7, 8]])
82 >>> len(instance_data)
83 2
84 >>> print(instance_data)
85 <InstanceData(
86 META INFORMATION
87 img_shape: (800, 1196, 3)
88 pad_shape: (800, 1216, 3)
89 DATA FIELDS
90 det_labels: tensor([2, 3])
91 det_scores: tensor([0.8000, 0.7000])

Callers 4

test_forwardMethod · 0.90
setup_dataMethod · 0.90
test_getitemMethod · 0.90
test_lenMethod · 0.90

Calls

no outgoing calls

Tested by 4

test_forwardMethod · 0.72
setup_dataMethod · 0.72
test_getitemMethod · 0.72
test_lenMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…