ultralytics 8.1.29__py3-none-any.whl → 8.3.63__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 (247) hide show
  1. tests/__init__.py +22 -0
  2. tests/conftest.py +83 -0
  3. tests/test_cli.py +122 -0
  4. tests/test_cuda.py +155 -0
  5. tests/test_engine.py +131 -0
  6. tests/test_exports.py +216 -0
  7. tests/test_integrations.py +150 -0
  8. tests/test_python.py +615 -0
  9. tests/test_solutions.py +94 -0
  10. ultralytics/__init__.py +11 -8
  11. ultralytics/cfg/__init__.py +569 -131
  12. ultralytics/cfg/datasets/Argoverse.yaml +2 -1
  13. ultralytics/cfg/datasets/DOTAv1.5.yaml +3 -2
  14. ultralytics/cfg/datasets/DOTAv1.yaml +3 -2
  15. ultralytics/cfg/datasets/GlobalWheat2020.yaml +3 -2
  16. ultralytics/cfg/datasets/ImageNet.yaml +2 -1
  17. ultralytics/cfg/datasets/Objects365.yaml +5 -4
  18. ultralytics/cfg/datasets/SKU-110K.yaml +2 -1
  19. ultralytics/cfg/datasets/VOC.yaml +3 -2
  20. ultralytics/cfg/datasets/VisDrone.yaml +6 -5
  21. ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
  22. ultralytics/cfg/datasets/brain-tumor.yaml +23 -0
  23. ultralytics/cfg/datasets/carparts-seg.yaml +3 -2
  24. ultralytics/cfg/datasets/coco-pose.yaml +7 -6
  25. ultralytics/cfg/datasets/coco.yaml +3 -2
  26. ultralytics/cfg/datasets/coco128-seg.yaml +4 -3
  27. ultralytics/cfg/datasets/coco128.yaml +4 -3
  28. ultralytics/cfg/datasets/coco8-pose.yaml +3 -2
  29. ultralytics/cfg/datasets/coco8-seg.yaml +3 -2
  30. ultralytics/cfg/datasets/coco8.yaml +3 -2
  31. ultralytics/cfg/datasets/crack-seg.yaml +3 -2
  32. ultralytics/cfg/datasets/dog-pose.yaml +24 -0
  33. ultralytics/cfg/datasets/dota8.yaml +3 -2
  34. ultralytics/cfg/datasets/hand-keypoints.yaml +26 -0
  35. ultralytics/cfg/datasets/lvis.yaml +1236 -0
  36. ultralytics/cfg/datasets/medical-pills.yaml +22 -0
  37. ultralytics/cfg/datasets/open-images-v7.yaml +2 -1
  38. ultralytics/cfg/datasets/package-seg.yaml +5 -4
  39. ultralytics/cfg/datasets/signature.yaml +21 -0
  40. ultralytics/cfg/datasets/tiger-pose.yaml +3 -2
  41. ultralytics/cfg/datasets/xView.yaml +2 -1
  42. ultralytics/cfg/default.yaml +14 -11
  43. ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +24 -0
  44. ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
  45. ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
  46. ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
  47. ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
  48. ultralytics/cfg/models/11/yolo11.yaml +50 -0
  49. ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +5 -2
  50. ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +5 -2
  51. ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +5 -2
  52. ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +5 -2
  53. ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
  54. ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
  55. ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
  56. ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
  57. ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
  58. ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
  59. ultralytics/cfg/models/v3/yolov3-spp.yaml +5 -2
  60. ultralytics/cfg/models/v3/yolov3-tiny.yaml +5 -2
  61. ultralytics/cfg/models/v3/yolov3.yaml +5 -2
  62. ultralytics/cfg/models/v5/yolov5-p6.yaml +5 -2
  63. ultralytics/cfg/models/v5/yolov5.yaml +5 -2
  64. ultralytics/cfg/models/v6/yolov6.yaml +5 -2
  65. ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +5 -2
  66. ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +5 -2
  67. ultralytics/cfg/models/v8/yolov8-cls.yaml +5 -2
  68. ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +6 -2
  69. ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +6 -2
  70. ultralytics/cfg/models/v8/yolov8-ghost.yaml +5 -2
  71. ultralytics/cfg/models/v8/yolov8-obb.yaml +5 -2
  72. ultralytics/cfg/models/v8/yolov8-p2.yaml +5 -2
  73. ultralytics/cfg/models/v8/yolov8-p6.yaml +10 -7
  74. ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +5 -2
  75. ultralytics/cfg/models/v8/yolov8-pose.yaml +5 -2
  76. ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +5 -2
  77. ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +5 -2
  78. ultralytics/cfg/models/v8/yolov8-seg.yaml +5 -2
  79. ultralytics/cfg/models/v8/yolov8-world.yaml +5 -2
  80. ultralytics/cfg/models/v8/yolov8-worldv2.yaml +5 -2
  81. ultralytics/cfg/models/v8/yolov8.yaml +5 -2
  82. ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
  83. ultralytics/cfg/models/v9/yolov9c.yaml +30 -25
  84. ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
  85. ultralytics/cfg/models/v9/yolov9e.yaml +46 -42
  86. ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
  87. ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
  88. ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
  89. ultralytics/cfg/solutions/default.yaml +24 -0
  90. ultralytics/cfg/trackers/botsort.yaml +8 -5
  91. ultralytics/cfg/trackers/bytetrack.yaml +8 -5
  92. ultralytics/data/__init__.py +14 -3
  93. ultralytics/data/annotator.py +37 -15
  94. ultralytics/data/augment.py +1783 -289
  95. ultralytics/data/base.py +62 -27
  96. ultralytics/data/build.py +37 -8
  97. ultralytics/data/converter.py +196 -36
  98. ultralytics/data/dataset.py +233 -94
  99. ultralytics/data/loaders.py +199 -96
  100. ultralytics/data/split_dota.py +39 -29
  101. ultralytics/data/utils.py +111 -41
  102. ultralytics/engine/__init__.py +1 -1
  103. ultralytics/engine/exporter.py +579 -244
  104. ultralytics/engine/model.py +604 -252
  105. ultralytics/engine/predictor.py +22 -11
  106. ultralytics/engine/results.py +1228 -218
  107. ultralytics/engine/trainer.py +191 -129
  108. ultralytics/engine/tuner.py +18 -18
  109. ultralytics/engine/validator.py +18 -15
  110. ultralytics/hub/__init__.py +31 -13
  111. ultralytics/hub/auth.py +11 -7
  112. ultralytics/hub/google/__init__.py +159 -0
  113. ultralytics/hub/session.py +128 -94
  114. ultralytics/hub/utils.py +20 -21
  115. ultralytics/models/__init__.py +4 -2
  116. ultralytics/models/fastsam/__init__.py +2 -3
  117. ultralytics/models/fastsam/model.py +26 -4
  118. ultralytics/models/fastsam/predict.py +127 -63
  119. ultralytics/models/fastsam/utils.py +1 -44
  120. ultralytics/models/fastsam/val.py +1 -1
  121. ultralytics/models/nas/__init__.py +1 -1
  122. ultralytics/models/nas/model.py +21 -10
  123. ultralytics/models/nas/predict.py +3 -6
  124. ultralytics/models/nas/val.py +4 -4
  125. ultralytics/models/rtdetr/__init__.py +1 -1
  126. ultralytics/models/rtdetr/model.py +1 -1
  127. ultralytics/models/rtdetr/predict.py +6 -8
  128. ultralytics/models/rtdetr/train.py +6 -2
  129. ultralytics/models/rtdetr/val.py +3 -3
  130. ultralytics/models/sam/__init__.py +3 -3
  131. ultralytics/models/sam/amg.py +29 -23
  132. ultralytics/models/sam/build.py +211 -13
  133. ultralytics/models/sam/model.py +91 -30
  134. ultralytics/models/sam/modules/__init__.py +1 -1
  135. ultralytics/models/sam/modules/blocks.py +1129 -0
  136. ultralytics/models/sam/modules/decoders.py +381 -53
  137. ultralytics/models/sam/modules/encoders.py +515 -324
  138. ultralytics/models/sam/modules/memory_attention.py +237 -0
  139. ultralytics/models/sam/modules/sam.py +969 -21
  140. ultralytics/models/sam/modules/tiny_encoder.py +425 -154
  141. ultralytics/models/sam/modules/transformer.py +159 -60
  142. ultralytics/models/sam/modules/utils.py +293 -0
  143. ultralytics/models/sam/predict.py +1263 -132
  144. ultralytics/models/utils/__init__.py +1 -1
  145. ultralytics/models/utils/loss.py +36 -24
  146. ultralytics/models/utils/ops.py +3 -7
  147. ultralytics/models/yolo/__init__.py +3 -3
  148. ultralytics/models/yolo/classify/__init__.py +1 -1
  149. ultralytics/models/yolo/classify/predict.py +7 -8
  150. ultralytics/models/yolo/classify/train.py +17 -22
  151. ultralytics/models/yolo/classify/val.py +8 -4
  152. ultralytics/models/yolo/detect/__init__.py +1 -1
  153. ultralytics/models/yolo/detect/predict.py +3 -5
  154. ultralytics/models/yolo/detect/train.py +11 -4
  155. ultralytics/models/yolo/detect/val.py +90 -52
  156. ultralytics/models/yolo/model.py +14 -9
  157. ultralytics/models/yolo/obb/__init__.py +1 -1
  158. ultralytics/models/yolo/obb/predict.py +2 -2
  159. ultralytics/models/yolo/obb/train.py +5 -3
  160. ultralytics/models/yolo/obb/val.py +41 -23
  161. ultralytics/models/yolo/pose/__init__.py +1 -1
  162. ultralytics/models/yolo/pose/predict.py +3 -5
  163. ultralytics/models/yolo/pose/train.py +2 -2
  164. ultralytics/models/yolo/pose/val.py +51 -17
  165. ultralytics/models/yolo/segment/__init__.py +1 -1
  166. ultralytics/models/yolo/segment/predict.py +3 -5
  167. ultralytics/models/yolo/segment/train.py +2 -2
  168. ultralytics/models/yolo/segment/val.py +60 -19
  169. ultralytics/models/yolo/world/__init__.py +5 -0
  170. ultralytics/models/yolo/world/train.py +92 -0
  171. ultralytics/models/yolo/world/train_world.py +109 -0
  172. ultralytics/nn/__init__.py +1 -1
  173. ultralytics/nn/autobackend.py +228 -93
  174. ultralytics/nn/modules/__init__.py +39 -14
  175. ultralytics/nn/modules/activation.py +21 -0
  176. ultralytics/nn/modules/block.py +526 -66
  177. ultralytics/nn/modules/conv.py +24 -7
  178. ultralytics/nn/modules/head.py +177 -34
  179. ultralytics/nn/modules/transformer.py +6 -5
  180. ultralytics/nn/modules/utils.py +1 -2
  181. ultralytics/nn/tasks.py +226 -82
  182. ultralytics/solutions/__init__.py +30 -1
  183. ultralytics/solutions/ai_gym.py +96 -143
  184. ultralytics/solutions/analytics.py +247 -0
  185. ultralytics/solutions/distance_calculation.py +78 -135
  186. ultralytics/solutions/heatmap.py +93 -247
  187. ultralytics/solutions/object_counter.py +184 -259
  188. ultralytics/solutions/parking_management.py +246 -0
  189. ultralytics/solutions/queue_management.py +112 -0
  190. ultralytics/solutions/region_counter.py +116 -0
  191. ultralytics/solutions/security_alarm.py +144 -0
  192. ultralytics/solutions/solutions.py +178 -0
  193. ultralytics/solutions/speed_estimation.py +86 -174
  194. ultralytics/solutions/streamlit_inference.py +190 -0
  195. ultralytics/solutions/trackzone.py +68 -0
  196. ultralytics/trackers/__init__.py +1 -1
  197. ultralytics/trackers/basetrack.py +32 -13
  198. ultralytics/trackers/bot_sort.py +61 -28
  199. ultralytics/trackers/byte_tracker.py +83 -51
  200. ultralytics/trackers/track.py +21 -6
  201. ultralytics/trackers/utils/__init__.py +1 -1
  202. ultralytics/trackers/utils/gmc.py +62 -48
  203. ultralytics/trackers/utils/kalman_filter.py +166 -35
  204. ultralytics/trackers/utils/matching.py +40 -21
  205. ultralytics/utils/__init__.py +511 -239
  206. ultralytics/utils/autobatch.py +40 -22
  207. ultralytics/utils/benchmarks.py +266 -85
  208. ultralytics/utils/callbacks/__init__.py +1 -1
  209. ultralytics/utils/callbacks/base.py +1 -3
  210. ultralytics/utils/callbacks/clearml.py +7 -6
  211. ultralytics/utils/callbacks/comet.py +39 -17
  212. ultralytics/utils/callbacks/dvc.py +1 -1
  213. ultralytics/utils/callbacks/hub.py +16 -16
  214. ultralytics/utils/callbacks/mlflow.py +28 -24
  215. ultralytics/utils/callbacks/neptune.py +6 -2
  216. ultralytics/utils/callbacks/raytune.py +3 -4
  217. ultralytics/utils/callbacks/tensorboard.py +18 -18
  218. ultralytics/utils/callbacks/wb.py +27 -20
  219. ultralytics/utils/checks.py +172 -100
  220. ultralytics/utils/dist.py +2 -1
  221. ultralytics/utils/downloads.py +40 -34
  222. ultralytics/utils/errors.py +1 -1
  223. ultralytics/utils/files.py +72 -38
  224. ultralytics/utils/instance.py +41 -19
  225. ultralytics/utils/loss.py +83 -55
  226. ultralytics/utils/metrics.py +61 -56
  227. ultralytics/utils/ops.py +94 -89
  228. ultralytics/utils/patches.py +30 -14
  229. ultralytics/utils/plotting.py +600 -269
  230. ultralytics/utils/tal.py +67 -26
  231. ultralytics/utils/torch_utils.py +305 -112
  232. ultralytics/utils/triton.py +2 -1
  233. ultralytics/utils/tuner.py +21 -12
  234. ultralytics-8.3.63.dist-info/METADATA +370 -0
  235. ultralytics-8.3.63.dist-info/RECORD +241 -0
  236. {ultralytics-8.1.29.dist-info → ultralytics-8.3.63.dist-info}/WHEEL +1 -1
  237. ultralytics/data/explorer/__init__.py +0 -5
  238. ultralytics/data/explorer/explorer.py +0 -472
  239. ultralytics/data/explorer/gui/__init__.py +0 -1
  240. ultralytics/data/explorer/gui/dash.py +0 -268
  241. ultralytics/data/explorer/utils.py +0 -166
  242. ultralytics/models/fastsam/prompt.py +0 -357
  243. ultralytics-8.1.29.dist-info/METADATA +0 -373
  244. ultralytics-8.1.29.dist-info/RECORD +0 -197
  245. {ultralytics-8.1.29.dist-info → ultralytics-8.3.63.dist-info}/LICENSE +0 -0
  246. {ultralytics-8.1.29.dist-info → ultralytics-8.3.63.dist-info}/entry_points.txt +0 -0
  247. {ultralytics-8.1.29.dist-info → ultralytics-8.3.63.dist-info}/top_level.txt +0 -0
@@ -1,107 +1,388 @@
1
- # Ultralytics YOLO 🚀, AGPL-3.0 license
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
3
  import math
4
4
  import random
5
5
  from copy import deepcopy
6
+ from typing import Tuple, Union
6
7
 
7
8
  import cv2
8
9
  import numpy as np
9
10
  import torch
10
- import torchvision.transforms as T
11
+ from PIL import Image
11
12
 
13
+ from ultralytics.data.utils import polygons2masks, polygons2masks_overlap
12
14
  from ultralytics.utils import LOGGER, colorstr
13
15
  from ultralytics.utils.checks import check_version
14
16
  from ultralytics.utils.instance import Instances
15
17
  from ultralytics.utils.metrics import bbox_ioa
16
18
  from ultralytics.utils.ops import segment2box, xyxyxyxy2xywhr
17
19
  from ultralytics.utils.torch_utils import TORCHVISION_0_10, TORCHVISION_0_11, TORCHVISION_0_13
18
- from .utils import polygons2masks, polygons2masks_overlap
19
20
 
20
21
  DEFAULT_MEAN = (0.0, 0.0, 0.0)
21
22
  DEFAULT_STD = (1.0, 1.0, 1.0)
22
- DEFAULT_CROP_FTACTION = 1.0
23
+ DEFAULT_CROP_FRACTION = 1.0
23
24
 
24
25
 
25
- # TODO: we might need a BaseTransform to make all these augments be compatible with both classification and semantic
26
26
  class BaseTransform:
27
27
  """
28
- Base class for image transformations.
28
+ Base class for image transformations in the Ultralytics library.
29
29
 
30
- This is a generic transformation class that can be extended for specific image processing needs.
31
- The class is designed to be compatible with both classification and semantic segmentation tasks.
30
+ This class serves as a foundation for implementing various image processing operations, designed to be
31
+ compatible with both classification and semantic segmentation tasks.
32
32
 
33
33
  Methods:
34
- __init__: Initializes the BaseTransform object.
35
- apply_image: Applies image transformation to labels.
34
+ apply_image: Applies image transformations to labels.
36
35
  apply_instances: Applies transformations to object instances in labels.
37
36
  apply_semantic: Applies semantic segmentation to an image.
38
37
  __call__: Applies all label transformations to an image, instances, and semantic masks.
38
+
39
+ Examples:
40
+ >>> transform = BaseTransform()
41
+ >>> labels = {"image": np.array(...), "instances": [...], "semantic": np.array(...)}
42
+ >>> transformed_labels = transform(labels)
39
43
  """
40
44
 
41
45
  def __init__(self) -> None:
42
- """Initializes the BaseTransform object."""
46
+ """
47
+ Initializes the BaseTransform object.
48
+
49
+ This constructor sets up the base transformation object, which can be extended for specific image
50
+ processing tasks. It is designed to be compatible with both classification and semantic segmentation.
51
+
52
+ Examples:
53
+ >>> transform = BaseTransform()
54
+ """
43
55
  pass
44
56
 
45
57
  def apply_image(self, labels):
