dgenerate-ultralytics-headless 8.3.189__py3-none-any.whl → 8.3.191__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 (111) hide show
  1. {dgenerate_ultralytics_headless-8.3.189.dist-info → dgenerate_ultralytics_headless-8.3.191.dist-info}/METADATA +1 -1
  2. {dgenerate_ultralytics_headless-8.3.189.dist-info → dgenerate_ultralytics_headless-8.3.191.dist-info}/RECORD +111 -109
  3. tests/test_cuda.py +6 -5
  4. tests/test_exports.py +1 -6
  5. tests/test_python.py +1 -4
  6. tests/test_solutions.py +1 -1
  7. ultralytics/__init__.py +1 -1
  8. ultralytics/cfg/__init__.py +16 -14
  9. ultralytics/cfg/datasets/VisDrone.yaml +4 -4
  10. ultralytics/data/annotator.py +6 -6
  11. ultralytics/data/augment.py +53 -51
  12. ultralytics/data/base.py +15 -13
  13. ultralytics/data/build.py +7 -4
  14. ultralytics/data/converter.py +9 -10
  15. ultralytics/data/dataset.py +24 -22
  16. ultralytics/data/loaders.py +13 -11
  17. ultralytics/data/split.py +4 -3
  18. ultralytics/data/split_dota.py +14 -12
  19. ultralytics/data/utils.py +31 -25
  20. ultralytics/engine/exporter.py +7 -4
  21. ultralytics/engine/model.py +16 -14
  22. ultralytics/engine/predictor.py +9 -7
  23. ultralytics/engine/results.py +59 -57
  24. ultralytics/engine/trainer.py +7 -0
  25. ultralytics/engine/tuner.py +4 -3
  26. ultralytics/engine/validator.py +3 -1
  27. ultralytics/hub/__init__.py +6 -2
  28. ultralytics/hub/auth.py +2 -2
  29. ultralytics/hub/google/__init__.py +9 -8
  30. ultralytics/hub/session.py +11 -11
  31. ultralytics/hub/utils.py +8 -9
  32. ultralytics/models/fastsam/model.py +8 -6
  33. ultralytics/models/nas/model.py +5 -3
  34. ultralytics/models/rtdetr/train.py +4 -3
  35. ultralytics/models/rtdetr/val.py +6 -4
  36. ultralytics/models/sam/amg.py +13 -10
  37. ultralytics/models/sam/model.py +3 -2
  38. ultralytics/models/sam/modules/blocks.py +21 -21
  39. ultralytics/models/sam/modules/decoders.py +11 -11
  40. ultralytics/models/sam/modules/encoders.py +25 -25
  41. ultralytics/models/sam/modules/memory_attention.py +9 -8
  42. ultralytics/models/sam/modules/sam.py +8 -10
  43. ultralytics/models/sam/modules/tiny_encoder.py +21 -20
  44. ultralytics/models/sam/modules/transformer.py +6 -5
  45. ultralytics/models/sam/modules/utils.py +7 -5
  46. ultralytics/models/sam/predict.py +32 -31
  47. ultralytics/models/utils/loss.py +29 -27
  48. ultralytics/models/utils/ops.py +10 -8
  49. ultralytics/models/yolo/classify/train.py +7 -5
  50. ultralytics/models/yolo/classify/val.py +10 -8
  51. ultralytics/models/yolo/detect/predict.py +3 -3
  52. ultralytics/models/yolo/detect/train.py +8 -6
  53. ultralytics/models/yolo/detect/val.py +23 -21
  54. ultralytics/models/yolo/model.py +14 -14
  55. ultralytics/models/yolo/obb/train.py +5 -3
  56. ultralytics/models/yolo/obb/val.py +13 -10
  57. ultralytics/models/yolo/pose/train.py +7 -5
  58. ultralytics/models/yolo/pose/val.py +11 -9
  59. ultralytics/models/yolo/segment/train.py +4 -5
  60. ultralytics/models/yolo/segment/val.py +12 -10
  61. ultralytics/models/yolo/world/train.py +9 -7
  62. ultralytics/models/yolo/yoloe/train.py +7 -6
  63. ultralytics/models/yolo/yoloe/val.py +10 -8
  64. ultralytics/nn/autobackend.py +40 -52
  65. ultralytics/nn/modules/__init__.py +3 -3
  66. ultralytics/nn/modules/block.py +12 -12
  67. ultralytics/nn/modules/conv.py +4 -3
  68. ultralytics/nn/modules/head.py +46 -38
  69. ultralytics/nn/modules/transformer.py +22 -21
  70. ultralytics/nn/tasks.py +2 -2
  71. ultralytics/nn/text_model.py +6 -5
  72. ultralytics/solutions/analytics.py +7 -5
  73. ultralytics/solutions/config.py +12 -10
  74. ultralytics/solutions/distance_calculation.py +3 -3
  75. ultralytics/solutions/heatmap.py +4 -2
  76. ultralytics/solutions/object_counter.py +5 -3
  77. ultralytics/solutions/parking_management.py +4 -2
  78. ultralytics/solutions/region_counter.py +7 -5
  79. ultralytics/solutions/similarity_search.py +5 -3
  80. ultralytics/solutions/solutions.py +38 -36
  81. ultralytics/solutions/streamlit_inference.py +8 -7
  82. ultralytics/trackers/bot_sort.py +11 -9
  83. ultralytics/trackers/byte_tracker.py +17 -15
  84. ultralytics/trackers/utils/gmc.py +4 -3
  85. ultralytics/utils/__init__.py +27 -77
  86. ultralytics/utils/autobatch.py +3 -2
  87. ultralytics/utils/autodevice.py +10 -10
  88. ultralytics/utils/benchmarks.py +11 -10
  89. ultralytics/utils/callbacks/comet.py +9 -9
  90. ultralytics/utils/callbacks/platform.py +2 -1
  91. ultralytics/utils/checks.py +20 -29
  92. ultralytics/utils/downloads.py +2 -2
  93. ultralytics/utils/export.py +12 -11
  94. ultralytics/utils/files.py +8 -7
  95. ultralytics/utils/git.py +139 -0
  96. ultralytics/utils/instance.py +8 -7
  97. ultralytics/utils/logger.py +7 -6
  98. ultralytics/utils/loss.py +15 -13
  99. ultralytics/utils/metrics.py +62 -62
  100. ultralytics/utils/nms.py +346 -0
  101. ultralytics/utils/ops.py +83 -251
  102. ultralytics/utils/patches.py +6 -4
  103. ultralytics/utils/plotting.py +18 -16
  104. ultralytics/utils/tal.py +1 -1
  105. ultralytics/utils/torch_utils.py +4 -2
  106. ultralytics/utils/tqdm.py +47 -33
  107. ultralytics/utils/triton.py +3 -2
  108. {dgenerate_ultralytics_headless-8.3.189.dist-info → dgenerate_ultralytics_headless-8.3.191.dist-info}/WHEEL +0 -0
  109. {dgenerate_ultralytics_headless-8.3.189.dist-info → dgenerate_ultralytics_headless-8.3.191.dist-info}/entry_points.txt +0 -0
  110. {dgenerate_ultralytics_headless-8.3.189.dist-info → dgenerate_ultralytics_headless-8.3.191.dist-info}/licenses/LICENSE +0 -0
  111. {dgenerate_ultralytics_headless-8.3.189.dist-info → dgenerate_ultralytics_headless-8.3.191.dist-info}/top_level.txt +0 -0
