dgenerate-ultralytics-headless 8.3.214__py3-none-any.whl → 8.3.248__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 (236) hide show
  1. {dgenerate_ultralytics_headless-8.3.214.dist-info → dgenerate_ultralytics_headless-8.3.248.dist-info}/METADATA +13 -14
  2. dgenerate_ultralytics_headless-8.3.248.dist-info/RECORD +298 -0
  3. tests/__init__.py +5 -7
  4. tests/conftest.py +8 -15
  5. tests/test_cli.py +1 -1
  6. tests/test_cuda.py +5 -8
  7. tests/test_engine.py +1 -1
  8. tests/test_exports.py +57 -12
  9. tests/test_integrations.py +4 -4
  10. tests/test_python.py +84 -53
  11. tests/test_solutions.py +160 -151
  12. ultralytics/__init__.py +1 -1
  13. ultralytics/cfg/__init__.py +56 -62
  14. ultralytics/cfg/datasets/Argoverse.yaml +7 -6
  15. ultralytics/cfg/datasets/DOTAv1.5.yaml +1 -1
  16. ultralytics/cfg/datasets/DOTAv1.yaml +1 -1
  17. ultralytics/cfg/datasets/ImageNet.yaml +1 -1
  18. ultralytics/cfg/datasets/VOC.yaml +15 -16
  19. ultralytics/cfg/datasets/african-wildlife.yaml +1 -1
  20. ultralytics/cfg/datasets/coco-pose.yaml +21 -0
  21. ultralytics/cfg/datasets/coco128-seg.yaml +1 -1
  22. ultralytics/cfg/datasets/coco8-pose.yaml +21 -0
  23. ultralytics/cfg/datasets/dog-pose.yaml +28 -0
  24. ultralytics/cfg/datasets/dota8-multispectral.yaml +1 -1
  25. ultralytics/cfg/datasets/dota8.yaml +2 -2
  26. ultralytics/cfg/datasets/hand-keypoints.yaml +26 -2
  27. ultralytics/cfg/datasets/kitti.yaml +27 -0
  28. ultralytics/cfg/datasets/lvis.yaml +5 -5
  29. ultralytics/cfg/datasets/open-images-v7.yaml +1 -1
  30. ultralytics/cfg/datasets/tiger-pose.yaml +16 -0
  31. ultralytics/cfg/datasets/xView.yaml +16 -16
  32. ultralytics/cfg/default.yaml +1 -1
  33. ultralytics/cfg/models/11/yolo11-pose.yaml +1 -1
  34. ultralytics/cfg/models/11/yoloe-11-seg.yaml +2 -2
  35. ultralytics/cfg/models/11/yoloe-11.yaml +2 -2
  36. ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +1 -1
  37. ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +1 -1
  38. ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +1 -1
  39. ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +1 -1
  40. ultralytics/cfg/models/v10/yolov10b.yaml +2 -2
  41. ultralytics/cfg/models/v10/yolov10l.yaml +2 -2
  42. ultralytics/cfg/models/v10/yolov10m.yaml +2 -2
  43. ultralytics/cfg/models/v10/yolov10n.yaml +2 -2
  44. ultralytics/cfg/models/v10/yolov10s.yaml +2 -2
  45. ultralytics/cfg/models/v10/yolov10x.yaml +2 -2
  46. ultralytics/cfg/models/v3/yolov3-tiny.yaml +1 -1
  47. ultralytics/cfg/models/v6/yolov6.yaml +1 -1
  48. ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +9 -6
  49. ultralytics/cfg/models/v8/yoloe-v8.yaml +9 -6
  50. ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +1 -1
  51. ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +1 -1
  52. ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +2 -2
  53. ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +2 -2
  54. ultralytics/cfg/models/v8/yolov8-ghost.yaml +2 -2
  55. ultralytics/cfg/models/v8/yolov8-obb.yaml +1 -1
  56. ultralytics/cfg/models/v8/yolov8-p2.yaml +1 -1
  57. ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +1 -1
  58. ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +1 -1
  59. ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +1 -1
  60. ultralytics/cfg/models/v8/yolov8-world.yaml +1 -1
  61. ultralytics/cfg/models/v8/yolov8-worldv2.yaml +6 -6
  62. ultralytics/cfg/models/v9/yolov9s.yaml +1 -1
  63. ultralytics/data/__init__.py +4 -4
  64. ultralytics/data/annotator.py +3 -4
  65. ultralytics/data/augment.py +285 -475
  66. ultralytics/data/base.py +18 -26
  67. ultralytics/data/build.py +147 -25
  68. ultralytics/data/converter.py +36 -46
  69. ultralytics/data/dataset.py +46 -74
  70. ultralytics/data/loaders.py +42 -49
  71. ultralytics/data/split.py +5 -6
  72. ultralytics/data/split_dota.py +8 -15
  73. ultralytics/data/utils.py +34 -43
  74. ultralytics/engine/exporter.py +319 -237
  75. ultralytics/engine/model.py +148 -188
  76. ultralytics/engine/predictor.py +29 -38
  77. ultralytics/engine/results.py +177 -311
  78. ultralytics/engine/trainer.py +83 -59
  79. ultralytics/engine/tuner.py +23 -34
  80. ultralytics/engine/validator.py +39 -22
  81. ultralytics/hub/__init__.py +16 -19
  82. ultralytics/hub/auth.py +6 -12
  83. ultralytics/hub/google/__init__.py +7 -10
  84. ultralytics/hub/session.py +15 -25
  85. ultralytics/hub/utils.py +5 -8
  86. ultralytics/models/__init__.py +1 -1
  87. ultralytics/models/fastsam/__init__.py +1 -1
  88. ultralytics/models/fastsam/model.py +8 -10
  89. ultralytics/models/fastsam/predict.py +17 -29
  90. ultralytics/models/fastsam/utils.py +1 -2
  91. ultralytics/models/fastsam/val.py +5 -7
  92. ultralytics/models/nas/__init__.py +1 -1
  93. ultralytics/models/nas/model.py +5 -8
  94. ultralytics/models/nas/predict.py +7 -9
  95. ultralytics/models/nas/val.py +1 -2
  96. ultralytics/models/rtdetr/__init__.py +1 -1
  97. ultralytics/models/rtdetr/model.py +5 -8
  98. ultralytics/models/rtdetr/predict.py +15 -19
  99. ultralytics/models/rtdetr/train.py +10 -13
  100. ultralytics/models/rtdetr/val.py +21 -23
  101. ultralytics/models/sam/__init__.py +15 -2
  102. ultralytics/models/sam/amg.py +14 -20
  103. ultralytics/models/sam/build.py +26 -19
  104. ultralytics/models/sam/build_sam3.py +377 -0
  105. ultralytics/models/sam/model.py +29 -32
  106. ultralytics/models/sam/modules/blocks.py +83 -144
  107. ultralytics/models/sam/modules/decoders.py +19 -37
  108. ultralytics/models/sam/modules/encoders.py +44 -101
  109. ultralytics/models/sam/modules/memory_attention.py +16 -30
  110. ultralytics/models/sam/modules/sam.py +200 -73
  111. ultralytics/models/sam/modules/tiny_encoder.py +64 -83
  112. ultralytics/models/sam/modules/transformer.py +18 -28
  113. ultralytics/models/sam/modules/utils.py +174 -50
  114. ultralytics/models/sam/predict.py +2248 -350
  115. ultralytics/models/sam/sam3/__init__.py +3 -0
  116. ultralytics/models/sam/sam3/decoder.py +546 -0
  117. ultralytics/models/sam/sam3/encoder.py +529 -0
  118. ultralytics/models/sam/sam3/geometry_encoders.py +415 -0
  119. ultralytics/models/sam/sam3/maskformer_segmentation.py +286 -0
  120. ultralytics/models/sam/sam3/model_misc.py +199 -0
  121. ultralytics/models/sam/sam3/necks.py +129 -0
  122. ultralytics/models/sam/sam3/sam3_image.py +339 -0
  123. ultralytics/models/sam/sam3/text_encoder_ve.py +307 -0
  124. ultralytics/models/sam/sam3/vitdet.py +547 -0
  125. ultralytics/models/sam/sam3/vl_combiner.py +160 -0
  126. ultralytics/models/utils/loss.py +14 -26
  127. ultralytics/models/utils/ops.py +13 -17
  128. ultralytics/models/yolo/__init__.py +1 -1
  129. ultralytics/models/yolo/classify/predict.py +9 -12
  130. ultralytics/models/yolo/classify/train.py +11 -32
  131. ultralytics/models/yolo/classify/val.py +29 -28
  132. ultralytics/models/yolo/detect/predict.py +7 -10
  133. ultralytics/models/yolo/detect/train.py +11 -20
  134. ultralytics/models/yolo/detect/val.py +70 -58
  135. ultralytics/models/yolo/model.py +36 -53
  136. ultralytics/models/yolo/obb/predict.py +5 -14
  137. ultralytics/models/yolo/obb/train.py +11 -14
  138. ultralytics/models/yolo/obb/val.py +39 -36
  139. ultralytics/models/yolo/pose/__init__.py +1 -1
  140. ultralytics/models/yolo/pose/predict.py +6 -21
  141. ultralytics/models/yolo/pose/train.py +10 -15
  142. ultralytics/models/yolo/pose/val.py +38 -57
  143. ultralytics/models/yolo/segment/predict.py +14 -18
  144. ultralytics/models/yolo/segment/train.py +3 -6
  145. ultralytics/models/yolo/segment/val.py +93 -45
  146. ultralytics/models/yolo/world/train.py +8 -14
  147. ultralytics/models/yolo/world/train_world.py +11 -34
  148. ultralytics/models/yolo/yoloe/__init__.py +7 -7
  149. ultralytics/models/yolo/yoloe/predict.py +16 -23
  150. ultralytics/models/yolo/yoloe/train.py +30 -43
  151. ultralytics/models/yolo/yoloe/train_seg.py +5 -10
  152. ultralytics/models/yolo/yoloe/val.py +15 -20
  153. ultralytics/nn/__init__.py +7 -7
  154. ultralytics/nn/autobackend.py +145 -77
  155. ultralytics/nn/modules/__init__.py +60 -60
  156. ultralytics/nn/modules/activation.py +4 -6
  157. ultralytics/nn/modules/block.py +132 -216
  158. ultralytics/nn/modules/conv.py +52 -97
  159. ultralytics/nn/modules/head.py +50 -103
  160. ultralytics/nn/modules/transformer.py +76 -88
  161. ultralytics/nn/modules/utils.py +16 -21
  162. ultralytics/nn/tasks.py +94 -154
  163. ultralytics/nn/text_model.py +40 -67
  164. ultralytics/solutions/__init__.py +12 -12
  165. ultralytics/solutions/ai_gym.py +11 -17
  166. ultralytics/solutions/analytics.py +15 -16
  167. ultralytics/solutions/config.py +5 -6
  168. ultralytics/solutions/distance_calculation.py +10 -13
  169. ultralytics/solutions/heatmap.py +7 -13
  170. ultralytics/solutions/instance_segmentation.py +5 -8
  171. ultralytics/solutions/object_blurrer.py +7 -10
  172. ultralytics/solutions/object_counter.py +12 -19
  173. ultralytics/solutions/object_cropper.py +8 -14
  174. ultralytics/solutions/parking_management.py +33 -31
  175. ultralytics/solutions/queue_management.py +10 -12
  176. ultralytics/solutions/region_counter.py +9 -12
  177. ultralytics/solutions/security_alarm.py +15 -20
  178. ultralytics/solutions/similarity_search.py +10 -15
  179. ultralytics/solutions/solutions.py +75 -74
  180. ultralytics/solutions/speed_estimation.py +7 -10
  181. ultralytics/solutions/streamlit_inference.py +2 -4
  182. ultralytics/solutions/templates/similarity-search.html +7 -18
  183. ultralytics/solutions/trackzone.py +7 -10
  184. ultralytics/solutions/vision_eye.py +5 -8
  185. ultralytics/trackers/__init__.py +1 -1
  186. ultralytics/trackers/basetrack.py +3 -5
  187. ultralytics/trackers/bot_sort.py +10 -27
  188. ultralytics/trackers/byte_tracker.py +14 -30
  189. ultralytics/trackers/track.py +3 -6
  190. ultralytics/trackers/utils/gmc.py +11 -22
  191. ultralytics/trackers/utils/kalman_filter.py +37 -48
  192. ultralytics/trackers/utils/matching.py +12 -15
  193. ultralytics/utils/__init__.py +116 -116
  194. ultralytics/utils/autobatch.py +2 -4
  195. ultralytics/utils/autodevice.py +17 -18
  196. ultralytics/utils/benchmarks.py +32 -46
  197. ultralytics/utils/callbacks/base.py +8 -10
  198. ultralytics/utils/callbacks/clearml.py +5 -13
  199. ultralytics/utils/callbacks/comet.py +32 -46
  200. ultralytics/utils/callbacks/dvc.py +13 -18
  201. ultralytics/utils/callbacks/mlflow.py +4 -5
  202. ultralytics/utils/callbacks/neptune.py +7 -15
  203. ultralytics/utils/callbacks/platform.py +314 -38
  204. ultralytics/utils/callbacks/raytune.py +3 -4
  205. ultralytics/utils/callbacks/tensorboard.py +23 -31
  206. ultralytics/utils/callbacks/wb.py +10 -13
  207. ultralytics/utils/checks.py +99 -76
  208. ultralytics/utils/cpu.py +3 -8
  209. ultralytics/utils/dist.py +8 -12
  210. ultralytics/utils/downloads.py +20 -30
  211. ultralytics/utils/errors.py +6 -14
  212. ultralytics/utils/events.py +2 -4
  213. ultralytics/utils/export/__init__.py +4 -236
  214. ultralytics/utils/export/engine.py +237 -0
  215. ultralytics/utils/export/imx.py +91 -55
  216. ultralytics/utils/export/tensorflow.py +231 -0
  217. ultralytics/utils/files.py +24 -28
  218. ultralytics/utils/git.py +9 -11
  219. ultralytics/utils/instance.py +30 -51
  220. ultralytics/utils/logger.py +212 -114
  221. ultralytics/utils/loss.py +14 -22
  222. ultralytics/utils/metrics.py +126 -155
  223. ultralytics/utils/nms.py +13 -16
  224. ultralytics/utils/ops.py +107 -165
  225. ultralytics/utils/patches.py +33 -21
  226. ultralytics/utils/plotting.py +72 -80
  227. ultralytics/utils/tal.py +25 -39
  228. ultralytics/utils/torch_utils.py +52 -78
  229. ultralytics/utils/tqdm.py +20 -20
  230. ultralytics/utils/triton.py +13 -19
  231. ultralytics/utils/tuner.py +17 -5
  232. dgenerate_ultralytics_headless-8.3.214.dist-info/RECORD +0 -283
  233. {dgenerate_ultralytics_headless-8.3.214.dist-info → dgenerate_ultralytics_headless-8.3.248.dist-info}/WHEEL +0 -0
  234. {dgenerate_ultralytics_headless-8.3.214.dist-info → dgenerate_ultralytics_headless-8.3.248.dist-info}/entry_points.txt +0 -0
  235. {dgenerate_ultralytics_headless-8.3.214.dist-info → dgenerate_ultralytics_headless-8.3.248.dist-info}/licenses/LICENSE +0 -0
  236. {dgenerate_ultralytics_headless-8.3.214.dist-info → dgenerate_ultralytics_headless-8.3.248.dist-info}/top_level.txt +0 -0
