dragon-ml-toolbox 13.0.0__py3-none-any.whl → 14.7.0__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 (36) hide show
  1. {dragon_ml_toolbox-13.0.0.dist-info → dragon_ml_toolbox-14.7.0.dist-info}/METADATA +12 -2
  2. dragon_ml_toolbox-14.7.0.dist-info/RECORD +49 -0
  3. {dragon_ml_toolbox-13.0.0.dist-info → dragon_ml_toolbox-14.7.0.dist-info}/licenses/LICENSE-THIRD-PARTY.md +10 -0
  4. ml_tools/MICE_imputation.py +207 -5
  5. ml_tools/ML_configuration.py +108 -0
  6. ml_tools/ML_datasetmaster.py +241 -260
  7. ml_tools/ML_evaluation.py +229 -76
  8. ml_tools/ML_evaluation_multi.py +45 -16
  9. ml_tools/ML_inference.py +0 -1
  10. ml_tools/ML_models.py +135 -55
  11. ml_tools/ML_models_advanced.py +323 -0
  12. ml_tools/ML_optimization.py +49 -36
  13. ml_tools/ML_trainer.py +498 -29
  14. ml_tools/ML_utilities.py +351 -4
  15. ml_tools/ML_vision_datasetmaster.py +1492 -0
  16. ml_tools/ML_vision_evaluation.py +260 -0
  17. ml_tools/ML_vision_inference.py +428 -0
  18. ml_tools/ML_vision_models.py +641 -0
  19. ml_tools/ML_vision_transformers.py +203 -0
  20. ml_tools/PSO_optimization.py +5 -1
  21. ml_tools/_ML_vision_recipe.py +88 -0
  22. ml_tools/__init__.py +1 -0
  23. ml_tools/_schema.py +96 -0
  24. ml_tools/custom_logger.py +37 -14
  25. ml_tools/data_exploration.py +576 -138
  26. ml_tools/ensemble_evaluation.py +53 -10
  27. ml_tools/keys.py +43 -1
  28. ml_tools/math_utilities.py +1 -1
  29. ml_tools/optimization_tools.py +65 -86
  30. ml_tools/serde.py +78 -17
  31. ml_tools/utilities.py +192 -3
  32. dragon_ml_toolbox-13.0.0.dist-info/RECORD +0 -41
  33. ml_tools/ML_simple_optimization.py +0 -413
  34. {dragon_ml_toolbox-13.0.0.dist-info → dragon_ml_toolbox-14.7.0.dist-info}/WHEEL +0 -0
  35. {dragon_ml_toolbox-13.0.0.dist-info → dragon_ml_toolbox-14.7.0.dist-info}/licenses/LICENSE +0 -0
  36. {dragon_ml_toolbox-13.0.0.dist-info → dragon_ml_toolbox-14.7.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,1492 @@
1
+ import torch
2
+ from torch.utils.data import Dataset, Subset
3
+ import numpy
4
+ from sklearn.model_selection import train_test_split
5
+ from typing import Union, Tuple, List, Optional, Callable, Dict, Any
6
+ from PIL import Image
7
+ from torchvision.datasets import ImageFolder
8
+ from torchvision import transforms
9
+ import torchvision.transforms.functional as TF
10
+ from pathlib import Path
11
+ import random
12
+ import json
13
+ import inspect
14
+
15
+ from .ML_datasetmaster import _BaseMaker
16
+ from .path_manager import make_fullpath
17
+ from ._logger import _LOGGER
18
+ from ._script_info import _script_info
19
+ from .keys import VisionTransformRecipeKeys, ObjectDetectionKeys
20
+ from ._ML_vision_recipe import save_recipe
21
+ from .ML_vision_transformers import TRANSFORM_REGISTRY
22
+ from .custom_logger import custom_logger
23
+
24
+
25
+ __all__ = [
26
+ "VisionDatasetMaker",
27
+ "SegmentationDatasetMaker",
28
+ "ObjectDetectionDatasetMaker"
29
+ ]
30
+
31
+
32
+ # --- VisionDatasetMaker ---
33
+ class VisionDatasetMaker(_BaseMaker):
34
+ """
35
+ Creates processed PyTorch datasets for computer vision tasks from an
36
+ image folder directory.
37
+
38
+ Supports two modes:
39
+ 1. `from_folder()`: Loads from one directory and splits into train/val/test.
40
+ 2. `from_folders()`: Loads from pre-split train/val/test directories.
41
+
42
+ Uses online augmentations per epoch (image augmentation without creating new files).
43
+ """
44
+ def __init__(self):
45
+ """
46
+ Typically not called directly. Use the class methods `from_folder()` or `from_folders()` to create an instance.
47
+ """
48
+ super().__init__()
49
+ self._full_dataset: Optional[ImageFolder] = None
50
+ self.labels: Optional[List[int]] = None
51
+ self.class_map: Optional[dict[str,int]] = None
52
+
53
+ self._is_split = False
54
+ self._are_transforms_configured = False
55
+ self._val_recipe_components = None
56
+ self._has_mean_std: bool = False
57
+
58
+ @classmethod
59
+ def from_folder(cls, root_dir: Union[str,Path]) -> 'VisionDatasetMaker':
60
+ """
61
+ Creates a maker instance from a single root directory of images.
62
+
63
+ This method assumes a single directory (e.g., 'data/') that
64
+ contains class subfolders (e.g., 'data/cat/', 'data/dog/').
65
+
66
+ The dataset will be loaded in its entirety, and you MUST call
67
+ `.split_data()` afterward to create train/validation/test sets.
68
+
69
+ Args:
70
+ root_dir (str | Path): The path to the root directory containing class subfolders.
71
+
72
+ Returns:
73
+ VisionDatasetMaker: A new instance with the full dataset loaded.
74
+ """
75
+ root_path = make_fullpath(root_dir, enforce="directory")
76
+ # Load with NO transform. We get PIL Images.
77
+ full_dataset = ImageFolder(root=root_path, transform=None)
78
+ _LOGGER.info(f"Found {len(full_dataset)} images in {len(full_dataset.classes)} classes.")
79
+
80
+ maker = cls()
81
+ maker._full_dataset = full_dataset
82
+ maker.labels = [s[1] for s in full_dataset.samples]
83
+ maker.class_map = full_dataset.class_to_idx
84
+ return maker
85
+
86
+ @classmethod
87
+ def from_folders(cls,
88
+ train_dir: Union[str,Path],
89
+ val_dir: Union[str,Path],
90
+ test_dir: Optional[Union[str,Path]] = None) -> 'VisionDatasetMaker':
91
+ """
92
+ Creates a maker instance from separate, pre-split directories.
93
+
94
+ This method is used when you already have 'train', 'val', and
95
+ optionally 'test' folders, each containing class subfolders.
96
+ It bypasses the need for `.split_data()`.
97
+
98
+ Args:
99
+ train_dir (str | Path): Path to the training data directory.
100
+ val_dir (str | Path): Path to the validation data directory.
101
+ test_dir (str | Path | None): Path to the test data directory.
102
+
103
+ Returns:
104
+ VisionDatasetMaker: A new, pre-split instance.
105
+
106
+ Raises:
107
+ ValueError: If the classes found in train, val, or test directories are inconsistent.
108
+ """
109
+ train_path = make_fullpath(train_dir, enforce="directory")
110
+ val_path = make_fullpath(val_dir, enforce="directory")
111
+
112
+ _LOGGER.info("Loading data from separate directories.")
113
+ # Load with NO transform. We get PIL Images.
114
+ train_ds = ImageFolder(root=train_path, transform=None)
115
+ val_ds = ImageFolder(root=val_path, transform=None)
116
+
117
+ # Check for class consistency
118
+ if train_ds.class_to_idx != val_ds.class_to_idx:
119
+ _LOGGER.error("Train and validation directories have different or inconsistent classes.")
120
+ raise ValueError()
121
+
122
+ maker = cls()
123
+ maker._train_dataset = train_ds
124
+ maker._val_dataset = val_ds
125
+ maker.class_map = train_ds.class_to_idx
126
+
127
+ if test_dir:
128
+ test_path = make_fullpath(test_dir, enforce="directory")
129
+ test_ds = ImageFolder(root=test_path, transform=None)
130
+ if train_ds.class_to_idx != test_ds.class_to_idx:
131
+ _LOGGER.error("Train and test directories have different or inconsistent classes.")
132
+ raise ValueError()
133
+ maker._test_dataset = test_ds
134
+ _LOGGER.info(f"Loaded: {len(train_ds)} train, {len(val_ds)} val, {len(test_ds)} test images.")
135
+ else:
136
+ _LOGGER.info(f"Loaded: {len(train_ds)} train, {len(val_ds)} val images.")
137
+
138
+ maker._is_split = True # Mark as "split" since data is pre-split
139
+ return maker
140
+
141
+ @staticmethod
142
+ def inspect_folder(path: Union[str, Path]):
143
+ """
144
+ Logs a report of the types, sizes, and channels of image files
145
+ found in the directory and its subdirectories.
146
+
147
+ This is a utility method to help diagnose potential dataset
148
+ issues (e.g., mixed image modes, corrupted files) before loading.
149
+
150
+ Args:
151
+ path (str, Path): The directory path to inspect.
152
+ """
153
+ path_obj = make_fullpath(path)
154
+
155
+ non_image_files = set()
156
+ img_types = set()
157
+ img_sizes = set()
158
+ img_channels = set()
159
+ img_counter = 0
160
+
161
+ _LOGGER.info(f"Inspecting folder: {path_obj}...")
162
+ # Use rglob to recursively find all files
163
+ for filepath in path_obj.rglob('*'):
164
+ if filepath.is_file():
165
+ try:
166
+ # Using PIL to open is a more reliable check
167
+ with Image.open(filepath) as img:
168
+ img_types.add(img.format)
169
+ img_sizes.add(img.size)
170
+ img_channels.update(img.getbands())
171
+ img_counter += 1
172
+ except (IOError, SyntaxError):
173
+ non_image_files.add(filepath.name)
174
+
175
+ if non_image_files:
176
+ _LOGGER.warning(f"Non-image or corrupted files found and ignored: {non_image_files}")
177
+
178
+ report = (
179
+ f"\n--- Inspection Report for '{path_obj.name}' ---\n"
180
+ f"Total images found: {img_counter}\n"
181
+ f"Image formats: {img_types or 'None'}\n"
182
+ f"Image sizes (WxH): {img_sizes or 'None'}\n"
183
+ f"Image channels (bands): {img_channels or 'None'}\n"
184
+ f"--------------------------------------"
185
+ )
186
+ print(report)
187
+
188
+ def split_data(self, val_size: float = 0.2, test_size: float = 0.0,
189
+ stratify: bool = True, random_state: Optional[int] = None) -> 'VisionDatasetMaker':
190
+ """
191
+ Splits the dataset into train, validation, and optional test sets.
192
+
193
+ This method MUST be called if you used `from_folder()`. It has no effect if you used `from_folders()`.
194
+
195
+ Args:
196
+ val_size (float): Proportion of the dataset to reserve for
197
+ validation (e.g., 0.2 for 20%).
198
+ test_size (float): Proportion of the dataset to reserve for
199
+ testing.
200
+ stratify (bool): If True, splits are performed in a stratified
201
+ fashion, preserving the class distribution.
202
+ random_state (int | None): Seed for the random number generator for reproducible splits.
203
+
204
+ Returns:
205
+ VisionDatasetMaker: The same instance, now with datasets split.
206
+
207
+ Raises:
208
+ ValueError: If `val_size` and `test_size` sum to 1.0 or more.
209
+ """
210
+ if self._is_split:
211
+ _LOGGER.warning("Data has already been split.")
212
+ return self
213
+
214
+ if val_size + test_size >= 1.0:
215
+ _LOGGER.error("The sum of val_size and test_size must be less than 1.")
216
+ raise ValueError()
217
+
218
+ if not self._full_dataset:
219
+ _LOGGER.error("There is no dataset to split.")
220
+ raise ValueError()
221
+
222
+ indices = list(range(len(self._full_dataset)))
223
+ labels_for_split = self.labels if stratify else None
224
+
225
+ train_indices, val_test_indices = train_test_split(
226
+ indices, test_size=(val_size + test_size), random_state=random_state, stratify=labels_for_split
227
+ )
228
+
229
+ if not self.labels:
230
+ _LOGGER.error("Error when getting full dataset labels.")
231
+ raise ValueError()
232
+
233
+ if test_size > 0:
234
+ val_test_labels = [self.labels[i] for i in val_test_indices]
235
+ stratify_val_test = val_test_labels if stratify else None
236
+ val_indices, test_indices = train_test_split(
237
+ val_test_indices, test_size=(test_size / (val_size + test_size)),
238
+ random_state=random_state, stratify=stratify_val_test
239
+ )
240
+ self._test_dataset = Subset(self._full_dataset, test_indices)
241
+ _LOGGER.info(f"Test set created with {len(self._test_dataset)} images.")
242
+ else:
243
+ val_indices = val_test_indices
244
+
245
+ self._train_dataset = Subset(self._full_dataset, train_indices)
246
+ self._val_dataset = Subset(self._full_dataset, val_indices)
247
+ self._is_split = True
248
+
249
+ _LOGGER.info(f"Data split into: \n- Training: {len(self._train_dataset)} images \n- Validation: {len(self._val_dataset)} images")
250
+ return self
251
+
252
+ def configure_transforms(self, resize_size: int = 256, crop_size: int = 224,
253
+ mean: Optional[List[float]] = [0.485, 0.456, 0.406],
254
+ std: Optional[List[float]] = [0.229, 0.224, 0.225],
255
+ pre_transforms: Optional[List[Callable]] = None,
256
+ extra_train_transforms: Optional[List[Callable]] = None) -> 'VisionDatasetMaker':
257
+ """
258
+ Configures and applies the image transformations and augmentations.
259
+
260
+ This method must be called AFTER data is loaded and split.
261
+
262
+ It sets up two pipelines:
263
+ 1. **Training Pipeline:** Includes random augmentations like
264
+ `RandomResizedCrop` and `RandomHorizontalFlip` (plus any
265
+ `extra_train_transforms`) for online augmentation.
266
+ 2. **Validation/Test Pipeline:** A deterministic pipeline using
267
+ `Resize` and `CenterCrop` for consistent evaluation.
268
+
269
+ Both pipelines finish with `ToTensor` and `Normalize`.
270
+
271
+ Args:
272
+ resize_size (int): The size to resize the smallest edge to
273
+ for validation/testing.
274
+ crop_size (int): The target size (square) for the final
275
+ cropped image.
276
+ mean (List[float] | None): The mean values for normalization (e.g., ImageNet mean).
277
+ std (List[float] | None): The standard deviation values for normalization (e.g., ImageNet std).
278
+ extra_train_transforms (List[Callable] | None): A list of additional torchvision transforms to add to the end of the training transformations.
279
+ pre_transforms (List[Callable] | None): An list of transforms to be applied at the very beginning of the transformations for all sets.
280
+
281
+ Returns:
282
+ VisionDatasetMaker: The same instance, with transforms applied.
283
+
284
+ Raises:
285
+ RuntimeError: If called before data is split.
286
+ """
287
+ if not self._is_split:
288
+ _LOGGER.error("Transforms must be configured AFTER splitting data (or using `from_folders`). Call .split_data() first if using `from_folder`.")
289
+ raise RuntimeError()
290
+
291
+ if (mean is None and std is not None) or (mean is not None and std is None):
292
+ _LOGGER.error(f"'mean' and 'std' must be both None or both defined, but only one was provided.")
293
+ raise ValueError()
294
+
295
+ # --- Define Transform Pipelines ---
296
+ # These now MUST include ToTensor and Normalize, as the ImageFolder was loaded with transform=None.
297
+
298
+ # --- Store components for validation recipe ---
299
+ self._val_recipe_components = {
300
+ VisionTransformRecipeKeys.PRE_TRANSFORMS: pre_transforms or [],
301
+ VisionTransformRecipeKeys.RESIZE_SIZE: resize_size,
302
+ VisionTransformRecipeKeys.CROP_SIZE: crop_size,
303
+ }
304
+
305
+ if mean is not None and std is not None:
306
+ self._val_recipe_components.update({
307
+ VisionTransformRecipeKeys.MEAN: mean,
308
+ VisionTransformRecipeKeys.STD: std
309
+ })
310
+ self._has_mean_std = True
311
+
312
+ base_pipeline = []
313
+ if pre_transforms:
314
+ base_pipeline.extend(pre_transforms)
315
+
316
+ # Base augmentations for training
317
+ base_train_transforms = [
318
+ transforms.RandomResizedCrop(crop_size),
319
+ transforms.RandomHorizontalFlip()
320
+ ]
321
+ if extra_train_transforms:
322
+ base_train_transforms.extend(extra_train_transforms)
323
+
324
+ # Final conversion and normalization
325
+ final_transforms: list[Callable] = [
326
+ transforms.ToTensor()
327
+ ]
328
+
329
+ if self._has_mean_std:
330
+ final_transforms.append(transforms.Normalize(mean=mean, std=std))
331
+
332
+ # Build the val/test pipeline
333
+ val_transform_list = [
334
+ *base_pipeline, # Apply pre_transforms first
335
+ transforms.Resize(resize_size),
336
+ transforms.CenterCrop(crop_size),
337
+ *final_transforms
338
+ ]
339
+
340
+ # Build the train pipeline
341
+ train_transform_list = [
342
+ *base_pipeline, # Apply pre_transforms first
343
+ *base_train_transforms,
344
+ *final_transforms
345
+ ]
346
+
347
+ val_transform = transforms.Compose(val_transform_list)
348
+ train_transform = transforms.Compose(train_transform_list)
349
+
350
+ # --- Apply Transforms using the Wrapper ---
351
+ # This correctly assigns the transform regardless of whether the dataset is a Subset (from_folder) or an ImageFolder (from_folders).
352
+
353
+ self._train_dataset = _DatasetTransformer(self._train_dataset, train_transform) # type: ignore
354
+ self._val_dataset = _DatasetTransformer(self._val_dataset, val_transform) # type: ignore
355
+ if self._test_dataset:
356
+ self._test_dataset = _DatasetTransformer(self._test_dataset, val_transform) # type: ignore
357
+
358
+ self._are_transforms_configured = True
359
+ _LOGGER.info("Image transforms configured and applied.")
360
+ return self
361
+
362
+ def get_datasets(self) -> Tuple[Dataset, ...]:
363
+ """
364
+ Returns the final train, validation, and optional test datasets.
365
+
366
+ This is the final step, used to retrieve the datasets for use in
367
+ a `MLTrainer` or `DataLoader`.
368
+
369
+ Returns:
370
+ (Tuple[Dataset, ...]): A tuple containing the (train, val)
371
+ or (train, val, test) datasets.
372
+
373
+ Raises:
374
+ RuntimeError: If called before data is split.
375
+ UserWarning: If called before transforms are configured.
376
+ """
377
+ if not self._is_split:
378
+ _LOGGER.error("Data has not been split. Call .split_data() first.")
379
+ raise RuntimeError()
380
+ if not self._are_transforms_configured:
381
+ _LOGGER.warning("Transforms have not been configured.")
382
+
383
+ if self._test_dataset:
384
+ return self._train_dataset, self._val_dataset, self._test_dataset
385
+ return self._train_dataset, self._val_dataset
386
+
387
+ def save_transform_recipe(self, filepath: Union[str, Path]) -> None:
388
+ """
389
+ Saves the validation transform pipeline as a JSON recipe file.
390
+
391
+ This recipe can be loaded by the PyTorchVisionInferenceHandler
392
+ to ensure identical preprocessing.
393
+
394
+ Args:
395
+ filepath (str | Path): The path to save the .json recipe file.
396
+ """
397
+ if not self._are_transforms_configured:
398
+ _LOGGER.error("Transforms are not configured. Call .configure_transforms() first.")
399
+ raise RuntimeError()
400
+
401
+ recipe: Dict[str, Any] = {
402
+ VisionTransformRecipeKeys.TASK: "classification",
403
+ VisionTransformRecipeKeys.PIPELINE: []
404
+ }
405
+
406
+ components = self._val_recipe_components
407
+
408
+ if not components:
409
+ _LOGGER.error(f"Error getting the transformers recipe for validation set.")
410
+ raise ValueError()
411
+
412
+ # validate path
413
+ file_path = make_fullpath(filepath, make=True, enforce="file")
414
+
415
+ # Handle pre_transforms
416
+ for t in components[VisionTransformRecipeKeys.PRE_TRANSFORMS]:
417
+ t_name = t.__class__.__name__
418
+ t_class = t.__class__
419
+ kwargs = {}
420
+
421
+ # 1. Check custom registry first
422
+ if t_name in TRANSFORM_REGISTRY:
423
+ _LOGGER.debug(f"Found '{t_name}' in TRANSFORM_REGISTRY.")
424
+ kwargs = getattr(t, VisionTransformRecipeKeys.KWARGS, {})
425
+
426
+ # 2. Else, try to introspect for standard torchvision transforms
427
+ else:
428
+ _LOGGER.debug(f"'{t_name}' not in registry. Attempting introspection...")
429
+ try:
430
+ # Get the __init__ signature of the transform's class
431
+ sig = inspect.signature(t_class.__init__)
432
+
433
+ # Iterate over its __init__ parameters (e.g., 'num_output_channels')
434
+ for param in sig.parameters.values():
435
+ if param.name == 'self':
436
+ continue
437
+
438
+ # Check if the *instance* 't' has that parameter as an attribute
439
+ attr_name_public = param.name
440
+ attr_name_private = '_' + param.name
441
+
442
+ attr_to_get = ""
443
+
444
+ if hasattr(t, attr_name_public):
445
+ attr_to_get = attr_name_public
446
+ elif hasattr(t, attr_name_private):
447
+ attr_to_get = attr_name_private
448
+ else:
449
+ # Parameter in __init__ has no matching attribute
450
+ continue
451
+
452
+ # Store the value under the __init__ parameter's name
453
+ kwargs[param.name] = getattr(t, attr_to_get)
454
+
455
+ _LOGGER.debug(f"Introspection for '{t_name}' found kwargs: {kwargs}")
456
+
457
+ except (ValueError, TypeError):
458
+ # Fails on some built-ins or C-implemented __init__
459
+ _LOGGER.warning(f"Could not introspect parameters for '{t_name}'. If this transform has parameters, they will not be saved.")
460
+ kwargs = {}
461
+
462
+ # 3. Add to pipeline
463
+ recipe[VisionTransformRecipeKeys.PIPELINE].append({
464
+ VisionTransformRecipeKeys.NAME: t_name,
465
+ VisionTransformRecipeKeys.KWARGS: kwargs
466
+ })
467
+
468
+ # 2. Add standard transforms
469
+ recipe[VisionTransformRecipeKeys.PIPELINE].extend([
470
+ {VisionTransformRecipeKeys.NAME: "Resize", "kwargs": {"size": components[VisionTransformRecipeKeys.RESIZE_SIZE]}},
471
+ {VisionTransformRecipeKeys.NAME: "CenterCrop", "kwargs": {"size": components[VisionTransformRecipeKeys.CROP_SIZE]}},
472
+ {VisionTransformRecipeKeys.NAME: "ToTensor", "kwargs": {}}
473
+ ])
474
+
475
+ if self._has_mean_std:
476
+ recipe[VisionTransformRecipeKeys.PIPELINE].append(
477
+ {VisionTransformRecipeKeys.NAME: "Normalize", "kwargs": {
478
+ "mean": components[VisionTransformRecipeKeys.MEAN],
479
+ "std": components[VisionTransformRecipeKeys.STD]
480
+ }}
481
+ )
482
+
483
+ # 3. Save the file
484
+ save_recipe(recipe, file_path)
485
+
486
+ def save_class_map(self, save_dir: Union[str,Path]) -> dict[str,int]:
487
+ """
488
+ Saves the class to index mapping {str: int} to a directory.
489
+ """
490
+ if not self.class_map:
491
+ _LOGGER.error(f"Class to index mapping is empty.")
492
+ raise ValueError()
493
+
494
+ custom_logger(data=self.class_map,
495
+ save_directory=save_dir,
496
+ log_name="Class_to_Index",
497
+ add_timestamp=False,
498
+ dict_as="json")
499
+
500
+ return self.class_map
501
+
502
+ def __repr__(self) -> str:
503
+ s = f"<{self.__class__.__name__}>:\n"
504
+ s += f" Split: {self._is_split}\n"
505
+ s += f" Transforms Configured: {self._are_transforms_configured}\n"
506
+
507
+ if self.class_map:
508
+ s += f" Classes: {len(self.class_map)}\n"
509
+
510
+ if self._is_split:
511
+ train_len = len(self._train_dataset) if self._train_dataset else 0
512
+ val_len = len(self._val_dataset) if self._val_dataset else 0
513
+ test_len = len(self._test_dataset) if self._test_dataset else 0
514
+ s += f" Datasets (Train/Val/Test): {train_len} / {val_len} / {test_len}\n"
515
+ elif self._full_dataset:
516
+ s += f" Full Dataset Size: {len(self._full_dataset)} images\n"
517
+
518
+ return s
519
+
520
+
521
+ class _DatasetTransformer(Dataset):
522
+ """
523
+ Internal wrapper class to apply a specific transform pipeline to any
524
+ dataset (e.g., a full ImageFolder or a Subset).
525
+ """
526
+ def __init__(self, dataset: Dataset, transform: Optional[transforms.Compose] = None):
527
+ self.dataset = dataset
528
+ self.transform = transform
529
+
530
+ # --- Propagate attributes for inspection ---
531
+ # For ImageFolder
532
+ if hasattr(dataset, 'class_to_idx'):
533
+ self.class_to_idx = getattr(dataset, 'class_to_idx')
534
+ if hasattr(dataset, 'classes'):
535
+ self.classes = getattr(dataset, 'classes')
536
+ # For Subset
537
+ if hasattr(dataset, 'indices'):
538
+ self.indices = getattr(dataset, 'indices')
539
+ if hasattr(dataset, 'dataset'):
540
+ # This allows access to the *original* full dataset
541
+ self.original_dataset = getattr(dataset, 'dataset')
542
+
543
+ def __getitem__(self, index):
544
+ # Get the original data (e.g., PIL Image, label)
545
+ x, y = self.dataset[index]
546
+
547
+ # Apply the specific transform for this dataset
548
+ if self.transform:
549
+ x = self.transform(x)
550
+ return x, y
551
+
552
+ def __len__(self):
553
+ return len(self.dataset) # type: ignore
554
+
555
+
556
+ # --- Segmentation dataset ----
557
+ class _SegmentationDataset(Dataset):
558
+ """
559
+ Internal helper class to load image-mask pairs.
560
+
561
+ Loads images as RGB and masks as 'L' (grayscale, 8-bit integer pixels).
562
+ """
563
+ def __init__(self, image_paths: List[Path], mask_paths: List[Path], transform: Optional[Callable] = None):
564
+ self.image_paths = image_paths
565
+ self.mask_paths = mask_paths
566
+ self.transform = transform
567
+
568
+ # --- Propagate 'classes' if they exist (for MLTrainer) ---
569
+ self.classes: List[str] = []
570
+
571
+ def __len__(self):
572
+ return len(self.image_paths)
573
+
574
+ def __getitem__(self, idx):
575
+ img_path = self.image_paths[idx]
576
+ mask_path = self.mask_paths[idx]
577
+
578
+ try:
579
+ # Open as PIL Images. Masks should be 'L'
580
+ image = Image.open(img_path).convert("RGB")
581
+ mask = Image.open(mask_path).convert("L")
582
+ except Exception as e:
583
+ _LOGGER.error(f"Error loading sample #{idx}: {img_path.name} / {mask_path.name}. Error: {e}")
584
+ # Return empty tensors
585
+ return torch.empty(3, 224, 224), torch.empty(224, 224, dtype=torch.long)
586
+
587
+ if self.transform:
588
+ image, mask = self.transform(image, mask)
589
+
590
+ return image, mask
591
+
592
+
593
+ # Internal Paired Transform Helpers
594
+ class _PairedCompose:
595
+ """A 'Compose' for paired image/mask transforms."""
596
+ def __init__(self, transforms: List[Callable]):
597
+ self.transforms = transforms
598
+
599
+ def __call__(self, image: Any, mask: Any) -> Tuple[Any, Any]:
600
+ for t in self.transforms:
601
+ image, mask = t(image, mask)
602
+ return image, mask
603
+
604
+ class _PairedToTensor:
605
+ """Converts a PIL Image pair (image, mask) to Tensors."""
606
+ def __call__(self, image: Image.Image, mask: Image.Image) -> Tuple[torch.Tensor, torch.Tensor]:
607
+ # Use new variable names to satisfy the linter
608
+ image_tensor = TF.to_tensor(image)
609
+ # Convert mask to LongTensor, not float.
610
+ # This creates a [H, W] tensor of integer class IDs.
611
+ mask_tensor = torch.from_numpy(numpy.array(mask, dtype=numpy.int64))
612
+ return image_tensor, mask_tensor
613
+
614
+ class _PairedNormalize:
615
+ """Normalizes the image tensor and leaves the mask untouched."""
616
+ def __init__(self, mean: List[float], std: List[float]):
617
+ self.normalize = transforms.Normalize(mean, std)
618
+
619
+ def __call__(self, image: torch.Tensor, mask: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
620
+ image = self.normalize(image)
621
+ return image, mask
622
+
623
+ class _PairedResize:
624
+ """Resizes an image and mask to the same size."""
625
+ def __init__(self, size: int):
626
+ self.size = [size, size]
627
+
628
+ def __call__(self, image: Image.Image, mask: Image.Image) -> Tuple[Image.Image, Image.Image]:
629
+ # Use new variable names to avoid linter confusion
630
+ resized_image = TF.resize(image, self.size, interpolation=TF.InterpolationMode.BILINEAR) # type: ignore
631
+ # Use NEAREST for mask to avoid interpolating class IDs (e.g., 1.5)
632
+ resized_mask = TF.resize(mask, self.size, interpolation=TF.InterpolationMode.NEAREST) # type: ignore
633
+ return resized_image, resized_mask # type: ignore
634
+
635
+ class _PairedCenterCrop:
636
+ """Center-crops an image and mask to the same size."""
637
+ def __init__(self, size: int):
638
+ self.size = [size, size]
639
+
640
+ def __call__(self, image: Image.Image, mask: Image.Image) -> Tuple[Image.Image, Image.Image]:
641
+ cropped_image = TF.center_crop(image, self.size) # type: ignore
642
+ cropped_mask = TF.center_crop(mask, self.size) # type: ignore
643
+ return cropped_image, cropped_mask # type: ignore
644
+
645
+ class _PairedRandomHorizontalFlip:
646
+ """Applies the same random horizontal flip to both image and mask."""
647
+ def __init__(self, p: float = 0.5):
648
+ self.p = p
649
+
650
+ def __call__(self, image: Image.Image, mask: Image.Image) -> Tuple[Image.Image, Image.Image]:
651
+ if random.random() < self.p:
652
+ flipped_image = TF.hflip(image) # type: ignore
653
+ flipped_mask = TF.hflip(mask) # type: ignore
654
+ return flipped_image, flipped_mask # type: ignore
655
+
656
+ class _PairedRandomResizedCrop:
657
+ """Applies the same random resized crop to both image and mask."""
658
+ def __init__(self, size: int, scale: Tuple[float, float]=(0.08, 1.0), ratio: Tuple[float, float]=(3./4., 4./3.)):
659
+ self.size = [size, size]
660
+ self.scale = scale
661
+ self.ratio = ratio
662
+ self.interpolation = TF.InterpolationMode.BILINEAR
663
+ self.mask_interpolation = TF.InterpolationMode.NEAREST
664
+
665
+ def __call__(self, image: Image.Image, mask: Image.Image) -> Tuple[Image.Image, Image.Image]:
666
+ # Get parameters for the random crop
667
+ # Convert scale/ratio tuples to lists to satisfy the linter's type hint
668
+ i, j, h, w = transforms.RandomResizedCrop.get_params(image, list(self.scale), list(self.ratio)) # type: ignore
669
+
670
+ # Apply the crop with the SAME parameters and use new variable names
671
+ cropped_image = TF.resized_crop(image, i, j, h, w, self.size, self.interpolation) # type: ignore
672
+ cropped_mask = TF.resized_crop(mask, i, j, h, w, self.size, self.mask_interpolation) # type: ignore
673
+
674
+ return cropped_image, cropped_mask # type: ignore
675
+
676
+ # --- SegmentationDatasetMaker ---
677
+ class SegmentationDatasetMaker(_BaseMaker):
678
+ """
679
+ Creates processed PyTorch datasets for segmentation from image and mask folders.
680
+
681
+ This maker finds all matching image-mask pairs from two directories,
682
+ splits them, and applies identical transformations (including augmentations)
683
+ to both the image and its corresponding mask.
684
+
685
+ Workflow:
686
+ 1. `maker = SegmentationDatasetMaker.from_folders(img_dir, mask_dir)`
687
+ 2. `maker.set_class_map({'background': 0, 'road': 1})`
688
+ 3. `maker.split_data(val_size=0.2)`
689
+ 4. `maker.configure_transforms(crop_size=256)`
690
+ 5. `train_ds, val_ds = maker.get_datasets()`
691
+ """
692
+ IMG_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff')
693
+
694
+ def __init__(self):
695
+ """
696
+ Typically not called directly. Use the class method `from_folders()` to create an instance.
697
+ """
698
+ super().__init__()
699
+ self.image_paths: List[Path] = []
700
+ self.mask_paths: List[Path] = []
701
+ self.class_map: Dict[str, int] = {}
702
+
703
+ self._is_split = False
704
+ self._are_transforms_configured = False
705
+ self.train_transform: Optional[Callable] = None
706
+ self.val_transform: Optional[Callable] = None
707
+ self._has_mean_std: bool = False
708
+
709
+ @classmethod
710
+ def from_folders(cls, image_dir: Union[str, Path], mask_dir: Union[str, Path]) -> 'SegmentationDatasetMaker':
711
+ """
712
+ Creates a maker instance by loading all matching image-mask pairs
713
+ from two corresponding directories.
714
+
715
+ This method assumes that for an image `images/img_001.png`, there
716
+ is a corresponding mask `masks/img_001.png`.
717
+
718
+ Args:
719
+ image_dir (str | Path): Path to the directory containing input images.
720
+ mask_dir (str | Path): Path to the directory containing segmentation masks.
721
+
722
+ Returns:
723
+ SegmentationDatasetMaker: A new instance with all pairs loaded.
724
+ """
725
+ maker = cls()
726
+ img_path_obj = make_fullpath(image_dir, enforce="directory")
727
+ msk_path_obj = make_fullpath(mask_dir, enforce="directory")
728
+
729
+ # Find all images
730
+ image_files = sorted([
731
+ p for p in img_path_obj.glob('*.*')
732
+ if p.suffix.lower() in cls.IMG_EXTENSIONS
733
+ ])
734
+
735
+ if not image_files:
736
+ _LOGGER.error(f"No images with extensions {cls.IMG_EXTENSIONS} found in {image_dir}")
737
+ raise FileNotFoundError()
738
+
739
+ _LOGGER.info(f"Found {len(image_files)} images. Searching for matching masks in {mask_dir}...")
740
+
741
+ good_img_paths = []
742
+ good_mask_paths = []
743
+
744
+ for img_file in image_files:
745
+ mask_file = None
746
+
747
+ # 1. Try to find mask with the exact same name
748
+ mask_file_primary = msk_path_obj / img_file.name
749
+ if mask_file_primary.exists():
750
+ mask_file = mask_file_primary
751
+
752
+ # 2. If not, try to find mask with same stem + common mask extension
753
+ if mask_file is None:
754
+ for ext in cls.IMG_EXTENSIONS: # Masks are often .png
755
+ mask_file_secondary = msk_path_obj / (img_file.stem + ext)
756
+ if mask_file_secondary.exists():
757
+ mask_file = mask_file_secondary
758
+ break
759
+
760
+ # 3. If a match is found, add the pair
761
+ if mask_file:
762
+ good_img_paths.append(img_file)
763
+ good_mask_paths.append(mask_file)
764
+ else:
765
+ _LOGGER.warning(f"No corresponding mask found for image: {img_file.name}")
766
+
767
+ if not good_img_paths:
768
+ _LOGGER.error("No matching image-mask pairs were found.")
769
+ raise FileNotFoundError()
770
+
771
+ _LOGGER.info(f"Successfully found {len(good_img_paths)} image-mask pairs.")
772
+ maker.image_paths = good_img_paths
773
+ maker.mask_paths = good_mask_paths
774
+
775
+ return maker
776
+
777
+ @staticmethod
778
+ def inspect_folder(path: Union[str, Path]):
779
+ """
780
+ Logs a report of the types, sizes, and channels of image files
781
+ found in the directory. Useful for checking masks.
782
+ """
783
+ VisionDatasetMaker.inspect_folder(path)
784
+
785
+ def set_class_map(self, class_map: Dict[str, int]) -> 'SegmentationDatasetMaker':
786
+ """
787
+ Sets a map of pixel_value -> class_name. This is used by the MLTrainer for clear evaluation reports.
788
+
789
+ Args:
790
+ class_map (Dict[int, str]): A dictionary mapping the integer pixel
791
+ value in a mask to its string name.
792
+ Example: {'background': 0, 'road': 1, 'car': 2}
793
+ """
794
+ self.class_map = class_map
795
+ _LOGGER.info(f"Class map set: {class_map}")
796
+ return self
797
+
798
+ @property
799
+ def classes(self) -> List[str]:
800
+ """Returns the list of class names, if set."""
801
+ if self.class_map:
802
+ return list(self.class_map.keys())
803
+ return []
804
+
805
+ def split_data(self, val_size: float = 0.2, test_size: float = 0.0,
806
+ random_state: Optional[int] = 42) -> 'SegmentationDatasetMaker':
807
+ """
808
+ Splits the loaded image-mask pairs into train, validation, and test sets.
809
+
810
+ Args:
811
+ val_size (float): Proportion of the dataset to reserve for validation.
812
+ test_size (float): Proportion of the dataset to reserve for testing.
813
+ random_state (int | None): Seed for reproducible splits.
814
+
815
+ Returns:
816
+ SegmentationDatasetMaker: The same instance, now with datasets created.
817
+ """
818
+ if self._is_split:
819
+ _LOGGER.warning("Data has already been split.")
820
+ return self
821
+
822
+ if val_size + test_size >= 1.0:
823
+ _LOGGER.error("The sum of val_size and test_size must be less than 1.")
824
+ raise ValueError()
825
+
826
+ if not self.image_paths:
827
+ _LOGGER.error("There is no data to split. Use .from_folders() first.")
828
+ raise RuntimeError()
829
+
830
+ indices = list(range(len(self.image_paths)))
831
+
832
+ # Split indices
833
+ train_indices, val_test_indices = train_test_split(
834
+ indices, test_size=(val_size + test_size), random_state=random_state
835
+ )
836
+
837
+ # Helper to get paths from indices
838
+ def get_paths(idx_list):
839
+ return [self.image_paths[i] for i in idx_list], [self.mask_paths[i] for i in idx_list]
840
+
841
+ train_imgs, train_masks = get_paths(train_indices)
842
+
843
+ if test_size > 0:
844
+ val_indices, test_indices = train_test_split(
845
+ val_test_indices, test_size=(test_size / (val_size + test_size)),
846
+ random_state=random_state
847
+ )
848
+ val_imgs, val_masks = get_paths(val_indices)
849
+ test_imgs, test_masks = get_paths(test_indices)
850
+
851
+ self._test_dataset = _SegmentationDataset(test_imgs, test_masks, transform=None)
852
+ self._test_dataset.classes = self.classes # type: ignore
853
+ _LOGGER.info(f"Test set created with {len(self._test_dataset)} images.")
854
+ else:
855
+ val_imgs, val_masks = get_paths(val_test_indices)
856
+
857
+ self._train_dataset = _SegmentationDataset(train_imgs, train_masks, transform=None)
858
+ self._val_dataset = _SegmentationDataset(val_imgs, val_masks, transform=None)
859
+
860
+ # Propagate class names to datasets for MLTrainer
861
+ self._train_dataset.classes = self.classes # type: ignore
862
+ self._val_dataset.classes = self.classes # type: ignore
863
+
864
+ self._is_split = True
865
+ _LOGGER.info(f"Data split into: \n- Training: {len(self._train_dataset)} images \n- Validation: {len(self._val_dataset)} images")
866
+ return self
867
+
868
+ def configure_transforms(self,
869
+ resize_size: int = 256,
870
+ crop_size: int = 224,
871
+ mean: Optional[List[float]] = [0.485, 0.456, 0.406],
872
+ std: Optional[List[float]] = [0.229, 0.224, 0.225]) -> 'SegmentationDatasetMaker':
873
+ """
874
+ Configures and applies the image and mask transformations.
875
+
876
+ This method must be called AFTER data is split.
877
+
878
+ Args:
879
+ resize_size (int): The size to resize the smallest edge to
880
+ for validation/testing.
881
+ crop_size (int): The target size (square) for the final
882
+ cropped image.
883
+ mean (List[float] | None): The mean values for image normalization.
884
+ std (List[float] | None): The std dev values for image normalization.
885
+
886
+ Returns:
887
+ SegmentationDatasetMaker: The same instance, with transforms applied.
888
+ """
889
+ if not self._is_split:
890
+ _LOGGER.error("Transforms must be configured AFTER splitting data. Call .split_data() first.")
891
+ raise RuntimeError()
892
+
893
+ if (mean is None and std is not None) or (mean is not None and std is None):
894
+ _LOGGER.error(f"'mean' and 'std' must be both None or both defined, but only one was provided.")
895
+ raise ValueError()
896
+
897
+ # --- Store components for validation recipe ---
898
+ self.val_recipe_components: dict[str,Any] = {
899
+ VisionTransformRecipeKeys.RESIZE_SIZE: resize_size,
900
+ VisionTransformRecipeKeys.CROP_SIZE: crop_size,
901
+ }
902
+
903
+ if mean is not None and std is not None:
904
+ self.val_recipe_components.update({
905
+ VisionTransformRecipeKeys.MEAN: mean,
906
+ VisionTransformRecipeKeys.STD: std
907
+ })
908
+ self._has_mean_std = True
909
+
910
+ # --- Validation/Test Pipeline (Deterministic) ---
911
+ if self._has_mean_std:
912
+ self.val_transform = _PairedCompose([
913
+ _PairedResize(resize_size),
914
+ _PairedCenterCrop(crop_size),
915
+ _PairedToTensor(),
916
+ _PairedNormalize(mean, std) # type: ignore
917
+ ])
918
+ # --- Training Pipeline (Augmentation) ---
919
+ self.train_transform = _PairedCompose([
920
+ _PairedRandomResizedCrop(crop_size),
921
+ _PairedRandomHorizontalFlip(p=0.5),
922
+ _PairedToTensor(),
923
+ _PairedNormalize(mean, std) # type: ignore
924
+ ])
925
+ else:
926
+ self.val_transform = _PairedCompose([
927
+ _PairedResize(resize_size),
928
+ _PairedCenterCrop(crop_size),
929
+ _PairedToTensor()
930
+ ])
931
+ # --- Training Pipeline (Augmentation) ---
932
+ self.train_transform = _PairedCompose([
933
+ _PairedRandomResizedCrop(crop_size),
934
+ _PairedRandomHorizontalFlip(p=0.5),
935
+ _PairedToTensor()
936
+ ])
937
+
938
+ # --- Apply Transforms to the Datasets ---
939
+ self._train_dataset.transform = self.train_transform # type: ignore
940
+ self._val_dataset.transform = self.val_transform # type: ignore
941
+ if self._test_dataset:
942
+ self._test_dataset.transform = self.val_transform # type: ignore
943
+
944
+ self._are_transforms_configured = True
945
+ _LOGGER.info("Paired segmentation transforms configured and applied.")
946
+ return self
947
+
948
+ def get_datasets(self) -> Tuple[Dataset, ...]:
949
+ """
950
+ Returns the final train, validation, and optional test datasets.
951
+
952
+ Raises:
953
+ RuntimeError: If called before data is split.
954
+ RuntimeError: If called before transforms are configured.
955
+ """
956
+ if not self._is_split:
957
+ _LOGGER.error("Data has not been split. Call .split_data() first.")
958
+ raise RuntimeError()
959
+ if not self._are_transforms_configured:
960
+ _LOGGER.error("Transforms have not been configured. Call .configure_transforms() first.")
961
+ raise RuntimeError()
962
+
963
+ if self._test_dataset:
964
+ return self._train_dataset, self._val_dataset, self._test_dataset
965
+ return self._train_dataset, self._val_dataset
966
+
967
+ def save_transform_recipe(self, filepath: Union[str, Path]) -> None:
968
+ """
969
+ Saves the validation transform pipeline as a JSON recipe file.
970
+
971
+ This recipe can be loaded by the PyTorchVisionInferenceHandler
972
+ to ensure identical preprocessing.
973
+
974
+ Args:
975
+ filepath (str | Path): The path to save the .json recipe file.
976
+ """
977
+ if not self._are_transforms_configured:
978
+ _LOGGER.error("Transforms are not configured. Call .configure_transforms() first.")
979
+ raise RuntimeError()
980
+
981
+ components = self.val_recipe_components
982
+
983
+ if not components:
984
+ _LOGGER.error(f"Error getting the transformers recipe for validation set.")
985
+ raise ValueError()
986
+
987
+ # validate path
988
+ file_path = make_fullpath(filepath, make=True, enforce="file")
989
+
990
+ # Add standard transforms
991
+ recipe: Dict[str, Any] = {
992
+ VisionTransformRecipeKeys.TASK: "segmentation",
993
+ VisionTransformRecipeKeys.PIPELINE: [
994
+ {VisionTransformRecipeKeys.NAME: "Resize", "kwargs": {"size": components[VisionTransformRecipeKeys.RESIZE_SIZE]}},
995
+ {VisionTransformRecipeKeys.NAME: "CenterCrop", "kwargs": {"size": components[VisionTransformRecipeKeys.CROP_SIZE]}},
996
+ {VisionTransformRecipeKeys.NAME: "ToTensor", "kwargs": {}}
997
+ ]
998
+ }
999
+
1000
+ if self._has_mean_std:
1001
+ recipe[VisionTransformRecipeKeys.PIPELINE].append(
1002
+ {VisionTransformRecipeKeys.NAME: "Normalize", "kwargs": {
1003
+ "mean": components[VisionTransformRecipeKeys.MEAN],
1004
+ "std": components[VisionTransformRecipeKeys.STD]
1005
+ }}
1006
+ )
1007
+
1008
+ # Save the file
1009
+ save_recipe(recipe, file_path)
1010
+
1011
+ def __repr__(self) -> str:
1012
+ s = f"<{self.__class__.__name__}>:\n"
1013
+ s += f" Total Image-Mask Pairs: {len(self.image_paths)}\n"
1014
+ s += f" Split: {self._is_split}\n"
1015
+ s += f" Transforms Configured: {self._are_transforms_configured}\n"
1016
+
1017
+ if self.class_map:
1018
+ s += f" Classes: {list(self.class_map.keys())}\n"
1019
+
1020
+ if self._is_split:
1021
+ train_len = len(self._train_dataset) if self._train_dataset else 0
1022
+ val_len = len(self._val_dataset) if self._val_dataset else 0
1023
+ test_len = len(self._test_dataset) if self._test_dataset else 0
1024
+ s += f" Datasets (Train/Val/Test): {train_len} / {val_len} / {test_len}\n"
1025
+
1026
+ return s
1027
+
1028
+
1029
+ # Object detection
1030
+ def _od_collate_fn(batch: List[Tuple[torch.Tensor, Dict[str, torch.Tensor]]]) -> Tuple[List[torch.Tensor], List[Dict[str, torch.Tensor]]]:
1031
+ """
1032
+ Custom collate function for object detection.
1033
+
1034
+ Takes a list of (image, target) tuples and zips them into two lists:
1035
+ (list_of_images, list_of_targets).
1036
+ This is required for models like Faster R-CNN, which accept a list
1037
+ of images of varying sizes.
1038
+ """
1039
+ return tuple(zip(*batch)) # type: ignore
1040
+
1041
+
1042
+ class _ObjectDetectionDataset(Dataset):
1043
+ """
1044
+ Internal helper class to load image-annotation pairs.
1045
+
1046
+ Loads an image as 'RGB' and parses its corresponding JSON annotation file
1047
+ to create the required target dictionary (boxes, labels).
1048
+ """
1049
+ def __init__(self, image_paths: List[Path], annotation_paths: List[Path], transform: Optional[Callable] = None):
1050
+ self.image_paths = image_paths
1051
+ self.annotation_paths = annotation_paths
1052
+ self.transform = transform
1053
+
1054
+ # --- Propagate 'classes' if they exist (for MLTrainer) ---
1055
+ self.classes: List[str] = []
1056
+
1057
+ def __len__(self):
1058
+ return len(self.image_paths)
1059
+
1060
+ def __getitem__(self, idx):
1061
+ img_path = self.image_paths[idx]
1062
+ ann_path = self.annotation_paths[idx]
1063
+
1064
+ try:
1065
+ # Open image
1066
+ image = Image.open(img_path).convert("RGB")
1067
+
1068
+ # Load and parse annotation
1069
+ with open(ann_path, 'r') as f:
1070
+ ann_data = json.load(f)
1071
+
1072
+ # Get boxes and labels from JSON
1073
+ boxes = ann_data[ObjectDetectionKeys.BOXES] # Expected: [[x1, y1, x2, y2], ...]
1074
+ labels = ann_data[ObjectDetectionKeys.LABELS] # Expected: [1, 2, 1, ...]
1075
+
1076
+ # Convert to tensors
1077
+ target: Dict[str, Any] = {}
1078
+ target[ObjectDetectionKeys.BOXES] = torch.as_tensor(boxes, dtype=torch.float32)
1079
+ target[ObjectDetectionKeys.LABELS] = torch.as_tensor(labels, dtype=torch.int64)
1080
+
1081
+ except Exception as e:
1082
+ _LOGGER.error(f"Error loading sample #{idx}: {img_path.name} / {ann_path.name}. Error: {e}")
1083
+ # Return empty/dummy data
1084
+ return torch.empty(3, 224, 224), {ObjectDetectionKeys.BOXES: torch.empty((0, 4)), ObjectDetectionKeys.LABELS: torch.empty(0, dtype=torch.long)}
1085
+
1086
+ if self.transform:
1087
+ image, target = self.transform(image, target)
1088
+
1089
+ return image, target
1090
+
1091
+ # Internal Paired Transform Helpers for Object Detection
1092
+ class _OD_PairedCompose:
1093
+ """A 'Compose' for paired image/target_dict transforms."""
1094
+ def __init__(self, transforms: List[Callable]):
1095
+ self.transforms = transforms
1096
+
1097
+ def __call__(self, image: Any, target: Any) -> Tuple[Any, Any]:
1098
+ for t in self.transforms:
1099
+ image, target = t(image, target)
1100
+ return image, target
1101
+
1102
+ class _OD_PairedToTensor:
1103
+ """Converts a PIL Image to Tensor, passes targets dict through."""
1104
+ def __call__(self, image: Image.Image, target: Dict[str, Any]) -> Tuple[torch.Tensor, Dict[str, Any]]:
1105
+ return TF.to_tensor(image), target
1106
+
1107
+ class _OD_PairedNormalize:
1108
+ """Normalizes the image tensor and leaves the target dict untouched."""
1109
+ def __init__(self, mean: List[float], std: List[float]):
1110
+ self.normalize = transforms.Normalize(mean, std)
1111
+
1112
+ def __call__(self, image: torch.Tensor, target: Dict[str, Any]) -> Tuple[torch.Tensor, Dict[str, Any]]:
1113
+ image_normalized = self.normalize(image)
1114
+ return image_normalized, target
1115
+
1116
+ class _OD_PairedRandomHorizontalFlip:
1117
+ """Applies the same random horizontal flip to both image and targets['boxes']."""
1118
+ def __init__(self, p: float = 0.5):
1119
+ self.p = p
1120
+
1121
+ def __call__(self, image: Image.Image, target: Dict[str, Any]) -> Tuple[Image.Image, Dict[str, Any]]:
1122
+ if random.random() < self.p:
1123
+ w, h = image.size
1124
+ # Use new variable names to avoid linter confusion
1125
+ flipped_image = TF.hflip(image) # type: ignore
1126
+
1127
+ # Flip boxes
1128
+ boxes = target[ObjectDetectionKeys.BOXES].clone() # [N, 4]
1129
+
1130
+ # xmin' = w - xmax
1131
+ # xmax' = w - xmin
1132
+ boxes[:, 0] = w - target[ObjectDetectionKeys.BOXES][:, 2] # xmin'
1133
+ boxes[:, 2] = w - target[ObjectDetectionKeys.BOXES][:, 0] # xmax'
1134
+ target[ObjectDetectionKeys.BOXES] = boxes
1135
+
1136
+ return flipped_image, target # type: ignore
1137
+
1138
+ return image, target
1139
+
1140
+
1141
+ class ObjectDetectionDatasetMaker(_BaseMaker):
1142
+ """
1143
+ Creates processed PyTorch datasets for object detection from image
1144
+ and JSON annotation folders.
1145
+
1146
+ This maker finds all matching image-annotation pairs from two directories,
1147
+ splits them, and applies identical transformations (including augmentations)
1148
+ to both the image and its corresponding target dictionary.
1149
+
1150
+ The `DragonFastRCNN` model expects a list of images and a list of targets,
1151
+ so this class provides a `collate_fn` to be used with a DataLoader.
1152
+
1153
+ Workflow:
1154
+ 1. `maker = ObjectDetectionDatasetMaker.from_folders(img_dir, ann_dir)`
1155
+ 2. `maker.set_class_map({'background': 0, 'person': 1, 'car': 2})`
1156
+ 3. `maker.split_data(val_size=0.2)`
1157
+ 4. `maker.configure_transforms()`
1158
+ 5. `train_ds, val_ds = maker.get_datasets()`
1159
+ 6. `collate_fn = maker.collate_fn`
1160
+ 7. `train_loader = DataLoader(train_ds, ..., collate_fn=collate_fn)`
1161
+ """
1162
+ IMG_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff')
1163
+
1164
+ def __init__(self):
1165
+ """
1166
+ Typically not called directly. Use the class method `from_folders()` to create an instance.
1167
+ """
1168
+ super().__init__()
1169
+ self.image_paths: List[Path] = []
1170
+ self.annotation_paths: List[Path] = []
1171
+ self.class_map: Dict[str, int] = {}
1172
+
1173
+ self._is_split = False
1174
+ self._are_transforms_configured = False
1175
+ self.train_transform: Optional[Callable] = None
1176
+ self.val_transform: Optional[Callable] = None
1177
+ self._val_recipe_components: Optional[Dict[str, Any]] = None
1178
+ self._has_mean_std: bool = False
1179
+
1180
+ @classmethod
1181
+ def from_folders(cls, image_dir: Union[str, Path], annotation_dir: Union[str, Path]) -> 'ObjectDetectionDatasetMaker':
1182
+ """
1183
+ Creates a maker instance by loading all matching image-annotation pairs
1184
+ from two corresponding directories.
1185
+
1186
+ This method assumes that for an image `images/img_001.png`, there
1187
+ is a corresponding annotation `annotations/img_001.json`.
1188
+
1189
+ The JSON file must contain "boxes" and "labels" keys:
1190
+ `{"boxes": [[x1,y1,x2,y2], ...], "labels": [1, 2, ...]}`
1191
+
1192
+ Args:
1193
+ image_dir (str | Path): Path to the directory containing input images.
1194
+ annotation_dir (str | Path): Path to the directory containing .json
1195
+ annotation files.
1196
+
1197
+ Returns:
1198
+ ObjectDetectionDatasetMaker: A new instance with all pairs loaded.
1199
+ """
1200
+ maker = cls()
1201
+ img_path_obj = make_fullpath(image_dir, enforce="directory")
1202
+ ann_path_obj = make_fullpath(annotation_dir, enforce="directory")
1203
+
1204
+ # Find all images
1205
+ image_files = sorted([
1206
+ p for p in img_path_obj.glob('*.*')
1207
+ if p.suffix.lower() in cls.IMG_EXTENSIONS
1208
+ ])
1209
+
1210
+ if not image_files:
1211
+ _LOGGER.error(f"No images with extensions {cls.IMG_EXTENSIONS} found in {image_dir}")
1212
+ raise FileNotFoundError()
1213
+
1214
+ _LOGGER.info(f"Found {len(image_files)} images. Searching for matching .json annotations in {annotation_dir}...")
1215
+
1216
+ good_img_paths = []
1217
+ good_ann_paths = []
1218
+
1219
+ for img_file in image_files:
1220
+ # Find annotation with same stem + .json
1221
+ ann_file = ann_path_obj / (img_file.stem + ".json")
1222
+
1223
+ if ann_file.exists():
1224
+ good_img_paths.append(img_file)
1225
+ good_ann_paths.append(ann_file)
1226
+ else:
1227
+ _LOGGER.warning(f"No corresponding .json annotation found for image: {img_file.name}")
1228
+
1229
+ if not good_img_paths:
1230
+ _LOGGER.error("No matching image-annotation pairs were found.")
1231
+ raise FileNotFoundError()
1232
+
1233
+ _LOGGER.info(f"Successfully found {len(good_img_paths)} image-annotation pairs.")
1234
+ maker.image_paths = good_img_paths
1235
+ maker.annotation_paths = good_ann_paths
1236
+
1237
+ return maker
1238
+
1239
+ @staticmethod
1240
+ def inspect_folder(path: Union[str, Path]):
1241
+ """
1242
+ Logs a report of the types, sizes, and channels of image files
1243
+ found in the directory.
1244
+ """
1245
+ VisionDatasetMaker.inspect_folder(path)
1246
+
1247
+ def set_class_map(self, class_map: Dict[str, int]) -> 'ObjectDetectionDatasetMaker':
1248
+ """
1249
+ Sets a map of class_name -> pixel_value. This is used by the
1250
+ MLTrainer for clear evaluation reports.
1251
+
1252
+ **Important:** For object detection models, 'background' MUST
1253
+ be included as class 0.
1254
+ Example: `{'background': 0, 'person': 1, 'car': 2}`
1255
+
1256
+ Args:
1257
+ class_map (Dict[str, int]): A dictionary mapping the string name
1258
+ to its integer label.
1259
+ """
1260
+ if 'background' not in class_map or class_map['background'] != 0:
1261
+ _LOGGER.warning("Object detection class map should include 'background' mapped to 0.")
1262
+
1263
+ self.class_map = class_map
1264
+ _LOGGER.info(f"Class map set: {class_map}")
1265
+ return self
1266
+
1267
+ @property
1268
+ def classes(self) -> List[str]:
1269
+ """Returns the list of class names, if set."""
1270
+ if self.class_map:
1271
+ return list(self.class_map.keys())
1272
+ return []
1273
+
1274
+ def split_data(self, val_size: float = 0.2, test_size: float = 0.0,
1275
+ random_state: Optional[int] = 42) -> 'ObjectDetectionDatasetMaker':
1276
+ """
1277
+ Splits the loaded image-annotation pairs into train, validation, and test sets.
1278
+
1279
+ Args:
1280
+ val_size (float): Proportion of the dataset to reserve for validation.
1281
+ test_size (float): Proportion of the dataset to reserve for testing.
1282
+ random_state (int | None): Seed for reproducible splits.
1283
+
1284
+ Returns:
1285
+ ObjectDetectionDatasetMaker: The same instance, now with datasets created.
1286
+ """
1287
+ if self._is_split:
1288
+ _LOGGER.warning("Data has already been split.")
1289
+ return self
1290
+
1291
+ if val_size + test_size >= 1.0:
1292
+ _LOGGER.error("The sum of val_size and test_size must be less than 1.")
1293
+ raise ValueError()
1294
+
1295
+ if not self.image_paths:
1296
+ _LOGGER.error("There is no data to split. Use .from_folders() first.")
1297
+ raise RuntimeError()
1298
+
1299
+ indices = list(range(len(self.image_paths)))
1300
+
1301
+ # Split indices
1302
+ train_indices, val_test_indices = train_test_split(
1303
+ indices, test_size=(val_size + test_size), random_state=random_state
1304
+ )
1305
+
1306
+ # Helper to get paths from indices
1307
+ def get_paths(idx_list):
1308
+ return [self.image_paths[i] for i in idx_list], [self.annotation_paths[i] for i in idx_list]
1309
+
1310
+ train_imgs, train_anns = get_paths(train_indices)
1311
+
1312
+ if test_size > 0:
1313
+ val_indices, test_indices = train_test_split(
1314
+ val_test_indices, test_size=(test_size / (val_size + test_size)),
1315
+ random_state=random_state
1316
+ )
1317
+ val_imgs, val_anns = get_paths(val_indices)
1318
+ test_imgs, test_anns = get_paths(test_indices)
1319
+
1320
+ self._test_dataset = _ObjectDetectionDataset(test_imgs, test_anns, transform=None)
1321
+ self._test_dataset.classes = self.classes # type: ignore
1322
+ _LOGGER.info(f"Test set created with {len(self._test_dataset)} images.")
1323
+ else:
1324
+ val_imgs, val_anns = get_paths(val_test_indices)
1325
+
1326
+ self._train_dataset = _ObjectDetectionDataset(train_imgs, train_anns, transform=None)
1327
+ self._val_dataset = _ObjectDetectionDataset(val_imgs, val_anns, transform=None)
1328
+
1329
+ # Propagate class names to datasets for MLTrainer
1330
+ self._train_dataset.classes = self.classes # type: ignore
1331
+ self._val_dataset.classes = self.classes # type: ignore
1332
+
1333
+ self._is_split = True
1334
+ _LOGGER.info(f"Data split into: \n- Training: {len(self._train_dataset)} images \n- Validation: {len(self._val_dataset)} images")
1335
+ return self
1336
+
1337
+ def configure_transforms(self,
1338
+ mean: Optional[List[float]] = [0.485, 0.456, 0.406],
1339
+ std: Optional[List[float]] = [0.229, 0.224, 0.225]) -> 'ObjectDetectionDatasetMaker':
1340
+ """
1341
+ Configures and applies the image and target transformations.
1342
+
1343
+ This method must be called AFTER data is split.
1344
+
1345
+ For object detection models like Faster R-CNN, images are NOT
1346
+ resized or cropped, as the model handles variable input sizes.
1347
+ Transforms are limited to augmentation (flip), ToTensor, and Normalize.
1348
+
1349
+ Args:
1350
+ mean (List[float] | None): The mean values for image normalization.
1351
+ std (List[float] | None): The std dev values for image normalization.
1352
+
1353
+ Returns:
1354
+ ObjectDetectionDatasetMaker: The same instance, with transforms applied.
1355
+ """
1356
+ if not self._is_split:
1357
+ _LOGGER.error("Transforms must be configured AFTER splitting data. Call .split_data() first.")
1358
+ raise RuntimeError()
1359
+
1360
+ if (mean is None and std is not None) or (mean is not None and std is None):
1361
+ _LOGGER.error(f"'mean' and 'std' must be both None or both defined, but only one was provided.")
1362
+ raise ValueError()
1363
+
1364
+ if mean is not None and std is not None:
1365
+ # --- Store components for validation recipe ---
1366
+ self._val_recipe_components = {
1367
+ VisionTransformRecipeKeys.MEAN: mean,
1368
+ VisionTransformRecipeKeys.STD: std
1369
+ }
1370
+ self._has_mean_std = True
1371
+
1372
+ if self._has_mean_std:
1373
+ # --- Validation/Test Pipeline (Deterministic) ---
1374
+ self.val_transform = _OD_PairedCompose([
1375
+ _OD_PairedToTensor(),
1376
+ _OD_PairedNormalize(mean, std) # type: ignore
1377
+ ])
1378
+
1379
+ # --- Training Pipeline (Augmentation) ---
1380
+ self.train_transform = _OD_PairedCompose([
1381
+ _OD_PairedRandomHorizontalFlip(p=0.5),
1382
+ _OD_PairedToTensor(),
1383
+ _OD_PairedNormalize(mean, std) # type: ignore
1384
+ ])
1385
+ else:
1386
+ # --- Validation/Test Pipeline (Deterministic) ---
1387
+ self.val_transform = _OD_PairedCompose([
1388
+ _OD_PairedToTensor()
1389
+ ])
1390
+
1391
+ # --- Training Pipeline (Augmentation) ---
1392
+ self.train_transform = _OD_PairedCompose([
1393
+ _OD_PairedRandomHorizontalFlip(p=0.5),
1394
+ _OD_PairedToTensor()
1395
+ ])
1396
+
1397
+ # --- Apply Transforms to the Datasets ---
1398
+ self._train_dataset.transform = self.train_transform # type: ignore
1399
+ self._val_dataset.transform = self.val_transform # type: ignore
1400
+ if self._test_dataset:
1401
+ self._test_dataset.transform = self.val_transform # type: ignore
1402
+
1403
+ self._are_transforms_configured = True
1404
+ _LOGGER.info("Paired object detection transforms configured and applied.")
1405
+ return self
1406
+
1407
+ def get_datasets(self) -> Tuple[Dataset, ...]:
1408
+ """
1409
+ Returns the final train, validation, and optional test datasets.
1410
+
1411
+ Raises:
1412
+ RuntimeError: If called before data is split.
1413
+ RuntimeError: If called before transforms are configured.
1414
+ """
1415
+ if not self._is_split:
1416
+ _LOGGER.error("Data has not been split. Call .split_data() first.")
1417
+ raise RuntimeError()
1418
+ if not self._are_transforms_configured:
1419
+ _LOGGER.error("Transforms have not been configured. Call .configure_transforms() first.")
1420
+ raise RuntimeError()
1421
+
1422
+ if self._test_dataset:
1423
+ return self._train_dataset, self._val_dataset, self._test_dataset
1424
+ return self._train_dataset, self._val_dataset
1425
+
1426
+ @property
1427
+ def collate_fn(self) -> Callable:
1428
+ """
1429
+ Returns the collate function required by a DataLoader for this
1430
+ dataset. This function ensures that images and targets are
1431
+ batched as separate lists.
1432
+ """
1433
+ return _od_collate_fn
1434
+
1435
+ def save_transform_recipe(self, filepath: Union[str, Path]) -> None:
1436
+ """
1437
+ Saves the validation transform pipeline as a JSON recipe file.
1438
+
1439
+ For object detection, this recipe only includes ToTensor and
1440
+ Normalize, as resizing is handled by the model.
1441
+
1442
+ Args:
1443
+ filepath (str | Path): The path to save the .json recipe file.
1444
+ """
1445
+ if not self._are_transforms_configured:
1446
+ _LOGGER.error("Transforms are not configured. Call .configure_transforms() first.")
1447
+ raise RuntimeError()
1448
+
1449
+ components = self._val_recipe_components
1450
+
1451
+ # validate path
1452
+ file_path = make_fullpath(filepath, make=True, enforce="file")
1453
+
1454
+ # Add standard transforms
1455
+ recipe: Dict[str, Any] = {
1456
+ VisionTransformRecipeKeys.TASK: "object_detection",
1457
+ VisionTransformRecipeKeys.PIPELINE: [
1458
+ {VisionTransformRecipeKeys.NAME: "ToTensor", "kwargs": {}},
1459
+ ]
1460
+ }
1461
+
1462
+ if self._has_mean_std and components:
1463
+ recipe[VisionTransformRecipeKeys.PIPELINE].append(
1464
+ {VisionTransformRecipeKeys.NAME: "Normalize", "kwargs": {
1465
+ "mean": components[VisionTransformRecipeKeys.MEAN],
1466
+ "std": components[VisionTransformRecipeKeys.STD]
1467
+ }}
1468
+ )
1469
+
1470
+ # Save the file
1471
+ save_recipe(recipe, file_path)
1472
+
1473
+ def __repr__(self) -> str:
1474
+ s = f"<{self.__class__.__name__}>:\n"
1475
+ s += f" Total Image-Annotation Pairs: {len(self.image_paths)}\n"
1476
+ s += f" Split: {self._is_split}\n"
1477
+ s += f" Transforms Configured: {self._are_transforms_configured}\n"
1478
+
1479
+ if self.class_map:
1480
+ s += f" Classes ({len(self.class_map)}): {list(self.class_map.keys())}\n"
1481
+
1482
+ if self._is_split:
1483
+ train_len = len(self._train_dataset) if self._train_dataset else 0
1484
+ val_len = len(self._val_dataset) if self._val_dataset else 0
1485
+ test_len = len(self._test_dataset) if self._test_dataset else 0
1486
+ s += f" Datasets (Train/Val/Test): {train_len} / {val_len} / {test_len}\n"
1487
+
1488
+ return s
1489
+
1490
+
1491
+ def info():
1492
+ _script_info(__all__)