46
- """Applies image transformations to labels."""
58
+ """
59
+ Applies image transformations to labels.
60
+
61
+ This method is intended to be overridden by subclasses to implement specific image transformation
62
+ logic. In its base form, it returns the input labels unchanged.
63
+
64
+ Args:
65
+ labels (Any): The input labels to be transformed. The exact type and structure of labels may
66
+ vary depending on the specific implementation.
67
+
68
+ Returns:
69
+ (Any): The transformed labels. In the base implementation, this is identical to the input.
70
+
71
+ Examples:
72
+ >>> transform = BaseTransform()
73
+ >>> original_labels = [1, 2, 3]
74
+ >>> transformed_labels = transform.apply_image(original_labels)
75
+ >>> print(transformed_labels)
76
+ [1, 2, 3]
77
+ """
47
78
  pass
48
79
 
49
80
  def apply_instances(self, labels):
50
- """Applies transformations to object instances in labels."""
81
+ """
82
+ Applies transformations to object instances in labels.
83
+
84
+ This method is responsible for applying various transformations to object instances within the given
85
+ labels. It is designed to be overridden by subclasses to implement specific instance transformation
86
+ logic.
87
+
88
+ Args:
89
+ labels (Dict): A dictionary containing label information, including object instances.
90
+
91
+ Returns:
92
+ (Dict): The modified labels dictionary with transformed object instances.
93
+
94
+ Examples:
95
+ >>> transform = BaseTransform()
96
+ >>> labels = {"instances": Instances(xyxy=torch.rand(5, 4), cls=torch.randint(0, 80, (5,)))}
97
+ >>> transformed_labels = transform.apply_instances(labels)
98
+ """
51
99
  pass
52
100
 
53
101
  def apply_semantic(self, labels):
54
- """Applies semantic segmentation to an image."""
102
+ """
103
+ Applies semantic segmentation transformations to an image.
104
+
105
+ This method is intended to be overridden by subclasses to implement specific semantic segmentation
106
+ transformations. In its base form, it does not perform any operations.
107
+
108
+ Args:
109
+ labels (Any): The input labels or semantic segmentation mask to be transformed.
110
+
111
+ Returns:
112
+ (Any): The transformed semantic segmentation mask or labels.
113
+
114
+ Examples:
115
+ >>> transform = BaseTransform()
116
+ >>> semantic_mask = np.zeros((100, 100), dtype=np.uint8)
117
+ >>> transformed_mask = transform.apply_semantic(semantic_mask)
118
+ """
55
119
  pass
56
120
 
57
121
  def __call__(self, labels):
58
- """Applies all label transformations to an image, instances, and semantic masks."""
122
+ """
123
+ Applies all label transformations to an image, instances, and semantic masks.
124
+
125
+ This method orchestrates the application of various transformations defined in the BaseTransform class
126
+ to the input labels. It sequentially calls the apply_image and apply_instances methods to process the
127
+ image and object instances, respectively.
128
+
129
+ Args:
130
+ labels (Dict): A dictionary containing image data and annotations. Expected keys include 'img' for
131
+ the image data, and 'instances' for object instances.
132
+
133
+ Returns:
134
+ (Dict): The input labels dictionary with transformed image and instances.
135
+
136
+ Examples:
137
+ >>> transform = BaseTransform()
138
+ >>> labels = {"img": np.random.rand(640, 640, 3), "instances": []}
139
+ >>> transformed_labels = transform(labels)
140
+ """
59
141
  self.apply_image(labels)
60
142
  self.apply_instances(labels)
61
143
  self.apply_semantic(labels)
62
144
 
63
145
 
64
146
  class Compose:
65
- """Class for composing multiple image transformations."""
147
+ """
148
+ A class for composing multiple image transformations.
149
+
150
+ Attributes:
151
+ transforms (List[Callable]): A list of transformation functions to be applied sequentially.
152
+
153
+ Methods:
154
+ __call__: Applies a series of transformations to input data.
155
+ append: Appends a new transform to the existing list of transforms.
156
+ insert: Inserts a new transform at a specified index in the list of transforms.
157
+ __getitem__: Retrieves a specific transform or a set of transforms using indexing.
158
+ __setitem__: Sets a specific transform or a set of transforms using indexing.
159
+ tolist: Converts the list of transforms to a standard Python list.
160
+
161
+ Examples:
162
+ >>> transforms = [RandomFlip(), RandomPerspective(30)]
163
+ >>> compose = Compose(transforms)
164
+ >>> transformed_data = compose(data)
165
+ >>> compose.append(CenterCrop((224, 224)))
166
+ >>> compose.insert(0, RandomFlip())
167
+ """
66
168
 
67
169
  def __init__(self, transforms):
68
- """Initializes the Compose object with a list of transforms."""
69
- self.transforms = transforms
170
+ """
171
+ Initializes the Compose object with a list of transforms.
172
+
173
+ Args:
174
+ transforms (List[Callable]): A list of callable transform objects to be applied sequentially.
175
+
176
+ Examples:
177
+ >>> from ultralytics.data.augment import Compose, RandomHSV, RandomFlip
178
+ >>> transforms = [RandomHSV(), RandomFlip()]
179
+ >>> compose = Compose(transforms)
180
+ """
181
+ self.transforms = transforms if isinstance(transforms, list) else [transforms]
70
182
 
71
183
  def __call__(self, data):
72
- """Applies a series of transformations to input data."""
184
+ """
185
+ Applies a series of transformations to input data. This method sequentially applies each transformation in the
186
+ Compose object's list of transforms to the input data.
187
+
188
+ Args:
189
+ data (Any): The input data to be transformed. This can be of any type, depending on the
190
+ transformations in the list.
191
+
192
+ Returns:
193
+ (Any): The transformed data after applying all transformations in sequence.
194
+
195
+ Examples:
196
+ >>> transforms = [Transform1(), Transform2(), Transform3()]
197
+ >>> compose = Compose(transforms)
198
+ >>> transformed_data = compose(input_data)
199
+ """
73
200
  for t in self.transforms:
74
201
  data = t(data)
75
202
  return data
76
203
 
77
204
  def append(self, transform):
78
- """Appends a new transform to the existing list of transforms."""
205
+ """
206
+ Appends a new transform to the existing list of transforms.
207
+
208
+ Args:
209
+ transform (BaseTransform): The transformation to be added to the composition.
210
+
211
+ Examples:
212
+ >>> compose = Compose([RandomFlip(), RandomPerspective()])
213
+ >>> compose.append(RandomHSV())
214
+ """
79
215
  self.transforms.append(transform)
80
216
 
217
+ def insert(self, index, transform):
218
+ """
219
+ Inserts a new transform at a specified index in the existing list of transforms.
220
+
221
+ Args:
222
+ index (int): The index at which to insert the new transform.
223
+ transform (BaseTransform): The transform object to be inserted.
224
+
225
+ Examples:
226
+ >>> compose = Compose([Transform1(), Transform2()])
227
+ >>> compose.insert(1, Transform3())
228
+ >>> len(compose.transforms)
229
+ 3
230
+ """
231
+ self.transforms.insert(index, transform)
232
+
233
+ def __getitem__(self, index: Union[list, int]) -> "Compose":
234
+ """
235
+ Retrieves a specific transform or a set of transforms using indexing.
236
+
237
+ Args:
238
+ index (int | List[int]): Index or list of indices of the transforms to retrieve.
239
+
240
+ Returns:
241
+ (Compose): A new Compose object containing the selected transform(s).
242
+
243
+ Raises:
244
+ AssertionError: If the index is not of type int or list.
245
+
246
+ Examples:
247
+ >>> transforms = [RandomFlip(), RandomPerspective(10), RandomHSV(0.5, 0.5, 0.5)]
248
+ >>> compose = Compose(transforms)
249
+ >>> single_transform = compose[1] # Returns a Compose object with only RandomPerspective
250
+ >>> multiple_transforms = compose[0:2] # Returns a Compose object with RandomFlip and RandomPerspective
251
+ """
252
+ assert isinstance(index, (int, list)), f"The indices should be either list or int type but got {type(index)}"
253
+ index = [index] if isinstance(index, int) else index
254
+ return Compose([self.transforms[i] for i in index])
255
+
256
+ def __setitem__(self, index: Union[list, int], value: Union[list, int]) -> None:
257
+ """
258
+ Sets one or more transforms in the composition using indexing.
259
+
260
+ Args:
261
+ index (int | List[int]): Index or list of indices to set transforms at.
262
+ value (Any | List[Any]): Transform or list of transforms to set at the specified index(es).
263
+
264
+ Raises:
265
+ AssertionError: If index type is invalid, value type doesn't match index type, or index is out of range.
266
+
267
+ Examples:
268
+ >>> compose = Compose([Transform1(), Transform2(), Transform3()])
269
+ >>> compose[1] = NewTransform() # Replace second transform
270
+ >>> compose[0:2] = [NewTransform1(), NewTransform2()] # Replace first two transforms
271
+ """
272
+ assert isinstance(index, (int, list)), f"The indices should be either list or int type but got {type(index)}"
273
+ if isinstance(index, list):
274
+ assert isinstance(value, list), (
275
+ f"The indices should be the same type as values, but got {type(index)} and {type(value)}"
276
+ )
277
+ if isinstance(index, int):
278
+ index, value = [index], [value]
279
+ for i, v in zip(index, value):
280
+ assert i < len(self.transforms), f"list index {i} out of range {len(self.transforms)}."
281
+ self.transforms[i] = v
282
+
81
283
  def tolist(self):
82
- """Converts the list of transforms to a standard Python list."""
284
+ """
285
+ Converts the list of transforms to a standard Python list.
286
+
287
+ Returns:
288
+ (List): A list containing all the transform objects in the Compose instance.
289
+
290
+ Examples:
291
+ >>> transforms = [RandomFlip(), RandomPerspective(10), CenterCrop()]
292
+ >>> compose = Compose(transforms)
293
+ >>> transform_list = compose.tolist()
294
+ >>> print(len(transform_list))
295
+ 3
296
+ """
83
297
  return self.transforms
84
298
 
85
299
  def __repr__(self):
86
- """Returns a string representation of the object."""
300
+ """
301
+ Returns a string representation of the Compose object.
302
+
303
+ Returns:
304
+ (str): A string representation of the Compose object, including the list of transforms.
305
+
306
+ Examples:
307
+ >>> transforms = [RandomFlip(), RandomPerspective(degrees=10, translate=0.1, scale=0.1)]
308
+ >>> compose = Compose(transforms)
309
+ >>> print(compose)
310
+ Compose([
311
+ RandomFlip(),
312
+ RandomPerspective(degrees=10, translate=0.1, scale=0.1)
313
+ ])
314
+ """
87
315
  return f"{self.__class__.__name__}({', '.join([f'{t}' for t in self.transforms])})"
88
316
 
89
317
 
90
318
  class BaseMixTransform:
91
319
  """
92
- Class for base mix (MixUp/Mosaic) transformations.
320
+ Base class for mix transformations like MixUp and Mosaic.
321
+
322
+ This class provides a foundation for implementing mix transformations on datasets. It handles the
323
+ probability-based application of transforms and manages the mixing of multiple images and labels.
93
324
 
94
- This implementation is from mmyolo.
325
+ Attributes:
326
+ dataset (Any): The dataset object containing images and labels.
327
+ pre_transform (Callable | None): Optional transform to apply before mixing.
328
+ p (float): Probability of applying the mix transformation.
329
+
330
+ Methods:
331
+ __call__: Applies the mix transformation to the input labels.
332
+ _mix_transform: Abstract method to be implemented by subclasses for specific mix operations.
333
+ get_indexes: Abstract method to get indexes of images to be mixed.
334
+ _update_label_text: Updates label text for mixed images.
335
+
336
+ Examples:
337
+ >>> class CustomMixTransform(BaseMixTransform):
338
+ ... def _mix_transform(self, labels):
339
+ ... # Implement custom mix logic here
340
+ ... return labels
341
+ ...
342
+ ... def get_indexes(self):
343
+ ... return [random.randint(0, len(self.dataset) - 1) for _ in range(3)]
344
+ >>> dataset = YourDataset()
345
+ >>> transform = CustomMixTransform(dataset, p=0.5)
346
+ >>> mixed_labels = transform(original_labels)
95
347
  """
96
348
 
97
349
  def __init__(self, dataset, pre_transform=None, p=0.0) -> None:
98
- """Initializes the BaseMixTransform object with dataset, pre_transform, and probability."""
350
+ """
351
+ Initializes the BaseMixTransform object for mix transformations like MixUp and Mosaic.
352
+
353
+ This class serves as a base for implementing mix transformations in image processing pipelines.
354
+
355
+ Args:
356
+ dataset (Any): The dataset object containing images and labels for mixing.
357
+ pre_transform (Callable | None): Optional transform to apply before mixing.
358
+ p (float): Probability of applying the mix transformation. Should be in the range [0.0, 1.0].
359
+
360
+ Examples:
361
+ >>> dataset = YOLODataset("path/to/data")
362
+ >>> pre_transform = Compose([RandomFlip(), RandomPerspective()])
363
+ >>> mix_transform = BaseMixTransform(dataset, pre_transform, p=0.5)
364
+ """
99
365
  self.dataset = dataset
100
366
  self.pre_transform = pre_transform
101
367
  self.p = p
102
368
 
103
369
  def __call__(self, labels):
104
- """Applies pre-processing transforms and mixup/mosaic transforms to labels data."""
370
+ """
371
+ Applies pre-processing transforms and mixup/mosaic transforms to labels data.
372
+
373
+ This method determines whether to apply the mix transform based on a probability factor. If applied, it
374
+ selects additional images, applies pre-transforms if specified, and then performs the mix transform.
375
+
376
+ Args:
377
+ labels (Dict): A dictionary containing label data for an image.
378
+
379
+ Returns:
380
+ (Dict): The transformed labels dictionary, which may include mixed data from other images.
381
+
382
+ Examples:
383
+ >>> transform = BaseMixTransform(dataset, pre_transform=None, p=0.5)
384
+ >>> result = transform({"image": img, "bboxes": boxes, "cls": classes})
385
+ """
105
386
  if random.uniform(0, 1) > self.p:
106
387
  return labels
107
388
 
@@ -118,53 +399,197 @@ class BaseMixTransform:
118
399
  mix_labels[i] = self.pre_transform(data)
119
400
  labels["mix_labels"] = mix_labels
120
401
 
402
+ # Update cls and texts
403
+ labels = self._update_label_text(labels)
121
404
  # Mosaic or MixUp
122
405
  labels = self._mix_transform(labels)
123
406
  labels.pop("mix_labels", None)
124
407
  return labels
125
408
 
126
409
  def _mix_transform(self, labels):
127
- """Applies MixUp or Mosaic augmentation to the label dictionary."""
410
+ """
411
+ Applies MixUp or Mosaic augmentation to the label dictionary.
412
+
413
+ This method should be implemented by subclasses to perform specific mix transformations like MixUp or
414
+ Mosaic. It modifies the input label dictionary in-place with the augmented data.
415
+
416
+ Args:
417
+ labels (Dict): A dictionary containing image and label data. Expected to have a 'mix_labels' key
418
+ with a list of additional image and label data for mixing.
419
+
420
+ Returns:
421
+ (Dict): The modified labels dictionary with augmented data after applying the mix transform.
422
+
423
+ Examples:
424
+ >>> transform = BaseMixTransform(dataset)
425
+ >>> labels = {"image": img, "bboxes": boxes, "mix_labels": [{"image": img2, "bboxes": boxes2}]}
426
+ >>> augmented_labels = transform._mix_transform(labels)
427
+ """
128
428
  raise NotImplementedError
129
429
 
130
430
  def get_indexes(self):
131
- """Gets a list of shuffled indexes for mosaic augmentation."""
431
+ """
432
+ Gets a list of shuffled indexes for mosaic augmentation.
433
+
434
+ Returns:
435
+ (List[int]): A list of shuffled indexes from the dataset.
436
+
437
+ Examples:
438
+ >>> transform = BaseMixTransform(dataset)
439
+ >>> indexes = transform.get_indexes()
440
+ >>> print(indexes) # [3, 18, 7, 2]
441
+ """
132
442
  raise NotImplementedError
133
443
 
444
+ @staticmethod
445
+ def _update_label_text(labels):
446
+ """
447
+ Updates label text and class IDs for mixed labels in image augmentation.
448
+
449
+ This method processes the 'texts' and 'cls' fields of the input labels dictionary and any mixed labels,
450
+ creating a unified set of text labels and updating class IDs accordingly.
451
+
452
+ Args:
453
+ labels (Dict): A dictionary containing label information, including 'texts' and 'cls' fields,
454
+ and optionally a 'mix_labels' field with additional label dictionaries.
455
+
456
+ Returns:
457
+ (Dict): The updated labels dictionary with unified text labels and updated class IDs.
458
+
459
+ Examples:
460
+ >>> labels = {
461
+ ... "texts": [["cat"], ["dog"]],
462
+ ... "cls": torch.tensor([[0], [1]]),
463
+ ... "mix_labels": [{"texts": [["bird"], ["fish"]], "cls": torch.tensor([[0], [1]])}],
464
+ ... }
465
+ >>> updated_labels = self._update_label_text(labels)
466
+ >>> print(updated_labels["texts"])
467
+ [['cat'], ['dog'], ['bird'], ['fish']]
468
+ >>> print(updated_labels["cls"])
469
+ tensor([[0],
470
+ [1]])
471
+ >>> print(updated_labels["mix_labels"][0]["cls"])
472
+ tensor([[2],
473
+ [3]])
474
+ """
475
+ if "texts" not in labels:
476
+ return labels
477
+
478
+ mix_texts = sum([labels["texts"]] + [x["texts"] for x in labels["mix_labels"]], [])
479
+ mix_texts = list({tuple(x) for x in mix_texts})
480
+ text2id = {text: i for i, text in enumerate(mix_texts)}
481
+
482
+ for label in [labels] + labels["mix_labels"]:
483
+ for i, cls in enumerate(label["cls"].squeeze(-1).tolist()):
484
+ text = label["texts"][int(cls)]
485
+ label["cls"][i] = text2id[tuple(text)]
486
+ label["texts"] = mix_texts
487
+ return labels
488
+
134
489
 
135
490
  class Mosaic(BaseMixTransform):
