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,357 @@
1
+ from __future__ import annotations
2
+ from torch.utils.data import Dataset, DataLoader
3
+ from typing import Union, Tuple, List, Dict, Callable, Sequence, Literal
4
+ from pathlib import Path
5
+ from deepvisiontools.data.data_reader.readers import (
6
+ BaseReader,
7
+ CocoReader,
8
+ ANNOTATION_TYPE_DICT,
9
+ SemanticReader,
10
+ )
11
+ from deepvisiontools.preprocessing.preprocessing import build_preprocessing
12
+ from deepvisiontools.preprocessing.image import (
13
+ load_image,
14
+ save_image,
15
+ save_mask,
16
+ load_mask,
17
+ )
18
+ from deepvisiontools import Configuration
19
+ from deepvisiontools.formats import (
20
+ BaseFormat,
21
+ BatchedFormat,
22
+ SemanticMaskFormat,
23
+ SemanticMaskData,
24
+ )
25
+ from deepvisiontools.utils import visualization
26
+ from torch import Tensor
27
+ from torchvision.transforms.v2 import Transform
28
+ from deepvisiontools.data.augmentation_class import Augmentation
29
+ import torchvision.transforms.v2 as T
30
+ import torch
31
+ import copy
32
+ from random import shuffle
33
+ import deepvisiontools.data.errors as er
34
+ from tqdm import tqdm
35
+ import json
36
+
37
+
38
+ class DeepVisionDataset(Dataset):
39
+ """Detection dataset class for deepvisiontools : load and return image, annotation, image name.
40
+
41
+ Args:
42
+ dataset_path (Union[str, Path]): path to dataset folder.
43
+ reader (BaseReader, optional): Class to read data from dataset folder. Defaults to CocoReader.
44
+ preprocessing (Callable, optional): Preprocessing images (normalization). Defaults to build_preprocessing().
45
+ augmentation (List[Transform], optional): Augmentation to apply to images / annotations. Must be from torchvision.transforms.v2.Transform Defaults to None.
46
+ label_converter (Dict[int, int], optional): Convert labels to another value. For e.g : {0: 2, 1: 5} etc. Defaults to None.
47
+
48
+ Example:
49
+ ----------
50
+
51
+ .. highlight:: python
52
+ .. code-block:: python
53
+
54
+ >>> from deepvisiontools import DeepVisionDataset
55
+ >>> data_path = \"path/to/data\"
56
+ >>> dataset = DeepVisionDataset(data_path)
57
+ >>> image, target, image_name = dataset[1]
58
+ >>> print(type(image), type(target), type(image_name))
59
+ <class 'torch.Tensor' >, <class 'BboxFormat' >, <class 'str'>
60
+ >>> print(image.shape, target.size, image_name)
61
+ torch.Size([3,512,512]), 5, 'img_01.png'
62
+
63
+
64
+ Attributes
65
+ ----------
66
+
67
+ Attributes:
68
+ - dataset_path (``Path``): path to dataset folder.
69
+ - reader (``BaseReader``): Class to read data from dataset folder. Defaults to CocoReader.
70
+ - preprocessing (``Callable``): Preprocessing images (normalization). Defaults to build_preprocessing().
71
+ - augmentation (``List[Transform]``): Augmentation to apply to images / annotations. Must be from torchvision.transforms.v2.Transform Defaults to None.
72
+ - category_ids (``Dict[int, str]``): Dict that associate a name to a category label index. Defaults is equal to self.reader.category_ids
73
+ - label_converter (``Dict[int, int]``): Convert labels to another value. For e.g : {0: 2, 1: 5} etc. Defaults to None.
74
+
75
+
76
+ **Methods**:
77
+ """
78
+
79
+ def __init__(
80
+ self,
81
+ dataset_path: Union[str, Path],
82
+ reader: Union[BaseReader, None] = None,
83
+ preprocessing: Callable = build_preprocessing(),
84
+ augmentation: List[Transform] = None,
85
+ label_converter: Dict[int, int] = None,
86
+ category_ids: Union[Dict[int, str], None] = None,
87
+ ):
88
+
89
+ self.dataset_path = (
90
+ dataset_path if isinstance(dataset_path, Path) else Path(dataset_path)
91
+ )
92
+ assert (
93
+ self.dataset_path.exists()
94
+ ), f"{self.dataset_path} is not a valid dataset folder. Please check that folder exists."
95
+ # Choose default reader between CocoReader and SemanticReader if None is provided for reader
96
+ if reader != None:
97
+ self.reader: BaseReader = reader(dataset_path)
98
+ elif Configuration().data_type == "semantic_mask":
99
+ self.reader = SemanticReader(dataset_path)
100
+ else:
101
+ self.reader = CocoReader(dataset_path)
102
+ self.preprocessing = preprocessing
103
+ self.augmentation = augmentation
104
+ self._category_ids = category_ids if category_ids else self.reader.category_ids
105
+ self.label_converter = label_converter
106
+ self._img_dir = self.dataset_path / "images"
107
+ self._device = Configuration().device
108
+ # The following is to handle splitting
109
+ self._indexes = list(range(len(self.reader)))
110
+
111
+ @property
112
+ def category_ids(self):
113
+ return self._category_ids
114
+
115
+ @category_ids.setter
116
+ def category_ids(self, val):
117
+ self.reader.category_ids = val
118
+ self._category_ids = val
119
+
120
+ def __getitem__(self, idx: int):
121
+ elem = self.reader[self._indexes[idx]]
122
+ img_name: str = elem[0]
123
+ target: BaseFormat = elem[1]
124
+ # Rename / regroup categories if wanted
125
+ if self.label_converter != None:
126
+ if isinstance(target, SemanticMaskFormat):
127
+ # change mask labels as per convert dict
128
+ new_data = copy.deepcopy(target.data.value)
129
+ for k, v in self.label_converter:
130
+ new_data[target.data.value == k] = v
131
+ target = SemanticMaskData(new_data)
132
+ target = SemanticMaskFormat(target)
133
+ else:
134
+ new_labels = [self.label_converter[l] for l in target.labels]
135
+ target = type(target)(target.data, new_labels)
136
+
137
+ image = load_image(self._img_dir / img_name)
138
+ image = image.to(self._device)
139
+ target.device = self._device
140
+
141
+ if self.augmentation != None:
142
+ augment = Augmentation(self.augmentation)
143
+ image, target = augment(image, target)
144
+
145
+ if self.preprocessing != None:
146
+ image = self.preprocessing(image)
147
+
148
+ target, _ = target.sanitize()
149
+ return image, target, img_name
150
+
151
+ def __len__(self) -> int:
152
+ return len(self._indexes)
153
+
154
+ def __iter__(self):
155
+ for x in range(len(self)):
156
+ yield self[x]
157
+
158
+ def split(
159
+ self, sequence: Sequence[float, float, float]
160
+ ) -> Tuple[DeepVisionDataset, DeepVisionDataset, DeepVisionDataset]:
161
+ """split dataset in 3 new datasets according to proportions
162
+
163
+ Args:
164
+ sequence (Sequence[float, float, float]): proportions to split the dataset into. Sum must be 1.
165
+
166
+ Example:
167
+ ----------
168
+ .. highlight:: python
169
+ .. code-block:: python
170
+
171
+ >>> dataset = DeepVisionDataset("path/to/dataset")
172
+ >>> train_dataset, valid_dataset, test_dataset = dataset.split((0.6, 0.2, 0.2))
173
+ """
174
+ seq_sum = sum(sequence)
175
+ assert round(seq_sum, 3) == 1, "sequence sum is not equal to 1."
176
+ idx = copy.copy(self._indexes)
177
+ shuffle(idx)
178
+ stop1 = int(sequence[0] * len(idx))
179
+ stop2 = int(sum(sequence[0:2]) * len(idx))
180
+ # stop3 = int(seq_sum * len(idx))
181
+ if stop2 > len(idx):
182
+ stop2 -= len(idx) - stop2
183
+ dataset1 = copy.deepcopy(self)
184
+ dataset1._indexes = idx[0:stop1]
185
+ dataset2 = copy.deepcopy(self)
186
+ dataset2._indexes = idx[stop1:stop2]
187
+ dataset3 = copy.deepcopy(self)
188
+ if len(idx[stop2:]) == 0:
189
+ dataset3 = None
190
+ else:
191
+ dataset3._indexes = idx[stop2:]
192
+ return dataset1, dataset2, dataset3
193
+
194
+ def keep_indexes(self, indexes: Union[list, slice, Tensor]):
195
+ """Filter dataset by keeping only indices given in arg.
196
+
197
+ Args:
198
+ indexes (``Union[list, slice, Tensor]``): can be slice, Tensor or list. To use slice please use : slice(i, j) with i, j desired slice indexes in arg.
199
+ """
200
+ if isinstance(indexes, Tensor):
201
+ assert (
202
+ indexes.dim() == 1
203
+ ), f"Must use Tensor of dim 1 for indexes, got {indexes.shape}"
204
+ indexes = indexes.tolist()
205
+ indx = torch.tensor(self._indexes)[indexes].tolist()
206
+ self._indexes = indx
207
+
208
+ def export_dataset(
209
+ self,
210
+ destination_folder: Union[str, Path],
211
+ number_visu: Union[Literal["all"], int] = "all",
212
+ file_extension: str = "",
213
+ ):
214
+ """Export dataset accordingly to BaseReader class. For example CocoReader will export in following structure:
215
+ Dataset Name -> Image_dir, coco_annotations.json
216
+
217
+ Args:
218
+ destination_folder (Union[str, Path]): Path to new dataset folder.
219
+ number_visu (Union[Literal[&quot;all&quot;], int], optional): number of visualization to create. If "all" will derive all of them. Defaults to "all".
220
+ file_extension (str, optional): if requires a specific file extension. If "" will use BaseReader's. Defaults to "".
221
+ """
222
+ if file_extension == "":
223
+ file_extension = self.reader.annotation_file_type
224
+ destination_folder = (
225
+ destination_folder
226
+ if isinstance(destination_folder, Path)
227
+ else Path(destination_folder)
228
+ )
229
+ number_visu = len(self) if number_visu == "all" else number_visu
230
+ img_folder = destination_folder / "images"
231
+ annot_folder = destination_folder / "annotations"
232
+ visu_folder = destination_folder / "visualizations"
233
+ if number_visu > 0:
234
+ visu_folder.mkdir(parents=True, exist_ok=True)
235
+ img_folder.mkdir(parents=True, exist_ok=True)
236
+ annot_folder.mkdir(parents=True, exist_ok=True)
237
+ for img, target, img_name in tqdm(
238
+ self, total=len(self), desc="Exporting dataset : "
239
+ ):
240
+ visu_saved = 0
241
+ if visu_saved < number_visu:
242
+ visu_path = visu_folder / f"visu__{img_name}"
243
+ visualization(img, target, self.category_ids, save_path=visu_path)
244
+ save_image(img, img_folder / img_name)
245
+ _, export_target = self.reader.export_annotation(
246
+ img_name, img, target, self.category_ids
247
+ )
248
+ annot_file_path = annot_folder / f"{Path(img_name).stem}.{file_extension}"
249
+ if ANNOTATION_TYPE_DICT[file_extension] == "file":
250
+ with open(annot_file_path, "w") as f:
251
+ if isinstance(export_target, dict):
252
+ json.dump(export_target, f)
253
+ else:
254
+ f.write(export_target)
255
+ elif ANNOTATION_TYPE_DICT[file_extension] == "mask":
256
+ save_mask(export_target, annot_file_path)
257
+ self.reader.group_export(
258
+ annot_folder,
259
+ destination_folder,
260
+ )
261
+
262
+
263
+ # TODO : add collate augmentations functions : mosaic
264
+
265
+
266
+ class DeepVisionLoader(DataLoader):
267
+ """Child class of ``DataLoader`` that batchify images and BaseFormats. DetectionLoader support any features from torch Dataloaders (Sampler, etc..).
268
+
269
+ Args:
270
+ *args
271
+ *kwargs
272
+
273
+ Example:
274
+ ----------
275
+ .. highlight:: python
276
+ .. code-block:: python
277
+
278
+ >>> from deepvisiontools import DeepVisionLoader
279
+ >>> loader = DeepVisionLoader(dataset, batch_size=2)
280
+ >>> for batch in loader:
281
+ >>> img, target, img_name = batch
282
+
283
+
284
+ **Methods**:
285
+ """
286
+
287
+ def __init__(self, *args, **kwargs):
288
+ self.batch_augmenter = kwargs.pop("batch_augmenter", None)
289
+ super().__init__(collate_fn=self.collate_fn, *args, **kwargs)
290
+
291
+ def collate_fn(
292
+ self, batch: List[Tuple[str, Tensor, BaseFormat]]
293
+ ) -> Tuple[Tensor, BaseFormat]:
294
+ """
295
+ Args:
296
+ batch (``List[Tuple[Tensor, BaseFormat]]``): List of pairs image/target.
297
+
298
+ Returns:
299
+ ``Tuple[Tensor, BatchedFormats]``:
300
+ - Batch images (N, 3, H, W).
301
+ - BaseFormats wrapped into BatchedFormats class.
302
+ """
303
+ images = [triplet[0] for triplet in batch]
304
+ targets = [triplet[1] for triplet in batch]
305
+ names = {i: triplet[2] for i, triplet in enumerate(batch)}
306
+ images, targets = self.pad_to_larger(images, targets)
307
+ er.check_images_targets_size(images, targets)
308
+ batch_images = torch.stack(images).to(Configuration().device)
309
+ batch_targets = BatchedFormat(targets)
310
+
311
+ # apply batch_augmenter if not None
312
+ if self.batch_augmenter != None:
313
+ batch_images, batch_targets = self.batch_augmenter.get_new_batch(
314
+ batch_images, batch_targets
315
+ )
316
+
317
+ return batch_images, batch_targets, names
318
+
319
+ def pad_to_larger(
320
+ self, images: List[Tensor], targets: List[BaseFormat]
321
+ ) -> Tuple[List[Tensor], List[BaseFormat]]:
322
+ """Pad images and targets to larger image size.
323
+
324
+ Args:
325
+ images (``List[Tensor]``): Images.
326
+ targets (``List[BaseFormat]``): Targets.
327
+ """
328
+ # get max borders sizes
329
+ larger_width = max([image.shape[-1] for image in images])
330
+ larger_height = max([image.shape[-2] for image in images])
331
+ padded_images, padded_targets = [], []
332
+
333
+ # for each image pad image & target
334
+ for i, image in enumerate(images):
335
+ t = int((larger_height - image.shape[-2]) / 2)
336
+ l = int((larger_width - image.shape[-1]) / 2)
337
+ r = int((larger_width - image.shape[-1]) - l)
338
+ b = int((larger_height - image.shape[-2]) - t)
339
+ # Order of t, l, b, r changes again in torchvision
340
+ padder = T.Pad((l, t, r, b))
341
+ padded_images.append(padder(image))
342
+ padded_targets.append(targets[i].pad_to((larger_height, larger_width))[0])
343
+
344
+ return padded_images, padded_targets
345
+
346
+ def visualize(self, dir_path: Union[str, Path]):
347
+ """Generate visualization through DeepVisionLoader. Can be useful to test batch_augmenter effect."""
348
+ dir_path = dir_path if isinstance(dir_path, Path) else Path(dir_path)
349
+ dir_path.mkdir(parents=True, exist_ok=True)
350
+ for i, item in tqdm(enumerate(self), total=len(self)):
351
+ imgs, targs, _ = item
352
+ for j in range(imgs.shape[0]):
353
+ visualization(
354
+ imgs[j].to(torch.uint8),
355
+ targs.formats[j],
356
+ save_path=dir_path / f"Batch_{i}__img_{j}.png",
357
+ )
@@ -0,0 +1,23 @@
1
+ from typing import List
2
+ from torch import Tensor
3
+ from deepvisiontools.formats import BaseFormat
4
+
5
+
6
+ def check_images_targets_size(images: List[Tensor], targets: List[BaseFormat]):
7
+ assert all([img.shape[-2] == images[0].shape[-2] for img in images]) and all(
8
+ [img.shape[-1] == images[0].shape[-1] for img in images]
9
+ ), "DataLoader : images do not have the same shape"
10
+ assert all(
11
+ [targ.canvas_size[0] == targets[0].canvas_size[0] for targ in targets]
12
+ ) and all(
13
+ [targets[0].canvas_size[1] == targ.canvas_size[1] for targ in targets]
14
+ ), "DataLoader: Targets don't have the same canvas_size"
15
+ assert (images[0].shape[-2] == targets[0].canvas_size[0]) and (
16
+ images[0].shape[-1] == targets[0].canvas_size[1]
17
+ ), "DataLoader : images and targets do not have the save shape / canvas_size. "
18
+
19
+
20
+ class UnknownFormatException(Exception):
21
+ def __init__(self, obj, message: str) -> None:
22
+ message = f"Unknown format {type(obj)}. {message}"
23
+ super().__init__(message)
@@ -0,0 +1,46 @@
1
+ from deepvisiontools.formats.base_formats import BaseFormat, FormatOperatorHandler
2
+ from deepvisiontools.formats.base_data import (
3
+ BboxData,
4
+ InstanceMaskData,
5
+ BaseData,
6
+ SemanticMaskData,
7
+ )
8
+ from deepvisiontools.formats.formats import (
9
+ InstanceMaskFormat,
10
+ BboxFormat,
11
+ BatchedFormat,
12
+ SemanticMaskFormat,
13
+ )
14
+ from deepvisiontools.formats.utils import (
15
+ mask2boxes,
16
+ reindex_mask_with_splitted_objects,
17
+ avg_stack,
18
+ min_stack,
19
+ max_stack,
20
+ combine_logits,
21
+ logit2pred,
22
+ get_preds_and_logits,
23
+ )
24
+
25
+
26
+ __all__ = (
27
+ "BaseData",
28
+ "BboxData",
29
+ "InstanceMaskData",
30
+ "SemanticMaskData",
31
+ "BaseFormat",
32
+ "FormatOperatorHandler",
33
+ "BboxFormat",
34
+ "InstanceMaskFormat",
35
+ "SemanticMaskFormat",
36
+ "BatchedFormat",
37
+ "mask2boxes",
38
+ "reindex_mask_with_splitted_objects",
39
+ "redefine_labels_scores",
40
+ "avg_stack",
41
+ "min_stack",
42
+ "max_stack",
43
+ "combine_logits",
44
+ "logit2pred",
45
+ "get_preds_and_logits",
46
+ )