This class stores the segmentation masks for all objects in one image, in the form of bitmaps. Attributes: tensor: bool Tensor of N,H,W, representing N instances in the image.
| 199 | |
| 200 | |
| 201 | class BitMasks: |
| 202 | """ |
| 203 | This class stores the segmentation masks for all objects in one image, in |
| 204 | the form of bitmaps. |
| 205 | |
| 206 | Attributes: |
| 207 | tensor: bool Tensor of N,H,W, representing N instances in the image. |
| 208 | """ |
| 209 | |
| 210 | def __init__(self, tensor: Union[torch.Tensor, np.ndarray]): |
| 211 | """ |
| 212 | Args: |
| 213 | tensor: bool Tensor of N,H,W, representing N instances in the image. |
| 214 | """ |
| 215 | device = tensor.device if isinstance(tensor, torch.Tensor) else torch.device("cpu") |
| 216 | tensor = torch.as_tensor(tensor, dtype=torch.bool, device=device) |
| 217 | assert tensor.dim() == 3, tensor.size() |
| 218 | self.image_size = tensor.shape[1:] |
| 219 | self.tensor = tensor |
| 220 | |
| 221 | def to(self, *args: Any, **kwargs: Any) -> "BitMasks": |
| 222 | return BitMasks(self.tensor.to(*args, **kwargs)) |
| 223 | |
| 224 | @property |
| 225 | def device(self) -> torch.device: |
| 226 | return self.tensor.device |
| 227 | |
| 228 | def __getitem__(self, item: Union[int, slice, torch.BoolTensor]) -> "BitMasks": |
| 229 | """ |
| 230 | Returns: |
| 231 | BitMasks: Create a new :class:`BitMasks` by indexing. |
| 232 | |
| 233 | The following usage are allowed: |
| 234 | |
| 235 | 1. `new_masks = masks[3]`: return a `BitMasks` which contains only one mask. |
| 236 | 2. `new_masks = masks[2:10]`: return a slice of masks. |
| 237 | 3. `new_masks = masks[vector]`, where vector is a torch.BoolTensor |
| 238 | with `length = len(masks)`. Nonzero elements in the vector will be selected. |
| 239 | |
| 240 | Note that the returned object might share storage with this object, |
| 241 | subject to Pytorch's indexing semantics. |
| 242 | """ |
| 243 | if isinstance(item, int): |
| 244 | return BitMasks(self.tensor[item].unsqueeze(0)) |
| 245 | m = self.tensor[item] |
| 246 | assert m.dim() == 3, "Indexing on BitMasks with {} returns a tensor with shape {}!".format( |
| 247 | item, m.shape |
| 248 | ) |
| 249 | return BitMasks(m) |
| 250 | |
| 251 | def __iter__(self) -> torch.Tensor: |
| 252 | yield from self.tensor |
| 253 | |
| 254 | def __repr__(self) -> str: |
| 255 | s = self.__class__.__name__ + "(" |
| 256 | s += "num_instances={})".format(len(self.tensor)) |
| 257 | return s |
| 258 |
no outgoing calls
no test coverage detected