@@ -1,8 +1,10 @@
1
1
  # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
+ from __future__ import annotations
4
+
3
5
  from copy import copy
4
6
  from pathlib import Path
5
- from typing import Any, List, Optional, Union
7
+ from typing import Any
6
8
 
7
9
  from ultralytics.models import yolo
8
10
  from ultralytics.nn.tasks import OBBModel
@@ -31,7 +33,7 @@ class OBBTrainer(yolo.detect.DetectionTrainer):
31
33
  >>> trainer.train()
32
34
  """
33
35
 
34
- def __init__(self, cfg=DEFAULT_CFG, overrides: Optional[dict] = None, _callbacks: Optional[List[Any]] = None):
36
+ def __init__(self, cfg=DEFAULT_CFG, overrides: dict | None = None, _callbacks: list[Any] | None = None):
35
37
  """
36
38
  Initialize an OBBTrainer object for training Oriented Bounding Box (OBB) models.
37
39
 
@@ -57,7 +59,7 @@ class OBBTrainer(yolo.detect.DetectionTrainer):
57
59
  super().__init__(cfg, overrides, _callbacks)
58
60
 
59
61
  def get_model(
60
- self, cfg: Optional[Union[str, dict]] = None, weights: Optional[Union[str, Path]] = None, verbose: bool = True
62
+ self, cfg: str | dict | None = None, weights: str | Path | None = None, verbose: bool = True
61
63
  ) -> OBBModel:
62
64
  """
63
65
  Return OBBModel initialized with specified config and weights.
@@ -1,7 +1,9 @@
1
1
  # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
+ from __future__ import annotations
4
+
3
5
  from pathlib import Path
4
- from typing import Any, Dict, List, Tuple
6
+ from typing import Any
5
7
 
6
8
  import numpy as np
7
9
  import torch
@@ -9,6 +11,7 @@ import torch
9
11
  from ultralytics.models.yolo.detect import DetectionValidator
10
12
  from ultralytics.utils import LOGGER, ops
11
13
  from ultralytics.utils.metrics import OBBMetrics, batch_probiou
14
+ from ultralytics.utils.nms import TorchNMS
12
15
 
13
16
 
14
17
  class OBBValidator(DetectionValidator):
@@ -69,7 +72,7 @@ class OBBValidator(DetectionValidator):
69
72
  self.is_dota = isinstance(val, str) and "DOTA" in val # check if dataset is DOTA format
70
73
  self.confusion_matrix.task = "obb" # set confusion matrix task to 'obb'
71
74
 
72
- def _process_batch(self, preds: Dict[str, torch.Tensor], batch: Dict[str, torch.Tensor]) -> Dict[str, np.ndarray]:
75
+ def _process_batch(self, preds: dict[str, torch.Tensor], batch: dict[str, torch.Tensor]) -> dict[str, np.ndarray]:
73
76
  """
74
77
  Compute the correct prediction matrix for a batch of detections and ground truth bounding boxes.
75
78
 
@@ -95,7 +98,7 @@ class OBBValidator(DetectionValidator):
95
98
  iou = batch_probiou(batch["bboxes"], preds["bboxes"])
96
99
  return {"tp": self.match_predictions(preds["cls"], batch["cls"], iou).cpu().numpy()}
97
100
 
98
- def postprocess(self, preds: torch.Tensor) -> List[Dict[str, torch.Tensor]]:
101
+ def postprocess(self, preds: torch.Tensor) -> list[dict[str, torch.Tensor]]:
99
102
  """
100
103
  Args:
101
104
  preds (torch.Tensor): Raw predictions from the model.
@@ -108,7 +111,7 @@ class OBBValidator(DetectionValidator):
108
111
  pred["bboxes"] = torch.cat([pred["bboxes"], pred.pop("extra")], dim=-1) # concatenate angle
109
112
  return preds
110
113
 
111
- def _prepare_batch(self, si: int, batch: Dict[str, Any]) -> Dict[str, Any]:
114
+ def _prepare_batch(self, si: int, batch: dict[str, Any]) -> dict[str, Any]:
112
115
  """
113
116
  Prepare batch data for OBB validation with proper scaling and formatting.
114
117
 
@@ -142,7 +145,7 @@ class OBBValidator(DetectionValidator):
142
145
  "im_file": batch["im_file"][si],
143
146
  }
144
147
 
145
- def plot_predictions(self, batch: Dict[str, Any], preds: List[torch.Tensor], ni: int) -> None:
148
+ def plot_predictions(self, batch: dict[str, Any], preds: list[torch.Tensor], ni: int) -> None:
146
149
  """
147
150
  Plot predicted bounding boxes on input images and save the result.
148
151
 
@@ -162,7 +165,7 @@ class OBBValidator(DetectionValidator):
162
165
  p["bboxes"][:, :4] = ops.xywh2xyxy(p["bboxes"][:, :4]) # convert to xyxy format for plotting
163
166
  super().plot_predictions(batch, preds, ni) # plot bboxes
164
167
 
165
- def pred_to_json(self, predn: Dict[str, torch.Tensor], pbatch: Dict[str, Any]) -> None:
168
+ def pred_to_json(self, predn: dict[str, torch.Tensor], pbatch: dict[str, Any]) -> None:
166
169
  """
167
170
  Convert YOLO predictions to COCO JSON format with rotated bounding box information.
168
171
 
@@ -193,7 +196,7 @@ class OBBValidator(DetectionValidator):
193
196
  }
194
197
  )
195
198
 
196
- def save_one_txt(self, predn: Dict[str, torch.Tensor], save_conf: bool, shape: Tuple[int, int], file: Path) -> None:
199
+ def save_one_txt(self, predn: dict[str, torch.Tensor], save_conf: bool, shape: tuple[int, int], file: Path) -> None:
197
200
  """
198
201
  Save YOLO OBB detections to a text file in normalized coordinates.
199
202
 
@@ -220,7 +223,7 @@ class OBBValidator(DetectionValidator):
220
223
  obb=torch.cat([predn["bboxes"], predn["conf"].unsqueeze(-1), predn["cls"].unsqueeze(-1)], dim=1),
221
224
  ).save_txt(file, save_conf=save_conf)
222
225
 
223
- def scale_preds(self, predn: Dict[str, torch.Tensor], pbatch: Dict[str, Any]) -> Dict[str, torch.Tensor]:
226
+ def scale_preds(self, predn: dict[str, torch.Tensor], pbatch: dict[str, Any]) -> dict[str, torch.Tensor]:
224
227
  """Scales predictions to the original image size."""
225
228
  return {
226
229
  **predn,
@@ -229,7 +232,7 @@ class OBBValidator(DetectionValidator):
229
232
  ),
230
233
  }
231
234
 
232
- def eval_json(self, stats: Dict[str, Any]) -> Dict[str, Any]:
235
+ def eval_json(self, stats: dict[str, Any]) -> dict[str, Any]:
233
236
  """