ultralytics/data/base.py CHANGED
@@ -21,11 +21,10 @@ from ultralytics.utils.patches import imread
21
21
 
22
22
 
23
23
  class BaseDataset(Dataset):
24
- """
25
- Base dataset class for loading and processing image data.
24
+ """Base dataset class for loading and processing image data.
26
25
 
27
- This class provides core functionality for loading images, caching, and preparing data for training and inference
28
- in object detection tasks.
26
+ This class provides core functionality for loading images, caching, and preparing data for training and inference in
27
+ object detection tasks.
29
28
 
30
29
  Attributes:
31
30
  img_path (str): Path to the folder containing images.
@@ -34,7 +33,8 @@ class BaseDataset(Dataset):
34
33
  single_cls (bool): Whether to treat all objects as a single class.
35
34
  prefix (str): Prefix to print in log messages.
36
35
  fraction (float): Fraction of dataset to utilize.
37
- channels (int): Number of channels in the images (1 for grayscale, 3 for RGB).
36
+ channels (int): Number of channels in the images (1 for grayscale, 3 for color). Color images loaded with OpenCV
37
+ are in BGR channel order.
38
38
  cv2_flag (int): OpenCV flag for reading images.
39
39
  im_files (list[str]): List of image file paths.
40
40
  labels (list[dict]): List of label data dictionaries.
