deepvisiontools 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. deepvisiontools/__init__.py +16 -0
  2. deepvisiontools/config/__init__.py +3 -0
  3. deepvisiontools/config/config.py +176 -0
  4. deepvisiontools/config/errors.py +12 -0
  5. deepvisiontools/data/__init__.py +24 -0
  6. deepvisiontools/data/additional_augmentations.py +201 -0
  7. deepvisiontools/data/augmentation_class.py +37 -0
  8. deepvisiontools/data/batch_augmentations.py +211 -0
  9. deepvisiontools/data/data_reader/__init__.py +3 -0
  10. deepvisiontools/data/data_reader/readers.py +490 -0
  11. deepvisiontools/data/dataset.py +357 -0
  12. deepvisiontools/data/errors.py +23 -0
  13. deepvisiontools/formats/__init__.py +46 -0
  14. deepvisiontools/formats/base_data.py +802 -0
  15. deepvisiontools/formats/base_formats.py +321 -0
  16. deepvisiontools/formats/errors.py +167 -0
  17. deepvisiontools/formats/formats.py +282 -0
  18. deepvisiontools/formats/utils.py +174 -0
  19. deepvisiontools/inference/__init__.py +4 -0
  20. deepvisiontools/inference/patchifier.py +343 -0
  21. deepvisiontools/inference/predictor.py +208 -0
  22. deepvisiontools/metrics/__init__.py +50 -0
  23. deepvisiontools/metrics/available_metrics.py +215 -0
  24. deepvisiontools/metrics/base_metric.py +436 -0
  25. deepvisiontools/metrics/func.py +34 -0
  26. deepvisiontools/metrics/matcher.py +99 -0
  27. deepvisiontools/metrics/utils.py +39 -0
  28. deepvisiontools/models/__init__.py +14 -0
  29. deepvisiontools/models/basemodel.py +100 -0
  30. deepvisiontools/models/mask2former/__init__.py +3 -0
  31. deepvisiontools/models/mask2former/mask2former.py +285 -0
  32. deepvisiontools/models/smp/__init__.py +0 -0
  33. deepvisiontools/models/smp/errors.py +4 -0
  34. deepvisiontools/models/smp/smp.py +149 -0
  35. deepvisiontools/models/yolov8/__init__.py +15 -0
  36. deepvisiontools/models/yolov8/errors.py +25 -0
  37. deepvisiontools/models/yolov8/utils.py +92 -0
  38. deepvisiontools/models/yolov8/yolov8.py +302 -0
  39. deepvisiontools/models/yolov8seg/__init__.py +4 -0
  40. deepvisiontools/models/yolov8seg/errors.py +14 -0
  41. deepvisiontools/models/yolov8seg/utils.py +135 -0
  42. deepvisiontools/models/yolov8seg/yolov8seg.py +370 -0
  43. deepvisiontools/preprocessing/__init__.py +19 -0
  44. deepvisiontools/preprocessing/image.py +79 -0
  45. deepvisiontools/preprocessing/preprocessing.py +65 -0
  46. deepvisiontools/train/__init__.py +3 -0
  47. deepvisiontools/train/trainer.py +335 -0
  48. deepvisiontools/utils/__init__.py +3 -0
  49. deepvisiontools/utils/errors.py +0 -0
  50. deepvisiontools/utils/visualization.py +318 -0
  51. deepvisiontools-0.0.1.dist-info/LICENSE.txt +661 -0
  52. deepvisiontools-0.0.1.dist-info/METADATA +725 -0
  53. deepvisiontools-0.0.1.dist-info/RECORD +55 -0
  54. deepvisiontools-0.0.1.dist-info/WHEEL +5 -0
  55. deepvisiontools-0.0.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,802 @@
