ultralytics-opencv-headless 8.3.246__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 (298) hide show
  1. tests/__init__.py +23 -0
  2. tests/conftest.py +59 -0
  3. tests/test_cli.py +131 -0
  4. tests/test_cuda.py +216 -0
  5. tests/test_engine.py +157 -0
  6. tests/test_exports.py +309 -0
  7. tests/test_integrations.py +151 -0
  8. tests/test_python.py +777 -0
  9. tests/test_solutions.py +371 -0
  10. ultralytics/__init__.py +48 -0
  11. ultralytics/assets/bus.jpg +0 -0
  12. ultralytics/assets/zidane.jpg +0 -0
  13. ultralytics/cfg/__init__.py +1026 -0
  14. ultralytics/cfg/datasets/Argoverse.yaml +78 -0
  15. ultralytics/cfg/datasets/DOTAv1.5.yaml +37 -0
  16. ultralytics/cfg/datasets/DOTAv1.yaml +36 -0
  17. ultralytics/cfg/datasets/GlobalWheat2020.yaml +68 -0
  18. ultralytics/cfg/datasets/HomeObjects-3K.yaml +32 -0
  19. ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
  20. ultralytics/cfg/datasets/Objects365.yaml +447 -0
  21. ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
  22. ultralytics/cfg/datasets/VOC.yaml +102 -0
  23. ultralytics/cfg/datasets/VisDrone.yaml +87 -0
  24. ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
  25. ultralytics/cfg/datasets/brain-tumor.yaml +22 -0
  26. ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
  27. ultralytics/cfg/datasets/coco-pose.yaml +64 -0
  28. ultralytics/cfg/datasets/coco.yaml +118 -0
  29. ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
  30. ultralytics/cfg/datasets/coco128.yaml +101 -0
  31. ultralytics/cfg/datasets/coco8-grayscale.yaml +103 -0
  32. ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
  33. ultralytics/cfg/datasets/coco8-pose.yaml +47 -0
  34. ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
  35. ultralytics/cfg/datasets/coco8.yaml +101 -0
  36. ultralytics/cfg/datasets/construction-ppe.yaml +32 -0
  37. ultralytics/cfg/datasets/crack-seg.yaml +22 -0
  38. ultralytics/cfg/datasets/dog-pose.yaml +52 -0
  39. ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
  40. ultralytics/cfg/datasets/dota8.yaml +35 -0
  41. ultralytics/cfg/datasets/hand-keypoints.yaml +50 -0
  42. ultralytics/cfg/datasets/kitti.yaml +27 -0
  43. ultralytics/cfg/datasets/lvis.yaml +1240 -0
  44. ultralytics/cfg/datasets/medical-pills.yaml +21 -0
  45. ultralytics/cfg/datasets/open-images-v7.yaml +663 -0
  46. ultralytics/cfg/datasets/package-seg.yaml +22 -0
  47. ultralytics/cfg/datasets/signature.yaml +21 -0
  48. ultralytics/cfg/datasets/tiger-pose.yaml +41 -0
  49. ultralytics/cfg/datasets/xView.yaml +155 -0
  50. ultralytics/cfg/default.yaml +130 -0
  51. ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
  52. ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
  53. ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
  54. ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
  55. ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
  56. ultralytics/cfg/models/11/yolo11.yaml +50 -0
  57. ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
  58. ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
  59. ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
  60. ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
  61. ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
  62. ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
  63. ultralytics/cfg/models/12/yolo12.yaml +48 -0
  64. ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
  65. ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
  66. ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
  67. ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
  68. ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
  69. ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
  70. ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
  71. ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
  72. ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
  73. ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
  74. ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
  75. ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
  76. ultralytics/cfg/models/v3/yolov3.yaml +49 -0
  77. ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
  78. ultralytics/cfg/models/v5/yolov5.yaml +51 -0
  79. ultralytics/cfg/models/v6/yolov6.yaml +56 -0
  80. ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +48 -0
  81. ultralytics/cfg/models/v8/yoloe-v8.yaml +48 -0
  82. ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
  83. ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
  84. ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
  85. ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
  86. ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
  87. ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
  88. ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
  89. ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
  90. ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
  91. ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
  92. ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
  93. ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
  94. ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
  95. ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
  96. ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
  97. ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
  98. ultralytics/cfg/models/v8/yolov8.yaml +49 -0
  99. ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
  100. ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
  101. ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
  102. ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
  103. ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
  104. ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
  105. ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
  106. ultralytics/cfg/trackers/botsort.yaml +21 -0
  107. ultralytics/cfg/trackers/bytetrack.yaml +12 -0
  108. ultralytics/data/__init__.py +26 -0
  109. ultralytics/data/annotator.py +66 -0
  110. ultralytics/data/augment.py +2801 -0
  111. ultralytics/data/base.py +435 -0
  112. ultralytics/data/build.py +437 -0
  113. ultralytics/data/converter.py +855 -0
  114. ultralytics/data/dataset.py +834 -0
  115. ultralytics/data/loaders.py +704 -0
  116. ultralytics/data/scripts/download_weights.sh +18 -0
  117. ultralytics/data/scripts/get_coco.sh +61 -0
  118. ultralytics/data/scripts/get_coco128.sh +18 -0
  119. ultralytics/data/scripts/get_imagenet.sh +52 -0
  120. ultralytics/data/split.py +138 -0
  121. ultralytics/data/split_dota.py +344 -0
  122. ultralytics/data/utils.py +798 -0
  123. ultralytics/engine/__init__.py +1 -0
  124. ultralytics/engine/exporter.py +1578 -0
  125. ultralytics/engine/model.py +1124 -0
  126. ultralytics/engine/predictor.py +508 -0
  127. ultralytics/engine/results.py +1522 -0
  128. ultralytics/engine/trainer.py +974 -0
  129. ultralytics/engine/tuner.py +448 -0
  130. ultralytics/engine/validator.py +384 -0
  131. ultralytics/hub/__init__.py +166 -0
  132. ultralytics/hub/auth.py +151 -0
  133. ultralytics/hub/google/__init__.py +174 -0
  134. ultralytics/hub/session.py +422 -0
  135. ultralytics/hub/utils.py +162 -0
  136. ultralytics/models/__init__.py +9 -0
  137. ultralytics/models/fastsam/__init__.py +7 -0
  138. ultralytics/models/fastsam/model.py +79 -0
  139. ultralytics/models/fastsam/predict.py +169 -0
  140. ultralytics/models/fastsam/utils.py +23 -0
  141. ultralytics/models/fastsam/val.py +38 -0
  142. ultralytics/models/nas/__init__.py +7 -0
  143. ultralytics/models/nas/model.py +98 -0
  144. ultralytics/models/nas/predict.py +56 -0
  145. ultralytics/models/nas/val.py +38 -0
  146. ultralytics/models/rtdetr/__init__.py +7 -0
  147. ultralytics/models/rtdetr/model.py +63 -0
  148. ultralytics/models/rtdetr/predict.py +88 -0
  149. ultralytics/models/rtdetr/train.py +89 -0
  150. ultralytics/models/rtdetr/val.py +216 -0
  151. ultralytics/models/sam/__init__.py +25 -0
  152. ultralytics/models/sam/amg.py +275 -0
  153. ultralytics/models/sam/build.py +365 -0
  154. ultralytics/models/sam/build_sam3.py +377 -0
  155. ultralytics/models/sam/model.py +169 -0
  156. ultralytics/models/sam/modules/__init__.py +1 -0
  157. ultralytics/models/sam/modules/blocks.py +1067 -0
  158. ultralytics/models/sam/modules/decoders.py +495 -0
  159. ultralytics/models/sam/modules/encoders.py +794 -0
  160. ultralytics/models/sam/modules/memory_attention.py +298 -0
  161. ultralytics/models/sam/modules/sam.py +1160 -0
  162. ultralytics/models/sam/modules/tiny_encoder.py +979 -0
  163. ultralytics/models/sam/modules/transformer.py +344 -0
  164. ultralytics/models/sam/modules/utils.py +512 -0
  165. ultralytics/models/sam/predict.py +3940 -0
  166. ultralytics/models/sam/sam3/__init__.py +3 -0
  167. ultralytics/models/sam/sam3/decoder.py +546 -0
  168. ultralytics/models/sam/sam3/encoder.py +529 -0
  169. ultralytics/models/sam/sam3/geometry_encoders.py +415 -0
  170. ultralytics/models/sam/sam3/maskformer_segmentation.py +286 -0
  171. ultralytics/models/sam/sam3/model_misc.py +199 -0
  172. ultralytics/models/sam/sam3/necks.py +129 -0
  173. ultralytics/models/sam/sam3/sam3_image.py +339 -0
  174. ultralytics/models/sam/sam3/text_encoder_ve.py +307 -0
  175. ultralytics/models/sam/sam3/vitdet.py +547 -0
  176. ultralytics/models/sam/sam3/vl_combiner.py +160 -0
  177. ultralytics/models/utils/__init__.py +1 -0
  178. ultralytics/models/utils/loss.py +466 -0
  179. ultralytics/models/utils/ops.py +315 -0
  180. ultralytics/models/yolo/__init__.py +7 -0
  181. ultralytics/models/yolo/classify/__init__.py +7 -0
  182. ultralytics/models/yolo/classify/predict.py +90 -0
  183. ultralytics/models/yolo/classify/train.py +202 -0
  184. ultralytics/models/yolo/classify/val.py +216 -0
  185. ultralytics/models/yolo/detect/__init__.py +7 -0
  186. ultralytics/models/yolo/detect/predict.py +122 -0
  187. ultralytics/models/yolo/detect/train.py +227 -0
  188. ultralytics/models/yolo/detect/val.py +507 -0
  189. ultralytics/models/yolo/model.py +430 -0
  190. ultralytics/models/yolo/obb/__init__.py +7 -0
  191. ultralytics/models/yolo/obb/predict.py +56 -0
  192. ultralytics/models/yolo/obb/train.py +79 -0
  193. ultralytics/models/yolo/obb/val.py +302 -0
  194. ultralytics/models/yolo/pose/__init__.py +7 -0
  195. ultralytics/models/yolo/pose/predict.py +65 -0
  196. ultralytics/models/yolo/pose/train.py +110 -0
  197. ultralytics/models/yolo/pose/val.py +248 -0
  198. ultralytics/models/yolo/segment/__init__.py +7 -0
  199. ultralytics/models/yolo/segment/predict.py +109 -0
  200. ultralytics/models/yolo/segment/train.py +69 -0
  201. ultralytics/models/yolo/segment/val.py +307 -0
  202. ultralytics/models/yolo/world/__init__.py +5 -0
  203. ultralytics/models/yolo/world/train.py +173 -0
  204. ultralytics/models/yolo/world/train_world.py +178 -0
  205. ultralytics/models/yolo/yoloe/__init__.py +22 -0
  206. ultralytics/models/yolo/yoloe/predict.py +162 -0
  207. ultralytics/models/yolo/yoloe/train.py +287 -0
  208. ultralytics/models/yolo/yoloe/train_seg.py +122 -0
  209. ultralytics/models/yolo/yoloe/val.py +206 -0
  210. ultralytics/nn/__init__.py +27 -0
  211. ultralytics/nn/autobackend.py +958 -0
  212. ultralytics/nn/modules/__init__.py +182 -0
  213. ultralytics/nn/modules/activation.py +54 -0
  214. ultralytics/nn/modules/block.py +1947 -0
  215. ultralytics/nn/modules/conv.py +669 -0
  216. ultralytics/nn/modules/head.py +1183 -0
  217. ultralytics/nn/modules/transformer.py +793 -0
  218. ultralytics/nn/modules/utils.py +159 -0
  219. ultralytics/nn/tasks.py +1768 -0
  220. ultralytics/nn/text_model.py +356 -0
  221. ultralytics/py.typed +1 -0
  222. ultralytics/solutions/__init__.py +41 -0
  223. ultralytics/solutions/ai_gym.py +108 -0
  224. ultralytics/solutions/analytics.py +264 -0
  225. ultralytics/solutions/config.py +107 -0
  226. ultralytics/solutions/distance_calculation.py +123 -0
  227. ultralytics/solutions/heatmap.py +125 -0
  228. ultralytics/solutions/instance_segmentation.py +86 -0
  229. ultralytics/solutions/object_blurrer.py +89 -0
  230. ultralytics/solutions/object_counter.py +190 -0
  231. ultralytics/solutions/object_cropper.py +87 -0
  232. ultralytics/solutions/parking_management.py +280 -0
  233. ultralytics/solutions/queue_management.py +93 -0
  234. ultralytics/solutions/region_counter.py +133 -0
  235. ultralytics/solutions/security_alarm.py +151 -0
  236. ultralytics/solutions/similarity_search.py +219 -0
  237. ultralytics/solutions/solutions.py +828 -0
  238. ultralytics/solutions/speed_estimation.py +114 -0
  239. ultralytics/solutions/streamlit_inference.py +260 -0
  240. ultralytics/solutions/templates/similarity-search.html +156 -0
  241. ultralytics/solutions/trackzone.py +88 -0
  242. ultralytics/solutions/vision_eye.py +67 -0
  243. ultralytics/trackers/__init__.py +7 -0
  244. ultralytics/trackers/basetrack.py +115 -0
  245. ultralytics/trackers/bot_sort.py +257 -0
  246. ultralytics/trackers/byte_tracker.py +469 -0
  247. ultralytics/trackers/track.py +116 -0
  248. ultralytics/trackers/utils/__init__.py +1 -0
  249. ultralytics/trackers/utils/gmc.py +339 -0
  250. ultralytics/trackers/utils/kalman_filter.py +482 -0
  251. ultralytics/trackers/utils/matching.py +154 -0
  252. ultralytics/utils/__init__.py +1450 -0
  253. ultralytics/utils/autobatch.py +118 -0
  254. ultralytics/utils/autodevice.py +205 -0
  255. ultralytics/utils/benchmarks.py +728 -0
  256. ultralytics/utils/callbacks/__init__.py +5 -0
  257. ultralytics/utils/callbacks/base.py +233 -0
  258. ultralytics/utils/callbacks/clearml.py +146 -0
  259. ultralytics/utils/callbacks/comet.py +625 -0
  260. ultralytics/utils/callbacks/dvc.py +197 -0
  261. ultralytics/utils/callbacks/hub.py +110 -0
  262. ultralytics/utils/callbacks/mlflow.py +134 -0
  263. ultralytics/utils/callbacks/neptune.py +126 -0
  264. ultralytics/utils/callbacks/platform.py +313 -0
  265. ultralytics/utils/callbacks/raytune.py +42 -0
  266. ultralytics/utils/callbacks/tensorboard.py +123 -0
  267. ultralytics/utils/callbacks/wb.py +188 -0
  268. ultralytics/utils/checks.py +1006 -0
  269. ultralytics/utils/cpu.py +85 -0
  270. ultralytics/utils/dist.py +123 -0
  271. ultralytics/utils/downloads.py +529 -0
  272. ultralytics/utils/errors.py +35 -0
  273. ultralytics/utils/events.py +113 -0
  274. ultralytics/utils/export/__init__.py +7 -0
  275. ultralytics/utils/export/engine.py +237 -0
  276. ultralytics/utils/export/imx.py +315 -0
  277. ultralytics/utils/export/tensorflow.py +231 -0
  278. ultralytics/utils/files.py +219 -0
  279. ultralytics/utils/git.py +137 -0
  280. ultralytics/utils/instance.py +484 -0
  281. ultralytics/utils/logger.py +501 -0
  282. ultralytics/utils/loss.py +849 -0
  283. ultralytics/utils/metrics.py +1563 -0
  284. ultralytics/utils/nms.py +337 -0
  285. ultralytics/utils/ops.py +664 -0
  286. ultralytics/utils/patches.py +201 -0
  287. ultralytics/utils/plotting.py +1045 -0
  288. ultralytics/utils/tal.py +403 -0
  289. ultralytics/utils/torch_utils.py +984 -0
  290. ultralytics/utils/tqdm.py +440 -0
  291. ultralytics/utils/triton.py +112 -0
  292. ultralytics/utils/tuner.py +160 -0
  293. ultralytics_opencv_headless-8.3.246.dist-info/METADATA +374 -0
  294. ultralytics_opencv_headless-8.3.246.dist-info/RECORD +298 -0
  295. ultralytics_opencv_headless-8.3.246.dist-info/WHEEL +5 -0
  296. ultralytics_opencv_headless-8.3.246.dist-info/entry_points.txt +3 -0
  297. ultralytics_opencv_headless-8.3.246.dist-info/licenses/LICENSE +661 -0
  298. ultralytics_opencv_headless-8.3.246.dist-info/top_level.txt +1 -0