@@ -86,8 +86,7 @@ class BaseDataset(Dataset):
86
86
  fraction: float = 1.0,
87
87
  channels: int = 3,
88
88
  ):
89
- """
90
- Initialize BaseDataset with given configuration and options.
89
+ """Initialize BaseDataset with given configuration and options.
91
90
 
92
91
  Args:
93
92
  img_path (str | list[str]): Path to the folder containing images or list of image paths.
@@ -103,7 +102,8 @@ class BaseDataset(Dataset):
103
102
  single_cls (bool): If True, single class training is used.
104
103
  classes (list[int], optional): List of included classes.
105
104
  fraction (float): Fraction of dataset to utilize.
106
- channels (int): Number of channels in the images (1 for grayscale, 3 for RGB).
105
+ channels (int): Number of channels in the images (1 for grayscale, 3 for color). Color images loaded with
106
+ OpenCV are in BGR channel order.
107
107
  """
108
108
  super().__init__()
109
109
  self.img_path = img_path
@@ -148,8 +148,7 @@ class BaseDataset(Dataset):
148
148
  self.transforms = self.build_transforms(hyp=hyp)
149
149
 
150
150
  def get_img_files(self, img_path: str | list[str]) -> list[str]:
151
- """
152
- Read image files from the specified path.
151
+ """Read image files from the specified path.
153
152
 
154
153
  Args:
155
154
  img_path (str | list[str]): Path or list of paths to image directories or files.
@@ -186,8 +185,7 @@ class BaseDataset(Dataset):
186
185
  return im_files
187
186
 
188
187
  def update_labels(self, include_class: list[int] | None) -> None:
189
- """
190
- Update labels to include only specified classes.
188
+ """Update labels to include only specified classes.
191
189
 
192
190
  Args:
193
191
  include_class (list[int], optional): List of classes to include. If None, all classes are included.
@@ -210,8 +208,7 @@ class BaseDataset(Dataset):
210
208
  self.labels[i]["cls"][:, 0] = 0
211
209
 
212
210
  def load_image(self, i: int, rect_mode: bool = True) -> tuple[np.ndarray, tuple[int, int], tuple[int, int]]:
213
- """
214
- Load an image from dataset index 'i'.
211
+ """Load an image from dataset index 'i'.
215
212
 
216
213
  Args:
217
214
  i (int): Index of the image to load.
@@ -286,8 +283,7 @@ class BaseDataset(Dataset):
286
283
  np.save(f.as_posix(), imread(self.im_files[i]), allow_pickle=False)
287
284
 
288
285
  def check_cache_disk(self, safety_margin: float = 0.5) -> bool:
289
- """
290
- Check if there's enough disk space for caching images.
286
+ """Check if there's enough disk space for caching images.
291
287
 
292
288
  Args:
293
289
  safety_margin (float): Safety margin factor for disk space calculation.
@@ -307,10 +303,10 @@ class BaseDataset(Dataset):
307
303
  b += im.nbytes
308
304
  if not os.access(Path(im_file).parent, os.W_OK):
309
305
  self.cache = None
310
- LOGGER.warning(f"{self.prefix}Skipping caching images to disk, directory not writeable")
306
+ LOGGER.warning(f"{self.prefix}Skipping caching images to disk, directory not writable")
311
307
  return False
312
308
  disk_required = b * self.ni / n * (1 + safety_margin) # bytes required to cache dataset to disk
313
- total, used, free = shutil.disk_usage(Path(self.im_files[0]).parent)
309
+ total, _used, free = shutil.disk_usage(Path(self.im_files[0]).parent)
314
310
  if disk_required > free:
315
311
  self.cache = None
316
312
  LOGGER.warning(
@@ -322,8 +318,7 @@ class BaseDataset(Dataset):
322
318
  return True
323
319
 
324
320
  def check_cache_ram(self, safety_margin: float = 0.5) -> bool:
325
- """
326
- Check if there's enough RAM for caching images.
321
+ """Check if there's enough RAM for caching images.
327
322
 
328
323
  Args:
329
324
  safety_margin (float): Safety margin factor for RAM calculation.
@@ -381,8 +376,7 @@ class BaseDataset(Dataset):
381
376
  return self.transforms(self.get_image_and_label(index))
382
377
 
383
378
  def get_image_and_label(self, index: int) -> dict[str, Any]:
384
- """
385
- Get and return label information from the dataset.
379
+ """Get and return label information from the dataset.
386
380
 
387
381
  Args:
388
382
  index (int): Index of the image to retrieve.
@@ -410,8 +404,7 @@ class BaseDataset(Dataset):
410
404
  return label
411
405
 
412
406
  def build_transforms(self, hyp: dict[str, Any] | None = None):
413
- """
414
- Users can customize augmentations here.
407
+ """Users can customize augmentations here.
415
408
 
416
409
  Examples:
417
410
  >>> if self.augment:
@@ -424,8 +417,7 @@ class BaseDataset(Dataset):
424
417
  raise NotImplementedError
425
418
 
426
419
  def get_labels(self) -> list[dict[str, Any]]:
427
- """
428
- Users can customize their own format here.
420
+ """Users can customize their own format here.
429
421
 
430
422
  Examples:
431
423
  Ensure output is a dictionary with the following keys:
ultralytics/data/build.py CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ import math
5
6
  import os
6
7
  import random
7
8
  from collections.abc import Iterator
@@ -11,8 +12,9 @@ from urllib.parse import urlsplit
11
12
 
12
13
  import numpy as np
13
14
  import torch
15
+ import torch.distributed as dist
14
16
  from PIL import Image
15
- from torch.utils.data import dataloader, distributed
17
+ from torch.utils.data import Dataset, dataloader, distributed
16
18
 
17
19
  from ultralytics.cfg import IterableSimpleNamespace
18
20
  from ultralytics.data.dataset import GroundingDataset, YOLODataset, YOLOMultiModalDataset
@@ -33,8 +35,7 @@ from ultralytics.utils.torch_utils import TORCH_2_0
33
35
 
34
36
 
35
37
  class InfiniteDataLoader(dataloader.DataLoader):
36
- """
37
- Dataloader that reuses workers for infinite iteration.
38
+ """DataLoader that reuses workers for infinite iteration.
38
39
 
39
40
  This dataloader extends the PyTorch DataLoader to provide infinite recycling of workers, which improves efficiency
40
41
  for training loops that need to iterate through the dataset multiple times without recreating workers.
@@ -50,7 +51,7 @@ class InfiniteDataLoader(dataloader.DataLoader):
50
51
  reset: Reset the iterator, useful when modifying dataset settings during training.
51
52
 
52
53
  Examples:
53
- Create an infinite dataloader for training
54
+ Create an infinite DataLoader for training
54
55
  >>> dataset = YOLODataset(...)
55
56
  >>> dataloader = InfiniteDataLoader(dataset, batch_size=16, shuffle=True)
56
57
  >>> for batch in dataloader: # Infinite iteration
@@ -75,7 +76,7 @@ class InfiniteDataLoader(dataloader.DataLoader):
75
76
  yield next(self.iterator)
76
77
 
77
78
  def __del__(self):
78
- """Ensure that workers are properly terminated when the dataloader is deleted."""
79
+ """Ensure that workers are properly terminated when the DataLoader is deleted."""
79
80
  try:
80
81
  if not hasattr(self.iterator, "_workers"):
81
82
  return
@@ -92,11 +93,10 @@ class InfiniteDataLoader(dataloader.DataLoader):
92
93
 
93
94
 
94
95
  class _RepeatSampler:
95
- """
96
- Sampler that repeats forever for infinite iteration.
96
+ """Sampler that repeats forever for infinite iteration.
97
97
 
98
- This sampler wraps another sampler and yields its contents indefinitely, allowing for infinite iteration
99
- over a dataset without recreating the sampler.
98
+ This sampler wraps another sampler and yields its contents indefinitely, allowing for infinite iteration over a
99
+ dataset without recreating the sampler.
100
100
 
101
101
  Attributes:
102
102
  sampler (Dataset.sampler): The sampler to repeat.
@@ -112,7 +112,109 @@ class _RepeatSampler:
112
112
  yield from iter(self.sampler)
113
113
 
114
114
 
115
- def seed_worker(worker_id: int): # noqa
115
+ class ContiguousDistributedSampler(torch.utils.data.Sampler):
116
+ """Distributed sampler that assigns contiguous batch-aligned chunks of the dataset to each GPU.
117
+
118
+ Unlike PyTorch's DistributedSampler which distributes samples in a round-robin fashion (GPU 0 gets indices
119
+ [0,2,4,...], GPU 1 gets [1,3,5,...]), this sampler gives each GPU contiguous batches of the dataset (GPU 0 gets
120
+ batches [0,1,2,...], GPU 1 gets batches [k,k+1,...], etc.). This preserves any ordering or grouping in the original
121
+ dataset, which is critical when samples are organized by similarity (e.g., images sorted by size to enable efficient
122
+ batching without padding when using rect=True).
123
+
124
+ The sampler handles uneven batch counts by distributing remainder batches to the first few ranks, ensuring all
125
+ samples are covered exactly once across all GPUs.
126
+
127
+ Args:
128
+ dataset (Dataset): Dataset to sample from. Must implement __len__.
129
+ num_replicas (int, optional): Number of distributed processes. Defaults to world size.
130
+ batch_size (int, optional): Batch size used by dataloader. Defaults to dataset batch size.
131
+ rank (int, optional): Rank of current process. Defaults to current rank.
132
+ shuffle (bool, optional): Whether to shuffle indices within each rank's chunk. Defaults to False. When True,
133
+ shuffling is deterministic and controlled by set_epoch() for reproducibility.
134
+
135
+ Examples:
136
+ >>> # For validation with size-grouped images
137
+ >>> sampler = ContiguousDistributedSampler(val_dataset, batch_size=32, shuffle=False)
138
+ >>> loader = DataLoader(val_dataset, batch_size=32, sampler=sampler)
139
+ >>> # For training with shuffling
140
+ >>> sampler = ContiguousDistributedSampler(train_dataset, batch_size=32, shuffle=True)
141
+ >>> for epoch in range(num_epochs):
142
+ ... sampler.set_epoch(epoch)
143
+ ... for batch in loader:
144
+ ... ...
145
+ """
146
+
147
+ def __init__(
148
+ self,
149
+ dataset: Dataset,
150
+ num_replicas: int | None = None,
151
+ batch_size: int | None = None,
152
+ rank: int | None = None,
153
+ shuffle: bool = False,
154
+ ) -> None:
155
+ """Initialize the sampler with dataset and distributed training parameters."""
156
+ if num_replicas is None:
157
+ num_replicas = dist.get_world_size() if dist.is_initialized() else 1
158
+ if rank is None:
159
+ rank = dist.get_rank() if dist.is_initialized() else 0
160
+ if batch_size is None:
161
+ batch_size = getattr(dataset, "batch_size", 1)
162
+
163
+ self.num_replicas = num_replicas
164
+ self.rank = rank
165
+ self.epoch = 0
166
+ self.shuffle = shuffle
167
+ self.total_size = len(dataset)
168
+ # ensure all ranks have a sample if batch size >= total size; degenerates to round-robin sampler
169
+ self.batch_size = 1 if batch_size >= self.total_size else batch_size
170
+ self.num_batches = math.ceil(self.total_size / self.batch_size)
171
+
172
+ def _get_rank_indices(self) -> tuple[int, int]:
173
+ """Calculate the start and end sample indices for this rank."""
174
+ # Calculate which batches this rank handles
175
+ batches_per_rank_base = self.num_batches // self.num_replicas
176
+ remainder = self.num_batches % self.num_replicas
177
+
178
+ # This rank gets an extra batch if rank < remainder
179
+ batches_for_this_rank = batches_per_rank_base + (1 if self.rank < remainder else 0)
180
+
181
+ # Calculate starting batch: base position + number of extra batches given to earlier ranks
182
+ start_batch = self.rank * batches_per_rank_base + min(self.rank, remainder)
183
+ end_batch = start_batch + batches_for_this_rank
184
+
185
+ # Convert batch indices to sample indices
186
+ start_idx = start_batch * self.batch_size
187
+ end_idx = min(end_batch * self.batch_size, self.total_size)
188
+
189
+ return start_idx, end_idx
190
+
191
+ def __iter__(self) -> Iterator:
192
+ """Generate indices for this rank's contiguous chunk of the dataset."""
193
+ start_idx, end_idx = self._get_rank_indices()
194
+ indices = list(range(start_idx, end_idx))
195
+
196
+ if self.shuffle:
197
+ g = torch.Generator()
198
+ g.manual_seed(self.epoch)
199
+ indices = [indices[i] for i in torch.randperm(len(indices), generator=g).tolist()]
200
+
201
+ return iter(indices)
202
+
203
+ def __len__(self) -> int:
204
+ """Return the number of samples in this rank's chunk."""
205
+ start_idx, end_idx = self._get_rank_indices()
206
+ return end_idx - start_idx
207
+
208
+ def set_epoch(self, epoch: int) -> None:
209
+ """Set the epoch for this sampler to ensure different shuffling patterns across epochs.
210
+
211
+ Args:
212
+ epoch (int): Epoch number to use as the random seed for shuffling.
213
+ """
214
+ self.epoch = epoch
215
+
216
+
217
+ def seed_worker(worker_id: int) -> None:
116
218
  """Set dataloader worker seed for reproducibility across worker processes."""