136
491
  """
137
- Mosaic augmentation.
492
+ Mosaic augmentation for image datasets.
138
493
 
139
494
  This class performs mosaic augmentation by combining multiple (4 or 9) images into a single mosaic image.
140
495
  The augmentation is applied to a dataset with a given probability.
141
496
 
142
497
  Attributes:
143
498
  dataset: The dataset on which the mosaic augmentation is applied.
144
- imgsz (int, optional): Image size (height and width) after mosaic pipeline of a single image. Default to 640.
145
- p (float, optional): Probability of applying the mosaic augmentation. Must be in the range 0-1. Default to 1.0.
146
- n (int, optional): The grid size, either 4 (for 2x2) or 9 (for 3x3).
499
+ imgsz (int): Image size (height and width) after mosaic pipeline of a single image.
500
+ p (float): Probability of applying the mosaic augmentation. Must be in the range 0-1.
501
+ n (int): The grid size, either 4 (for 2x2) or 9 (for 3x3).
502
+ border (Tuple[int, int]): Border size for width and height.
503
+
504
+ Methods:
505
+ get_indexes: Returns a list of random indexes from the dataset.
506
+ _mix_transform: Applies mixup transformation to the input image and labels.
507
+ _mosaic3: Creates a 1x3 image mosaic.
508
+ _mosaic4: Creates a 2x2 image mosaic.
509
+ _mosaic9: Creates a 3x3 image mosaic.
510
+ _update_labels: Updates labels with padding.
511
+ _cat_labels: Concatenates labels and clips mosaic border instances.
512
+
513
+ Examples:
514
+ >>> from ultralytics.data.augment import Mosaic
515
+ >>> dataset = YourDataset(...) # Your image dataset
516
+ >>> mosaic_aug = Mosaic(dataset, imgsz=640, p=0.5, n=4)
517
+ >>> augmented_labels = mosaic_aug(original_labels)
147
518
  """
148
519
 
149
520
  def __init__(self, dataset, imgsz=640, p=1.0, n=4):
150
- """Initializes the object with a dataset, image size, probability, and border."""
521
+ """
522
+ Initializes the Mosaic augmentation object.
523
+
524
+ This class performs mosaic augmentation by combining multiple (4 or 9) images into a single mosaic image.
525
+ The augmentation is applied to a dataset with a given probability.
526
+
527
+ Args:
528
+ dataset (Any): The dataset on which the mosaic augmentation is applied.
529
+ imgsz (int): Image size (height and width) after mosaic pipeline of a single image.
530
+ p (float): Probability of applying the mosaic augmentation. Must be in the range 0-1.
531
+ n (int): The grid size, either 4 (for 2x2) or 9 (for 3x3).
532
+
533
+ Examples:
534
+ >>> from ultralytics.data.augment import Mosaic
535
+ >>> dataset = YourDataset(...)
536
+ >>> mosaic_aug = Mosaic(dataset, imgsz=640, p=0.5, n=4)
537
+ """
151
538
  assert 0 <= p <= 1.0, f"The probability should be in range [0, 1], but got {p}."
152
- assert n in (4, 9), "grid must be equal to 4 or 9."
539
+ assert n in {4, 9}, "grid must be equal to 4 or 9."
153
540
  super().__init__(dataset=dataset, p=p)
154
- self.dataset = dataset
155
541
  self.imgsz = imgsz
156
542
  self.border = (-imgsz // 2, -imgsz // 2) # width, height
157
543
  self.n = n
158
544
 
159
545
  def get_indexes(self, buffer=True):
160
- """Return a list of random indexes from the dataset."""
546
+ """
547
+ Returns a list of random indexes from the dataset for mosaic augmentation.
548
+
549
+ This method selects random image indexes either from a buffer or from the entire dataset, depending on
550
+ the 'buffer' parameter. It is used to choose images for creating mosaic augmentations.
551
+
552
+ Args:
553
+ buffer (bool): If True, selects images from the dataset buffer. If False, selects from the entire
554
+ dataset.
555
+
556
+ Returns:
557
+ (List[int]): A list of random image indexes. The length of the list is n-1, where n is the number
558
+ of images used in the mosaic (either 3 or 8, depending on whether n is 4 or 9).
559
+
560
+ Examples:
561
+ >>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=4)
562
+ >>> indexes = mosaic.get_indexes()
563
+ >>> print(len(indexes)) # Output: 3
564
+ """
161
565
  if buffer: # select images from buffer
162
566
  return random.choices(list(self.dataset.buffer), k=self.n - 1)
163
567
  else: # select any images
164
568
  return [random.randint(0, len(self.dataset) - 1) for _ in range(self.n - 1)]
165
569
 
166
570
  def _mix_transform(self, labels):
167
- """Apply mixup transformation to the input image and labels."""
571
+ """
572
+ Applies mosaic augmentation to the input image and labels.
573
+
574
+ This method combines multiple images (3, 4, or 9) into a single mosaic image based on the 'n' attribute.
575
+ It ensures that rectangular annotations are not present and that there are other images available for
576
+ mosaic augmentation.
577
+
578
+ Args:
579
+ labels (Dict): A dictionary containing image data and annotations. Expected keys include:
580
+ - 'rect_shape': Should be None as rect and mosaic are mutually exclusive.
581
+ - 'mix_labels': A list of dictionaries containing data for other images to be used in the mosaic.
582
+
583
+ Returns:
584
+ (Dict): A dictionary containing the mosaic-augmented image and updated annotations.
585
+
586
+ Raises:
587
+ AssertionError: If 'rect_shape' is not None or if 'mix_labels' is empty.
588
+
589
+ Examples:
590
+ >>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=4)
591
+ >>> augmented_data = mosaic._mix_transform(labels)
592
+ """
168
593
  assert labels.get("rect_shape", None) is None, "rect and mosaic are mutually exclusive."
169
594
  assert len(labels.get("mix_labels", [])), "There are no other images for mosaic augment."
170
595
  return (
@@ -172,7 +597,32 @@ class Mosaic(BaseMixTransform):
172
597
  ) # This code is modified for mosaic3 method.
173
598
 
174
599
  def _mosaic3(self, labels):
175
- """Create a 1x3 image mosaic."""
600
+ """
601
+ Creates a 1x3 image mosaic by combining three images.
602
+
603
+ This method arranges three images in a horizontal layout, with the main image in the center and two
604
+ additional images on either side. It's part of the Mosaic augmentation technique used in object detection.
605
+
606
+ Args:
607
+ labels (Dict): A dictionary containing image and label information for the main (center) image.
608
+ Must include 'img' key with the image array, and 'mix_labels' key with a list of two
609
+ dictionaries containing information for the side images.
610
+
611
+ Returns:
612
+ (Dict): A dictionary with the mosaic image and updated labels. Keys include:
613
+ - 'img' (np.ndarray): The mosaic image array with shape (H, W, C).
614
+ - Other keys from the input labels, updated to reflect the new image dimensions.
615
+
616
+ Examples:
617
+ >>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=3)
618
+ >>> labels = {
619
+ ... "img": np.random.rand(480, 640, 3),
620
+ ... "mix_labels": [{"img": np.random.rand(480, 640, 3)} for _ in range(2)],
621
+ ... }
622
+ >>> result = mosaic._mosaic3(labels)
623
+ >>> print(result["img"].shape)
624
+ (640, 640, 3)
625
+ """
176
626
  mosaic_labels = []
177
627
  s = self.imgsz
178
628
  for i in range(3):
@@ -192,7 +642,7 @@ class Mosaic(BaseMixTransform):
192
642
  c = s - w, s + h0 - h, s, s + h0
193
643
 
194
644
  padw, padh = c[:2]
195
- x1, y1, x2, y2 = (max(x, 0) for x in c) # allocate coords
645
+ x1, y1, x2, y2 = (max(x, 0) for x in c) # allocate coordinates
196
646
 
197
647
  img3[y1:y2, x1:x2] = img[y1 - padh :, x1 - padw :] # img3[ymin:ymax, xmin:xmax]
198
648
  # hp, wp = h, w # height, width previous for next iteration
@@ -206,7 +656,29 @@ class Mosaic(BaseMixTransform):
206
656
  return final_labels
207
657
 
208
658
  def _mosaic4(self, labels):
209
- """Create a 2x2 image mosaic."""
659
+ """
660
+ Creates a 2x2 image mosaic from four input images.
661
+
662
+ This method combines four images into a single mosaic image by placing them in a 2x2 grid. It also
663
+ updates the corresponding labels for each image in the mosaic.
664
+
665
+ Args:
666
+ labels (Dict): A dictionary containing image data and labels for the base image (index 0) and three
667
+ additional images (indices 1-3) in the 'mix_labels' key.
668
+
669
+ Returns:
670
+ (Dict): A dictionary containing the mosaic image and updated labels. The 'img' key contains the mosaic
671
+ image as a numpy array, and other keys contain the combined and adjusted labels for all four images.
672
+
673
+ Examples:
674
+ >>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=4)
675
+ >>> labels = {
676
+ ... "img": np.random.rand(480, 640, 3),
677
+ ... "mix_labels": [{"img": np.random.rand(480, 640, 3)} for _ in range(3)],
678
+ ... }
679
+ >>> result = mosaic._mosaic4(labels)
680
+ >>> assert result["img"].shape == (1280, 1280, 3)
681
+ """
210
682
  mosaic_labels = []
211
683
  s = self.imgsz
212
684
  yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in self.border) # mosaic center x, y
@@ -242,7 +714,31 @@ class Mosaic(BaseMixTransform):
242
714
  return final_labels
243
715
 
244
716
  def _mosaic9(self, labels):
245
- """Create a 3x3 image mosaic."""
717
+ """
718
+ Creates a 3x3 image mosaic from the input image and eight additional images.
719
+
720
+ This method combines nine images into a single mosaic image. The input image is placed at the center,
721
+ and eight additional images from the dataset are placed around it in a 3x3 grid pattern.
722
+
723
+ Args:
724
+ labels (Dict): A dictionary containing the input image and its associated labels. It should have
725
+ the following keys:
726
+ - 'img' (numpy.ndarray): The input image.
727
+ - 'resized_shape' (Tuple[int, int]): The shape of the resized image (height, width).
728
+ - 'mix_labels' (List[Dict]): A list of dictionaries containing information for the additional
729
+ eight images, each with the same structure as the input labels.
730
+
731
+ Returns:
732
+ (Dict): A dictionary containing the mosaic image and updated labels. It includes the following keys:
733
+ - 'img' (numpy.ndarray): The final mosaic image.
734
+ - Other keys from the input labels, updated to reflect the new mosaic arrangement.
735
+
736
+ Examples:
737
+ >>> mosaic = Mosaic(dataset, imgsz=640, p=1.0, n=9)
738
+ >>> input_labels = dataset[0]
739
+ >>> mosaic_result = mosaic._mosaic9(input_labels)
740
+ >>> mosaic_image = mosaic_result["img"]
741
+ """
246
742
  mosaic_labels = []
247
743
  s = self.imgsz
248
744
  hp, wp = -1, -1 # height, width previous
@@ -275,7 +771,7 @@ class Mosaic(BaseMixTransform):
275
771
  c = s - w, s + h0 - hp - h, s, s + h0 - hp
276
772
 
277
773
  padw, padh = c[:2]
278
- x1, y1, x2, y2 = (max(x, 0) for x in c) # allocate coords
774
+ x1, y1, x2, y2 = (max(x, 0) for x in c) # allocate coordinates
279
775
 
280
776
  # Image
281
777
  img9[y1:y2, x1:x2] = img[y1 - padh :, x1 - padw :] # img9[ymin:ymax, xmin:xmax]
@@ -291,7 +787,25 @@ class Mosaic(BaseMixTransform):
291
787
 
292
788
  @staticmethod
293
789
  def _update_labels(labels, padw, padh):
294
- """Update labels."""
790
+ """
791
+ Updates label coordinates with padding values.
792
+
793
+ This method adjusts the bounding box coordinates of object instances in the labels by adding padding
794
+ values. It also denormalizes the coordinates if they were previously normalized.
795
+
796
+ Args:
797
+ labels (Dict): A dictionary containing image and instance information.
798
+ padw (int): Padding width to be added to the x-coordinates.
799
+ padh (int): Padding height to be added to the y-coordinates.
800
+
801
+ Returns:
802
+ (Dict): Updated labels dictionary with adjusted instance coordinates.
803
+
804
+ Examples:
805
+ >>> labels = {"img": np.zeros((100, 100, 3)), "instances": Instances(...)}
806
+ >>> padw, padh = 50, 50
807
+ >>> updated_labels = Mosaic._update_labels(labels, padw, padh)
808
+ """
295
809
  nh, nw = labels["img"].shape[:2]
296
810
  labels["instances"].convert_bbox(format="xyxy")
297
811
  labels["instances"].denormalize(nw, nh)
@@ -299,7 +813,32 @@ class Mosaic(BaseMixTransform):
299
813
  return labels
300
814
 
301
815
  def _cat_labels(self, mosaic_labels):
302
- """Return labels with mosaic border instances clipped."""
816
+ """
817
+ Concatenates and processes labels for mosaic augmentation.
818
+
819
+ This method combines labels from multiple images used in mosaic augmentation, clips instances to the
820
+ mosaic border, and removes zero-area boxes.
821
+
822
+ Args:
823
+ mosaic_labels (List[Dict]): A list of label dictionaries for each image in the mosaic.
824
+
825
+ Returns:
826
+ (Dict): A dictionary containing concatenated and processed labels for the mosaic image, including:
827
+ - im_file (str): File path of the first image in the mosaic.
828
+ - ori_shape (Tuple[int, int]): Original shape of the first image.
829
+ - resized_shape (Tuple[int, int]): Shape of the mosaic image (imgsz * 2, imgsz * 2).
830
+ - cls (np.ndarray): Concatenated class labels.
831
+ - instances (Instances): Concatenated instance annotations.
832
+ - mosaic_border (Tuple[int, int]): Mosaic border size.
833
+ - texts (List[str], optional): Text labels if present in the original labels.
834
+
835
+ Examples:
836
+ >>> mosaic = Mosaic(dataset, imgsz=640)
837
+ >>> mosaic_labels = [{"cls": np.array([0, 1]), "instances": Instances(...)} for _ in range(4)]
838
+ >>> result = mosaic._cat_labels(mosaic_labels)
839
+ >>> print(result.keys())
840
+ dict_keys(['im_file', 'ori_shape', 'resized_shape', 'cls', 'instances', 'mosaic_border'])
841
+ """
303
842
  if len(mosaic_labels) == 0:
304
843
  return {}
305
844
  cls = []
@@ -320,22 +859,88 @@ class Mosaic(BaseMixTransform):
320
859
  final_labels["instances"].clip(imgsz, imgsz)
321
860
  good = final_labels["instances"].remove_zero_area_boxes()
322
861
  final_labels["cls"] = final_labels["cls"][good]
862
+ if "texts" in mosaic_labels[0]:
863
+ final_labels["texts"] = mosaic_labels[0]["texts"]
323
864
  return final_labels
324
865
 
325
866
 
326
867
  class MixUp(BaseMixTransform):
327
- """Class for applying MixUp augmentation to the dataset."""
868
+ """
869
+ Applies MixUp augmentation to image datasets.
870
+
871
+ This class implements the MixUp augmentation technique as described in the paper "mixup: Beyond Empirical Risk
872
+ Minimization" (https://arxiv.org/abs/1710.09412). MixUp combines two images and their labels using a random weight.
873
+
874
+ Attributes:
875
+ dataset (Any): The dataset to which MixUp augmentation will be applied.
876
+ pre_transform (Callable | None): Optional transform to apply before MixUp.
877
+ p (float): Probability of applying MixUp augmentation.
878
+
879
+ Methods:
880
+ get_indexes: Returns a random index from the dataset.
881
+ _mix_transform: Applies MixUp augmentation to the input labels.
882
+
883
+ Examples:
884
+ >>> from ultralytics.data.augment import MixUp
885
+ >>> dataset = YourDataset(...) # Your image dataset
886
+ >>> mixup = MixUp(dataset, p=0.5)
887
+ >>> augmented_labels = mixup(original_labels)
888
+ """
328
889
 
329
890
  def __init__(self, dataset, pre_transform=None, p=0.0) -> None:
330
- """Initializes MixUp object with dataset, pre_transform, and probability of applying MixUp."""
891
+ """
892
+ Initializes the MixUp augmentation object.
893
+
894
+ MixUp is an image augmentation technique that combines two images by taking a weighted sum of their pixel
895
+ values and labels. This implementation is designed for use with the Ultralytics YOLO framework.
896
+
897
+ Args:
898
+ dataset (Any): The dataset to which MixUp augmentation will be applied.
899
+ pre_transform (Callable | None): Optional transform to apply to images before MixUp.
900
+ p (float): Probability of applying MixUp augmentation to an image. Must be in the range [0, 1].
901
+
902
+ Examples:
903
+ >>> from ultralytics.data.dataset import YOLODataset
904
+ >>> dataset = YOLODataset("path/to/data.yaml")
905
+ >>> mixup = MixUp(dataset, pre_transform=None, p=0.5)
906
+ """
331
907
  super().__init__(dataset=dataset, pre_transform=pre_transform, p=p)
332
908
 
333
909
  def get_indexes(self):
334
- """Get a random index from the dataset."""
910
+ """
911
+ Get a random index from the dataset.
912
+
913
+ This method returns a single random index from the dataset, which is used to select an image for MixUp
914
+ augmentation.
915
+
916
+ Returns:
917
+ (int): A random integer index within the range of the dataset length.
918
+
919
+ Examples:
920
+ >>> mixup = MixUp(dataset)
921
+ >>> index = mixup.get_indexes()
922
+ >>> print(index)
923
+ 42
924
+ """
335
925
  return random.randint(0, len(self.dataset) - 1)
336
926
 
337
927
  def _mix_transform(self, labels):
338
- """Applies MixUp augmentation as per https://arxiv.org/pdf/1710.09412.pdf."""
928
+ """
929
+ Applies MixUp augmentation to the input labels.
930
+
931
+ This method implements the MixUp augmentation technique as described in the paper
932
+ "mixup: Beyond Empirical Risk Minimization" (https://arxiv.org/abs/1710.09412).
933
+
934
+ Args:
935
+ labels (Dict): A dictionary containing the original image and label information.
936
+
937
+ Returns:
938
+ (Dict): A dictionary containing the mixed-up image and combined label information.
939
+
940
+ Examples:
941
+ >>> mixer = MixUp(dataset)
942
+ >>> mixed_labels = mixer._mix_transform(labels)
943
+ """
339
944
  r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0
340
945
  labels2 = labels["mix_labels"][0]
341
946
  labels["img"] = (labels["img"] * r + labels2["img"] * (1 - r)).astype(np.uint8)
@@ -346,33 +951,61 @@ class MixUp(BaseMixTransform):
346
951
 
347
952
  class RandomPerspective:
348
953
  """
349
- Implements random perspective and affine transformations on images and corresponding bounding boxes, segments, and
350
- keypoints. These transformations include rotation, translation, scaling, and shearing. The class also offers the
351
- option to apply these transformations conditionally with a specified probability.
954
+ Implements random perspective and affine transformations on images and corresponding annotations.
955
+
956
+ This class applies random rotations, translations, scaling, shearing, and perspective transformations
957
+ to images and their associated bounding boxes, segments, and keypoints. It can be used as part of an
958
+ augmentation pipeline for object detection and instance segmentation tasks.
352
959
 
353
960
  Attributes:
354
- degrees (float): Degree range for random rotations.
355
- translate (float): Fraction of total width and height for random translation.
356
- scale (float): Scaling factor interval, e.g., a scale factor of 0.1 allows a resize between 90%-110%.
357
- shear (float): Shear intensity (angle in degrees).
961
+ degrees (float): Maximum absolute degree range for random rotations.
962
+ translate (float): Maximum translation as a fraction of the image size.
963
+ scale (float): Scaling factor range, e.g., scale=0.1 means 0.9-1.1.
964
+ shear (float): Maximum shear angle in degrees.
358
965
  perspective (float): Perspective distortion factor.
359
- border (tuple): Tuple specifying mosaic border.
360
- pre_transform (callable): A function/transform to apply to the image before starting the random transformation.
966
+ border (Tuple[int, int]): Mosaic border size as (x, y).
967
+ pre_transform (Callable | None): Optional transform to apply before the random perspective.
361
968
 
362
969
  Methods:
363
- affine_transform(img, border): Applies a series of affine transformations to the image.
364
- apply_bboxes(bboxes, M): Transforms bounding boxes using the calculated affine matrix.
365
- apply_segments(segments, M): Transforms segments and generates new bounding boxes.
366
- apply_keypoints(keypoints, M): Transforms keypoints.
367
- __call__(labels): Main method to apply transformations to both images and their corresponding annotations.
368
- box_candidates(box1, box2): Filters out bounding boxes that don't meet certain criteria post-transformation.
970
+ affine_transform: Applies affine transformations to the input image.
971
+ apply_bboxes: Transforms bounding boxes using the affine matrix.
972
+ apply_segments: Transforms segments and generates new bounding boxes.
973
+ apply_keypoints: Transforms keypoints using the affine matrix.
974
+ __call__: Applies the random perspective transformation to images and annotations.
975
+ box_candidates: Filters transformed bounding boxes based on size and aspect ratio.
976
+
977
+ Examples:
978
+ >>> transform = RandomPerspective(degrees=10, translate=0.1, scale=0.1, shear=10)
979
+ >>> image = np.random.randint(0, 255, (640, 640, 3), dtype=np.uint8)
980
+ >>> labels = {"img": image, "cls": np.array([0, 1]), "instances": Instances(...)}
981
+ >>> result = transform(labels)
982
+ >>> transformed_image = result["img"]
983
+ >>> transformed_instances = result["instances"]
369
984
  """
370
985
 
371
986
  def __init__(
372
987
  self, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, border=(0, 0), pre_transform=None
373
988
  ):
374
- """Initializes RandomPerspective object with transformation parameters."""
989
+ """
990
+ Initializes RandomPerspective object with transformation parameters.
375
991
 
992
+ This class implements random perspective and affine transformations on images and corresponding bounding boxes,
993
+ segments, and keypoints. Transformations include rotation, translation, scaling, and shearing.
994
+
995
+ Args:
996
+ degrees (float): Degree range for random rotations.
997
+ translate (float): Fraction of total width and height for random translation.
998
+ scale (float): Scaling factor interval, e.g., a scale factor of 0.5 allows a resize between 50%-150%.
999
+ shear (float): Shear intensity (angle in degrees).
1000
+ perspective (float): Perspective distortion factor.
1001
+ border (Tuple[int, int]): Tuple specifying mosaic border (top/bottom, left/right).
1002
+ pre_transform (Callable | None): Function/transform to apply to the image before starting the random
1003
+ transformation.
1004
+
1005
+ Examples:
1006
+ >>> transform = RandomPerspective(degrees=10.0, translate=0.1, scale=0.5, shear=5.0)
1007
+ >>> result = transform(labels) # Apply random perspective to labels
1008
+ """
376
1009
  self.degrees = degrees
377
1010
  self.translate = translate
378
1011
  self.scale = scale
@@ -385,16 +1018,26 @@ class RandomPerspective:
385
1018
  """
386
1019
  Applies a sequence of affine transformations centered around the image center.
387
1020
 
1021
+ This function performs a series of geometric transformations on the input image, including
1022
+ translation, perspective change, rotation, scaling, and shearing. The transformations are
1023
+ applied in a specific order to maintain consistency.
1024
+
388
1025
  Args:
389
- img (ndarray): Input image.
390
- border (tuple): Border dimensions.
1026
+ img (np.ndarray): Input image to be transformed.
1027
+ border (Tuple[int, int]): Border dimensions for the transformed image.
391
1028
 
392
1029
  Returns:
393
- img (ndarray): Transformed image.
394
- M (ndarray): Transformation matrix.
395
- s (float): Scale factor.
1030
+ (Tuple[np.ndarray, np.ndarray, float]): A tuple containing:
1031
+ - np.ndarray: Transformed image.
1032
+ - np.ndarray: 3x3 transformation matrix.
1033
+ - float: Scale factor applied during the transformation.
1034
+
1035
+ Examples:
1036
+ >>> import numpy as np
1037
+ >>> img = np.random.rand(100, 100, 3)
1038
+ >>> border = (10, 10)
1039
+ >>> transformed_img, matrix, scale = affine_transform(img, border)
396
1040
  """
397
-
398
1041
  # Center
399
1042
  C = np.eye(3, dtype=np.float32)
400
1043
 
@@ -436,14 +1079,23 @@ class RandomPerspective:
436
1079
 
437
1080
  def apply_bboxes(self, bboxes, M):
438
1081
  """
439
- Apply affine to bboxes only.
1082
+ Apply affine transformation to bounding boxes.
1083
+
1084
+ This function applies an affine transformation to a set of bounding boxes using the provided
1085
+ transformation matrix.
440
1086
 
441
1087
  Args:
442
- bboxes (ndarray): list of bboxes, xyxy format, with shape (num_bboxes, 4).
443
- M (ndarray): affine matrix.
1088
+ bboxes (torch.Tensor): Bounding boxes in xyxy format with shape (N, 4), where N is the number
1089
+ of bounding boxes.
1090
+ M (torch.Tensor): Affine transformation matrix with shape (3, 3).
444
1091
 
445
1092
  Returns:
446
- new_bboxes (ndarray): bboxes after affine, [num_bboxes, 4].
1093
+ (torch.Tensor): Transformed bounding boxes in xyxy format with shape (N, 4).
1094
+
1095
+ Examples:
1096
+ >>> bboxes = torch.tensor([[10, 10, 20, 20], [30, 30, 40, 40]])
1097
+ >>> M = torch.eye(3)
1098
+ >>> transformed_bboxes = apply_bboxes(bboxes, M)
447
1099
  """
448
1100
  n = len(bboxes)
449
1101
  if n == 0:
@@ -461,15 +1113,25 @@ class RandomPerspective:
461
1113
 
462
1114
  def apply_segments(self, segments, M):
463
1115
  """
464
- Apply affine to segments and generate new bboxes from segments.
1116
+ Apply affine transformations to segments and generate new bounding boxes.
1117
+
1118
+ This function applies affine transformations to input segments and generates new bounding boxes based on
1119
+ the transformed segments. It clips the transformed segments to fit within the new bounding boxes.
465
1120
 
466
1121
  Args:
467
- segments (ndarray): list of segments, [num_samples, 500, 2].
468
- M (ndarray): affine matrix.
1122
+ segments (np.ndarray): Input segments with shape (N, M, 2), where N is the number of segments and M is the
1123
+ number of points in each segment.
1124
+ M (np.ndarray): Affine transformation matrix with shape (3, 3).
469
1125
 
470
1126
  Returns:
471
- new_segments (ndarray): list of segments after affine, [num_samples, 500, 2].
472
- new_bboxes (ndarray): bboxes after affine, [N, 4].
1127
+ (Tuple[np.ndarray, np.ndarray]): A tuple containing:
1128
+ - New bounding boxes with shape (N, 4) in xyxy format.
1129
+ - Transformed and clipped segments with shape (N, M, 2).
1130
+
1131
+ Examples:
1132
+ >>> segments = np.random.rand(10, 500, 2) # 10 segments with 500 points each
1133
+ >>> M = np.eye(3) # Identity transformation matrix
1134
+ >>> new_bboxes, new_segments = apply_segments(segments, M)
473
1135
  """
474
1136
  n, num = segments.shape[:2]
475
1137
  if n == 0:
@@ -488,14 +1150,25 @@ class RandomPerspective:
488
1150
 
489
1151
  def apply_keypoints(self, keypoints, M):
490
1152
  """
491
- Apply affine to keypoints.
1153
+ Applies affine transformation to keypoints.
1154
+
1155
+ This method transforms the input keypoints using the provided affine transformation matrix. It handles
1156
+ perspective rescaling if necessary and updates the visibility of keypoints that fall outside the image
1157
+ boundaries after transformation.
492
1158
 
493
1159
  Args:
494
- keypoints (ndarray): keypoints, [N, 17, 3].
495
- M (ndarray): affine matrix.
1160
+ keypoints (np.ndarray): Array of keypoints with shape (N, 17, 3), where N is the number of instances,
1161
+ 17 is the number of keypoints per instance, and 3 represents (x, y, visibility).
1162
+ M (np.ndarray): 3x3 affine transformation matrix.
496
1163
 
497
1164
  Returns:
498
- new_keypoints (ndarray): keypoints after affine, [N, 17, 3].
1165
+ (np.ndarray): Transformed keypoints array with the same shape as input (N, 17, 3).
1166
+
1167
+ Examples:
1168
+ >>> random_perspective = RandomPerspective()
1169
+ >>> keypoints = np.random.rand(5, 17, 3) # 5 instances, 17 keypoints each
1170
+ >>> M = np.eye(3) # Identity transformation
1171
+ >>> transformed_keypoints = random_perspective.apply_keypoints(keypoints, M)
499
1172
  """
500
1173
  n, nkpt = keypoints.shape[:2]
501
1174
  if n == 0:
@@ -511,10 +1184,38 @@ class RandomPerspective:
511
1184
 
512
1185
  def __call__(self, labels):
513
1186
  """
514
- Affine images and targets.
1187
+ Applies random perspective and affine transformations to an image and its associated labels.
1188
+
1189
+ This method performs a series of transformations including rotation, translation, scaling, shearing,
1190
+ and perspective distortion on the input image and adjusts the corresponding bounding boxes, segments,
1191
+ and keypoints accordingly.
515
1192
 
516
1193
  Args:
517
- labels (dict): a dict of `bboxes`, `segments`, `keypoints`.
1194
+ labels (Dict): A dictionary containing image data and annotations.
1195
+ Must include:
1196
+ 'img' (ndarray): The input image.
1197
+ 'cls' (ndarray): Class labels.
1198
+ 'instances' (Instances): Object instances with bounding boxes, segments, and keypoints.
1199
+ May include:
1200
+ 'mosaic_border' (Tuple[int, int]): Border size for mosaic augmentation.
1201
+
1202
+ Returns:
1203
+ (Dict): Transformed labels dictionary containing:
1204
+ - 'img' (np.ndarray): The transformed image.
1205
+ - 'cls' (np.ndarray): Updated class labels.
1206
+ - 'instances' (Instances): Updated object instances.
1207
+ - 'resized_shape' (Tuple[int, int]): New image shape after transformation.
1208
+
1209
+ Examples:
1210
+ >>> transform = RandomPerspective()
1211
+ >>> image = np.random.randint(0, 255, (640, 640, 3), dtype=np.uint8)
1212
+ >>> labels = {
1213
+ ... "img": image,
1214
+ ... "cls": np.array([0, 1, 2]),
1215
+ ... "instances": Instances(bboxes=np.array([[10, 10, 50, 50], [100, 100, 150, 150]])),
1216
+ ... }
1217
+ >>> result = transform(labels)
1218
+ >>> assert result["img"].shape[:2] == result["resized_shape"]
518
1219
  """
519
1220
  if self.pre_transform and "mosaic_border" not in labels:
520
1221
  labels = self.pre_transform(labels)
@@ -559,21 +1260,39 @@ class RandomPerspective:
559
1260
  labels["resized_shape"] = img.shape[:2]
560
1261
  return labels
561
1262
 
562
- def box_candidates(self, box1, box2, wh_thr=2, ar_thr=100, area_thr=0.1, eps=1e-16):
1263
+ @staticmethod
1264
+ def box_candidates(box1, box2, wh_thr=2, ar_thr=100, area_thr=0.1, eps=1e-16):
563
1265
  """
564
- Compute box candidates based on a set of thresholds. This method compares the characteristics of the boxes
565
- before and after augmentation to decide whether a box is a candidate for further processing.
1266
+ Compute candidate boxes for further processing based on size and aspect ratio criteria.
1267
+
1268
+ This method compares boxes before and after augmentation to determine if they meet specified
1269
+ thresholds for width, height, aspect ratio, and area. It's used to filter out boxes that have
1270
+ been overly distorted or reduced by the augmentation process.
566
1271
 
567
1272
  Args:
568
- box1 (numpy.ndarray): The 4,n bounding box before augmentation, represented as [x1, y1, x2, y2].
569
- box2 (numpy.ndarray): The 4,n bounding box after augmentation, represented as [x1, y1, x2, y2].
570
- wh_thr (float, optional): The width and height threshold in pixels. Default is 2.
571
- ar_thr (float, optional): The aspect ratio threshold. Default is 100.
572
- area_thr (float, optional): The area ratio threshold. Default is 0.1.
573
- eps (float, optional): A small epsilon value to prevent division by zero. Default is 1e-16.
1273
+ box1 (numpy.ndarray): Original boxes before augmentation, shape (4, N) where n is the
1274
+ number of boxes. Format is [x1, y1, x2, y2] in absolute coordinates.
1275
+ box2 (numpy.ndarray): Augmented boxes after transformation, shape (4, N). Format is
1276
+ [x1, y1, x2, y2] in absolute coordinates.
1277
+ wh_thr (float): Width and height threshold in pixels. Boxes smaller than this in either
1278
+ dimension are rejected.
1279
+ ar_thr (float): Aspect ratio threshold. Boxes with an aspect ratio greater than this
1280
+ value are rejected.
1281
+ area_thr (float): Area ratio threshold. Boxes with an area ratio (new/old) less than
1282
+ this value are rejected.
1283
+ eps (float): Small epsilon value to prevent division by zero.
574
1284
 
575
1285
  Returns:
576
- (numpy.ndarray): A boolean array indicating which boxes are candidates based on the given thresholds.
1286
+ (numpy.ndarray): Boolean array of shape (n) indicating which boxes are candidates.
1287
+ True values correspond to boxes that meet all criteria.
1288
+
1289
+ Examples:
1290
+ >>> random_perspective = RandomPerspective()
1291
+ >>> box1 = np.array([[0, 0, 100, 100], [0, 0, 50, 50]]).T
1292
+ >>> box2 = np.array([[10, 10, 90, 90], [5, 5, 45, 45]]).T
1293
+ >>> candidates = random_perspective.box_candidates(box1, box2)
1294
+ >>> print(candidates)
1295
+ [True True]
577
1296
  """
578
1297
  w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
579
1298
  w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
@@ -583,20 +1302,42 @@ class RandomPerspective:
583
1302
 
584
1303
  class RandomHSV:
585
1304
  """
586
- This class is responsible for performing random adjustments to the Hue, Saturation, and Value (HSV) channels of an
587
- image.
1305
+ Randomly adjusts the Hue, Saturation, and Value (HSV) channels of an image.
1306
+
1307
+ This class applies random HSV augmentation to images within predefined limits set by hgain, sgain, and vgain.
1308
+
1309
+ Attributes:
1310
+ hgain (float): Maximum variation for hue. Range is typically [0, 1].
1311
+ sgain (float): Maximum variation for saturation. Range is typically [0, 1].
1312
+ vgain (float): Maximum variation for value. Range is typically [0, 1].
588
1313
 
589
- The adjustments are random but within limits set by hgain, sgain, and vgain.
1314
+ Methods:
1315
+ __call__: Applies random HSV augmentation to an image.
1316
+
1317
+ Examples:
1318
+ >>> import numpy as np
1319
+ >>> from ultralytics.data.augment import RandomHSV
1320
+ >>> augmenter = RandomHSV(hgain=0.5, sgain=0.5, vgain=0.5)
1321
+ >>> image = np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)
1322
+ >>> labels = {"img": image}
1323
+ >>> augmenter(labels)
1324
+ >>> augmented_image = augmented_labels["img"]
590
1325
  """
591
1326
 
592
1327
  def __init__(self, hgain=0.5, sgain=0.5, vgain=0.5) -> None:
593
1328
  """
594
- Initialize RandomHSV class with gains for each HSV channel.
1329
+ Initializes the RandomHSV object for random HSV (Hue, Saturation, Value) augmentation.
1330
+
1331
+ This class applies random adjustments to the HSV channels of an image within specified limits.
595
1332
 
596
1333
  Args:
597
- hgain (float, optional): Maximum variation for hue. Default is 0.5.
598
- sgain (float, optional): Maximum variation for saturation. Default is 0.5.
599
- vgain (float, optional): Maximum variation for value. Default is 0.5.
1334
+ hgain (float): Maximum variation for hue. Should be in the range [0, 1].
1335
+ sgain (float): Maximum variation for saturation. Should be in the range [0, 1].
1336
+ vgain (float): Maximum variation for value. Should be in the range [0, 1].
1337
+
1338
+ Examples:
1339
+ >>> hsv_aug = RandomHSV(hgain=0.5, sgain=0.5, vgain=0.5)
1340
+ >>> hsv_aug(image)
600
1341
  """
601
1342
  self.hgain = hgain
602
1343
  self.sgain = sgain
@@ -604,9 +1345,24 @@ class RandomHSV:
604
1345
 
605
1346
  def __call__(self, labels):