234
237
  Evaluate YOLO output in JSON format and save predictions in DOTA format.
235
238
 
@@ -281,7 +284,7 @@ class OBBValidator(DetectionValidator):
281
284
  b = bbox[:, :5].clone()
282
285
  b[:, :2] += c
283
286
  # 0.3 could get results close to the ones from official merging script, even slightly better.
284
- i = ops.nms_rotated(b, scores, 0.3)
287
+ i = TorchNMS.fast_nms(b, scores, 0.3, iou_func=batch_probiou)
285
288
  bbox = bbox[i]
286
289
 
287
290
  b = ops.xywhr2xyxyxyxy(bbox[:, :5]).view(-1, 8)
@@ -1,8 +1,10 @@
1
1
  # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
+ from __future__ import annotations
4
+
3
5
  from copy import copy
4
6
  from pathlib import Path
5
- from typing import Any, Dict, Optional, Union
7
+ from typing import Any
6
8
 
7
9
  from ultralytics.models import yolo
8
10
  from ultralytics.nn.tasks import PoseModel
@@ -38,7 +40,7 @@ class PoseTrainer(yolo.detect.DetectionTrainer):
38
40
  >>> trainer.train()
39
41
  """
40
42
 
41
- def __init__(self, cfg=DEFAULT_CFG, overrides: Optional[Dict[str, Any]] = None, _callbacks=None):
43
+ def __init__(self, cfg=DEFAULT_CFG, overrides: dict[str, Any] | None = None, _callbacks=None):
42
44
  """
43
45
  Initialize a PoseTrainer object for training YOLO pose estimation models.
44
46
 
@@ -73,8 +75,8 @@ class PoseTrainer(yolo.detect.DetectionTrainer):
73
75
 
74
76
  def get_model(
75
77
  self,
76
- cfg: Optional[Union[str, Path, Dict[str, Any]]] = None,
77
- weights: Optional[Union[str, Path]] = None,
78
+ cfg: str | Path | dict[str, Any] | None = None,
79
+ weights: str | Path | None = None,
78
80
  verbose: bool = True,
79
81
  ) -> PoseModel:
80
82
  """
@@ -112,7 +114,7 @@ class PoseTrainer(yolo.detect.DetectionTrainer):
112
114
  """Plot training/validation metrics."""
113
115
  plot_results(file=self.csv, pose=True, on_plot=self.on_plot) # save results.png
114
116
 
115
- def get_dataset(self) -> Dict[str, Any]:
117
+ def get_dataset(self) -> dict[str, Any]:
116
118
  """
117
119
  Retrieve the dataset and ensure it contains the required `kpt_shape` key.
118
120
 
@@ -1,7 +1,9 @@
1
1
  # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
+ from __future__ import annotations
4
+
3
5
  from pathlib import Path
4
- from typing import Any, Dict, Tuple
6
+ from typing import Any
5
7
 
6
8
  import numpy as np
7
9
  import torch
@@ -81,7 +83,7 @@ class PoseValidator(DetectionValidator):
81
83
  "See https://github.com/ultralytics/ultralytics/issues/4031."
82
84
  )
83
85
 
84
- def preprocess(self, batch: Dict[str, Any]) -> Dict[str, Any]:
86
+ def preprocess(self, batch: dict[str, Any]) -> dict[str, Any]:
85
87
  """Preprocess batch by converting keypoints data to float and moving it to the device."""
86
88
  batch = super().preprocess(batch)
87
89
  batch["keypoints"] = batch["keypoints"].to(self.device).float()
@@ -116,7 +118,7 @@ class PoseValidator(DetectionValidator):
116
118
  nkpt = self.kpt_shape[0]
117
119
  self.sigma = OKS_SIGMA if is_pose else np.ones(nkpt) / nkpt
118
120
 
119
- def postprocess(self, preds: torch.Tensor) -> Dict[str, torch.Tensor]:
121
+ def postprocess(self, preds: torch.Tensor) -> dict[str, torch.Tensor]:
120
122
  """
121
123
  Postprocess YOLO predictions to extract and reshape keypoints for pose estimation.
122
124
 
@@ -146,7 +148,7 @@ class PoseValidator(DetectionValidator):
146
148
  pred["keypoints"] = pred.pop("extra").view(-1, *self.kpt_shape) # remove extra if exists
147
149
  return preds
148
150
 
149
- def _prepare_batch(self, si: int, batch: Dict[str, Any]) -> Dict[str, Any]:
151
+ def _prepare_batch(self, si: int, batch: dict[str, Any]) -> dict[str, Any]:
150
152
  """