117
219
  worker_seed = torch.initial_seed() % 2**32
118
220
  np.random.seed(worker_seed)
@@ -128,7 +230,7 @@ def build_yolo_dataset(
128
230
  rect: bool = False,
129
231
  stride: int = 32,
130
232
  multi_modal: bool = False,
131
- ):
233
+ ) -> Dataset:
132
234
  """Build and return a YOLO dataset based on configuration parameters."""
133
235
  dataset = YOLOMultiModalDataset if multi_modal else YOLODataset
134
236
  return dataset(
@@ -159,7 +261,7 @@ def build_grounding(
159
261
  rect: bool = False,
160
262
  stride: int = 32,
161
263
  max_samples: int = 80,
162
- ):
264
+ ) -> Dataset:
163
265
  """Build and return a GroundingDataset based on configuration parameters."""
164
266
  return GroundingDataset(
165
267
  img_path=img_path,
@@ -181,9 +283,16 @@ def build_grounding(
181
283
  )
182
284
 
183
285
 
184
- def build_dataloader(dataset, batch: int, workers: int, shuffle: bool = True, rank: int = -1, drop_last: bool = False):
185
- """
186
- Create and return an InfiniteDataLoader or DataLoader for training or validation.
286
+ def build_dataloader(
287
+ dataset,
288
+ batch: int,
289
+ workers: int,
290
+ shuffle: bool = True,
291
+ rank: int = -1,
292
+ drop_last: bool = False,
293
+ pin_memory: bool = True,
294
+ ) -> InfiniteDataLoader:
295
+ """Create and return an InfiniteDataLoader or DataLoader for training or validation.
187
296
 
188
297
  Args:
189
298
  dataset (Dataset): Dataset to load data from.
@@ -192,6 +301,7 @@ def build_dataloader(dataset, batch: int, workers: int, shuffle: bool = True, ra
192
301
  shuffle (bool, optional): Whether to shuffle the dataset.
193
302
  rank (int, optional): Process rank in distributed training. -1 for single-GPU training.
194
303
  drop_last (bool, optional): Whether to drop the last incomplete batch.
304
+ pin_memory (bool, optional): Whether to use pinned memory for dataloader.
195
305
 
196
306
  Returns:
197
307
  (InfiniteDataLoader): A dataloader that can be used for training or validation.
@@ -204,7 +314,13 @@ def build_dataloader(dataset, batch: int, workers: int, shuffle: bool = True, ra
204
314
  batch = min(batch, len(dataset))
205
315
  nd = torch.cuda.device_count() # number of CUDA devices
206
316
  nw = min(os.cpu_count() // max(nd, 1), workers) # number of workers
207
- sampler = None if rank == -1 else distributed.DistributedSampler(dataset, shuffle=shuffle)
317
+ sampler = (
318
+ None
319
+ if rank == -1
320
+ else distributed.DistributedSampler(dataset, shuffle=shuffle)
321
+ if shuffle
322
+ else ContiguousDistributedSampler(dataset)
323
+ )
208
324
  generator = torch.Generator()
209
325
  generator.manual_seed(6148914691236517205 + RANK)
210
326
  return InfiniteDataLoader(
@@ -214,7 +330,7 @@ def build_dataloader(dataset, batch: int, workers: int, shuffle: bool = True, ra
214
330
  num_workers=nw,
215
331
  sampler=sampler,
216
332
  prefetch_factor=4 if nw > 0 else None, # increase over default 2
217
- pin_memory=nd > 0,
333
+ pin_memory=nd > 0 and pin_memory,
218
334
  collate_fn=getattr(dataset, "collate_fn", None),
219
335
  worker_init_fn=seed_worker,
220
336
  generator=generator,
@@ -222,9 +338,10 @@ def build_dataloader(dataset, batch: int, workers: int, shuffle: bool = True, ra
222
338
  )
223
339
 
224
340
 
225
- def check_source(source):
226
- """
227
- Check the type of input source and return corresponding flag values.
341
+ def check_source(
342
+ source: str | int | Path | list | tuple | np.ndarray | Image.Image | torch.Tensor,
343
+ ) -> tuple[Any, bool, bool, bool, bool, bool]:
344
+ """Check the type of input source and return corresponding flag values.
228
345
 
229
346
  Args:
230
347
  source (str | int | Path | list | tuple | np.ndarray | PIL.Image | torch.Tensor): The input source to check.
@@ -271,12 +388,17 @@ def check_source(source):
271
388
  return source, webcam, screenshot, from_img, in_memory, tensor
272
389
 
273
390
 
274
- def load_inference_source(source=None, batch: int = 1, vid_stride: int = 1, buffer: bool = False, channels: int = 3):
275
- """
276
- Load an inference source for object detection and apply necessary transformations.
391
+ def load_inference_source(
392
+ source: str | int | Path | list | tuple | np.ndarray | Image.Image | torch.Tensor,
393
+ batch: int = 1,
394
+ vid_stride: int = 1,
395
+ buffer: bool = False,
396
+ channels: int = 3,
397
+ ):
398
+ """Load an inference source for object detection and apply necessary transformations.
277
399
 
278
400
  Args:
279
- source (str | Path | torch.Tensor | PIL.Image | np.ndarray, optional): The input source for inference.
401
+ source (str | Path | list | tuple | torch.Tensor | PIL.Image | np.ndarray): The input source for inference.
280
402
  batch (int, optional): Batch size for dataloaders.
281
403
  vid_stride (int, optional): The frame interval for video sources.
282
404
  buffer (bool, optional): Whether stream frames will be buffered.
@@ -295,7 +417,7 @@ def load_inference_source(source=None, batch: int = 1, vid_stride: int = 1, buff
295
417
  source, stream, screenshot, from_img, in_memory, tensor = check_source(source)
296
418
  source_type = source.source_type if in_memory else SourceTypes(stream, screenshot, from_img, tensor)
297
419
 
298
- # Dataloader
420
+ # DataLoader
299
421
  if tensor:
300
422
  dataset = LoadTensor(source)
301
423
  elif in_memory:
@@ -21,12 +21,11 @@ from ultralytics.utils.files import increment_path
21
21
 
22
22
 
23
23
  def coco91_to_coco80_class() -> list[int]:
24
- """
25
- Convert 91-index COCO class IDs to 80-index COCO class IDs.
24
+ """Convert 91-index COCO class IDs to 80-index COCO class IDs.
26
25
 
27
26
  Returns:
28
- (list[int]): A list of 91 class IDs where the index represents the 80-index class ID and the value
29
- is the corresponding 91-index class ID.
27
+ (list[int]): A list of 91 class IDs where the index represents the 80-index class ID and the value is the
28
+ corresponding 91-index class ID.
30
29
  """
31
30
  return [
32
31
  0,
@@ -124,15 +123,11 @@ def coco91_to_coco80_class() -> list[int]:
124
123
 
125
124
 
126
125
  def coco80_to_coco91_class() -> list[int]:
127
- r"""
128
- Convert 80-index (val2014) to 91-index (paper).
126
+ r"""Convert 80-index (val2014) to 91-index (paper).
129
127
 
130
128
  Returns:
131
129
  (list[int]): A list of 80 class IDs where each value is the corresponding 91-index class ID.
132
130
 
133
- References:
134
- https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/
135
-
136
131
  Examples:
137
132
  >>> import numpy as np
138
133
  >>> a = np.loadtxt("data/coco.names", dtype="str", delimiter="\n")
@@ -143,6 +138,9 @@ def coco80_to_coco91_class() -> list[int]:
143
138
 
144
139
  Convert the COCO to darknet format
145
140
  >>> x2 = [list(b[i] == a).index(True) if any(b[i] == a) else None for i in range(91)]
141
+
142
+ References:
143
+ https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/
146
144
  """
147
145
  return [
148
146
  1,
@@ -236,8 +234,7 @@ def convert_coco(
236
234
  cls91to80: bool = True,
237
235
  lvis: bool = False,
238
236
  ):
239
- """
240
- Convert COCO dataset annotations to a YOLO annotation format suitable for training YOLO models.
237
+ """Convert COCO dataset annotations to a YOLO annotation format suitable for training YOLO models.
241
238
 
242
239
  Args:
243
240
  labels_dir (str, optional): Path to directory containing COCO dataset annotation files.
@@ -308,7 +305,7 @@ def convert_coco(
308
305
  continue
309
306
 
310
307
  cls = coco80[ann["category_id"] - 1] if cls91to80 else ann["category_id"] - 1 # class
311
- box = [cls] + box.tolist()
308
+ box = [cls, *box.tolist()]
312
309
  if box not in bboxes:
313
310
  bboxes.append(box)
314
311
  if use_segments and ann.get("segmentation") is not None:
@@ -321,7 +318,7 @@ def convert_coco(
321
318
  else:
322
319
  s = [j for i in ann["segmentation"] for j in i] # all segments concatenated
323
320
  s = (np.array(s).reshape(-1, 2) / np.array([w, h])).reshape(-1).tolist()
324
- s = [cls] + s
321
+ s = [cls, *s]
325
322
  segments.append(s)
326
323
  if use_keypoints and ann.get("keypoints") is not None:
327
324
  keypoints.append(
@@ -348,8 +345,7 @@ def convert_coco(
348
345
 
349
346
 
350
347
  def convert_segment_masks_to_yolo_seg(masks_dir: str, output_dir: str, classes: int):
351
- """
352
- Convert a dataset of segmentation mask images to the YOLO segmentation format.
348
+ """Convert a dataset of segmentation mask images to the YOLO segmentation format.
353
349
 
354
350
  This function takes the directory containing the binary format mask images and converts them into YOLO segmentation
355
351
  format. The converted masks are saved in the specified output directory.
@@ -357,7 +353,7 @@ def convert_segment_masks_to_yolo_seg(masks_dir: str, output_dir: str, classes:
357
353
  Args:
358
354
  masks_dir (str): The path to the directory where all mask images (png, jpg) are stored.
359
355
  output_dir (str): The path to the directory where the converted YOLO segmentation masks will be stored.
360
- classes (int): Total classes in the dataset i.e. for COCO classes=80
356
+ classes (int): Total number of classes in the dataset, e.g., 80 for COCO.
361
357
 
362
358
  Examples:
363
359
  >>> from ultralytics.data.converter import convert_segment_masks_to_yolo_seg
@@ -424,8 +420,7 @@ def convert_segment_masks_to_yolo_seg(masks_dir: str, output_dir: str, classes:
424
420
 
425
421
 
426
422
  def convert_dota_to_yolo_obb(dota_root_path: str):
427
- """
428
- Convert DOTA dataset annotations to YOLO OBB (Oriented Bounding Box) format.
423
+ """Convert DOTA dataset annotations to YOLO OBB (Oriented Bounding Box) format.
429
424
 
430
425
  The function processes images in the 'train' and 'val' folders of the DOTA dataset. For each image, it reads the
431
426
  associated label from the original labels directory and writes new labels in YOLO OBB format to a new directory.
@@ -517,8 +512,7 @@ def convert_dota_to_yolo_obb(dota_root_path: str):
517
512
 
518
513
 
519
514
  def min_index(arr1: np.ndarray, arr2: np.ndarray):
520
- """
521
- Find a pair of indexes with the shortest distance between two arrays of 2D points.
515
+ """Find a pair of indexes with the shortest distance between two arrays of 2D points.
522
516
 
523
517
  Args:
524
518
  arr1 (np.ndarray): A NumPy array of shape (N, 2) representing N 2D points.
@@ -533,14 +527,14 @@ def min_index(arr1: np.ndarray, arr2: np.ndarray):
533
527
 
534
528
 
535
529
  def merge_multi_segment(segments: list[list]):
536
- """
537
- Merge multiple segments into one list by connecting the coordinates with the minimum distance between each segment.
530
+ """Merge multiple segments into one list by connecting the coordinates with the minimum distance between each
531
+ segment.
538
532
 
539
533
  This function connects these coordinates with a thin line to merge all segments into one.
540
534
 
541
535
  Args:
542
- segments (list[list]): Original segmentations in COCO's JSON file.
543
- Each element is a list of coordinates, like [segmentation1, segmentation2,...].
536
+ segments (list[list]): Original segmentations in COCO's JSON file. Each element is a list of coordinates, like
537
+ [segmentation1, segmentation2,...].
544
538
 
545
539
  Returns:
546
540
  s (list[np.ndarray]): A list of connected segments represented as NumPy arrays.
@@ -584,14 +578,13 @@ def merge_multi_segment(segments: list[list]):
584
578
 
585
579
 
586
580
  def yolo_bbox2segment(im_dir: str | Path, save_dir: str | Path | None = None, sam_model: str = "sam_b.pt", device=None):
587
- """
588
- Convert existing object detection dataset (bounding boxes) to segmentation dataset or oriented bounding box (OBB) in
589
- YOLO format. Generate segmentation data using SAM auto-annotator as needed.
581
+ """Convert existing object detection dataset (bounding boxes) to segmentation dataset or oriented bounding box (OBB)
582
+ in YOLO format. Generate segmentation data using SAM auto-annotator as needed.
590
583
 
591
584
  Args:
592
585
  im_dir (str | Path): Path to image directory to convert.
593
- save_dir (str | Path, optional): Path to save the generated labels, labels will be saved
594
- into `labels-segment` in the same directory level of `im_dir` if save_dir is None.
586
+ save_dir (str | Path, optional): Path to save the generated labels, labels will be saved into `labels-segment`
587
+ in the same directory level of `im_dir` if save_dir is None.
595
588
  sam_model (str): Segmentation model to use for intermediate segmentation data.
596
589
  device (int | str, optional): The specific device to run SAM models.
597
590
 
@@ -648,12 +641,11 @@ def yolo_bbox2segment(im_dir: str | Path, save_dir: str | Path | None = None, sa
648
641
 
649
642
 
650
643
  def create_synthetic_coco_dataset():
651
- """
652
- Create a synthetic COCO dataset with random images based on filenames from label lists.
644
+ """Create a synthetic COCO dataset with random images based on filenames from label lists.
653
645
 
654
- This function downloads COCO labels, reads image filenames from label list files,
655
- creates synthetic images for train2017 and val2017 subsets, and organizes
656
- them in the COCO dataset structure. It uses multithreading to generate images efficiently.
646
+ This function downloads COCO labels, reads image filenames from label list files, creates synthetic images for
647
+ train2017 and val2017 subsets, and organizes them in the COCO dataset structure. It uses multithreading to generate
648
+ images efficiently.
657
649
 
658
650
  Examples:
659
651
  >>> from ultralytics.data.converter import create_synthetic_coco_dataset
@@ -704,11 +696,10 @@ def create_synthetic_coco_dataset():
704
696
 
705
697
 
706
698
  def convert_to_multispectral(path: str | Path, n_channels: int = 10, replace: bool = False, zip: bool = False):
707
- """
708
- Convert RGB images to multispectral images by interpolating across wavelength bands.
699
+ """Convert RGB images to multispectral images by interpolating across wavelength bands.
709
700
 
710
- This function takes RGB images and interpolates them to create multispectral images with a specified number
711
- of channels. It can process either a single image or a directory of images.
701
+ This function takes RGB images and interpolates them to create multispectral images with a specified number of
702
+ channels. It can process either a single image or a directory of images.
712
703
 
713
704
  Args:
714
705
  path (str | Path): Path to an image file or directory containing images to convert.
@@ -730,7 +721,7 @@ def convert_to_multispectral(path: str | Path, n_channels: int = 10, replace: bo
730
721
  path = Path(path)
731
722
  if path.is_dir():
732
723
  # Process directory
733
- im_files = sum((list(path.rglob(f"*.{ext}")) for ext in (IMG_FORMATS - {"tif", "tiff"})), [])
724
+ im_files = [f for ext in (IMG_FORMATS - {"tif", "tiff"}) for f in path.rglob(f"*.{ext}")]
734
725
  for im_path in im_files:
735
726
  try:
736
727
  convert_to_multispectral(im_path, n_channels)
@@ -756,12 +747,11 @@ def convert_to_multispectral(path: str | Path, n_channels: int = 10, replace: bo
756
747
 
757
748
 
758
749
  async def convert_ndjson_to_yolo(ndjson_path: str | Path, output_path: str | Path | None = None) -> Path:
759
- """
760
- Convert NDJSON dataset format to Ultralytics YOLO11 dataset structure.
750
+ """Convert NDJSON dataset format to Ultralytics YOLO11 dataset structure.
761
751
 
762
- This function converts datasets stored in NDJSON (Newline Delimited JSON) format to the standard YOLO
763
- format with separate directories for images and labels. It supports parallel processing for efficient
764
- conversion of large datasets and can download images from URLs if they don't exist locally.
752
+ This function converts datasets stored in NDJSON (Newline Delimited JSON) format to the standard YOLO format with
753
+ separate directories for images and labels. It supports parallel processing for efficient conversion of large
754
+ datasets and can download images from URLs if they don't exist locally.
765
755
 
766
756
  The NDJSON format consists of:
767
757
  - First line: Dataset metadata with class names and configuration
@@ -769,8 +759,8 @@ async def convert_ndjson_to_yolo(ndjson_path: str | Path, output_path: str | Pat
769
759
 
770
760
  Args:
771
761
  ndjson_path (Union[str, Path]): Path to the input NDJSON file containing dataset information.
772
- output_path (Optional[Union[str, Path]], optional): Directory where the converted YOLO dataset
773
- will be saved. If None, uses the parent directory of the NDJSON file. Defaults to None.
762
+ output_path (Optional[Union[str, Path]], optional): Directory where the converted YOLO dataset will be saved. If
763
+ None, uses the parent directory of the NDJSON file. Defaults to None.
774
764
 
775
765
  Returns:
776
766
  (Path): Path to the generated data.yaml file that can be used for YOLO training.