1
+ from __future__ import annotations
2
+ from abc import ABC, abstractmethod
3
+ from torch import Tensor
4
+ from typing import Union, Literal, Tuple
5
+ from torchvision.tv_tensors import BoundingBoxes, Mask, BoundingBoxFormat
6
+ from torchvision.transforms.v2 import ConvertBoundingBoxFormat
7
+ import deepvisiontools.formats.errors as er
8
+ from deepvisiontools import Configuration
9
+ from torchvision.transforms.v2.functional import (
10
+ crop_bounding_boxes,
11
+ pad_bounding_boxes,
12
+ pad_mask,
13
+ crop_mask,
14
+ )
15
+ import torch
16
+ import copy
17
+ import deepvisiontools.formats.utils as ut
18
+ from torchvision.transforms.v2 import Transform
19
+
20
+
21
+ class BaseData(ABC):
22
+ """Abstract class for base data."""
23
+
24
+ # abstract class method
25
+
26
+ @classmethod
27
+ def empty(cls, canvas_size) -> BaseData:
28
+ pass
29
+
30
+ # abstract method
31
+
32
+ @abstractmethod
33
+ def crop(self, t: int, l: int, h: int, w: int) -> Tuple[BaseData, Tensor]:
34
+ pass
35
+
36
+ @abstractmethod
37
+ def pad(self, t: int, l: int, r: int, b: int) -> Tuple[BaseData, Tensor]:
38
+ pass
39
+
40
+ @abstractmethod
41
+ def __getitem__(
42
+ self, indexes: Union[int, Tensor, slice]
43
+ ) -> Tuple[BaseData, Tensor]:
44
+ pass
45
+
46
+ @abstractmethod
47
+ def sanitize(self) -> Tuple[BaseData, Tensor]:
48
+ pass
49
+
50
+ @abstractmethod
51
+ def __add__(self, obj2: BaseData) -> BaseData:
52
+ pass
53
+
54
+ @abstractmethod
55
+ def apply_augmentation(
56
+ self, image: Tensor, transform: Transform
57
+ ) -> Tuple[BaseData, Tensor, Tensor]:
58
+ """Need to be defined in concrete class : apply augmentation on it
59
+
60
+ Args:
61
+ image (``Tensor``): image to augment
62
+ transform (``Transform``) : torchvision transform v2 augmentation
63
+
64
+ Returns:
65
+ ``Tuple[BaseData, Tensor, Tensor]``:
66
+ - transformed BaseData, present tensor, transformed image
67
+ """
68
+ pass
69
+
70
+ # concrete property
71
+
72
+ @property
73
+ def value(self):
74
+ return self._value
75
+
76
+ @property
77
+ def device(self):
78
+ return self._device
79
+
80
+ @device.setter
81
+ def device(self, val):
82
+ assert val in ["cpu", "cuda"], f"device is not one of cuda nor cpu, got {val}"
83
+ self._device = val
84
+ self._value = self._value.to(val)
85
+
86
+ @property
87
+ def nb_object(self):
88
+ return self._nb_object
89
+
90
+ @nb_object.setter
91
+ def nb_object(self, v):
92
+ raise er.ProtectedAttributeException()
93
+
94
+ @property
95
+ def canvas_size(self):
96
+ return self._canvas_size
97
+
98
+ @canvas_size.setter
99
+ def canvas_size(self, v):
100
+ raise er.ProtectedAttributeException()
101
+
102
+ # abstract property
103
+
104
+ @value.setter
105
+ @abstractmethod
106
+ def value(self, val):
107
+ pass
108
+
109
+
110
+ class SemanticMaskData(BaseData):
111
+ """Semantic segmentation data class (Child class of BaseData)
112
+
113
+ Args:
114
+ mask (Union[Mask, Tensor]): Semantic mask with value in [0, ..., N_cls] range.
115
+
116
+ Attributes
117
+ ----------
118
+
119
+ Properties:
120
+ - device (``Literal["cpu", "cuda"]``)
121
+ - value (``Mask``): Tensor value of semantic mask
122
+ - nb_object (``int``): number of objects contained. In this case, nb of objects is the number of differents classes present in mask.
123
+ - canvas_size (Tuple[int, int]): dim of mask (h, w)
124
+
125
+
126
+ **Methods**:
127
+ """
128
+
129
+ @classmethod
130
+ def empty(cls, canvas_size):
131
+ """generate empty semantic mask data (full of 0) with given canvas_size"""
132
+ er.check_canvas_size(canvas_size)
133
+ mask = torch.zeros(canvas_size)
134
+ return SemanticMaskData(mask)
135
+
136
+ def __init__(self, mask: Union[Tensor, Mask]):
137
+ mask = mask.long() if isinstance(mask, Mask) else Mask(mask.long())
138
+ self._value = copy.deepcopy(mask) # convert to long
139
+ self._canvas_size = (mask.shape[-2], mask.shape[-1])
140
+ self._nb_object = torch.unique(mask).shape[0] - 1
141
+ self._device = Configuration().device
142
+ # we use here a trick : handling scores operation is complicated at format level.
143
+ # We dynamically will modify it in the operationhandler class and implement the modification here
144
+ self._scores = None
145
+
146
+ @property
147
+ def value(self) -> Tensor:
148
+ return self._value
149
+
150
+ @value.setter
151
+ def value(self, val: Union[Tensor, Mask]):
152
+ assert isinstance(
153
+ val, Tensor
154
+ ), f"SemanticMaskData value must be Tensor, got {type(val)}"
155
+ mask = val.long()
156
+ # covert to torchvision tv_tensor
157
+ mask = mask if isinstance(mask, Mask) else Mask(mask)
158
+ mask = mask.to(self.device)
159
+ self._canvas_size = mask.shape[-2:]
160
+ self._nb_object = torch.unique(mask).shape[0] - 1
161
+
162
+ def sanitize(self) -> Tuple[BaseData | Tensor]:
163
+ """Clean SemanticMaskData (simply checks if class have a number of pixel > Configuration().mask.min_size)
164
+
165
+ Returns:
166
+ Tuple[BaseData | Tensor]: cleaned data, present objects
167
+ """
168
+ new_scores = copy.deepcopy(self._scores)
169
+ m_min_size = Configuration().mask_min_size
170
+ new_val = copy.deepcopy(self.value)
171
+ present = []
172
+ for i, cls in enumerate(torch.unique(new_val)):
173
+ cls = cls.item()
174
+ if cls == 0:
175
+ continue
176
+ if torch.count_nonzero(new_val == cls) < m_min_size:
177
+ new_val[new_val == cls] = 0
178
+ if self._scores != None:
179
+ new_scores[:, new_val == cls] = 0
180
+ else:
181
+ # class is preserved and written in present tensor
182
+ present.append(i - 1)
183
+ present = torch.tensor(present).to(self.device)
184
+ new_val = SemanticMaskData(new_val)
185
+ if self._scores != None:
186
+ new_val._scores = new_scores
187
+ return new_val, present
188
+
189
+ def crop(self, t: int, l: int, h: int, w: int) -> Tuple[BaseData | Tensor]:
190
+ """Crop SemanticMaskData to desired coordinates.
191
+
192
+ Args:
193
+ t (int): top crop coord
194
+ l (int): left crop coord
195
+ h (int): height of crop
196
+ w (int): width of crop
197
+
198
+ Returns:
199
+ Tuple[SemanticMaskData, Tensor]: padded InstanceMaskData, indices of present objects
200
+ """
201
+ new_scores = copy.deepcopy(self._scores)
202
+ cropped_mask = copy.deepcopy(self.value)
203
+ new_mask_data = SemanticMaskData(crop_mask(cropped_mask, t, l, h, w))
204
+ if self._scores != None:
205
+ new_scores = crop_mask(new_scores, t, l, h, w)
206
+ new_mask_data._scores = new_scores
207
+ new_mask_data.device = self.device
208
+ new_mask_data, present = new_mask_data.sanitize()
209
+ return new_mask_data, present
210
+
211
+ def pad(self, t: int, l: int, r: int, b: int) -> Tuple[BaseData | Tensor]:
212
+ """Pad SemanticMaskData to desired coordinates. Note : the order t, l, r, b is different between deepvisiontools and torchvision.
213
+
214
+ Args:
215
+ t (int): top padding
216
+ l (int): left padding
217
+ r (int): right padding
218
+ b (int): bottom padding
219
+
220
+ Returns:
221
+ Tuple[SemanticMaskData, Tensor]: padded InstanceMaskData, indices of present objects
222
+ """
223
+ new_scores = copy.deepcopy(self._scores)
224
+ padded_mask = copy.deepcopy(self.value)
225
+ # Ordering top, left inversed in torchvision crop vs pad in torchvision
226
+ new_mask_data = SemanticMaskData(pad_mask(padded_mask, (l, t, r, b)))
227
+ if self._scores != None:
228
+ new_scores = pad_mask(new_scores, (l, t, r, b))
229
+ new_mask_data._scores = new_scores
230
+ new_mask_data.device = self.device
231
+ new_mask_data, present = new_mask_data.sanitize()
232
+ return new_mask_data, present
233
+
234
+ def apply_augmentation(
235
+ self, image: Tensor, transform: Transform, scores: Union[Tensor, None] = None
236
+ ) -> Tuple[BaseData | Tensor | Tensor]:
237
+ """Apply transform on self and associated image
238
+
239
+ Args:
240
+ image (``Tensor``)
241
+ transform (``Transform``)
242
+ scores (``Tensor`` | ``None``) : associated logits score if present
243
+
244
+ Returns:
245
+ - ``Tuple[InstanceMaskData, Tensor, Tensor]``:
246
+ - augmented data, present Tensor, image
247
+ or
248
+ - ``Tuple[InstanceMaskData, Tensor, Tensor]``:
249
+ - augmented data, present Tensor, image, augmented scores
250
+ """
251
+ if scores != None:
252
+ augmented_img, augmented_value, augmented_scores = transform(
253
+ image, self.value, scores
254
+ )
255
+ augmented_data, present = SemanticMaskData(augmented_value).sanitize()
256
+ return augmented_data, present, augmented_img, augmented_scores
257
+ else:
258
+ augmented_img, augmented_value = transform(image, self.value)
259
+ augmented_data, present = SemanticMaskData(augmented_value).sanitize()
260
+ return augmented_data, present, augmented_img
261
+
262
+ def __getitem__(self, indexes: int | Tensor | slice) -> Tuple[BaseData | Tensor]:
263
+ """Access objects in semantic mask. Objects are basically the different classes presents ordered in increasing order.
264
+
265
+ Args:
266
+ indexes (int | Tensor | slice)
267
+
268
+ Returns:
269
+ Tuple[BaseData | Tensor]: new data, present objects
270
+ """
271
+ if self._scores != None:
272
+ new_scores = torch.zeros(self._scores.shape)
273
+ if self.nb_object == 0:
274
+ return self, torch.tensor([])
275
+ indexes = torch.arange(self.nb_object).to(self.device)[indexes]
276
+ if indexes.nelement() == 0:
277
+ empty = SemanticMaskData.empty(self.canvas_size)
278
+ empty.device = self.device
279
+ return empty, torch.tensor([]).to(self.device)
280
+ new_val = torch.zeros(self.canvas_size).to(self.device)
281
+ for i, cls in torch.unique(new_val):
282
+ if i == 0:
283
+ break
284
+ else:
285
+ i = i - 1
286
+ if i in indexes:
287
+ new_val[self.value == cls] = cls
288
+ if self._scores != None:
289
+ new_scores[:, self.value == cls] = self._scores[
290
+ :, self.value == cls
291
+ ]
292
+ new_val = SemanticMaskData(new_val)
293
+ new_val, present = new_val.sanitize()
294
+ # return sanitized new item and adapted indexes (if object disappeared during sanitize)
295
+ return new_val, indexes[present]
296
+
297
+ def __add__(self, obj2: BaseData) -> BaseData:
298
+ """Raise error if called : combination of semantic mask does not make sense at data level. More useful when called at format level when scores are logits to combine model predictions."""
299
+ raise er.SemanticMaskOperationError()
300
+
301
+
302
+ class BboxData(BaseData):
303
+ """Bounding box data class (child of BaseData)
304
+
305
+ Args:
306
+ bbox (Union[BoundingBoxes, Tensor]): tensor value of bounding box. Shape must be [N, 4]
307
+ format (Literal[&quot;XYXY&quot;, &quot;XYWH&quot;, &quot;CXCYWH&quot;], optional): format of created BoundingBox. Defaults to "XYXY".
308
+ canvas_size (Tuple[int, int], optional): Size of associated image [h, w]. Defaults to None.
309
+
310
+ Attributes
311
+ ----------
312
+
313
+ Properties:
314
+ - device (``Literal[&quot;cpu&quot;, &quot;cuda&quot;]``)
315
+ - value (``BoundingBoxes``): Tensor value of bounding box
316
+ - format (Literal[&quot;XYXY&quot;, &quot;XYWH&quot;, &quot;CXCYWH&quot;]): if changed directly will automatically re-derive value
317
+ - nb_object (``int``): number of objects contained.
318
+ - canvas_size (Tuple[int, int])
319
+
320
+
321
+ **Methods**:
322
+ """
323
+
324
+ @classmethod
325
+ def from_mask(cls, mask: Union[InstanceMaskData, Tensor]) -> BboxData:
326
+ """Generate BboxData object from mask
327
+
328
+ Args:
329
+ mask (``Union[InstanceMaskData, Tensor]``)
330
+
331
+ Returns:
332
+ ``BboxData``
333
+
334
+ """
335
+ assert isinstance(mask, Tensor) or (
336
+ mask,
337
+ InstanceMaskData,
338
+ ), f"Invalid mask type, must be InstanceMaskData or Tensor got : {type(mask)}"
339
+ mask = InstanceMaskData(mask) if isinstance(mask, Tensor) else mask
340
+ # verify if object present in mask, if not -> empty BboxData
341
+ if mask.nb_object == 0:
342
+ return BboxData.empty(mask.canvas_size)
343
+ # create one hot for masks_to_boxes and ignore 0 encoding (background)
344
+ boxes = ut.mask2boxes(mask.value)
345
+ bbox = BboxData(boxes, format="XYXY", canvas_size=mask.canvas_size)
346
+ bbox.device = Configuration().device
347
+ return bbox
348
+
349
+ @classmethod
350
+ def empty(cls, canvas_size: Tuple[int, int]) -> BboxData:
351
+ """Return an empty BboxData with value = Tensor of shape [0, 4]
352
+
353
+ Args:
354
+ canvas_size (Tuple[int, int]): size of associated image.
355
+
356
+ Returns:
357
+ BboxData: empty BboxData
358
+ """
359
+ er.check_canvas_size(canvas_size)
360
+ bbox = BoundingBoxes(
361
+ torch.empty((0, 4)), canvas_size=canvas_size, format="XYXY"
362
+ )
363
+ return BboxData(bbox)
364
+
365
+ @classmethod
366
+ def _create_bbox(
367
+ cls,
368
+ bbox: Union[BoundingBoxes, Tensor],
369
+ format: Literal["XYXY", "XYWH", "CXCYWH"] = None,
370
+ canvas_size: Tuple[int, int] = None,
371
+ ) -> BoundingBoxes:
372
+ """Check sanity and create bbox object in __init__
373
+
374
+ Args:
375
+ bbox (``Union[BoundingBoxes, Tensor]``): torchvision BoundingBoxes object or Tensor. Shape must be [N, 4]
376
+ format (``Literal[&quot;XYXY&quot;, &quot;XYWH&quot;, &quot;CXCXWH&quot;]``, **optional**): Defaults to None.
377
+ canvas_size (``Tuple[int, int]``, **optional**): Defaults to None.
378
+
379
+ Returns:
380
+ ``BoundingBoxes``:
381
+ - A sane BoundingBoxes object
382
+ """
383
+
384
+ if not (
385
+ isinstance(bbox, BoundingBoxes) or (canvas_size != None and format != None)
386
+ ):
387
+ raise er.BboxFormatException(bbox, format, canvas_size)
388
+
389
+ er.check_bbox_dim(bbox)
390
+
391
+ if not (isinstance(bbox, BoundingBoxes)):
392
+ bbox = BoundingBoxes(bbox, format=format, canvas_size=canvas_size)
393
+ if bbox.format != format:
394
+ converter = ConvertBoundingBoxFormat(format)
395
+ bbox = converter(bbox)
396
+ return bbox
397
+
398
+ def __init__(
399
+ self,
400
+ bbox: Union[BoundingBoxes, Tensor],
401
+ format: Literal["XYXY", "XYWH", "CXCYWH"] = "XYXY",
402
+ canvas_size: Tuple[int, int] = None,
403
+ ):
404
+ bbox.to(torch.uint8)
405
+ bbox = BboxData._create_bbox(bbox, format, canvas_size)
406
+ self._format = bbox.format
407
+ self._canvas_size = bbox.canvas_size
408
+ self._value = bbox
409
+ self._nb_object = self.value.shape[0]
410
+ self.device = Configuration().device
411
+
412
+ def __getitem__(self, indexes: int | Tensor | slice) -> Tuple[BboxData, Tensor]:
413
+ # Handles empty self and empty Tensor indices (empty slice not handled):
414
+ # - indexes is empty -> return empty BboxData, self is empty return copy of self whatever indexes
415
+ if self.nb_object == 0:
416
+ return self, torch.tensor([])
417
+ indexes = torch.arange(self.nb_object).to(self.device)[indexes]
418
+ if indexes.nelement() == 0:
419
+ new_bbox = BboxData.empty(self.canvas_size)
420
+ new_bbox.format = self.format
421
+ else:
422
+ new_bbox = BoundingBoxes(
423
+ self.value[indexes],
424
+ canvas_size=self.canvas_size,
425
+ format=self.format,
426
+ )
427
+ new_bbox = BboxData(new_bbox)
428
+ new_bbox, present = new_bbox.sanitize()
429
+ present = present.to(self.device)
430
+ indexes = indexes.view(1) if indexes.dim() == 0 else indexes
431
+ present = present if present.nelement() == 0 else indexes[present]
432
+ return new_bbox, present
433
+
434
+ @BaseData.value.setter
435
+ def value(self, val: Union[Tensor, BoundingBoxes]):
436
+ """When changing bounding box value : if BoundingBoxes -> change all adequate params (format, canvas_size, nb_object etc.) otherwise replace Tensor value but preserve formats"""
437
+ er.check_bbox_dim(val)
438
+ bbox = val.to(self.device)
439
+ if isinstance(bbox, BoundingBoxes):
440
+ self._format = bbox.format
441
+ self._canvas_size = bbox.canvas_size
442
+ self._nb_object = bbox.shape[0]
443
+ self._value = bbox
444
+ elif isinstance(bbox, Tensor):
445
+ self._value = BoundingBoxes(
446
+ bbox, format=self._format, canvas_size=self._canvas_size
447
+ )
448
+ self._nb_object = bbox.shape[0]
449
+ else:
450
+ raise er.BboxFormatException(bbox, None, None)
451
+
452
+ @property
453
+ def format(self):
454
+ return self._format
455
+
456
+ @format.setter
457
+ def format(self, f: Union[str, BoundingBoxFormat]):
458
+ f = f if isinstance(f, BoundingBoxFormat) else BoundingBoxFormat(f)
459
+ self._format = f
460
+ convert = ConvertBoundingBoxFormat(f)
461
+ self.value = convert(self.value)
462
+
463
+ def crop(self, t: int, l: int, h: int, w: int) -> Tuple[BboxData, Tensor]:
464
+ """Crop the BboxData object and update values, canvas etc.
465
+ Note : forcing XYXY format to be compatible with torchvision func but restore format after.
466
+
467
+ Args:
468
+ t (``int``): top coordinate of crop
469
+ l (``int``): left coordinate of crop
470
+ h (``int``): height value of crop
471
+ w (``int``): width value of crop
472
+ """
473
+ er.check_crop_coords(self, t, l, h, w)
474
+
475
+ old_format = self.format
476
+ # change format to be compatible with torch vision
477
+ self.format = "XYXY"
478
+ new_bbox, new_canvas = crop_bounding_boxes(
479
+ self.value, self.format, top=t, left=l, height=h, width=w
480
+ )
481
+ # restore format
482
+ self.format = old_format
483
+ # create output BboxData
484
+ cropped_bbox = BoundingBoxes(new_bbox, format="XYXY", canvas_size=new_canvas)
485
+ cropped_bbox = BboxData(cropped_bbox)
486
+ cropped_bbox.device = self.device
487
+ cropped_bbox.format = self.format
488
+ cropped_bbox, present = cropped_bbox.sanitize()
489
+ return cropped_bbox, present
490
+
491
+ def pad(self, t: int, l: int, r: int, b: int) -> Tuple[BboxData, Tensor]:
492
+ """Pad the ``BboxData`` object and update values, canvas etc.
493
+ Note : forcing XYXY format to be compatible with torchvision func but restore format after.
494
+
495
+ Args:
496
+ t (``int``): top value of crop
497
+ l (``int``): left value of crop
498
+ r(``int``): right value of crop
499
+ b (``int``): bottom value of crop
500
+ """
501
+
502
+ old_format = self.format
503
+ self.format = "XYXY"
504
+ # Ordering top, left inversed in torchvision crop vs pad in torchvision
505
+ new_bbox, new_canvas = pad_bounding_boxes(
506
+ self.value, self.format, self.canvas_size, list((l, t, r, b))
507
+ )
508
+ # restore format
509
+ self.format = old_format
510
+ # create output BboxData
511
+ padded_bbox = BoundingBoxes(new_bbox, format="XYXY", canvas_size=new_canvas)
512
+ padded_bbox = BboxData(padded_bbox)
513
+ padded_bbox.device = self.device
514
+ padded_bbox.format = self.format
515
+ padded_bbox, present = padded_bbox.sanitize()
516
+ return padded_bbox, present
517
+
518
+ def sanitize(self) -> Tuple[BboxData | Tensor]:
519
+ """remove boxes with width or height = 0
520
+ returns as well a tensor of kept objects (useful for labels handling in BaseFormat)
521
+ """
522
+ boxes = copy.deepcopy(self)
523
+ boxes.format = "XYWH"
524
+ test_h_w_zero = boxes.value[:, 2:] == 0
525
+ to_keep = torch.tensor(
526
+ [torch.logical_not(torch.any(test)) for test in test_h_w_zero]
527
+ )
528
+ # infer from it indexes of removed objs
529
+ present = torch.arange(to_keep.shape[0]).to(self.device)
530
+ if present.nelement() != 0 and to_keep.nelement() != 0:
531
+ present = present[to_keep]
532
+ else:
533
+ present = torch.tensor([])
534
+ if present.nelement() != 0:
535
+ boxes.value = boxes.value[to_keep]
536
+ boxes.format = self.format
537
+ return boxes, present
538
+ else:
539
+ return BboxData.empty(boxes.canvas_size), present
540
+
541
+ def __add__(self, obj2: BboxData) -> BboxData:
542
+ assert isinstance(
543
+ obj2, BboxData
544
+ ), f"Can't add object of type {type(obj2)} to object BboxData."
545
+ assert (
546
+ self.canvas_size == obj2.canvas_size
547
+ ), "Bbox data do not have same canvas_size, can't add them."
548
+ if self.format != obj2.format:
549
+ self.format = "XYXY"
550
+ obj2.format = "XYXY"
551
+ new_boxes = torch.cat([self.value, obj2.value], dim=0)
552
+ return BboxData(new_boxes, self.format, self.canvas_size)
553
+
554
+ def apply_augmentation(
555
+ self, image: Tensor, transform: Transform
556
+ ) -> Tuple[BboxData, Tensor, Tensor]:
557
+ """Apply transform on self and associated image
558
+
559
+ Args:
560
+ image (``Tensor``)
561
+ transform (``Transform``)
562
+
563
+ Returns:
564
+ ``Tuple[BboxData, Tensor, Tensor]``:
565
+ - augmented data, present Tensor, image
566
+ """
567
+ old_format = self.format
568
+ self.format = "XYXY"
569
+ augmented_img, augmented_value = transform(image, self.value)
570
+ augmented_data, present = BboxData(augmented_value).sanitize()
571
+ self.format = old_format
572
+ return augmented_data, present, augmented_img
573
+
574
+
575
+ class InstanceMaskData(BaseData):
576
+ """Instance segmentation data class (Child class of BaseData)
577
+
578
+ Args:
579
+ mask (Union[Mask, Tensor]): Stacked mask (Tensor) of shape [H, W]. Each object is indexed in [1...N] range.
580
+
581
+ Attributes
582
+ ----------
583
+
584
+ Properties:
585
+ - device (``Literal[&quot;cpu&quot;, &quot;cuda&quot;]``)
586
+ - value (``BoundingBoxes``): Tensor value of stacked instance mask
587
+ - nb_object (``int``): number of objects contained.
588
+ - canvas_size (Tuple[int, int]): dim of mask (h, w)
589
+
590
+
591
+ **Methods**:
592
+ """
593
+
594
+ @classmethod
595
+ def empty(cls, canvas_size):
596
+ """generate empty instance mask (full of 0) with given canvas_size"""
597
+ er.check_canvas_size(canvas_size)
598
+ mask = torch.zeros(canvas_size)
599
+ return InstanceMaskData(mask)
600
+
601
+ @classmethod
602
+ def from_binary_masks(cls, mask: Tensor) -> InstanceMaskData:
603
+ """Generate InstanceMaskData from one_hot (binary) mask of shape [N, H, W] where N = number of objects. Note that background must not be included.
604
+
605
+ Args:
606
+ mask (Tensor): one hot mask of shape [N, H, W]
607
+
608
+ Returns:
609
+ InstanceMaskData: Stacked InstanceMaskData
610
+ """
611
+ assert (
612
+ mask.dim() == 3
613
+ ), f"Mask is not in (N_obj, h, w) format. Got shape {mask.shape}"
614
+ stacked = torch.zeros(mask.shape[-2:]).to(mask.device)
615
+ for i, m in enumerate(mask):
616
+ stacked, _ = torch.max(torch.stack([m * (i + 1), stacked]), dim=0)
617
+ return InstanceMaskData(stacked)
618
+
619
+ def __init__(
620
+ self,
621
+ mask: Union[Mask, Tensor],
622
+ ):
623
+ er.check_mask_dim(mask)
624
+ mask = mask.long()
625
+ self._value = mask if isinstance(mask, Mask) else Mask(mask)
626
+ self._canvas_size = (self._value.shape[0], self._value.shape[1])
627
+ self._nb_object = torch.max(self.value).item()
628
+ self.device = Configuration().device
629
+
630
+ @BaseData.value.setter
631
+ def value(self, val: Union[Tensor, Mask]):
632
+ er.check_mask_dim(val)
633
+ self._mask = val if isinstance(val, Mask) else Mask(val)
634
+ self._canvas_size = (val.shape[0], val.shape[1])
635
+ self._nb_object = torch.max(val)
636
+
637
+ def _reindex(self) -> Tuple[InstanceMaskData, Tensor]:
638
+ """If missing object in mask reidex accordingly
639
+
640
+ Returns:
641
+ ``Tuple[InstanceMaskData, Tensor]``:
642
+ - reindexed InstanceMaskData, id of objects which are present (CAREFUL : 0 is written as 1 in stacked mask, here we index per object not mask)
643
+ """
644
+ if self.nb_object == 0:
645
+ return self, torch.tensor([])
646
+ unique = torch.unique(self.value)
647
+ should_be = range(1, self.nb_object + 1)
648
+ # present will have values in [0, N_obj-1]
649
+ present = torch.tensor(
650
+ [i for i, val in enumerate(should_be) if (val in unique)]
651
+ )
652
+ new_mask = copy.deepcopy(self.value)
653
+ for i, u in enumerate(unique):
654
+ new_mask[self.value == u] = i
655
+ # reindex from mask object id to object id
656
+ return InstanceMaskData(new_mask), present.to(new_mask.device)
657
+
658
+ def _remove_small_objects(self) -> Tuple[InstanceMaskData, Tensor]:
659
+ """remove all small objects from mask. Handles splitted_mask_handling option
660
+
661
+ Returns:
662
+ ``Tuple[InstanceMaskData, Tensor]``:
663
+ - new InstanceMaskData without small objects, id of objects which were kept (CAREFUL : 0 is written as 1 in stacked mask, here we index per object not mask)
664
+ """
665
+ if Configuration().splitted_mask_handling:
666
+ return self.__remove_small_objs_with_splitting()
667
+ else:
668
+ return self.__remove_small_objs_no_splitting()
669
+
670
+ def __remove_small_objs_no_splitting(self) -> Tuple[InstanceMaskData, Tensor]:
671
+ """Handles cleaning of small objects in case of no mask splitting handling option
672
+
673
+ Returns:
674
+ Tuple[InstanceMaskData, Tensor]: cleaned_mask, indices of kept objects
675
+ """
676
+ msk_threshold = Configuration().mask_min_size
677
+ new_mask = copy.deepcopy(self.value)
678
+ for i in torch.unique(self.value):
679
+ # replace all values that occurence are smaller than mask threshold
680
+ new_mask[new_mask == i] = (
681
+ 0 if torch.count_nonzero(self.value == i) < msk_threshold else i
682
+ )
683
+ new_mask, present = InstanceMaskData(new_mask)._reindex()
684
+ return new_mask, present
685
+
686
+ def __remove_small_objs_with_splitting(self) -> Tuple[InstanceMaskData, Tensor]:
687
+ """Same as __remove_small_objs_no_splitting but includes a redefinition of objects that are discontinuous as new objects.
688
+ This leads to duplicated original object indices in the returned <present> Tensor.
689
+
690
+ Returns:
691
+ Tuple[InstanceMaskData, Tensor]: cleaned_mask, indices of kept objects
692
+ """
693
+ new_mask: Tensor = copy.deepcopy(self.value)
694
+ new_mask, present = ut.reindex_mask_with_splitted_objects(new_mask)
695
+ new_mask = InstanceMaskData(new_mask)
696
+ new_mask, keep = new_mask.__remove_small_objs_no_splitting()
697
+ if present.nelement() != 0 and keep.nelement() != 0:
698
+ present = present[keep]
699
+ # The minus 1 is for reidexing the objects from 0 to N-1 (while masks are from 1 to N_objs)
700
+ return new_mask, present - 1
701
+
702
+ def sanitize(self) -> Tuple[InstanceMaskData, Tensor]:
703
+ """reindex and remove all small objects from mask
704
+
705
+ Returns:
706
+ ``Tuple[InstanceMaskData, Tensor]``:
707
+ - new InstanceMaskData without small objects, id of objects which were removed because too small (CAREFUL : 0 is written as 1 in stacked mask, here we index per object not mask)
708
+ """
709
+ new_mask, _ = self._reindex()
710
+ new_mask, present = self._remove_small_objects()
711
+ return new_mask, present
712
+
713
+ def __getitem__(
714
+ self, indexes: int | Tensor | slice
715
+ ) -> Tuple[InstanceMaskData, Tensor]:
716
+ if self.nb_object == 0:
717
+ return self, torch.tensor([])
718
+ indexes = torch.arange(self.nb_object).to(self.device)[indexes]
719
+ if indexes.nelement() == 0:
720
+ empty = InstanceMaskData.empty(self.canvas_size)
721
+ empty.device = self.device
722
+ return empty, torch.tensor([]).to(self.device)
723
+ mask = torch.zeros(self.canvas_size).to(self.value.dtype).to(self.device)
724
+ new_objects = torch.arange(0, self.nb_object, dtype=torch.uint8).to(
725
+ self.device
726
+ )[indexes]
727
+ for i in new_objects.view(-1):
728
+ mask[self.value == i + 1] = (i + 1).to(self.value.dtype)
729
+ newmask, present = InstanceMaskData(mask).sanitize()
730
+ present = present.to(self.device)
731
+ # after sanitize, objects might be removed : reindex new_objects if are in present
732
+ present = new_objects.view(-1)[[n in present for n in new_objects.view(-1)]]
733
+ newmask.device = self.device
734
+ return newmask, present
735
+
736
+ def __add__(self, obj2: InstanceMaskData) -> InstanceMaskData:
737
+ """Return new mask from 2 InstanceMaskData object. NB : This loose overlapping mask information"""
738
+ assert isinstance(
739
+ obj2, InstanceMaskData
740
+ ), f"Can't add object of type {type(obj2)} to object InstanceMaskData."
741
+ assert (
742
+ self.canvas_size == obj2.canvas_size
743
+ ), "InstanceMaskData data do not have same canvas_size, can't add them."
744
+ reindex_new_mask = obj2.value
745
+ reindex_new_mask[reindex_new_mask != 0] += self.nb_object
746
+ # extract max of masks. NB : this lose overlapping mask information !!
747
+ new_mask, _ = torch.max(torch.stack([self.value, reindex_new_mask]), dim=0)
748
+ return InstanceMaskData(new_mask)
749
+
750
+ def pad(self, t: int, l: int, r: int, b: int) -> Tuple[InstanceMaskData, Tensor]:
751
+ """Pad InstanceMaskData to desired coordinates. Note : the order t, l, r, b is different between deepvisiontools and torchvision.
752
+
753
+ Args:
754
+ t (int): top padding
755
+ l (int): left padding
756
+ r (int): right padding
757
+ b (int): bottom padding
758
+
759
+ Returns:
760
+ Tuple[InstanceMaskData, Tensor]: padded InstanceMaskData, indices of present objects
761
+ """
762
+ padded_mask = copy.deepcopy(self.value)
763
+ # Ordering top, left inversed in torchvision crop vs pad in torchvision
764
+ new_mask_data = InstanceMaskData(pad_mask(padded_mask, (l, t, r, b)))
765
+ new_mask_data.device = self.device
766
+ new_mask_data, present = new_mask_data.sanitize()
767
+ return new_mask_data, present
768
+
769
+ def crop(self, t: int, l: int, h: int, w: int) -> Tuple[InstanceMaskData, Tensor]:
770
+ """Crop InstanceMaskData to desired coordinates.
771
+
772
+ Args:
773
+ t (int): top crop coord
774
+ l (int): left crop coord
775
+ h (int): height of crop
776
+ w (int): width of crop
777
+
778
+ Returns:
779
+ Tuple[InstanceMaskData, Tensor]: padded InstanceMaskData, indices of present objects
780
+ """
781
+ cropped_mask = copy.deepcopy(self.value)
782
+ new_mask_data = InstanceMaskData(crop_mask(cropped_mask, t, l, h, w))
783
+ new_mask_data.device = self.device
784
+ new_mask_data, present = new_mask_data.sanitize()
785
+ return new_mask_data, present
786
+
787
+ def apply_augmentation(
788
+ self, image: Tensor, transform: Transform
789
+ ) -> Tuple[InstanceMaskData, Tensor, Tensor]:
790
+ """Apply transform on self and associated image
791
+
792
+ Args:
793
+ image (``Tensor``)
794
+ transform (``Transform``)
795
+
796
+ Returns:
797
+ ``Tuple[InstanceMaskData, Tensor, Tensor]``:
798
+ - augmented data, present Tensor, image
799
+ """
800
+ augmented_img, augmented_value = transform(image, self.value)
801
+ augmented_data, present = InstanceMaskData(augmented_value).sanitize()
802
+ return augmented_data, present, augmented_img