151
153
  Prepare a batch for processing by converting keypoints to float and scaling to original dimensions.
152
154
 
@@ -170,7 +172,7 @@ class PoseValidator(DetectionValidator):
170
172
  pbatch["keypoints"] = kpts
171
173
  return pbatch
172
174
 
173
- def _process_batch(self, preds: Dict[str, torch.Tensor], batch: Dict[str, Any]) -> Dict[str, np.ndarray]:
175
+ def _process_batch(self, preds: dict[str, torch.Tensor], batch: dict[str, Any]) -> dict[str, np.ndarray]:
174
176
  """
175
177
  Return correct prediction matrix by computing Intersection over Union (IoU) between detections and ground truth.
176
178
 
@@ -200,7 +202,7 @@ class PoseValidator(DetectionValidator):
200
202
  tp.update({"tp_p": tp_p}) # update tp with kpts IoU
201
203
  return tp
202
204
 
203
- def save_one_txt(self, predn: Dict[str, torch.Tensor], save_conf: bool, shape: Tuple[int, int], file: Path) -> None:
205
+ def save_one_txt(self, predn: dict[str, torch.Tensor], save_conf: bool, shape: tuple[int, int], file: Path) -> None:
204
206
  """
205
207
  Save YOLO pose detections to a text file in normalized coordinates.
206
208
 
@@ -224,7 +226,7 @@ class PoseValidator(DetectionValidator):
224
226
  keypoints=predn["keypoints"],
225
227
  ).save_txt(file, save_conf=save_conf)
226
228
 
227
- def pred_to_json(self, predn: Dict[str, torch.Tensor], pbatch: Dict[str, Any]) -> None:
229
+ def pred_to_json(self, predn: dict[str, torch.Tensor], pbatch: dict[str, Any]) -> None:
228
230
  """
229
231
  Convert YOLO predictions to COCO JSON format.
230
232
 
@@ -246,7 +248,7 @@ class PoseValidator(DetectionValidator):
246
248
  for i, k in enumerate(kpts.flatten(1, 2).tolist()):
247
249
  self.jdict[-len(kpts) + i]["keypoints"] = k # keypoints
248
250
 
249
- def scale_preds(self, predn: Dict[str, torch.Tensor], pbatch: Dict[str, Any]) -> Dict[str, torch.Tensor]:
251
+ def scale_preds(self, predn: dict[str, torch.Tensor], pbatch: dict[str, Any]) -> dict[str, torch.Tensor]:
250
252
  """Scales predictions to the original image size."""
251
253
  return {
252
254
  **super().scale_preds(predn, pbatch),
@@ -258,7 +260,7 @@ class PoseValidator(DetectionValidator):
258
260
  ),
259
261
  }
260
262
 
261
- def eval_json(self, stats: Dict[str, Any]) -> Dict[str, Any]:
263
+ def eval_json(self, stats: dict[str, Any]) -> dict[str, Any]:
262
264
  """Evaluate object detection model using COCO JSON format."""
263
265
  anno_json = self.data["path"] / "annotations/person_keypoints_val2017.json" # annotations
264
266
  pred_json = self.save_dir / "predictions.json" # predictions
@@ -1,8 +1,9 @@
1
1
  # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
+ from __future__ import annotations
4
+
3
5
  from copy import copy
4
6
  from pathlib import Path
5
- from typing import Dict, Optional, Union
6
7
 
7
8
  from ultralytics.models import yolo
8
9
  from ultralytics.nn.tasks import SegmentationModel
@@ -27,7 +28,7 @@ class SegmentationTrainer(yolo.detect.DetectionTrainer):
27
28
  >>> trainer.train()
28
29
  """
29
30
 
30
- def __init__(self, cfg=DEFAULT_CFG, overrides: Optional[Dict] = None, _callbacks=None):
31
+ def __init__(self, cfg=DEFAULT_CFG, overrides: dict | None = None, _callbacks=None):
31
32
  """
32
33
  Initialize a SegmentationTrainer object.
33
34
 
@@ -50,9 +51,7 @@ class SegmentationTrainer(yolo.detect.DetectionTrainer):
50
51
  overrides["task"] = "segment"
51
52
  super().__init__(cfg, overrides, _callbacks)
52
53
 
53
- def get_model(
54
- self, cfg: Optional[Union[Dict, str]] = None, weights: Optional[Union[str, Path]] = None, verbose: bool = True
55
- ):
54
+ def get_model(self, cfg: dict | str | None = None, weights: str | Path | None = None, verbose: bool = True):
56
55
  """
57
56
  Initialize and return a SegmentationModel with specified configuration and weights.
58
57
 
@@ -1,8 +1,10 @@
1
1
  # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
+ from __future__ import annotations
4
+
3
5
  from multiprocessing.pool import ThreadPool