606
1347
  """
607
- Applies random HSV augmentation to an image within the predefined limits.
1348
+ Applies random HSV augmentation to an image within predefined limits.
608
1349
 
609
- The modified image replaces the original image in the input 'labels' dict.
1350
+ This method modifies the input image by randomly adjusting its Hue, Saturation, and Value (HSV) channels.
1351
+ The adjustments are made within the limits set by hgain, sgain, and vgain during initialization.
1352
+
1353
+ Args:
1354
+ labels (Dict): A dictionary containing image data and metadata. Must include an 'img' key with
1355
+ the image as a numpy array.
1356
+
1357
+ Returns:
1358
+ (None): The function modifies the input 'labels' dictionary in-place, updating the 'img' key
1359
+ with the HSV-augmented image.
1360
+
1361
+ Examples:
1362
+ >>> hsv_augmenter = RandomHSV(hgain=0.5, sgain=0.5, vgain=0.5)
1363
+ >>> labels = {"img": np.random.randint(0, 255, (100, 100, 3), dtype=np.uint8)}
1364
+ >>> hsv_augmenter(labels)
1365
+ >>> augmented_img = labels["img"]
610
1366
  """
611
1367
  img = labels["img"]
612
1368
  if self.hgain or self.sgain or self.vgain:
@@ -628,21 +1384,45 @@ class RandomFlip:
628
1384
  """
629
1385
  Applies a random horizontal or vertical flip to an image with a given probability.
630
1386
 
631
- Also updates any instances (bounding boxes, keypoints, etc.) accordingly.
1387
+ This class performs random image flipping and updates corresponding instance annotations such as
1388
+ bounding boxes and keypoints.
1389
+
1390
+ Attributes:
1391
+ p (float): Probability of applying the flip. Must be between 0 and 1.
1392
+ direction (str): Direction of flip, either 'horizontal' or 'vertical'.
1393
+ flip_idx (array-like): Index mapping for flipping keypoints, if applicable.
1394
+
1395
+ Methods:
1396
+ __call__: Applies the random flip transformation to an image and its annotations.
1397
+
1398
+ Examples:
1399
+ >>> transform = RandomFlip(p=0.5, direction="horizontal")
1400
+ >>> result = transform({"img": image, "instances": instances})
1401
+ >>> flipped_image = result["img"]
1402
+ >>> flipped_instances = result["instances"]
632
1403
  """
633
1404
 
634
1405
  def __init__(self, p=0.5, direction="horizontal", flip_idx=None) -> None:
635
1406
  """
636
1407
  Initializes the RandomFlip class with probability and direction.
637
1408
 
1409
+ This class applies a random horizontal or vertical flip to an image with a given probability.
1410
+ It also updates any instances (bounding boxes, keypoints, etc.) accordingly.
1411
+
638
1412
  Args:
639
- p (float, optional): The probability of applying the flip. Must be between 0 and 1. Default is 0.5.
640
- direction (str, optional): The direction to apply the flip. Must be 'horizontal' or 'vertical'.
641
- Default is 'horizontal'.
642
- flip_idx (array-like, optional): Index mapping for flipping keypoints, if any.
1413
+ p (float): The probability of applying the flip. Must be between 0 and 1.
1414
+ direction (str): The direction to apply the flip. Must be 'horizontal' or 'vertical'.
1415
+ flip_idx (List[int] | None): Index mapping for flipping keypoints, if any.
1416
+
1417
+ Raises:
1418
+ AssertionError: If direction is not 'horizontal' or 'vertical', or if p is not between 0 and 1.
1419
+
1420
+ Examples:
1421
+ >>> flip = RandomFlip(p=0.5, direction="horizontal")
1422
+ >>> flip_with_idx = RandomFlip(p=0.7, direction="vertical", flip_idx=[1, 0, 3, 2, 5, 4])
643
1423
  """
644
- assert direction in ["horizontal", "vertical"], f"Support direction `horizontal` or `vertical`, got {direction}"
645
- assert 0 <= p <= 1.0
1424
+ assert direction in {"horizontal", "vertical"}, f"Support direction `horizontal` or `vertical`, got {direction}"
1425
+ assert 0 <= p <= 1.0, f"The probability should be in range [0, 1], but got {p}."
646
1426
 
647
1427
  self.p = p
648
1428
  self.direction = direction
@@ -652,12 +1432,25 @@ class RandomFlip:
652
1432
  """
653
1433
  Applies random flip to an image and updates any instances like bounding boxes or keypoints accordingly.
654
1434
 
1435
+ This method randomly flips the input image either horizontally or vertically based on the initialized
1436
+ probability and direction. It also updates the corresponding instances (bounding boxes, keypoints) to
1437
+ match the flipped image.
1438
+
655
1439
  Args:
656
- labels (dict): A dictionary containing the keys 'img' and 'instances'. 'img' is the image to be flipped.
657
- 'instances' is an object containing bounding boxes and optionally keypoints.
1440
+ labels (Dict): A dictionary containing the following keys:
1441
+ 'img' (numpy.ndarray): The image to be flipped.
1442
+ 'instances' (ultralytics.utils.instance.Instances): An object containing bounding boxes and
1443
+ optionally keypoints.
658
1444
 
659
1445
  Returns:
660
- (dict): The same dict with the flipped image and updated instances under the 'img' and 'instances' keys.
1446
+ (Dict): The same dictionary with the flipped image and updated instances:
1447
+ 'img' (numpy.ndarray): The flipped image.
1448
+ 'instances' (ultralytics.utils.instance.Instances): Updated instances matching the flipped image.
1449
+
1450
+ Examples:
1451
+ >>> labels = {"img": np.random.rand(640, 640, 3), "instances": Instances(...)}
1452
+ >>> random_flip = RandomFlip(p=0.5, direction="horizontal")
1453
+ >>> flipped_labels = random_flip(labels)
661
1454
  """
662
1455
  img = labels["img"]
663
1456
  instances = labels.pop("instances")
@@ -682,10 +1475,56 @@ class RandomFlip:
682
1475
 
683
1476
 
684
1477
  class LetterBox:
685
- """Resize image and padding for detection, instance segmentation, pose."""
1478
+ """
1479
+ Resize image and padding for detection, instance segmentation, pose.
1480
+
1481
+ This class resizes and pads images to a specified shape while preserving aspect ratio. It also updates
1482
+ corresponding labels and bounding boxes.
1483
+
1484
+ Attributes:
1485
+ new_shape (tuple): Target shape (height, width) for resizing.
1486
+ auto (bool): Whether to use minimum rectangle.
1487
+ scaleFill (bool): Whether to stretch the image to new_shape.
1488
+ scaleup (bool): Whether to allow scaling up. If False, only scale down.
1489
+ stride (int): Stride for rounding padding.
1490
+ center (bool): Whether to center the image or align to top-left.
1491
+
1492
+ Methods:
1493
+ __call__: Resize and pad image, update labels and bounding boxes.
1494
+
1495
+ Examples:
1496
+ >>> transform = LetterBox(new_shape=(640, 640))
1497
+ >>> result = transform(labels)
1498
+ >>> resized_img = result["img"]
1499
+ >>> updated_instances = result["instances"]
1500
+ """
686
1501
 
687
1502
  def __init__(self, new_shape=(640, 640), auto=False, scaleFill=False, scaleup=True, center=True, stride=32):
688
- """Initialize LetterBox object with specific parameters."""
1503
+ """
1504
+ Initialize LetterBox object for resizing and padding images.
1505
+
1506
+ This class is designed to resize and pad images for object detection, instance segmentation, and pose estimation
1507
+ tasks. It supports various resizing modes including auto-sizing, scale-fill, and letterboxing.
1508
+
1509
+ Args:
1510
+ new_shape (Tuple[int, int]): Target size (height, width) for the resized image.
1511
+ auto (bool): If True, use minimum rectangle to resize. If False, use new_shape directly.
1512
+ scaleFill (bool): If True, stretch the image to new_shape without padding.
1513
+ scaleup (bool): If True, allow scaling up. If False, only scale down.
1514
+ center (bool): If True, center the placed image. If False, place image in top-left corner.
1515
+ stride (int): Stride of the model (e.g., 32 for YOLOv5).
1516
+
1517
+ Attributes:
1518
+ new_shape (Tuple[int, int]): Target size for the resized image.
1519
+ auto (bool): Flag for using minimum rectangle resizing.
1520
+ scaleFill (bool): Flag for stretching image without padding.
1521
+ scaleup (bool): Flag for allowing upscaling.
1522
+ stride (int): Stride value for ensuring image size is divisible by stride.
1523
+
1524
+ Examples:
1525
+ >>> letterbox = LetterBox(new_shape=(640, 640), auto=False, scaleFill=False, scaleup=True, stride=32)
1526
+ >>> resized_img = letterbox(original_img)
1527
+ """
689
1528
  self.new_shape = new_shape
690
1529
  self.auto = auto
691
1530
  self.scaleFill = scaleFill
@@ -694,7 +1533,27 @@ class LetterBox:
694
1533
  self.center = center # Put the image in the middle or top-left
695
1534
 
696
1535
  def __call__(self, labels=None, image=None):
697
- """Return updated labels and image with added border."""
1536
+ """
1537
+ Resizes and pads an image for object detection, instance segmentation, or pose estimation tasks.
1538
+
1539
+ This method applies letterboxing to the input image, which involves resizing the image while maintaining its
1540
+ aspect ratio and adding padding to fit the new shape. It also updates any associated labels accordingly.
1541
+
1542
+ Args:
1543
+ labels (Dict | None): A dictionary containing image data and associated labels, or empty dict if None.
1544
+ image (np.ndarray | None): The input image as a numpy array. If None, the image is taken from 'labels'.
1545
+
1546
+ Returns:
1547
+ (Dict | Tuple): If 'labels' is provided, returns an updated dictionary with the resized and padded image,
1548
+ updated labels, and additional metadata. If 'labels' is empty, returns a tuple containing the resized
1549
+ and padded image, and a tuple of (ratio, (left_pad, top_pad)).
1550
+
1551
+ Examples:
1552
+ >>> letterbox = LetterBox(new_shape=(640, 640))
1553
+ >>> result = letterbox(labels={"img": np.zeros((480, 640, 3)), "instances": Instances(...)})
1554
+ >>> resized_img = result["img"]
1555
+ >>> updated_instances = result["instances"]
1556
+ """
698
1557
  if labels is None:
699
1558
  labels = {}
700
1559
  img = labels.get("img") if image is None else image
@@ -734,15 +1593,37 @@ class LetterBox:
734
1593
  labels["ratio_pad"] = (labels["ratio_pad"], (left, top)) # for evaluation
735
1594
 
736
1595
  if len(labels):
737
- labels = self._update_labels(labels, ratio, dw, dh)
1596
+ labels = self._update_labels(labels, ratio, left, top)
738
1597
  labels["img"] = img
739
1598
  labels["resized_shape"] = new_shape
740
1599
  return labels
741
1600
  else:
742
1601
  return img
743
1602
 
744
- def _update_labels(self, labels, ratio, padw, padh):
745
- """Update labels."""
1603
+ @staticmethod
1604
+ def _update_labels(labels, ratio, padw, padh):
1605
+ """
1606
+ Updates labels after applying letterboxing to an image.
1607
+
1608
+ This method modifies the bounding box coordinates of instances in the labels
1609
+ to account for resizing and padding applied during letterboxing.
1610
+
1611
+ Args:
1612
+ labels (Dict): A dictionary containing image labels and instances.
1613
+ ratio (Tuple[float, float]): Scaling ratios (width, height) applied to the image.
1614
+ padw (float): Padding width added to the image.
1615
+ padh (float): Padding height added to the image.
1616
+
1617
+ Returns:
1618
+ (Dict): Updated labels dictionary with modified instance coordinates.
1619
+
1620
+ Examples:
1621
+ >>> letterbox = LetterBox(new_shape=(640, 640))
1622
+ >>> labels = {"instances": Instances(...)}
1623
+ >>> ratio = (0.5, 0.5)
1624
+ >>> padw, padh = 10, 20
1625
+ >>> updated_labels = letterbox._update_labels(labels, ratio, padw, padh)
1626
+ """
746
1627
  labels["instances"].convert_bbox(format="xyxy")
747
1628
  labels["instances"].denormalize(*labels["img"].shape[:2][::-1])
748
1629
  labels["instances"].scale(*ratio)
@@ -750,91 +1631,217 @@ class LetterBox:
750
1631
  return labels
751
1632
 
752
1633
 
753
- class CopyPaste:
1634
+ class CopyPaste(BaseMixTransform):
754
1635
  """
755
- Implements the Copy-Paste augmentation as described in the paper https://arxiv.org/abs/2012.07177. This class is
756
- responsible for applying the Copy-Paste augmentation on images and their corresponding instances.
1636
+ CopyPaste class for applying Copy-Paste augmentation to image datasets.
1637
+
1638
+ This class implements the Copy-Paste augmentation technique as described in the paper "Simple Copy-Paste is a Strong
1639
+ Data Augmentation Method for Instance Segmentation" (https://arxiv.org/abs/2012.07177). It combines objects from
1640
+ different images to create new training samples.
1641
+
1642
+ Attributes:
1643
+ dataset (Any): The dataset to which Copy-Paste augmentation will be applied.
1644
+ pre_transform (Callable | None): Optional transform to apply before Copy-Paste.
1645
+ p (float): Probability of applying Copy-Paste augmentation.
1646
+
1647
+ Methods:
1648
+ get_indexes: Returns a random index from the dataset.
1649
+ _mix_transform: Applies Copy-Paste augmentation to the input labels.
1650
+ __call__: Applies the Copy-Paste transformation to images and annotations.
1651
+
1652
+ Examples:
1653
+ >>> from ultralytics.data.augment import CopyPaste
1654
+ >>> dataset = YourDataset(...) # Your image dataset
1655
+ >>> copypaste = CopyPaste(dataset, p=0.5)
1656
+ >>> augmented_labels = copypaste(original_labels)
757
1657
  """
758
1658
 
759
- def __init__(self, p=0.5) -> None:
760
- """
761
- Initializes the CopyPaste class with a given probability.
1659
+ def __init__(self, dataset=None, pre_transform=None, p=0.5, mode="flip") -> None:
1660
+ """Initializes CopyPaste object with dataset, pre_transform, and probability of applying MixUp."""
1661
+ super().__init__(dataset=dataset, pre_transform=pre_transform, p=p)
1662
+ assert mode in {"flip", "mixup"}, f"Expected `mode` to be `flip` or `mixup`, but got {mode}."
1663
+ self.mode = mode
762
1664
 
763
- Args:
764
- p (float, optional): The probability of applying the Copy-Paste augmentation. Must be between 0 and 1.
765
- Default is 0.5.
766
- """
767
- self.p = p
1665
+ def get_indexes(self):
1666
+ """Returns a list of random indexes from the dataset for CopyPaste augmentation."""
1667
+ return random.randint(0, len(self.dataset) - 1)
1668
+
1669
+ def _mix_transform(self, labels):
1670
+ """Applies Copy-Paste augmentation to combine objects from another image into the current image."""
1671
+ labels2 = labels["mix_labels"][0]
1672
+ return self._transform(labels, labels2)
768
1673
 
769
1674
  def __call__(self, labels):
770
- """
771
- Applies the Copy-Paste augmentation to the given image and instances.
1675
+ """Applies Copy-Paste augmentation to an image and its labels."""
1676
+ if len(labels["instances"].segments) == 0 or self.p == 0:
1677
+ return labels
1678
+ if self.mode == "flip":
1679
+ return self._transform(labels)
772
1680
 
773
- Args:
774
- labels (dict): A dictionary containing:
775
- - 'img': The image to augment.
776
- - 'cls': Class labels associated with the instances.
777
- - 'instances': Object containing bounding boxes, and optionally, keypoints and segments.
1681
+ # Get index of one or three other images
1682
+ indexes = self.get_indexes()
1683
+ if isinstance(indexes, int):
1684
+ indexes = [indexes]
778
1685
 
779
- Returns:
780
- (dict): Dict with augmented image and updated instances under the 'img', 'cls', and 'instances' keys.
1686
+ # Get images information will be used for Mosaic or MixUp
1687
+ mix_labels = [self.dataset.get_image_and_label(i) for i in indexes]
781
1688
 
782
- Notes:
783
- 1. Instances are expected to have 'segments' as one of their attributes for this augmentation to work.
784
- 2. This method modifies the input dictionary 'labels' in place.
785
- """
786
- im = labels["img"]
787
- cls = labels["cls"]
1689
+ if self.pre_transform is not None:
1690
+ for i, data in enumerate(mix_labels):
1691
+ mix_labels[i] = self.pre_transform(data)
1692
+ labels["mix_labels"] = mix_labels
1693
+
1694
+ # Update cls and texts
1695
+ labels = self._update_label_text(labels)
1696
+ # Mosaic or MixUp
1697
+ labels = self._mix_transform(labels)
1698
+ labels.pop("mix_labels", None)
1699
+ return labels
1700
+
1701
+ def _transform(self, labels1, labels2={}):
1702
+ """Applies Copy-Paste augmentation to combine objects from another image into the current image."""
1703
+ im = labels1["img"]
1704
+ cls = labels1["cls"]
788
1705
  h, w = im.shape[:2]
789
- instances = labels.pop("instances")
1706
+ instances = labels1.pop("instances")
790
1707
  instances.convert_bbox(format="xyxy")
791
1708
  instances.denormalize(w, h)
792
- if self.p and len(instances.segments):
793
- n = len(instances)
794
- _, w, _ = im.shape # height, width, channels
795
- im_new = np.zeros(im.shape, np.uint8)
796
-
797
- # Calculate ioa first then select indexes randomly
798
- ins_flip = deepcopy(instances)
799
- ins_flip.fliplr(w)
800
-
801
- ioa = bbox_ioa(ins_flip.bboxes, instances.bboxes) # intersection over area, (N, M)
802
- indexes = np.nonzero((ioa < 0.30).all(1))[0] # (N, )
803
- n = len(indexes)
804
- for j in random.sample(list(indexes), k=round(self.p * n)):
805
- cls = np.concatenate((cls, cls[[j]]), axis=0)
806
- instances = Instances.concatenate((instances, ins_flip[[j]]), axis=0)
807
- cv2.drawContours(im_new, instances.segments[[j]].astype(np.int32), -1, (1, 1, 1), cv2.FILLED)
808
-
809
- result = cv2.flip(im, 1) # augment segments (flip left-right)
810
- i = cv2.flip(im_new, 1).astype(bool)
811
- im[i] = result[i]
812
-
813
- labels["img"] = im
814
- labels["cls"] = cls
815
- labels["instances"] = instances
816
- return labels
1709
+
1710
+ im_new = np.zeros(im.shape, np.uint8)
1711
+ instances2 = labels2.pop("instances", None)
1712
+ if instances2 is None:
1713
+ instances2 = deepcopy(instances)
1714
+ instances2.fliplr(w)
1715
+ ioa = bbox_ioa(instances2.bboxes, instances.bboxes) # intersection over area, (N, M)
1716
+ indexes = np.nonzero((ioa < 0.30).all(1))[0] # (N, )
1717
+ n = len(indexes)
1718
+ sorted_idx = np.argsort(ioa.max(1)[indexes])
1719
+ indexes = indexes[sorted_idx]
1720
+ for j in indexes[: round(self.p * n)]:
1721
+ cls = np.concatenate((cls, labels2.get("cls", cls)[[j]]), axis=0)
1722
+ instances = Instances.concatenate((instances, instances2[[j]]), axis=0)
1723
+ cv2.drawContours(im_new, instances2.segments[[j]].astype(np.int32), -1, (1, 1, 1), cv2.FILLED)
1724
+
1725
+ result = labels2.get("img", cv2.flip(im, 1)) # augment segments
1726
+ i = im_new.astype(bool)
1727
+ im[i] = result[i]
1728
+
1729
+ labels1["img"] = im
1730
+ labels1["cls"] = cls
1731
+ labels1["instances"] = instances
1732
+ return labels1
817
1733
 
