This class represents a list of instances in an image. It stores the attributes of instances (e.g., boxes, masks, labels, scores) as "fields". All fields must have the same ``__len__`` which is the number of instances. All other (non-field) attributes of this class are considered p
| 5 | |
| 6 | |
| 7 | class Instances: |
| 8 | """ |
| 9 | This class represents a list of instances in an image. |
| 10 | It stores the attributes of instances (e.g., boxes, masks, labels, scores) as "fields". |
| 11 | All fields must have the same ``__len__`` which is the number of instances. |
| 12 | |
| 13 | All other (non-field) attributes of this class are considered private: |
| 14 | they must start with '_' and are not modifiable by a user. |
| 15 | |
| 16 | Some basic usage: |
| 17 | |
| 18 | 1. Set/get/check a field: |
| 19 | |
| 20 | .. code-block:: python |
| 21 | |
| 22 | instances.gt_boxes = Boxes(...) |
| 23 | print(instances.pred_masks) # a tensor of shape (N, H, W) |
| 24 | print('gt_masks' in instances) |
| 25 | |
| 26 | 2. ``len(instances)`` returns the number of instances |
| 27 | 3. Indexing: ``instances[indices]`` will apply the indexing on all the fields |
| 28 | and returns a new :class:`Instances`. |
| 29 | Typically, ``indices`` is a integer vector of indices, |
| 30 | or a binary mask of length ``num_instances`` |
| 31 | |
| 32 | .. code-block:: python |
| 33 | |
| 34 | category_3_detections = instances[instances.pred_classes == 3] |
| 35 | confident_detections = instances[instances.scores > 0.9] |
| 36 | """ |
| 37 | |
| 38 | def __init__(self, image_size: Tuple[int, int], **kwargs: Any): |
| 39 | """ |
| 40 | Args: |
| 41 | image_size (height, width): the spatial size of the image. |
| 42 | kwargs: fields to add to this `Instances`. |
| 43 | """ |
| 44 | self._image_size = image_size |
| 45 | self._fields: Dict[str, Any] = {} |
| 46 | for k, v in kwargs.items(): |
| 47 | self.set(k, v) |
| 48 | |
| 49 | @property |
| 50 | def image_size(self) -> Tuple[int, int]: |
| 51 | """ |
| 52 | Returns: |
| 53 | tuple: height, width |
| 54 | """ |
| 55 | return self._image_size |
| 56 | |
| 57 | def __setattr__(self, name: str, val: Any) -> None: |
| 58 | if name.startswith("_"): |
| 59 | super().__setattr__(name, val) |
| 60 | else: |
| 61 | self.set(name, val) |
| 62 | |
| 63 | def __getattr__(self, name: str) -> Any: |
| 64 | if name == "_fields" or name not in self._fields: |
no outgoing calls