4
6
  from pathlib import Path
5
- from typing import Any, Dict, List, Tuple
7
+ from typing import Any
6
8
 
7
9
  import numpy as np
8
10
  import torch
@@ -50,7 +52,7 @@ class SegmentationValidator(DetectionValidator):
50
52
  self.args.task = "segment"
51
53
  self.metrics = SegmentMetrics()
52
54
 
53
- def preprocess(self, batch: Dict[str, Any]) -> Dict[str, Any]:
55
+ def preprocess(self, batch: dict[str, Any]) -> dict[str, Any]:
54
56
  """
55
57
  Preprocess batch of images for YOLO segmentation validation.
56
58
 
@@ -93,7 +95,7 @@ class SegmentationValidator(DetectionValidator):
93
95
  "mAP50-95)",
94
96
  )
95
97
 
96
- def postprocess(self, preds: List[torch.Tensor]) -> List[Dict[str, torch.Tensor]]:
98
+ def postprocess(self, preds: list[torch.Tensor]) -> list[dict[str, torch.Tensor]]:
97
99
  """
98
100
  Post-process YOLO predictions and return output detections with proto.
99
101
 
@@ -119,7 +121,7 @@ class SegmentationValidator(DetectionValidator):
119
121
  )
120
122
  return preds
121
123
 
122
- def _prepare_batch(self, si: int, batch: Dict[str, Any]) -> Dict[str, Any]:
124
+ def _prepare_batch(self, si: int, batch: dict[str, Any]) -> dict[str, Any]:
123
125
  """
124
126
  Prepare a batch for training or inference by processing images and targets.
125
127
 
@@ -135,7 +137,7 @@ class SegmentationValidator(DetectionValidator):
135
137
  prepared_batch["masks"] = batch["masks"][midx]
136
138
  return prepared_batch
137
139
 
138
- def _process_batch(self, preds: Dict[str, torch.Tensor], batch: Dict[str, Any]) -> Dict[str, np.ndarray]:
140
+ def _process_batch(self, preds: dict[str, torch.Tensor], batch: dict[str, Any]) -> dict[str, np.ndarray]:
139
141
  """
140
142
  Compute correct prediction matrix for a batch based on bounding boxes and optional masks.
141
143
 
@@ -174,7 +176,7 @@ class SegmentationValidator(DetectionValidator):
174
176
  tp.update({"tp_m": tp_m}) # update tp with mask IoU
175
177
  return tp
176
178
 
177
- def plot_predictions(self, batch: Dict[str, Any], preds: List[Dict[str, torch.Tensor]], ni: int) -> None:
179
+ def plot_predictions(self, batch: dict[str, Any], preds: list[dict[str, torch.Tensor]], ni: int) -> None:
178
180
  """
179
181
  Plot batch predictions with masks and bounding boxes.
180
182
 
@@ -190,7 +192,7 @@ class SegmentationValidator(DetectionValidator):
190
192
  p["masks"] = torch.as_tensor(masks[:50], dtype=torch.uint8).cpu()
191
193
  super().plot_predictions(batch, preds, ni, max_det=50) # plot bboxes
192
194
 
193
- def save_one_txt(self, predn: torch.Tensor, save_conf: bool, shape: Tuple[int, int], file: Path) -> None:
195
+ def save_one_txt(self, predn: torch.Tensor, save_conf: bool, shape: tuple[int, int], file: Path) -> None:
194
196
  """
195
197
  Save YOLO detections to a txt file in normalized coordinates in a specific format.
196
198
 
@@ -210,7 +212,7 @@ class SegmentationValidator(DetectionValidator):
210
212
  masks=torch.as_tensor(predn["masks"], dtype=torch.uint8),
211
213
  ).save_txt(file, save_conf=save_conf)
212
214
 