818
1734
 
819
1735
  class Albumentations:
820
1736
  """
821
- Albumentations transformations.
1737
+ Albumentations transformations for image augmentation.
1738
+
1739
+ This class applies various image transformations using the Albumentations library. It includes operations such as
1740
+ Blur, Median Blur, conversion to grayscale, Contrast Limited Adaptive Histogram Equalization (CLAHE), random changes
1741
+ in brightness and contrast, RandomGamma, and image quality reduction through compression.
822
1742
 
823
- Optional, uninstall package to disable. Applies Blur, Median Blur, convert to grayscale, Contrast Limited Adaptive
824
- Histogram Equalization, random change of brightness and contrast, RandomGamma and lowering of image quality by
825
- compression.
1743
+ Attributes:
1744
+ p (float): Probability of applying the transformations.
1745
+ transform (albumentations.Compose): Composed Albumentations transforms.
1746
+ contains_spatial (bool): Indicates if the transforms include spatial operations.
1747
+
1748
+ Methods:
1749
+ __call__: Applies the Albumentations transformations to the input labels.
1750
+
1751
+ Examples:
1752
+ >>> transform = Albumentations(p=0.5)
1753
+ >>> augmented_labels = transform(labels)
1754
+
1755
+ Notes:
1756
+ - The Albumentations package must be installed to use this class.
1757
+ - If the package is not installed or an error occurs during initialization, the transform will be set to None.
1758
+ - Spatial transforms are handled differently and require special processing for bounding boxes.
826
1759
  """
827
1760
 
828
1761
  def __init__(self, p=1.0):
829
- """Initialize the transform object for YOLO bbox formatted params."""
1762
+ """
1763
+ Initialize the Albumentations transform object for YOLO bbox formatted parameters.
1764
+
1765
+ This class applies various image augmentations using the Albumentations library, including Blur, Median Blur,
1766
+ conversion to grayscale, Contrast Limited Adaptive Histogram Equalization, random changes of brightness and
1767
+ contrast, RandomGamma, and image quality reduction through compression.
1768
+
1769
+ Args:
1770
+ p (float): Probability of applying the augmentations. Must be between 0 and 1.
1771
+
1772
+ Attributes:
1773
+ p (float): Probability of applying the augmentations.
1774
+ transform (albumentations.Compose): Composed Albumentations transforms.
1775
+ contains_spatial (bool): Indicates if the transforms include spatial transformations.
1776
+
1777
+ Raises:
1778
+ ImportError: If the Albumentations package is not installed.
1779
+ Exception: For any other errors during initialization.
1780
+
1781
+ Examples:
1782
+ >>> transform = Albumentations(p=0.5)
1783
+ >>> augmented = transform(image=image, bboxes=bboxes, class_labels=classes)
1784
+ >>> augmented_image = augmented["image"]
1785
+ >>> augmented_bboxes = augmented["bboxes"]
1786
+
1787
+ Notes:
1788
+ - Requires Albumentations version 1.0.3 or higher.
1789
+ - Spatial transforms are handled differently to ensure bbox compatibility.
1790
+ - Some transforms are applied with very low probability (0.01) by default.
1791
+ """
830
1792
  self.p = p
831
1793
  self.transform = None
832
1794
  prefix = colorstr("albumentations: ")
1795
+
833
1796
  try:
834
1797
  import albumentations as A
835
1798
 
836
1799
  check_version(A.__version__, "1.0.3", hard=True) # version requirement
837
1800
 
1801
+ # List of possible spatial transforms
1802
+ spatial_transforms = {
1803
+ "Affine",
1804
+ "BBoxSafeRandomCrop",
1805
+ "CenterCrop",
1806
+ "CoarseDropout",
1807
+ "Crop",
1808
+ "CropAndPad",
1809
+ "CropNonEmptyMaskIfExists",
1810
+ "D4",
1811
+ "ElasticTransform",
1812
+ "Flip",
1813
+ "GridDistortion",
1814
+ "GridDropout",
1815
+ "HorizontalFlip",
1816
+ "Lambda",
1817
+ "LongestMaxSize",
1818
+ "MaskDropout",
1819
+ "MixUp",
1820
+ "Morphological",
1821
+ "NoOp",
1822
+ "OpticalDistortion",
1823
+ "PadIfNeeded",
1824
+ "Perspective",
1825
+ "PiecewiseAffine",
1826
+ "PixelDropout",
1827
+ "RandomCrop",
1828
+ "RandomCropFromBorders",
1829
+ "RandomGridShuffle",
1830
+ "RandomResizedCrop",
1831
+ "RandomRotate90",
1832
+ "RandomScale",
1833
+ "RandomSizedBBoxSafeCrop",
1834
+ "RandomSizedCrop",
1835
+ "Resize",
1836
+ "Rotate",
1837
+ "SafeRotate",
1838
+ "ShiftScaleRotate",
1839
+ "SmallestMaxSize",
1840
+ "Transpose",
1841
+ "VerticalFlip",
1842
+ "XYMasking",
1843
+ } # from https://albumentations.ai/docs/getting_started/transforms_and_targets/#spatial-level-transforms
1844
+
838
1845
  # Transforms
839
1846
  T = [
840
1847
  A.Blur(p=0.01),
@@ -845,8 +1852,17 @@ class Albumentations:
845
1852
  A.RandomGamma(p=0.0),
846
1853
  A.ImageCompression(quality_lower=75, p=0.0),
847
1854
  ]
848
- self.transform = A.Compose(T, bbox_params=A.BboxParams(format="yolo", label_fields=["class_labels"]))
849
1855
 
1856
+ # Compose transforms
1857
+ self.contains_spatial = any(transform.__class__.__name__ in spatial_transforms for transform in T)
1858
+ self.transform = (
1859
+ A.Compose(T, bbox_params=A.BboxParams(format="yolo", label_fields=["class_labels"]))
1860
+ if self.contains_spatial
1861
+ else A.Compose(T)
1862
+ )
1863
+ if hasattr(self.transform, "set_random_seed"):
1864
+ # Required for deterministic transforms in albumentations>=1.4.21
1865
+ self.transform.set_random_seed(torch.initial_seed())
850
1866
  LOGGER.info(prefix + ", ".join(f"{x}".replace("always_apply=False, ", "") for x in T if x.p))
851
1867
  except ImportError: # package not installed, skip
852
1868
  pass
@@ -854,38 +1870,87 @@ class Albumentations:
854
1870
  LOGGER.info(f"{prefix}{e}")
855
1871
 
856
1872
  def __call__(self, labels):
857
- """Generates object detections and returns a dictionary with detection results."""
858
- im = labels["img"]
859
- cls = labels["cls"]
860
- if len(cls):
861
- labels["instances"].convert_bbox("xywh")
862
- labels["instances"].normalize(*im.shape[:2][::-1])
863
- bboxes = labels["instances"].bboxes
864
- # TODO: add supports of segments and keypoints
865
- if self.transform and random.random() < self.p:
1873
+ """
1874
+ Applies Albumentations transformations to input labels.
1875
+
1876
+ This method applies a series of image augmentations using the Albumentations library. It can perform both
1877
+ spatial and non-spatial transformations on the input image and its corresponding labels.
1878
+
1879
+ Args:
1880
+ labels (Dict): A dictionary containing image data and annotations. Expected keys are:
1881
+ - 'img': numpy.ndarray representing the image
1882
+ - 'cls': numpy.ndarray of class labels
1883
+ - 'instances': object containing bounding boxes and other instance information
1884
+
1885
+ Returns:
1886
+ (Dict): The input dictionary with augmented image and updated annotations.
1887
+
1888
+ Examples:
1889
+ >>> transform = Albumentations(p=0.5)
1890
+ >>> labels = {
1891
+ ... "img": np.random.rand(640, 640, 3),
1892
+ ... "cls": np.array([0, 1]),
1893
+ ... "instances": Instances(bboxes=np.array([[0, 0, 1, 1], [0.5, 0.5, 0.8, 0.8]])),
1894
+ ... }
1895
+ >>> augmented = transform(labels)
1896
+ >>> assert augmented["img"].shape == (640, 640, 3)
1897
+
1898
+ Notes:
1899
+ - The method applies transformations with probability self.p.
1900
+ - Spatial transforms update bounding boxes, while non-spatial transforms only modify the image.
1901
+ - Requires the Albumentations library to be installed.
1902
+ """
1903
+ if self.transform is None or random.random() > self.p:
1904
+ return labels
1905
+
1906
+ if self.contains_spatial:
1907
+ cls = labels["cls"]
1908
+ if len(cls):
1909
+ im = labels["img"]
1910
+ labels["instances"].convert_bbox("xywh")
1911
+ labels["instances"].normalize(*im.shape[:2][::-1])
1912
+ bboxes = labels["instances"].bboxes
1913
+ # TODO: add supports of segments and keypoints
866
1914
  new = self.transform(image=im, bboxes=bboxes, class_labels=cls) # transformed
867
1915
  if len(new["class_labels"]) > 0: # skip update if no bbox in new im
868
1916
  labels["img"] = new["image"]
869
1917
  labels["cls"] = np.array(new["class_labels"])
870
1918
  bboxes = np.array(new["bboxes"], dtype=np.float32)
871
- labels["instances"].update(bboxes=bboxes)
1919
+ labels["instances"].update(bboxes=bboxes)
1920
+ else:
1921
+ labels["img"] = self.transform(image=labels["img"])["image"] # transformed
1922
+
872
1923
  return labels
873
1924
 
874
1925
 
875
- # TODO: technically this is not an augmentation, maybe we should put this to another files
876
1926
  class Format:
877
1927
  """
878
- Formats image annotations for object detection, instance segmentation, and pose estimation tasks. The class
879
- standardizes the image and instance annotations to be used by the `collate_fn` in PyTorch DataLoader.
1928
+ A class for formatting image annotations for object detection, instance segmentation, and pose estimation tasks.
1929
+
1930
+ This class standardizes image and instance annotations to be used by the `collate_fn` in PyTorch DataLoader.
880
1931
 
881
1932
  Attributes:
882
- bbox_format (str): Format for bounding boxes. Default is 'xywh'.
883
- normalize (bool): Whether to normalize bounding boxes. Default is True.
884
- return_mask (bool): Return instance masks for segmentation. Default is False.
885
- return_keypoint (bool): Return keypoints for pose estimation. Default is False.
886
- mask_ratio (int): Downsample ratio for masks. Default is 4.
887
- mask_overlap (bool): Whether to overlap masks. Default is True.
888
- batch_idx (bool): Keep batch indexes. Default is True.
1933
+ bbox_format (str): Format for bounding boxes. Options are 'xywh' or 'xyxy'.
1934
+ normalize (bool): Whether to normalize bounding boxes.
1935
+ return_mask (bool): Whether to return instance masks for segmentation.
1936
+ return_keypoint (bool): Whether to return keypoints for pose estimation.
1937
+ return_obb (bool): Whether to return oriented bounding boxes.
1938
+ mask_ratio (int): Downsample ratio for masks.
1939
+ mask_overlap (bool): Whether to overlap masks.
1940
+ batch_idx (bool): Whether to keep batch indexes.
1941
+ bgr (float): The probability to return BGR images.
1942
+
1943
+ Methods:
1944
+ __call__: Formats labels dictionary with image, classes, bounding boxes, and optionally masks and keypoints.
1945
+ _format_img: Converts image from Numpy array to PyTorch tensor.
1946
+ _format_segments: Converts polygon points to bitmap masks.
1947
+
1948
+ Examples:
1949
+ >>> formatter = Format(bbox_format="xywh", normalize=True, return_mask=True)
1950
+ >>> formatted_labels = formatter(labels)
1951
+ >>> img = formatted_labels["img"]
1952
+ >>> bboxes = formatted_labels["bboxes"]
1953
+ >>> masks = formatted_labels["masks"]
889
1954
  """
890
1955
 
891
1956
  def __init__(
@@ -898,8 +1963,41 @@ class Format:
898
1963
  mask_ratio=4,
899
1964
  mask_overlap=True,
900
1965
  batch_idx=True,
1966
+ bgr=0.0,
901
1967
  ):
902
- """Initializes the Format class with given parameters."""
1968
+ """
1969
+ Initializes the Format class with given parameters for image and instance annotation formatting.
1970
+
1971
+ This class standardizes image and instance annotations for object detection, instance segmentation, and pose
1972
+ estimation tasks, preparing them for use in PyTorch DataLoader's `collate_fn`.
1973
+
1974
+ Args:
1975
+ bbox_format (str): Format for bounding boxes. Options are 'xywh', 'xyxy', etc.
1976
+ normalize (bool): Whether to normalize bounding boxes to [0,1].
1977
+ return_mask (bool): If True, returns instance masks for segmentation tasks.
1978
+ return_keypoint (bool): If True, returns keypoints for pose estimation tasks.
1979
+ return_obb (bool): If True, returns oriented bounding boxes.
1980
+ mask_ratio (int): Downsample ratio for masks.
1981
+ mask_overlap (bool): If True, allows mask overlap.
1982
+ batch_idx (bool): If True, keeps batch indexes.
1983
+ bgr (float): Probability of returning BGR images instead of RGB.
1984
+
1985
+ Attributes:
1986
+ bbox_format (str): Format for bounding boxes.
1987
+ normalize (bool): Whether bounding boxes are normalized.
1988
+ return_mask (bool): Whether to return instance masks.
1989
+ return_keypoint (bool): Whether to return keypoints.
1990
+ return_obb (bool): Whether to return oriented bounding boxes.
1991
+ mask_ratio (int): Downsample ratio for masks.
1992
+ mask_overlap (bool): Whether masks can overlap.
1993
+ batch_idx (bool): Whether to keep batch indexes.
1994
+ bgr (float): The probability to return BGR images.
1995
+
1996
+ Examples:
1997
+ >>> format = Format(bbox_format="xyxy", return_mask=True, return_keypoint=False)
1998
+ >>> print(format.bbox_format)
1999
+ xyxy
2000
+ """
903
2001
  self.bbox_format = bbox_format
904
2002
  self.normalize = normalize
905
2003
  self.return_mask = return_mask # set False when training detection only
@@ -908,9 +2006,37 @@ class Format:
908
2006
  self.mask_ratio = mask_ratio
909
2007
  self.mask_overlap = mask_overlap
910
2008
  self.batch_idx = batch_idx # keep the batch indexes
2009
+ self.bgr = bgr
911
2010
 
912
2011
  def __call__(self, labels):
913
- """Return formatted image, classes, bounding boxes & keypoints to be used by 'collate_fn'."""
2012
+ """
2013
+ Formats image annotations for object detection, instance segmentation, and pose estimation tasks.
2014
+
2015
+ This method standardizes the image and instance annotations to be used by the `collate_fn` in PyTorch
2016
+ DataLoader. It processes the input labels dictionary, converting annotations to the specified format and
2017
+ applying normalization if required.
2018
+
2019
+ Args:
2020
+ labels (Dict): A dictionary containing image and annotation data with the following keys:
2021
+ - 'img': The input image as a numpy array.
2022
+ - 'cls': Class labels for instances.
2023
+ - 'instances': An Instances object containing bounding boxes, segments, and keypoints.
2024
+
2025
+ Returns:
2026
+ (Dict): A dictionary with formatted data, including:
2027
+ - 'img': Formatted image tensor.
2028
+ - 'cls': Class label's tensor.
2029
+ - 'bboxes': Bounding boxes tensor in the specified format.
2030
+ - 'masks': Instance masks tensor (if return_mask is True).
2031
+ - 'keypoints': Keypoints tensor (if return_keypoint is True).
2032
+ - 'batch_idx': Batch index tensor (if batch_idx is True).
2033
+
2034
+ Examples:
2035
+ >>> formatter = Format(bbox_format="xywh", normalize=True, return_mask=True)
2036
+ >>> labels = {"img": np.random.rand(640, 640, 3), "cls": np.array([0, 1]), "instances": Instances(...)}
2037
+ >>> formatted_labels = formatter(labels)
2038
+ >>> print(formatted_labels.keys())
2039
+ """
914
2040
  img = labels.pop("img")
915
2041
  h, w = img.shape[:2]
916
2042
  cls = labels.pop("cls")
@@ -928,32 +2054,78 @@ class Format:
928
2054
  1 if self.mask_overlap else nl, img.shape[0] // self.mask_ratio, img.shape[1] // self.mask_ratio
929
2055
  )
930
2056
  labels["masks"] = masks
931
- if self.normalize:
932
- instances.normalize(w, h)
933
2057
  labels["img"] = self._format_img(img)
934
2058
  labels["cls"] = torch.from_numpy(cls) if nl else torch.zeros(nl)
935
2059
  labels["bboxes"] = torch.from_numpy(instances.bboxes) if nl else torch.zeros((nl, 4))
936
2060
  if self.return_keypoint:
937
2061
  labels["keypoints"] = torch.from_numpy(instances.keypoints)
2062
+ if self.normalize:
2063
+ labels["keypoints"][..., 0] /= w
2064
+ labels["keypoints"][..., 1] /= h
938
2065
  if self.return_obb:
939
2066
  labels["bboxes"] = (
940
2067
  xyxyxyxy2xywhr(torch.from_numpy(instances.segments)) if len(instances.segments) else torch.zeros((0, 5))
941
2068
  )