@@ -0,0 +1,430 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from __future__ import annotations
4
+
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+ import torch
9
+
10
+ from ultralytics.data.build import load_inference_source
11
+ from ultralytics.engine.model import Model
12
+ from ultralytics.models import yolo
13
+ from ultralytics.nn.tasks import (
14
+ ClassificationModel,
15
+ DetectionModel,
16
+ OBBModel,
17
+ PoseModel,
18
+ SegmentationModel,
19
+ WorldModel,
20
+ YOLOEModel,
21
+ YOLOESegModel,
22
+ )
23
+ from ultralytics.utils import ROOT, YAML
24
+
25
+
26
+ class YOLO(Model):
27
+ """YOLO (You Only Look Once) object detection model.
28
+
29
+ This class provides a unified interface for YOLO models, automatically switching to specialized model types
30
+ (YOLOWorld or YOLOE) based on the model filename. It supports various computer vision tasks including object
31
+ detection, segmentation, classification, pose estimation, and oriented bounding box detection.
32
+
33
+ Attributes:
34
+ model: The loaded YOLO model instance.
35
+ task: The task type (detect, segment, classify, pose, obb).
36
+ overrides: Configuration overrides for the model.
37
+
38
+ Methods:
39
+ __init__: Initialize a YOLO model with automatic type detection.
40
+ task_map: Map tasks to their corresponding model, trainer, validator, and predictor classes.
41
+
42
+ Examples:
43
+ Load a pretrained YOLO11n detection model
44
+ >>> model = YOLO("yolo11n.pt")
45
+
46
+ Load a pretrained YOLO11n segmentation model
47
+ >>> model = YOLO("yolo11n-seg.pt")
48
+
49
+ Initialize from a YAML configuration
50
+ >>> model = YOLO("yolo11n.yaml")
51
+ """
52
+
53
+ def __init__(self, model: str | Path = "yolo11n.pt", task: str | None = None, verbose: bool = False):
54
+ """Initialize a YOLO model.
55
+
56
+ This constructor initializes a YOLO model, automatically switching to specialized model types (YOLOWorld or
57
+ YOLOE) based on the model filename.
58
+
59
+ Args:
60
+ model (str | Path): Model name or path to model file, i.e. 'yolo11n.pt', 'yolo11n.yaml'.
61
+ task (str, optional): YOLO task specification, i.e. 'detect', 'segment', 'classify', 'pose', 'obb'. Defaults
62
+ to auto-detection based on model.
63
+ verbose (bool): Display model info on load.
64
+ """
65
+ path = Path(model if isinstance(model, (str, Path)) else "")
66
+ if "-world" in path.stem and path.suffix in {".pt", ".yaml", ".yml"}: # if YOLOWorld PyTorch model
67
+ new_instance = YOLOWorld(path, verbose=verbose)
68
+ self.__class__ = type(new_instance)
69
+ self.__dict__ = new_instance.__dict__
70
+ elif "yoloe" in path.stem and path.suffix in {".pt", ".yaml", ".yml"}: # if YOLOE PyTorch model
71
+ new_instance = YOLOE(path, task=task, verbose=verbose)
72
+ self.__class__ = type(new_instance)
73
+ self.__dict__ = new_instance.__dict__
74
+ else:
75
+ # Continue with default YOLO initialization
76
+ super().__init__(model=model, task=task, verbose=verbose)
77
+ if hasattr(self.model, "model") and "RTDETR" in self.model.model[-1]._get_name(): # if RTDETR head
78
+ from ultralytics import RTDETR
79
+
80
+ new_instance = RTDETR(self)
81
+ self.__class__ = type(new_instance)
82
+ self.__dict__ = new_instance.__dict__
83
+
84
+ @property
85
+ def task_map(self) -> dict[str, dict[str, Any]]:
86
+ """Map head to model, trainer, validator, and predictor classes."""
87
+ return {
88
+ "classify": {
89
+ "model": ClassificationModel,
90
+ "trainer": yolo.classify.ClassificationTrainer,
91
+ "validator": yolo.classify.ClassificationValidator,
92
+ "predictor": yolo.classify.ClassificationPredictor,
93
+ },
94
+ "detect": {
95
+ "model": DetectionModel,
96
+ "trainer": yolo.detect.DetectionTrainer,
97
+ "validator": yolo.detect.DetectionValidator,
98
+ "predictor": yolo.detect.DetectionPredictor,
99
+ },
100
+ "segment": {
101
+ "model": SegmentationModel,
102
+ "trainer": yolo.segment.SegmentationTrainer,
103
+ "validator": yolo.segment.SegmentationValidator,
104
+ "predictor": yolo.segment.SegmentationPredictor,
105
+ },
106
+ "pose": {
107
+ "model": PoseModel,
108
+ "trainer": yolo.pose.PoseTrainer,
109
+ "validator": yolo.pose.PoseValidator,
110
+ "predictor": yolo.pose.PosePredictor,
111
+ },
112
+ "obb": {
113
+ "model": OBBModel,
114
+ "trainer": yolo.obb.OBBTrainer,
115
+ "validator": yolo.obb.OBBValidator,
116
+ "predictor": yolo.obb.OBBPredictor,
117
+ },
118
+ }
119
+
120
+
121
+ class YOLOWorld(Model):
122
+ """YOLO-World object detection model.
123
+
124
+ YOLO-World is an open-vocabulary object detection model that can detect objects based on text descriptions without
125
+ requiring training on specific classes. It extends the YOLO architecture to support real-time open-vocabulary
126
+ detection.
127
+
128
+ Attributes:
129
+ model: The loaded YOLO-World model instance.
130
+ task: Always set to 'detect' for object detection.
131
+ overrides: Configuration overrides for the model.
132
+
133
+ Methods:
134
+ __init__: Initialize YOLOv8-World model with a pre-trained model file.
135
+ task_map: Map tasks to their corresponding model, trainer, validator, and predictor classes.
136
+ set_classes: Set the model's class names for detection.
137
+
138
+ Examples:
139
+ Load a YOLOv8-World model
140
+ >>> model = YOLOWorld("yolov8s-world.pt")
141
+
142
+ Set custom classes for detection
143
+ >>> model.set_classes(["person", "car", "bicycle"])
144
+ """
145
+
146
+ def __init__(self, model: str | Path = "yolov8s-world.pt", verbose: bool = False) -> None:
147
+ """Initialize YOLOv8-World model with a pre-trained model file.
148
+
149
+ Loads a YOLOv8-World model for object detection. If no custom class names are provided, it assigns default COCO
150
+ class names.
151
+
152
+ Args:
153
+ model (str | Path): Path to the pre-trained model file. Supports *.pt and *.yaml formats.
154
+ verbose (bool): If True, prints additional information during initialization.
155
+ """
156
+ super().__init__(model=model, task="detect", verbose=verbose)
157
+
158
+ # Assign default COCO class names when there are no custom names
159
+ if not hasattr(self.model, "names"):
160
+ self.model.names = YAML.load(ROOT / "cfg/datasets/coco8.yaml").get("names")
161
+
162
+ @property
163
+ def task_map(self) -> dict[str, dict[str, Any]]:
164
+ """Map head to model, validator, and predictor classes."""
165
+ return {
166
+ "detect": {
167
+ "model": WorldModel,
168
+ "validator": yolo.detect.DetectionValidator,
169
+ "predictor": yolo.detect.DetectionPredictor,
170
+ "trainer": yolo.world.WorldTrainer,
171
+ }
172
+ }
173
+
174
+ def set_classes(self, classes: list[str]) -> None:
175
+ """Set the model's class names for detection.
176
+
177
+ Args:
178
+ classes (list[str]): A list of categories i.e. ["person"].
179
+ """
180
+ self.model.set_classes(classes)
181
+ # Remove background if it's given
182
+ background = " "
183
+ if background in classes:
184
+ classes.remove(background)
185
+ self.model.names = classes
186
+
187
+ # Reset method class names
188
+ if self.predictor:
189
+ self.predictor.model.names = classes
190
+
191
+
192
+ class YOLOE(Model):
193
+ """YOLOE object detection and segmentation model.
194
+
195
+ YOLOE is an enhanced YOLO model that supports both object detection and instance segmentation tasks with improved
196
+ performance and additional features like visual and text positional embeddings.
197
+
198
+ Attributes:
199
+ model: The loaded YOLOE model instance.
200
+ task: The task type (detect or segment).
201
+ overrides: Configuration overrides for the model.
202
+
203
+ Methods:
204
+ __init__: Initialize YOLOE model with a pre-trained model file.
205
+ task_map: Map tasks to their corresponding model, trainer, validator, and predictor classes.
206
+ get_text_pe: Get text positional embeddings for the given texts.
207
+ get_visual_pe: Get visual positional embeddings for the given image and visual features.
208
+ set_vocab: Set vocabulary and class names for the YOLOE model.
209
+ get_vocab: Get vocabulary for the given class names.
210
+ set_classes: Set the model's class names and embeddings for detection.
211
+ val: Validate the model using text or visual prompts.
212
+ predict: Run prediction on images, videos, directories, streams, etc.
213
+
214
+ Examples:
215
+ Load a YOLOE detection model
216
+ >>> model = YOLOE("yoloe-11s-seg.pt")
217
+
218
+ Set vocabulary and class names
219
+ >>> model.set_vocab(["person", "car", "dog"], ["person", "car", "dog"])
220
+
221
+ Predict with visual prompts
222
+ >>> prompts = {"bboxes": [[10, 20, 100, 200]], "cls": ["person"]}
223
+ >>> results = model.predict("image.jpg", visual_prompts=prompts)
224
+ """
225
+
226
+ def __init__(self, model: str | Path = "yoloe-11s-seg.pt", task: str | None = None, verbose: bool = False) -> None:
227
+ """Initialize YOLOE model with a pre-trained model file.
228
+
229
+ Args:
230
+ model (str | Path): Path to the pre-trained model file. Supports *.pt and *.yaml formats.
231
+ task (str, optional): Task type for the model. Auto-detected if None.
232
+ verbose (bool): If True, prints additional information during initialization.
233
+ """
234
+ super().__init__(model=model, task=task, verbose=verbose)
235
+
236
+ @property
237
+ def task_map(self) -> dict[str, dict[str, Any]]:
238
+ """Map head to model, validator, and predictor classes."""
239
+ return {
240
+ "detect": {
241
+ "model": YOLOEModel,
242
+ "validator": yolo.yoloe.YOLOEDetectValidator,
243
+ "predictor": yolo.detect.DetectionPredictor,
244
+ "trainer": yolo.yoloe.YOLOETrainer,
245
+ },
246
+ "segment": {
247
+ "model": YOLOESegModel,
248
+ "validator": yolo.yoloe.YOLOESegValidator,
249
+ "predictor": yolo.segment.SegmentationPredictor,
250
+ "trainer": yolo.yoloe.YOLOESegTrainer,
251
+ },
252
+ }
253
+
254
+ def get_text_pe(self, texts):
255
+ """Get text positional embeddings for the given texts."""
256
+ assert isinstance(self.model, YOLOEModel)
257
+ return self.model.get_text_pe(texts)
258
+
259
+ def get_visual_pe(self, img, visual):
260
+ """Get visual positional embeddings for the given image and visual features.
261
+
262
+ This method extracts positional embeddings from visual features based on the input image. It requires that the
263
+ model is an instance of YOLOEModel.
264
+
265
+ Args:
266
+ img (torch.Tensor): Input image tensor.
267
+ visual (torch.Tensor): Visual features extracted from the image.
268
+
269
+ Returns:
270
+ (torch.Tensor): Visual positional embeddings.
271
+
272
+ Examples:
273
+ >>> model = YOLOE("yoloe-11s-seg.pt")
274
+ >>> img = torch.rand(1, 3, 640, 640)
275
+ >>> visual_features = torch.rand(1, 1, 80, 80)
276
+ >>> pe = model.get_visual_pe(img, visual_features)
277
+ """
278
+ assert isinstance(self.model, YOLOEModel)
279
+ return self.model.get_visual_pe(img, visual)
280
+
281
+ def set_vocab(self, vocab: list[str], names: list[str]) -> None:
282
+ """Set vocabulary and class names for the YOLOE model.
283
+
284
+ This method configures the vocabulary and class names used by the model for text processing and classification
285
+ tasks. The model must be an instance of YOLOEModel.
286
+
287
+ Args:
288
+ vocab (list[str]): Vocabulary list containing tokens or words used by the model for text processing.
289
+ names (list[str]): List of class names that the model can detect or classify.
290
+
291
+ Raises:
292
+ AssertionError: If the model is not an instance of YOLOEModel.
293
+
294
+ Examples:
295
+ >>> model = YOLOE("yoloe-11s-seg.pt")
296
+ >>> model.set_vocab(["person", "car", "dog"], ["person", "car", "dog"])
297
+ """
298
+ assert isinstance(self.model, YOLOEModel)
299
+ self.model.set_vocab(vocab, names=names)
300
+
301
+ def get_vocab(self, names):
302
+ """Get vocabulary for the given class names."""
303
+ assert isinstance(self.model, YOLOEModel)
304
+ return self.model.get_vocab(names)
305
+
306
+ def set_classes(self, classes: list[str], embeddings: torch.Tensor | None = None) -> None:
307
+ """Set the model's class names and embeddings for detection.
308
+
309
+ Args:
310
+ classes (list[str]): A list of categories i.e. ["person"].
311
+ embeddings (torch.Tensor): Embeddings corresponding to the classes.
312
+ """
313
+ assert isinstance(self.model, YOLOEModel)
314
+ if embeddings is None:
315
+ embeddings = self.get_text_pe(classes) # generate text embeddings if not provided
316
+ self.model.set_classes(classes, embeddings)
317
+ # Verify no background class is present
318
+ assert " " not in classes
319
+ self.model.names = classes
320
+
321
+ # Reset method class names
322
+ if self.predictor:
323
+ self.predictor.model.names = classes
324
+
325
+ def val(
326
+ self,
327
+ validator=None,
328
+ load_vp: bool = False,
329
+ refer_data: str | None = None,
330
+ **kwargs,
331
+ ):
332
+ """Validate the model using text or visual prompts.
333
+
334
+ Args:
335
+ validator (callable, optional): A callable validator function. If None, a default validator is loaded.
336
+ load_vp (bool): Whether to load visual prompts. If False, text prompts are used.
337
+ refer_data (str, optional): Path to the reference data for visual prompts.
338
+ **kwargs (Any): Additional keyword arguments to override default settings.
339
+
340
+ Returns:
341
+ (dict): Validation statistics containing metrics computed during validation.
342
+ """
343
+ custom = {"rect": not load_vp} # method defaults
344
+ args = {**self.overrides, **custom, **kwargs, "mode": "val"} # highest priority args on the right
345
+
346
+ validator = (validator or self._smart_load("validator"))(args=args, _callbacks=self.callbacks)
347
+ validator(model=self.model, load_vp=load_vp, refer_data=refer_data)
348
+ self.metrics = validator.metrics
349
+ return validator.metrics
350
+
351
+ def predict(
352
+ self,
353
+ source=None,
354
+ stream: bool = False,
355
+ visual_prompts: dict[str, list] = {},
356
+ refer_image=None,
357
+ predictor=yolo.yoloe.YOLOEVPDetectPredictor,
358
+ **kwargs,
359
+ ):
360
+ """Run prediction on images, videos, directories, streams, etc.
361
+
362
+ Args:
363
+ source (str | int | PIL.Image | np.ndarray, optional): Source for prediction. Accepts image paths, directory
364
+ paths, URL/YouTube streams, PIL images, numpy arrays, or webcam indices.
365
+ stream (bool): Whether to stream the prediction results. If True, results are yielded as a generator as they
366
+ are computed.
367
+ visual_prompts (dict[str, list]): Dictionary containing visual prompts for the model. Must include 'bboxes'
368
+ and 'cls' keys when non-empty.
369
+ refer_image (str | PIL.Image | np.ndarray, optional): Reference image for visual prompts.
370
+ predictor (callable, optional): Custom predictor function. If None, a predictor is automatically loaded
371
+ based on the task.
372
+ **kwargs (Any): Additional keyword arguments passed to the predictor.
373
+
374
+ Returns:
375
+ (list | generator): List of Results objects or generator of Results objects if stream=True.
376
+
377
+ Examples:
378
+ >>> model = YOLOE("yoloe-11s-seg.pt")
379
+ >>> results = model.predict("path/to/image.jpg")
380
+ >>> # With visual prompts
381
+ >>> prompts = {"bboxes": [[10, 20, 100, 200]], "cls": ["person"]}
382
+ >>> results = model.predict("path/to/image.jpg", visual_prompts=prompts)
383
+ """
384
+ if len(visual_prompts):
385
+ assert "bboxes" in visual_prompts and "cls" in visual_prompts, (
386
+ f"Expected 'bboxes' and 'cls' in visual prompts, but got {visual_prompts.keys()}"
387
+ )
388
+ assert len(visual_prompts["bboxes"]) == len(visual_prompts["cls"]), (
389
+ f"Expected equal number of bounding boxes and classes, but got {len(visual_prompts['bboxes'])} and "
390
+ f"{len(visual_prompts['cls'])} respectively"
391
+ )
392
+ if type(self.predictor) is not predictor:
393
+ self.predictor = predictor(
394
+ overrides={
395
+ "task": self.model.task,
396
+ "mode": "predict",
397
+ "save": False,
398
+ "verbose": refer_image is None,
399
+ "batch": 1,
400
+ "device": kwargs.get("device", None),
401
+ "half": kwargs.get("half", False),
402
+ "imgsz": kwargs.get("imgsz", self.overrides["imgsz"]),
403
+ },
404
+ _callbacks=self.callbacks,
405
+ )
406
+
407
+ num_cls = (
408
+ max(len(set(c)) for c in visual_prompts["cls"])
409
+ if isinstance(source, list) and refer_image is None # means multiple images
410
+ else len(set(visual_prompts["cls"]))
411
+ )
412
+ self.model.model[-1].nc = num_cls
413
+ self.model.names = [f"object{i}" for i in range(num_cls)]
414
+ self.predictor.set_prompts(visual_prompts.copy())
415
+ self.predictor.setup_model(model=self.model)
416
+
417
+ if refer_image is None and source is not None:
418
+ dataset = load_inference_source(source)
419
+ if dataset.mode in {"video", "stream"}:
420
+ # NOTE: set the first frame as refer image for videos/streams inference
421
+ refer_image = next(iter(dataset))[1][0]
422
+ if refer_image is not None:
423
+ vpe = self.predictor.get_vpe(refer_image)
424
+ self.model.set_classes(self.model.names, vpe)
425
+ self.task = "segment" if isinstance(self.predictor, yolo.segment.SegmentationPredictor) else "detect"
426
+ self.predictor = None # reset predictor
427
+ elif isinstance(self.predictor, yolo.yoloe.YOLOEVPDetectPredictor):
428
+ self.predictor = None # reset predictor if no visual prompts
429
+
430
+ return super().predict(source, stream, **kwargs)
@@ -0,0 +1,7 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from .predict import OBBPredictor
4
+ from .train import OBBTrainer
5
+ from .val import OBBValidator
6
+
7
+ __all__ = "OBBPredictor", "OBBTrainer", "OBBValidator"
@@ -0,0 +1,56 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ import torch
4
+
5
+ from ultralytics.engine.results import Results
6
+ from ultralytics.models.yolo.detect.predict import DetectionPredictor
7
+ from ultralytics.utils import DEFAULT_CFG, ops
8
+
9
+
10
+ class OBBPredictor(DetectionPredictor):
11
+ """A class extending the DetectionPredictor class for prediction based on an Oriented Bounding Box (OBB) model.
12
+
13
+ This predictor handles oriented bounding box detection tasks, processing images and returning results with rotated
14
+ bounding boxes.
15
+
16
+ Attributes:
17
+ args (namespace): Configuration arguments for the predictor.
18
+ model (torch.nn.Module): The loaded YOLO OBB model.
19
+
20
+ Examples:
21
+ >>> from ultralytics.utils import ASSETS
22
+ >>> from ultralytics.models.yolo.obb import OBBPredictor
23
+ >>> args = dict(model="yolo11n-obb.pt", source=ASSETS)
24
+ >>> predictor = OBBPredictor(overrides=args)
25
+ >>> predictor.predict_cli()
26
+ """
27
+
28
+ def __init__(self, cfg=DEFAULT_CFG, overrides=None, _callbacks=None):
29
+ """Initialize OBBPredictor with optional model and data configuration overrides.
30
+
31
+ Args:
32
+ cfg (dict, optional): Default configuration for the predictor.
33
+ overrides (dict, optional): Configuration overrides that take precedence over the default config.
34
+ _callbacks (list, optional): List of callback functions to be invoked during prediction.
35
+ """
36
+ super().__init__(cfg, overrides, _callbacks)
37
+ self.args.task = "obb"
38
+
39
+ def construct_result(self, pred, img, orig_img, img_path):
40
+ """Construct the result object from the prediction.
41
+
42
+ Args:
43
+ pred (torch.Tensor): The predicted bounding boxes, scores, and rotation angles with shape (N, 7) where the
44
+ last dimension contains [x, y, w, h, confidence, class_id, angle].
45
+ img (torch.Tensor): The image after preprocessing with shape (B, C, H, W).
46
+ orig_img (np.ndarray): The original image before preprocessing.
47
+ img_path (str): The path to the original image.
48
+
49
+ Returns:
50
+ (Results): The result object containing the original image, image path, class names, and oriented bounding
51
+ boxes.
52
+ """
53
+ rboxes = ops.regularize_rboxes(torch.cat([pred[:, :4], pred[:, -1:]], dim=-1))
54
+ rboxes[:, :4] = ops.scale_boxes(img.shape[2:], rboxes[:, :4], orig_img.shape, xywh=True)
55
+ obb = torch.cat([rboxes, pred[:, 4:6]], dim=-1)
56
+ return Results(orig_img, path=img_path, names=self.model.names, obb=obb)
@@ -0,0 +1,79 @@
1
+ # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
+
3
+ from __future__ import annotations
4
+
5
+ from copy import copy
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from ultralytics.models import yolo
10
+ from ultralytics.nn.tasks import OBBModel
11
+ from ultralytics.utils import DEFAULT_CFG, RANK
12
+
13
+
14
+ class OBBTrainer(yolo.detect.DetectionTrainer):
15
+ """A class extending the DetectionTrainer class for training based on an Oriented Bounding Box (OBB) model.
16
+
17
+ This trainer specializes in training YOLO models that detect oriented bounding boxes, which are useful for detecting
18
+ objects at arbitrary angles rather than just axis-aligned rectangles.
19
+
20
+ Attributes:
21
+ loss_names (tuple): Names of the loss components used during training including box_loss, cls_loss, and
22
+ dfl_loss.
23
+
24
+ Methods:
25
+ get_model: Return OBBModel initialized with specified config and weights.
26
+ get_validator: Return an instance of OBBValidator for validation of YOLO model.
27
+
28
+ Examples:
29
+ >>> from ultralytics.models.yolo.obb import OBBTrainer
30
+ >>> args = dict(model="yolo11n-obb.pt", data="dota8.yaml", epochs=3)
31
+ >>> trainer = OBBTrainer(overrides=args)
32
+ >>> trainer.train()
33
+ """
34
+
35
+ def __init__(self, cfg=DEFAULT_CFG, overrides: dict | None = None, _callbacks: list[Any] | None = None):
36
+ """Initialize an OBBTrainer object for training Oriented Bounding Box (OBB) models.
37
+
38
+ Args:
39
+ cfg (dict, optional): Configuration dictionary for the trainer. Contains training parameters and model
40
+ configuration.
41
+ overrides (dict, optional): Dictionary of parameter overrides for the configuration. Any values here will
42
+ take precedence over those in cfg.
43
+ _callbacks (list[Any], optional): List of callback functions to be invoked during training.
44
+ """
45
+ if overrides is None:
46
+ overrides = {}
47
+ overrides["task"] = "obb"
48
+ super().__init__(cfg, overrides, _callbacks)
49
+
50
+ def get_model(
51
+ self, cfg: str | dict | None = None, weights: str | Path | None = None, verbose: bool = True
52
+ ) -> OBBModel:
53
+ """Return OBBModel initialized with specified config and weights.
54
+
55
+ Args:
56
+ cfg (str | dict, optional): Model configuration. Can be a path to a YAML config file, a dictionary
57
+ containing configuration parameters, or None to use default configuration.
58
+ weights (str | Path, optional): Path to pretrained weights file. If None, random initialization is used.
59
+ verbose (bool): Whether to display model information during initialization.
60
+
61
+ Returns:
62
+ (OBBModel): Initialized OBBModel with the specified configuration and weights.
63
+
64
+ Examples:
65
+ >>> trainer = OBBTrainer()
66
+ >>> model = trainer.get_model(cfg="yolo11n-obb.yaml", weights="yolo11n-obb.pt")
67
+ """
68
+ model = OBBModel(cfg, nc=self.data["nc"], ch=self.data["channels"], verbose=verbose and RANK == -1)
69
+ if weights:
70
+ model.load(weights)
71
+
72
+ return model
73
+
74
+ def get_validator(self):
75
+ """Return an instance of OBBValidator for validation of YOLO model."""
76
+ self.loss_names = "box_loss", "cls_loss", "dfl_loss"
77
+ return yolo.obb.OBBValidator(
78
+ self.test_loader, save_dir=self.save_dir, args=copy(self.args), _callbacks=self.callbacks
79
+ )