213
- def pred_to_json(self, predn: Dict[str, torch.Tensor], pbatch: Dict[str, Any]) -> None:
215
+ def pred_to_json(self, predn: dict[str, torch.Tensor], pbatch: dict[str, Any]) -> None:
214
216
  """
215
217
  Save one JSON result for COCO evaluation.
216
218
 
@@ -233,7 +235,7 @@ class SegmentationValidator(DetectionValidator):
233
235
  for i, r in enumerate(rles):
234
236
  self.jdict[-len(rles) + i]["segmentation"] = r # segmentation
235
237
 
236
- def scale_preds(self, predn: Dict[str, torch.Tensor], pbatch: Dict[str, Any]) -> Dict[str, torch.Tensor]:
238
+ def scale_preds(self, predn: dict[str, torch.Tensor], pbatch: dict[str, Any]) -> dict[str, torch.Tensor]:
237
239
  """Scales predictions to the original image size."""
238
240
  return {
239
241
  **super().scale_preds(predn, pbatch),
@@ -244,7 +246,7 @@ class SegmentationValidator(DetectionValidator):
244
246
  ),
245
247
  }
246
248
 
247
- def eval_json(self, stats: Dict[str, Any]) -> Dict[str, Any]:
249
+ def eval_json(self, stats: dict[str, Any]) -> dict[str, Any]:
248
250
  """Return COCO-style instance segmentation evaluation metrics."""
249
251
  pred_json = self.save_dir / "predictions.json" # predictions
250
252
  anno_json = (
@@ -1,8 +1,10 @@
1
1
  # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
+ from __future__ import annotations
4
+
3
5
  import itertools
4
6
  from pathlib import Path
5
- from typing import Any, Dict, List, Optional
7
+ from typing import Any
6
8
 
7
9
  import torch
8
10
 
@@ -51,7 +53,7 @@ class WorldTrainer(DetectionTrainer):
51
53
  >>> trainer.train()
52
54
  """
53
55
 
54
- def __init__(self, cfg=DEFAULT_CFG, overrides: Optional[Dict[str, Any]] = None, _callbacks=None):
56
+ def __init__(self, cfg=DEFAULT_CFG, overrides: dict[str, Any] | None = None, _callbacks=None):
55
57
  """
56
58
  Initialize a WorldTrainer object with given arguments.
57
59
 
@@ -65,7 +67,7 @@ class WorldTrainer(DetectionTrainer):
65
67
  super().__init__(cfg, overrides, _callbacks)
66
68
  self.text_embeddings = None
67
69
 
68
- def get_model(self, cfg=None, weights: Optional[str] = None, verbose: bool = True) -> WorldModel:
70
+ def get_model(self, cfg=None, weights: str | None = None, verbose: bool = True) -> WorldModel:
69
71
  """
70
72
  Return WorldModel initialized with specified config and weights.
71
73
 
@@ -91,7 +93,7 @@ class WorldTrainer(DetectionTrainer):
91
93
 
92
94
  return model
93
95
 
94
- def build_dataset(self, img_path: str, mode: str = "train", batch: Optional[int] = None):
96
+ def build_dataset(self, img_path: str, mode: str = "train", batch: int | None = None):
95
97
  """
96
98
  Build YOLO Dataset for training or validation.
97
99
 
@@ -111,7 +113,7 @@ class WorldTrainer(DetectionTrainer):
111
113
  self.set_text_embeddings([dataset], batch) # cache text embeddings to accelerate training
112
114
  return dataset
113
115
 
114
- def set_text_embeddings(self, datasets: List[Any], batch: Optional[int]) -> None:
116
+ def set_text_embeddings(self, datasets: list[Any], batch: int | None) -> None:
115
117
  """
116
118
  Set text embeddings for datasets to accelerate training by caching category names.
117
119
 
@@ -137,7 +139,7 @@ class WorldTrainer(DetectionTrainer):
137
139
  )
138
140
  self.text_embeddings = text_embeddings
139
141
 
140
- def generate_text_embeddings(self, texts: List[str], batch: int, cache_dir: Path) -> Dict[str, torch.Tensor]:
142
+ def generate_text_embeddings(self, texts: list[str], batch: int, cache_dir: Path) -> dict[str, torch.Tensor]:
141
143
  """
142
144
  Generate text embeddings for a list of text samples.
143
145
 
@@ -163,7 +165,7 @@ class WorldTrainer(DetectionTrainer):
163
165
  torch.save(txt_map, cache_path)
164
166
  return txt_map
165
167
 
166
- def preprocess_batch(self, batch: Dict[str, Any]) -> Dict[str, Any]:
168
+ def preprocess_batch(self, batch: dict[str, Any]) -> dict[str, Any]:
167
169
  """Preprocess a batch of images and text for YOLOWorld training."""
168
170
  batch = DetectionTrainer.preprocess_batch(self, batch)
169
171
 
@@ -1,9 +1,10 @@
1
1
  # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
+ from __future__ import annotations
4
+
3
5
  import itertools
4
6
  from copy import copy, deepcopy
5
7
  from pathlib import Path
6
- from typing import Dict, List, Optional, Union
7
8
 
8
9
  import torch
9
10
 
@@ -34,7 +35,7 @@ class YOLOETrainer(DetectionTrainer):
34
35
  build_dataset: Build YOLO dataset with multi-modal support for training.
35
36
  """
36
37
 