2069
+ # NOTE: need to normalize obb in xywhr format for width-height consistency
2070
+ if self.normalize:
2071
+ labels["bboxes"][:, [0, 2]] /= w
2072
+ labels["bboxes"][:, [1, 3]] /= h
942
2073
  # Then we can use collate_fn
943
2074
  if self.batch_idx:
944
2075
  labels["batch_idx"] = torch.zeros(nl)
945
2076
  return labels
946
2077
 
947
2078
  def _format_img(self, img):
948
- """Format the image for YOLO from Numpy array to PyTorch tensor."""
2079
+ """
2080
+ Formats an image for YOLO from a Numpy array to a PyTorch tensor.
2081
+
2082
+ This function performs the following operations:
2083
+ 1. Ensures the image has 3 dimensions (adds a channel dimension if needed).
2084
+ 2. Transposes the image from HWC to CHW format.
2085
+ 3. Optionally flips the color channels from RGB to BGR.
2086
+ 4. Converts the image to a contiguous array.
2087
+ 5. Converts the Numpy array to a PyTorch tensor.
2088
+
2089
+ Args:
2090
+ img (np.ndarray): Input image as a Numpy array with shape (H, W, C) or (H, W).
2091
+
2092
+ Returns:
2093
+ (torch.Tensor): Formatted image as a PyTorch tensor with shape (C, H, W).
2094
+
2095
+ Examples:
2096
+ >>> import numpy as np
2097
+ >>> img = np.random.rand(100, 100, 3)
2098
+ >>> formatted_img = self._format_img(img)
2099
+ >>> print(formatted_img.shape)
2100
+ torch.Size([3, 100, 100])
2101
+ """
949
2102
  if len(img.shape) < 3:
950
2103
  img = np.expand_dims(img, -1)
951
- img = np.ascontiguousarray(img.transpose(2, 0, 1)[::-1])
2104
+ img = img.transpose(2, 0, 1)
2105
+ img = np.ascontiguousarray(img[::-1] if random.uniform(0, 1) > self.bgr else img)
952
2106
  img = torch.from_numpy(img)
953
2107
  return img
954
2108
 
955
2109
  def _format_segments(self, instances, cls, w, h):
956
- """Convert polygon points to bitmap."""
2110
+ """
2111
+ Converts polygon segments to bitmap masks.
2112
+
2113
+ Args:
2114
+ instances (Instances): Object containing segment information.
2115
+ cls (numpy.ndarray): Class labels for each instance.
2116
+ w (int): Width of the image.
2117
+ h (int): Height of the image.
2118
+
2119
+ Returns:
2120
+ masks (numpy.ndarray): Bitmap masks with shape (N, H, W) or (1, H, W) if mask_overlap is True.
2121
+ instances (Instances): Updated instances object with sorted segments if mask_overlap is True.
2122
+ cls (numpy.ndarray): Updated class labels, sorted if mask_overlap is True.
2123
+
2124
+ Notes:
2125
+ - If self.mask_overlap is True, masks are overlapped and sorted by area.
2126
+ - If self.mask_overlap is False, each mask is represented separately.
2127
+ - Masks are downsampled according to self.mask_ratio.
2128
+ """
957
2129
  segments = instances.segments
958
2130
  if self.mask_overlap:
959
2131
  masks, sorted_idx = polygons2masks_overlap((h, w), segments, downsample_ratio=self.mask_ratio)
@@ -966,22 +2138,189 @@ class Format:
966
2138
  return masks, instances, cls
967
2139
 
968
2140
 
2141
+ class RandomLoadText:
2142
+ """
2143
+ Randomly samples positive and negative texts and updates class indices accordingly.
2144
+
2145
+ This class is responsible for sampling texts from a given set of class texts, including both positive
2146
+ (present in the image) and negative (not present in the image) samples. It updates the class indices
2147
+ to reflect the sampled texts and can optionally pad the text list to a fixed length.
2148
+
2149
+ Attributes:
2150
+ prompt_format (str): Format string for text prompts.
2151
+ neg_samples (Tuple[int, int]): Range for randomly sampling negative texts.
2152
+ max_samples (int): Maximum number of different text samples in one image.
2153
+ padding (bool): Whether to pad texts to max_samples.
2154
+ padding_value (str): The text used for padding when padding is True.
2155
+
2156
+ Methods:
2157
+ __call__: Processes the input labels and returns updated classes and texts.
2158
+
2159
+ Examples:
2160
+ >>> loader = RandomLoadText(prompt_format="Object: {}", neg_samples=(5, 10), max_samples=20)
2161
+ >>> labels = {"cls": [0, 1, 2], "texts": [["cat"], ["dog"], ["bird"]], "instances": [...]}
2162
+ >>> updated_labels = loader(labels)
2163
+ >>> print(updated_labels["texts"])
2164
+ ['Object: cat', 'Object: dog', 'Object: bird', 'Object: elephant', 'Object: car']
2165
+ """
2166
+
2167
+ def __init__(
2168
+ self,
2169
+ prompt_format: str = "{}",
2170
+ neg_samples: Tuple[int, int] = (80, 80),
2171
+ max_samples: int = 80,
2172
+ padding: bool = False,
2173
+ padding_value: str = "",
2174
+ ) -> None:
2175
+ """
2176
+ Initializes the RandomLoadText class for randomly sampling positive and negative texts.
2177
+
2178
+ This class is designed to randomly sample positive texts and negative texts, and update the class
2179
+ indices accordingly to the number of samples. It can be used for text-based object detection tasks.
2180
+
2181
+ Args:
2182
+ prompt_format (str): Format string for the prompt. Default is '{}'. The format string should
2183
+ contain a single pair of curly braces {} where the text will be inserted.
2184
+ neg_samples (Tuple[int, int]): A range to randomly sample negative texts. The first integer
2185
+ specifies the minimum number of negative samples, and the second integer specifies the
2186
+ maximum. Default is (80, 80).
2187
+ max_samples (int): The maximum number of different text samples in one image. Default is 80.
2188
+ padding (bool): Whether to pad texts to max_samples. If True, the number of texts will always
2189
+ be equal to max_samples. Default is False.
2190
+ padding_value (str): The padding text to use when padding is True. Default is an empty string.
2191
+
2192
+ Attributes:
2193
+ prompt_format (str): The format string for the prompt.
2194
+ neg_samples (Tuple[int, int]): The range for sampling negative texts.
2195
+ max_samples (int): The maximum number of text samples.
2196
+ padding (bool): Whether padding is enabled.
2197
+ padding_value (str): The value used for padding.
2198
+
2199
+ Examples:
2200
+ >>> random_load_text = RandomLoadText(prompt_format="Object: {}", neg_samples=(50, 100), max_samples=120)
2201
+ >>> random_load_text.prompt_format
2202
+ 'Object: {}'
2203
+ >>> random_load_text.neg_samples
2204
+ (50, 100)
2205
+ >>> random_load_text.max_samples
2206
+ 120
2207
+ """
2208
+ self.prompt_format = prompt_format
2209
+ self.neg_samples = neg_samples
2210
+ self.max_samples = max_samples
2211
+ self.padding = padding
2212
+ self.padding_value = padding_value
2213
+
2214
+ def __call__(self, labels: dict) -> dict:
2215
+ """
2216
+ Randomly samples positive and negative texts and updates class indices accordingly.
2217
+
2218
+ This method samples positive texts based on the existing class labels in the image, and randomly
2219
+ selects negative texts from the remaining classes. It then updates the class indices to match the
2220
+ new sampled text order.
2221
+
2222
+ Args:
2223
+ labels (Dict): A dictionary containing image labels and metadata. Must include 'texts' and 'cls' keys.
2224
+
2225
+ Returns:
2226
+ (Dict): Updated labels dictionary with new 'cls' and 'texts' entries.
2227
+
2228
+ Examples:
2229
+ >>> loader = RandomLoadText(prompt_format="A photo of {}", neg_samples=(5, 10), max_samples=20)
2230
+ >>> labels = {"cls": np.array([[0], [1], [2]]), "texts": [["dog"], ["cat"], ["bird"]]}
2231
+ >>> updated_labels = loader(labels)
2232
+ """
2233
+ assert "texts" in labels, "No texts found in labels."
2234
+ class_texts = labels["texts"]
2235
+ num_classes = len(class_texts)
2236
+ cls = np.asarray(labels.pop("cls"), dtype=int)
2237
+ pos_labels = np.unique(cls).tolist()
2238
+
2239
+ if len(pos_labels) > self.max_samples:
2240
+ pos_labels = random.sample(pos_labels, k=self.max_samples)
2241
+
2242
+ neg_samples = min(min(num_classes, self.max_samples) - len(pos_labels), random.randint(*self.neg_samples))
2243
+ neg_labels = [i for i in range(num_classes) if i not in pos_labels]
2244
+ neg_labels = random.sample(neg_labels, k=neg_samples)
2245
+
2246
+ sampled_labels = pos_labels + neg_labels
2247
+ random.shuffle(sampled_labels)
2248
+
2249
+ label2ids = {label: i for i, label in enumerate(sampled_labels)}
2250
+ valid_idx = np.zeros(len(labels["instances"]), dtype=bool)
2251
+ new_cls = []
2252
+ for i, label in enumerate(cls.squeeze(-1).tolist()):
2253
+ if label not in label2ids:
2254
+ continue
2255
+ valid_idx[i] = True
2256
+ new_cls.append([label2ids[label]])
2257
+ labels["instances"] = labels["instances"][valid_idx]
2258
+ labels["cls"] = np.array(new_cls)
2259
+
2260
+ # Randomly select one prompt when there's more than one prompts
2261
+ texts = []
2262
+ for label in sampled_labels:
2263
+ prompts = class_texts[label]
2264
+ assert len(prompts) > 0
2265
+ prompt = self.prompt_format.format(prompts[random.randrange(len(prompts))])
2266
+ texts.append(prompt)
2267
+
2268
+ if self.padding:
2269
+ valid_labels = len(pos_labels) + len(neg_labels)
2270
+ num_padding = self.max_samples - valid_labels
2271
+ if num_padding > 0:
2272
+ texts += [self.padding_value] * num_padding
2273
+
2274
+ labels["texts"] = texts
2275
+ return labels
2276
+
2277
+
969
2278
  def v8_transforms(dataset, imgsz, hyp, stretch=False):