37
- def __init__(self, cfg=DEFAULT_CFG, overrides: Optional[Dict] = None, _callbacks=None):
38
+ def __init__(self, cfg=DEFAULT_CFG, overrides: dict | None = None, _callbacks=None):
38
39
  """
39
40
  Initialize the YOLOE Trainer with specified configurations.
40
41
 
@@ -89,7 +90,7 @@ class YOLOETrainer(DetectionTrainer):
89
90
  self.test_loader, save_dir=self.save_dir, args=copy(self.args), _callbacks=self.callbacks
90
91
  )
91
92
 
92
- def build_dataset(self, img_path: str, mode: str = "train", batch: Optional[int] = None):
93
+ def build_dataset(self, img_path: str, mode: str = "train", batch: int | None = None):
93
94
  """
94
95
  Build YOLO Dataset.
95
96
 
@@ -174,7 +175,7 @@ class YOLOETrainerFromScratch(YOLOETrainer, WorldTrainerFromScratch):
174
175
  generate_text_embeddings: Generate and cache text embeddings for training.
175
176
  """
176
177
 
177
- def build_dataset(self, img_path: Union[List[str], str], mode: str = "train", batch: Optional[int] = None):
178
+ def build_dataset(self, img_path: list[str] | str, mode: str = "train", batch: int | None = None):
178
179
  """
179
180
  Build YOLO Dataset for training or validation.
180
181
 
@@ -201,7 +202,7 @@ class YOLOETrainerFromScratch(YOLOETrainer, WorldTrainerFromScratch):
201
202
  batch["txt_feats"] = txt_feats
202
203
  return batch
203
204
 
204
- def generate_text_embeddings(self, texts: List[str], batch: int, cache_dir: Path):
205
+ def generate_text_embeddings(self, texts: list[str], batch: int, cache_dir: Path):
205
206
  """
206
207
  Generate text embeddings for a list of text samples.
207
208
 
@@ -285,7 +286,7 @@ class YOLOEVPTrainer(YOLOETrainerFromScratch):
285
286
  preprocess_batch: Preprocess batches with visual prompts.
286
287
  """
287
288
 
288
- def build_dataset(self, img_path: Union[List[str], str], mode: str = "train", batch: Optional[int] = None):
289
+ def build_dataset(self, img_path: list[str] | str, mode: str = "train", batch: int | None = None):
289
290
  """
290
291
  Build YOLO Dataset for training or validation with visual prompts.
291
292
 
@@ -1,8 +1,10 @@
1
1
  # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
2
2
 
3
+ from __future__ import annotations
4
+
3
5
  from copy import deepcopy
4
6
  from pathlib import Path
5
- from typing import Any, Dict, Optional, Union
7
+ from typing import Any
6
8
 
7
9
  import torch
8
10
  from torch.nn import functional as F
@@ -96,14 +98,14 @@ class YOLOEDetectValidator(DetectionValidator):
96
98
  visual_pe[cls_visual_num == 0] = 0
97
99
  return visual_pe.unsqueeze(0)
98
100
 
99
- def preprocess(self, batch: Dict[str, Any]) -> Dict[str, Any]:
101
+ def preprocess(self, batch: dict[str, Any]) -> dict[str, Any]:
100
102
  """Preprocess batch data, ensuring visuals are on the same device as images."""
101
103
  batch = super().preprocess(batch)
102
104
  if "visuals" in batch:
103
105
  batch["visuals"] = batch["visuals"].to(batch["img"].device)
104
106
  return batch
105
107
 
106
- def get_vpe_dataloader(self, data: Dict[str, Any]) -> torch.utils.data.DataLoader:
108
+ def get_vpe_dataloader(self, data: dict[str, Any]) -> torch.utils.data.DataLoader:
107
109
  """
108
110
  Create a dataloader for LVIS training visual prompt samples.
109
111
 
@@ -141,11 +143,11 @@ class YOLOEDetectValidator(DetectionValidator):
141
143
  @smart_inference_mode()
142
144
  def __call__(
143
145
  self,
144
- trainer: Optional[Any] = None,
145
- model: Optional[Union[YOLOEModel, str]] = None,
146
- refer_data: Optional[str] = None,
146
+ trainer: Any | None = None,
147
+ model: YOLOEModel | str | None = None,
148
+ refer_data: str | None = None,
147
149
  load_vp: bool = False,
148
- ) -> Dict[str, Any]:
150
+ ) -> dict[str, Any]:
149
151
  """
150
152
  Run validation on the model using either text or visual prompt embeddings.
151
153
 
@@ -186,7 +188,7 @@ class YOLOEDetectValidator(DetectionValidator):
186
188
  if isinstance(model, (str, Path)):
187
189
  from ultralytics.nn.tasks import attempt_load_weights
188
190
 
189
- model = attempt_load_weights(model, device=self.device, inplace=True)
191
+ model = attempt_load_weights(model, device=self.device)
190
192
  model.eval().to(self.device)
191
193
  data = check_det_dataset(refer_data or self.args.data)
192
194
  names = [name.split("/", 1)[0] for name in list(data["names"].values())]