970
- """Convert images to a size suitable for YOLOv8 training."""
971
- pre_transform = Compose(
972
- [
973
- Mosaic(dataset, imgsz=imgsz, p=hyp.mosaic),
974
- CopyPaste(p=hyp.copy_paste),
975
- RandomPerspective(
976
- degrees=hyp.degrees,
977
- translate=hyp.translate,
978
- scale=hyp.scale,
979
- shear=hyp.shear,
980
- perspective=hyp.perspective,
981
- pre_transform=None if stretch else LetterBox(new_shape=(imgsz, imgsz)),
982
- ),
983
- ]
2279
+ """
2280
+ Applies a series of image transformations for training.
2281
+
2282
+ This function creates a composition of image augmentation techniques to prepare images for YOLO training.
2283
+ It includes operations such as mosaic, copy-paste, random perspective, mixup, and various color adjustments.
2284
+
2285
+ Args:
2286
+ dataset (Dataset): The dataset object containing image data and annotations.
2287
+ imgsz (int): The target image size for resizing.
2288
+ hyp (Namespace): A dictionary of hyperparameters controlling various aspects of the transformations.
2289
+ stretch (bool): If True, applies stretching to the image. If False, uses LetterBox resizing.
2290
+
2291
+ Returns:
2292
+ (Compose): A composition of image transformations to be applied to the dataset.
2293
+
2294
+ Examples:
2295
+ >>> from ultralytics.data.dataset import YOLODataset
2296
+ >>> from ultralytics.utils import IterableSimpleNamespace
2297
+ >>> dataset = YOLODataset(img_path="path/to/images", imgsz=640)
2298
+ >>> hyp = IterableSimpleNamespace(mosaic=1.0, copy_paste=0.5, degrees=10.0, translate=0.2, scale=0.9)
2299
+ >>> transforms = v8_transforms(dataset, imgsz=640, hyp=hyp)
2300
+ >>> augmented_data = transforms(dataset[0])
2301
+ """
2302
+ mosaic = Mosaic(dataset, imgsz=imgsz, p=hyp.mosaic)
2303
+ affine = RandomPerspective(
2304
+ degrees=hyp.degrees,
2305
+ translate=hyp.translate,
2306
+ scale=hyp.scale,
2307
+ shear=hyp.shear,
2308
+ perspective=hyp.perspective,
2309
+ pre_transform=None if stretch else LetterBox(new_shape=(imgsz, imgsz)),
984
2310
  )
2311
+
2312
+ pre_transform = Compose([mosaic, affine])
2313
+ if hyp.copy_paste_mode == "flip":
2314
+ pre_transform.insert(1, CopyPaste(p=hyp.copy_paste, mode=hyp.copy_paste_mode))
2315
+ else:
2316
+ pre_transform.append(
2317
+ CopyPaste(
2318
+ dataset,
2319
+ pre_transform=Compose([Mosaic(dataset, imgsz=imgsz, p=hyp.mosaic), affine]),
2320
+ p=hyp.copy_paste,
2321
+ mode=hyp.copy_paste_mode,
2322
+ )
2323
+ )
985
2324
  flip_idx = dataset.data.get("flip_idx", []) # for keypoints augmentation
986
2325
  if dataset.use_keypoints:
987
2326
  kpt_shape = dataset.data.get("kpt_shape", None)
@@ -1008,51 +2347,59 @@ def classify_transforms(
1008
2347
  size=224,
1009
2348
  mean=DEFAULT_MEAN,
1010
2349
  std=DEFAULT_STD,
1011
- interpolation: T.InterpolationMode = T.InterpolationMode.BILINEAR,
1012
- crop_fraction: float = DEFAULT_CROP_FTACTION,
2350
+ interpolation="BILINEAR",
2351
+ crop_fraction: float = DEFAULT_CROP_FRACTION,
1013
2352
  ):
1014
2353
  """
1015
- Classification transforms for evaluation/inference. Inspired by timm/data/transforms_factory.py.
2354
+ Creates a composition of image transforms for classification tasks.
2355
+
2356
+ This function generates a sequence of torchvision transforms suitable for preprocessing images
2357
+ for classification models during evaluation or inference. The transforms include resizing,
2358
+ center cropping, conversion to tensor, and normalization.
1016
2359
 
1017
2360
  Args:
1018
- size (int): image size
1019
- mean (tuple): mean values of RGB channels
1020
- std (tuple): std values of RGB channels
1021
- interpolation (T.InterpolationMode): interpolation mode. default is T.InterpolationMode.BILINEAR.
1022
- crop_fraction (float): fraction of image to crop. default is 1.0.
2361
+ size (int | tuple): The target size for the transformed image. If an int, it defines the shortest edge. If a
2362
+ tuple, it defines (height, width).
2363
+ mean (tuple): Mean values for each RGB channel used in normalization.
2364
+ std (tuple): Standard deviation values for each RGB channel used in normalization.
2365
+ interpolation (str): Interpolation method of either 'NEAREST', 'BILINEAR' or 'BICUBIC'.
2366
+ crop_fraction (float): Fraction of the image to be cropped.
1023
2367
 
1024
2368
  Returns:
1025
- (T.Compose): torchvision transforms
2369
+ (torchvision.transforms.Compose): A composition of torchvision transforms.
2370
+
2371
+ Examples:
2372
+ >>> transforms = classify_transforms(size=224)
2373
+ >>> img = Image.open("path/to/image.jpg")
2374
+ >>> transformed_img = transforms(img)
1026
2375
  """
2376
+ import torchvision.transforms as T # scope for faster 'import ultralytics'
1027
2377
 
1028
2378
  if isinstance(size, (tuple, list)):
1029
- assert len(size) == 2
2379
+ assert len(size) == 2, f"'size' tuples must be length 2, not length {len(size)}"
1030
2380
  scale_size = tuple(math.floor(x / crop_fraction) for x in size)
1031
2381
  else:
1032
2382
  scale_size = math.floor(size / crop_fraction)
1033
2383
  scale_size = (scale_size, scale_size)
1034
2384
 
1035
- # aspect ratio is preserved, crops center within image, no borders are added, image is lost
2385
+ # Aspect ratio is preserved, crops center within image, no borders are added, image is lost
1036
2386
  if scale_size[0] == scale_size[1]:
1037
- # simple case, use torchvision built-in Resize w/ shortest edge mode (scalar size arg)
1038
- tfl = [T.Resize(scale_size[0], interpolation=interpolation)]
2387
+ # Simple case, use torchvision built-in Resize with the shortest edge mode (scalar size arg)
2388
+ tfl = [T.Resize(scale_size[0], interpolation=getattr(T.InterpolationMode, interpolation))]
1039
2389
  else:
1040
- # resize shortest edge to matching target dim for non-square target
2390
+ # Resize the shortest edge to matching target dim for non-square target
1041
2391
  tfl = [T.Resize(scale_size)]
1042
- tfl += [T.CenterCrop(size)]
1043
-
1044
- tfl += [
1045
- T.ToTensor(),
1046
- T.Normalize(
1047
- mean=torch.tensor(mean),
1048
- std=torch.tensor(std),
1049
- ),
1050
- ]
1051
-
2392
+ tfl.extend(
2393
+ [
2394
+ T.CenterCrop(size),
2395
+ T.ToTensor(),
2396
+ T.Normalize(mean=torch.tensor(mean), std=torch.tensor(std)),
2397
+ ]
2398
+ )
1052
2399
  return T.Compose(tfl)
1053
2400
 
1054
2401
 
1055
- # Classification augmentations train ---------------------------------------------------------------------------------------
2402
+ # Classification training augmentations --------------------------------------------------------------------------------
1056
2403
  def classify_augmentations(
1057
2404
  size=224,
1058
2405
  mean=DEFAULT_MEAN,
@@ -1067,64 +2414,74 @@ def classify_augmentations(
1067
2414
  hsv_v=0.4, # image HSV-Value augmentation (fraction)
1068
2415
  force_color_jitter=False,
1069
2416
  erasing=0.0,
1070
- interpolation: T.InterpolationMode = T.InterpolationMode.BILINEAR,
2417
+ interpolation="BILINEAR",
1071
2418
  ):
1072
2419
  """
1073
- Classification transforms with augmentation for training. Inspired by timm/data/transforms_factory.py.
2420
+ Creates a composition of image augmentation transforms for classification tasks.
2421
+
2422
+ This function generates a set of image transformations suitable for training classification models. It includes
2423
+ options for resizing, flipping, color jittering, auto augmentation, and random erasing.
1074
2424
 
1075
2425
  Args:
1076
- size (int): image size
1077
- scale (tuple): scale range of the image. default is (0.08, 1.0)
1078
- ratio (tuple): aspect ratio range of the image. default is (3./4., 4./3.)
1079
- mean (tuple): mean values of RGB channels
1080
- std (tuple): std values of RGB channels
1081
- hflip (float): probability of horizontal flip
1082
- vflip (float): probability of vertical flip
1083
- auto_augment (str): auto augmentation policy. can be 'randaugment', 'augmix', 'autoaugment' or None.
1084
- hsv_h (float): image HSV-Hue augmentation (fraction)
1085
- hsv_s (float): image HSV-Saturation augmentation (fraction)
1086
- hsv_v (float): image HSV-Value augmentation (fraction)
1087
- force_color_jitter (bool): force to apply color jitter even if auto augment is enabled
1088
- erasing (float): probability of random erasing
1089
- interpolation (T.InterpolationMode): interpolation mode. default is T.InterpolationMode.BILINEAR.
2426
+ size (int): Target size for the image after transformations.
2427
+ mean (tuple): Mean values for normalization, one per channel.
2428
+ std (tuple): Standard deviation values for normalization, one per channel.
2429
+ scale (tuple | None): Range of size of the origin size cropped.
2430
+ ratio (tuple | None): Range of aspect ratio of the origin aspect ratio cropped.
2431
+ hflip (float): Probability of horizontal flip.
2432
+ vflip (float): Probability of vertical flip.
2433
+ auto_augment (str | None): Auto augmentation policy. Can be 'randaugment', 'augmix', 'autoaugment' or None.
2434
+ hsv_h (float): Image HSV-Hue augmentation factor.
2435
+ hsv_s (float): Image HSV-Saturation augmentation factor.
2436
+ hsv_v (float): Image HSV-Value augmentation factor.
2437
+ force_color_jitter (bool): Whether to apply color jitter even if auto augment is enabled.
2438
+ erasing (float): Probability of random erasing.
2439
+ interpolation (str): Interpolation method of either 'NEAREST', 'BILINEAR' or 'BICUBIC'.
1090
2440
 
1091
2441
  Returns:
1092
- (T.Compose): torchvision transforms
2442
+ (torchvision.transforms.Compose): A composition of image augmentation transforms.
2443
+
2444
+ Examples:
2445
+ >>> transforms = classify_augmentations(size=224, auto_augment="randaugment")
2446
+ >>> augmented_image = transforms(original_image)
1093
2447
  """
1094
- # Transforms to apply if albumentations not installed
2448
+ # Transforms to apply if Albumentations not installed
2449
+ import torchvision.transforms as T # scope for faster 'import ultralytics'
2450
+
1095
2451
  if not isinstance(size, int):
1096
2452
  raise TypeError(f"classify_transforms() size {size} must be integer, not (list, tuple)")
1097
2453
  scale = tuple(scale or (0.08, 1.0)) # default imagenet scale range
1098
2454
  ratio = tuple(ratio or (3.0 / 4.0, 4.0 / 3.0)) # default imagenet ratio range
2455
+ interpolation = getattr(T.InterpolationMode, interpolation)
1099
2456
  primary_tfl = [T.RandomResizedCrop(size, scale=scale, ratio=ratio, interpolation=interpolation)]
1100
2457
  if hflip > 0.0:
1101
- primary_tfl += [T.RandomHorizontalFlip(p=hflip)]
2458
+ primary_tfl.append(T.RandomHorizontalFlip(p=hflip))
1102
2459
  if vflip > 0.0:
1103
- primary_tfl += [T.RandomVerticalFlip(p=vflip)]
2460
+ primary_tfl.append(T.RandomVerticalFlip(p=vflip))
1104
2461
 
1105
2462
  secondary_tfl = []
1106
2463
  disable_color_jitter = False
1107
2464
  if auto_augment:
1108
- assert isinstance(auto_augment, str)
2465
+ assert isinstance(auto_augment, str), f"Provided argument should be string, but got type {type(auto_augment)}"
1109
2466
  # color jitter is typically disabled if AA/RA on,
1110
2467
  # this allows override without breaking old hparm cfgs
1111
2468
  disable_color_jitter = not force_color_jitter
1112
2469
 
1113
2470
  if auto_augment == "randaugment":
1114
2471
  if TORCHVISION_0_11:
1115
- secondary_tfl += [T.RandAugment(interpolation=interpolation)]
2472
+ secondary_tfl.append(T.RandAugment(interpolation=interpolation))
1116
2473
  else:
1117
2474
  LOGGER.warning('"auto_augment=randaugment" requires torchvision >= 0.11.0. Disabling it.')
1118
2475
 
1119
2476
  elif auto_augment == "augmix":
1120
2477
  if TORCHVISION_0_13:
1121
- secondary_tfl += [T.AugMix(interpolation=interpolation)]
2478
+ secondary_tfl.append(T.AugMix(interpolation=interpolation))
1122
2479
  else:
1123
2480
  LOGGER.warning('"auto_augment=augmix" requires torchvision >= 0.13.0. Disabling it.')
1124
2481
 
1125
2482
  elif auto_augment == "autoaugment":
1126
2483
  if TORCHVISION_0_10:
1127
- secondary_tfl += [T.AutoAugment(interpolation=interpolation)]
2484
+ secondary_tfl.append(T.AutoAugment(interpolation=interpolation))
1128
2485
  else:
1129
2486
  LOGGER.warning('"auto_augment=autoaugment" requires torchvision >= 0.10.0. Disabling it.')
1130
2487
 
@@ -1135,7 +2492,7 @@ def classify_augmentations(
1135
2492
  )
1136
2493
 
1137
2494
  if not disable_color_jitter:
1138
- secondary_tfl += [T.ColorJitter(brightness=hsv_v, contrast=hsv_v, saturation=hsv_s, hue=hsv_h)]
2495
+ secondary_tfl.append(T.ColorJitter(brightness=hsv_v, contrast=hsv_v, saturation=hsv_s, hue=hsv_h))
1139
2496
 
1140
2497
  final_tfl = [
1141
2498
  T.ToTensor(),
@@ -1149,24 +2506,53 @@ def classify_augmentations(
1149
2506
  # NOTE: keep this class for backward compatibility
1150
2507
  class ClassifyLetterBox:
1151
2508
  """
1152
- YOLOv8 LetterBox class for image preprocessing, designed to be part of a transformation pipeline, e.g.,
1153
- T.Compose([LetterBox(size), ToTensor()]).
2509
+ A class for resizing and padding images for classification tasks.
2510
+
2511
+ This class is designed to be part of a transformation pipeline, e.g., T.Compose([LetterBox(size), ToTensor()]).
2512
+ It resizes and pads images to a specified size while maintaining the original aspect ratio.
1154
2513
 
1155
2514
  Attributes:
1156
2515
  h (int): Target height of the image.
1157
2516
  w (int): Target width of the image.
1158
- auto (bool): If True, automatically solves for short side using stride.
2517
+ auto (bool): If True, automatically calculates the short side using stride.
1159
2518
  stride (int): The stride value, used when 'auto' is True.
2519
+
2520
+ Methods:
2521
+ __call__: Applies the letterbox transformation to an input image.
2522
+
2523
+ Examples:
2524
+ >>> transform = ClassifyLetterBox(size=(640, 640), auto=False, stride=32)
2525
+ >>> img = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
2526
+ >>> result = transform(img)
2527
+ >>> print(result.shape)
2528
+ (640, 640, 3)
1160
2529
  """
1161
2530
 
1162
2531
  def __init__(self, size=(640, 640), auto=False, stride=32):
1163
2532
  """
1164
- Initializes the ClassifyLetterBox class with a target size, auto-flag, and stride.
2533
+ Initializes the ClassifyLetterBox object for image preprocessing.
2534
+
2535
+ This class is designed to be part of a transformation pipeline for image classification tasks. It resizes and
2536
+ pads images to a specified size while maintaining the original aspect ratio.
1165
2537
 
1166
2538
  Args:
1167
- size (Union[int, Tuple[int, int]]): The target dimensions (height, width) for the letterbox.
1168
- auto (bool): If True, automatically calculates the short side based on stride.
1169
- stride (int): The stride value, used when 'auto' is True.
2539
+ size (int | Tuple[int, int]): Target size for the letterboxed image. If an int, a square image of
2540
+ (size, size) is created. If a tuple, it should be (height, width).
2541
+ auto (bool): If True, automatically calculates the short side based on stride. Default is False.
2542
+ stride (int): The stride value, used when 'auto' is True. Default is 32.
2543
+
2544
+ Attributes:
2545
+ h (int): Target height of the letterboxed image.
2546
+ w (int): Target width of the letterboxed image.
2547
+ auto (bool): Flag indicating whether to automatically calculate short side.
2548
+ stride (int): Stride value for automatic short side calculation.
2549
+
2550
+ Examples:
2551
+ >>> transform = ClassifyLetterBox(size=224)
2552
+ >>> img = np.random.randint(0, 255, (480, 640, 3), dtype=np.uint8)
2553
+ >>> result = transform(img)
2554
+ >>> print(result.shape)
2555
+ (224, 224, 3)
1170
2556
  """
1171
2557
  super().__init__()
1172
2558
  self.h, self.w = (size, size) if isinstance(size, int) else size
@@ -1175,13 +2561,24 @@ class ClassifyLetterBox:
1175
2561
 
1176
2562
  def __call__(self, im):
1177
2563
  """
1178
- Resizes the image and pads it with a letterbox method.
2564
+ Resizes and pads an image using the letterbox method.
2565
+
2566
+ This method resizes the input image to fit within the specified dimensions while maintaining its aspect ratio,
2567
+ then pads the resized image to match the target size.
1179
2568
 
1180
2569
  Args:
1181
- im (numpy.ndarray): The input image as a numpy array of shape HWC.
2570
+ im (numpy.ndarray): Input image as a numpy array with shape (H, W, C).
1182
2571
 
1183
2572
  Returns:
1184
- (numpy.ndarray): The letterboxed and resized image as a numpy array.
2573
+ (numpy.ndarray): Resized and padded image as a numpy array with shape (hs, ws, 3), where hs and ws are
2574
+ the target height and width respectively.
2575
+
2576
+ Examples:
2577
+ >>> letterbox = ClassifyLetterBox(size=(640, 640))
2578
+ >>> image = np.random.randint(0, 255, (720, 1280, 3), dtype=np.uint8)
2579
+ >>> resized_image = letterbox(image)
2580
+ >>> print(resized_image.shape)
2581
+ (640, 640, 3)
1185
2582
  """
1186
2583
  imh, imw = im.shape[:2]
1187
2584
  r = min(self.h / imh, self.w / imw) # ratio of new/old dimensions
@@ -1199,25 +2596,73 @@ class ClassifyLetterBox:
1199
2596
 
1200
2597
  # NOTE: keep this class for backward compatibility
1201
2598
  class CenterCrop:
1202
- """YOLOv8 CenterCrop class for image preprocessing, designed to be part of a transformation pipeline, e.g.,
1203
- T.Compose([CenterCrop(size), ToTensor()]).
2599
+ """
2600
+ Applies center cropping to images for classification tasks.
2601
+
2602
+ This class performs center cropping on input images, resizing them to a specified size while maintaining the aspect
2603
+ ratio. It is designed to be part of a transformation pipeline, e.g., T.Compose([CenterCrop(size), ToTensor()]).
2604
+
2605
+ Attributes:
2606
+ h (int): Target height of the cropped image.
2607
+ w (int): Target width of the cropped image.
2608
+
2609
+ Methods:
2610
+ __call__: Applies the center crop transformation to an input image.
2611
+
2612
+ Examples:
2613
+ >>> transform = CenterCrop(640)
2614
+ >>> image = np.random.randint(0, 255, (1080, 1920, 3), dtype=np.uint8)
2615
+ >>> cropped_image = transform(image)
2616
+ >>> print(cropped_image.shape)
2617
+ (640, 640, 3)
1204
2618
  """
1205
2619
 
1206
2620
  def __init__(self, size=640):
1207
- """Converts an image from numpy array to PyTorch tensor."""
2621
+ """
2622
+ Initializes the CenterCrop object for image preprocessing.
2623
+
2624
+ This class is designed to be part of a transformation pipeline, e.g., T.Compose([CenterCrop(size), ToTensor()]).
2625
+ It performs a center crop on input images to a specified size.
2626
+
2627
+ Args:
2628
+ size (int | Tuple[int, int]): The desired output size of the crop. If size is an int, a square crop
2629
+ (size, size) is made. If size is a sequence like (h, w), it is used as the output size.
2630
+
2631
+ Returns:
2632
+ (None): This method initializes the object and does not return anything.
2633
+
2634
+ Examples:
2635
+ >>> transform = CenterCrop(224)
2636
+ >>> img = np.random.rand(300, 300, 3)
2637
+ >>> cropped_img = transform(img)
2638
+ >>> print(cropped_img.shape)
2639
+ (224, 224, 3)
2640
+ """
1208
2641
  super().__init__()
1209
2642
  self.h, self.w = (size, size) if isinstance(size, int) else size
1210
2643
 
1211
2644
  def __call__(self, im):
1212
2645
  """
1213
- Resizes and crops the center of the image using a letterbox method.
2646
+ Applies center cropping to an input image.
2647
+
2648
+ This method resizes and crops the center of the image using a letterbox method. It maintains the aspect
2649
+ ratio of the original image while fitting it into the specified dimensions.
1214
2650
 
1215
2651
  Args:
1216
- im (numpy.ndarray): The input image as a numpy array of shape HWC.
2652
+ im (numpy.ndarray | PIL.Image.Image): The input image as a numpy array of shape (H, W, C) or a
2653
+ PIL Image object.
1217
2654
 
1218
2655
  Returns:
1219
- (numpy.ndarray): The center-cropped and resized image as a numpy array.
2656
+ (numpy.ndarray): The center-cropped and resized image as a numpy array of shape (self.h, self.w, C).
2657
+
2658
+ Examples:
2659
+ >>> transform = CenterCrop(size=224)
2660
+ >>> image = np.random.randint(0, 255, (640, 480, 3), dtype=np.uint8)
2661
+ >>> cropped_image = transform(image)
2662
+ >>> assert cropped_image.shape == (224, 224, 3)
1220
2663
  """
2664
+ if isinstance(im, Image.Image): # convert from PIL to numpy array if required
2665
+ im = np.asarray(im)
1221
2666
  imh, imw = im.shape[:2]
1222
2667
  m = min(imh, imw) # min dimension
1223
2668
  top, left = (imh - m) // 2, (imw - m) // 2
@@ -1226,22 +2671,71 @@ class CenterCrop:
1226
2671
 
1227
2672
  # NOTE: keep this class for backward compatibility
1228
2673
  class ToTensor:
1229
- """YOLOv8 ToTensor class for image preprocessing, i.e., T.Compose([LetterBox(size), ToTensor()])."""
2674
+ """
2675
+ Converts an image from a numpy array to a PyTorch tensor.
2676
+
2677
+ This class is designed to be part of a transformation pipeline, e.g., T.Compose([LetterBox(size), ToTensor()]).
2678
+
2679
+ Attributes:
2680
+ half (bool): If True, converts the image to half precision (float16).
2681
+
2682
+ Methods:
2683
+ __call__: Applies the tensor conversion to an input image.
2684
+
2685
+ Examples:
2686
+ >>> transform = ToTensor(half=True)
2687
+ >>> img = np.random.randint(0, 255, (640, 640, 3), dtype=np.uint8)
2688
+ >>> tensor_img = transform(img)
2689
+ >>> print(tensor_img.shape, tensor_img.dtype)
2690
+ torch.Size([3, 640, 640]) torch.float16
2691
+
2692
+ Notes:
2693
+ The input image is expected to be in BGR format with shape (H, W, C).
2694
+ The output tensor will be in RGB format with shape (C, H, W), normalized to [0, 1].
2695
+ """
1230
2696
 
1231
2697
  def __init__(self, half=False):
1232
- """Initialize YOLOv8 ToTensor object with optional half-precision support."""
2698
+ """
2699
+ Initializes the ToTensor object for converting images to PyTorch tensors.
2700
+
2701
+ This class is designed to be used as part of a transformation pipeline for image preprocessing in the
2702
+ Ultralytics YOLO framework. It converts numpy arrays or PIL Images to PyTorch tensors, with an option
2703
+ for half-precision (float16) conversion.
2704
+
2705
+ Args:
2706
+ half (bool): If True, converts the tensor to half precision (float16). Default is False.
2707
+
2708
+ Examples:
2709
+ >>> transform = ToTensor(half=True)
2710
+ >>> img = np.random.rand(640, 640, 3)
2711
+ >>> tensor_img = transform(img)
2712
+ >>> print(tensor_img.dtype)
2713
+ torch.float16
2714
+ """
1233
2715
  super().__init__()
1234
2716
  self.half = half
1235
2717
 
1236
2718
  def __call__(self, im):
1237
2719
  """
1238
- Transforms an image from a numpy array to a PyTorch tensor, applying optional half-precision and normalization.
2720
+ Transforms an image from a numpy array to a PyTorch tensor.
2721
+
2722
+ This method converts the input image from a numpy array to a PyTorch tensor, applying optional
2723
+ half-precision conversion and normalization. The image is transposed from HWC to CHW format and
2724
+ the color channels are reversed from BGR to RGB.
1239
2725
 
1240
2726
  Args:
1241
2727
  im (numpy.ndarray): Input image as a numpy array with shape (H, W, C) in BGR order.
1242
2728
 
1243
2729
  Returns:
1244
- (torch.Tensor): The transformed image as a PyTorch tensor in float32 or float16, normalized to [0, 1].
2730
+ (torch.Tensor): The transformed image as a PyTorch tensor in float32 or float16, normalized
2731
+ to [0, 1] with shape (C, H, W) in RGB order.
2732
+
2733
+ Examples:
2734
+ >>> transform = ToTensor(half=True)
2735
+ >>> img = np.random.randint(0, 255, (640, 640, 3), dtype=np.uint8)
2736
+ >>> tensor_img = transform(img)
2737
+ >>> print(tensor_img.shape, tensor_img.dtype)
2738
+ torch.Size([3, 640, 640]) torch.float16
1245
2739
  """
1246
2740
  im = np.ascontiguousarray(im.transpose((2, 0, 1))[::-1]) # HWC to CHW -> BGR to RGB -> contiguous
1247
2741
  im = torch.from_numpy(im) # to torch