dgenerate-ultralytics-headless 8.3.222__py3-none-any.whl → 8.3.225__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.
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/METADATA +2 -2
- dgenerate_ultralytics_headless-8.3.225.dist-info/RECORD +286 -0
- tests/conftest.py +5 -8
- tests/test_cli.py +1 -8
- tests/test_python.py +1 -2
- ultralytics/__init__.py +1 -1
- ultralytics/cfg/__init__.py +34 -49
- ultralytics/cfg/datasets/ImageNet.yaml +1 -1
- ultralytics/cfg/datasets/kitti.yaml +27 -0
- ultralytics/cfg/datasets/lvis.yaml +5 -5
- ultralytics/cfg/datasets/open-images-v7.yaml +1 -1
- ultralytics/data/annotator.py +3 -4
- ultralytics/data/augment.py +244 -323
- ultralytics/data/base.py +12 -22
- ultralytics/data/build.py +47 -40
- ultralytics/data/converter.py +32 -42
- ultralytics/data/dataset.py +43 -71
- ultralytics/data/loaders.py +22 -34
- ultralytics/data/split.py +5 -6
- ultralytics/data/split_dota.py +8 -15
- ultralytics/data/utils.py +27 -36
- ultralytics/engine/exporter.py +49 -116
- ultralytics/engine/model.py +144 -180
- ultralytics/engine/predictor.py +18 -29
- ultralytics/engine/results.py +165 -231
- ultralytics/engine/trainer.py +11 -19
- ultralytics/engine/tuner.py +13 -23
- ultralytics/engine/validator.py +6 -10
- ultralytics/hub/__init__.py +7 -12
- ultralytics/hub/auth.py +6 -12
- ultralytics/hub/google/__init__.py +7 -10
- ultralytics/hub/session.py +15 -25
- ultralytics/hub/utils.py +3 -6
- ultralytics/models/fastsam/model.py +6 -8
- ultralytics/models/fastsam/predict.py +5 -10
- ultralytics/models/fastsam/utils.py +1 -2
- ultralytics/models/fastsam/val.py +2 -4
- ultralytics/models/nas/model.py +5 -8
- ultralytics/models/nas/predict.py +7 -9
- ultralytics/models/nas/val.py +1 -2
- ultralytics/models/rtdetr/model.py +5 -8
- ultralytics/models/rtdetr/predict.py +15 -18
- ultralytics/models/rtdetr/train.py +10 -13
- ultralytics/models/rtdetr/val.py +13 -20
- ultralytics/models/sam/amg.py +12 -18
- ultralytics/models/sam/build.py +6 -9
- ultralytics/models/sam/model.py +16 -23
- ultralytics/models/sam/modules/blocks.py +62 -84
- ultralytics/models/sam/modules/decoders.py +17 -24
- ultralytics/models/sam/modules/encoders.py +40 -56
- ultralytics/models/sam/modules/memory_attention.py +10 -16
- ultralytics/models/sam/modules/sam.py +41 -47
- ultralytics/models/sam/modules/tiny_encoder.py +64 -83
- ultralytics/models/sam/modules/transformer.py +17 -27
- ultralytics/models/sam/modules/utils.py +31 -42
- ultralytics/models/sam/predict.py +172 -209
- ultralytics/models/utils/loss.py +14 -26
- ultralytics/models/utils/ops.py +13 -17
- ultralytics/models/yolo/classify/predict.py +8 -11
- ultralytics/models/yolo/classify/train.py +8 -16
- ultralytics/models/yolo/classify/val.py +13 -20
- ultralytics/models/yolo/detect/predict.py +4 -8
- ultralytics/models/yolo/detect/train.py +11 -20
- ultralytics/models/yolo/detect/val.py +38 -48
- ultralytics/models/yolo/model.py +35 -47
- ultralytics/models/yolo/obb/predict.py +5 -8
- ultralytics/models/yolo/obb/train.py +11 -14
- ultralytics/models/yolo/obb/val.py +20 -28
- ultralytics/models/yolo/pose/predict.py +5 -8
- ultralytics/models/yolo/pose/train.py +4 -8
- ultralytics/models/yolo/pose/val.py +31 -39
- ultralytics/models/yolo/segment/predict.py +9 -14
- ultralytics/models/yolo/segment/train.py +3 -6
- ultralytics/models/yolo/segment/val.py +16 -26
- ultralytics/models/yolo/world/train.py +8 -14
- ultralytics/models/yolo/world/train_world.py +11 -16
- ultralytics/models/yolo/yoloe/predict.py +16 -23
- ultralytics/models/yolo/yoloe/train.py +30 -43
- ultralytics/models/yolo/yoloe/train_seg.py +5 -10
- ultralytics/models/yolo/yoloe/val.py +15 -20
- ultralytics/nn/autobackend.py +10 -18
- ultralytics/nn/modules/activation.py +4 -6
- ultralytics/nn/modules/block.py +99 -185
- ultralytics/nn/modules/conv.py +45 -90
- ultralytics/nn/modules/head.py +44 -98
- ultralytics/nn/modules/transformer.py +44 -76
- ultralytics/nn/modules/utils.py +14 -19
- ultralytics/nn/tasks.py +86 -146
- ultralytics/nn/text_model.py +25 -40
- ultralytics/solutions/ai_gym.py +10 -16
- ultralytics/solutions/analytics.py +7 -10
- ultralytics/solutions/config.py +4 -5
- ultralytics/solutions/distance_calculation.py +9 -12
- ultralytics/solutions/heatmap.py +7 -13
- ultralytics/solutions/instance_segmentation.py +5 -8
- ultralytics/solutions/object_blurrer.py +7 -10
- ultralytics/solutions/object_counter.py +8 -12
- ultralytics/solutions/object_cropper.py +5 -8
- ultralytics/solutions/parking_management.py +12 -14
- ultralytics/solutions/queue_management.py +4 -6
- ultralytics/solutions/region_counter.py +7 -10
- ultralytics/solutions/security_alarm.py +14 -19
- ultralytics/solutions/similarity_search.py +7 -12
- ultralytics/solutions/solutions.py +31 -53
- ultralytics/solutions/speed_estimation.py +6 -9
- ultralytics/solutions/streamlit_inference.py +2 -4
- ultralytics/solutions/trackzone.py +7 -10
- ultralytics/solutions/vision_eye.py +5 -8
- ultralytics/trackers/basetrack.py +2 -4
- ultralytics/trackers/bot_sort.py +6 -11
- ultralytics/trackers/byte_tracker.py +10 -15
- ultralytics/trackers/track.py +3 -6
- ultralytics/trackers/utils/gmc.py +6 -12
- ultralytics/trackers/utils/kalman_filter.py +35 -43
- ultralytics/trackers/utils/matching.py +6 -10
- ultralytics/utils/__init__.py +61 -100
- ultralytics/utils/autobatch.py +2 -4
- ultralytics/utils/autodevice.py +11 -13
- ultralytics/utils/benchmarks.py +25 -35
- ultralytics/utils/callbacks/base.py +8 -10
- ultralytics/utils/callbacks/clearml.py +2 -4
- ultralytics/utils/callbacks/comet.py +30 -44
- ultralytics/utils/callbacks/dvc.py +13 -18
- ultralytics/utils/callbacks/mlflow.py +4 -5
- ultralytics/utils/callbacks/neptune.py +4 -6
- ultralytics/utils/callbacks/raytune.py +3 -4
- ultralytics/utils/callbacks/tensorboard.py +4 -6
- ultralytics/utils/callbacks/wb.py +10 -13
- ultralytics/utils/checks.py +29 -56
- ultralytics/utils/cpu.py +1 -2
- ultralytics/utils/dist.py +8 -12
- ultralytics/utils/downloads.py +17 -27
- ultralytics/utils/errors.py +6 -8
- ultralytics/utils/events.py +2 -4
- ultralytics/utils/export/__init__.py +4 -239
- ultralytics/utils/export/engine.py +237 -0
- ultralytics/utils/export/imx.py +11 -17
- ultralytics/utils/export/tensorflow.py +217 -0
- ultralytics/utils/files.py +10 -15
- ultralytics/utils/git.py +5 -7
- ultralytics/utils/instance.py +30 -51
- ultralytics/utils/logger.py +11 -15
- ultralytics/utils/loss.py +8 -14
- ultralytics/utils/metrics.py +98 -138
- ultralytics/utils/nms.py +13 -16
- ultralytics/utils/ops.py +47 -74
- ultralytics/utils/patches.py +11 -18
- ultralytics/utils/plotting.py +29 -42
- ultralytics/utils/tal.py +25 -39
- ultralytics/utils/torch_utils.py +45 -73
- ultralytics/utils/tqdm.py +6 -8
- ultralytics/utils/triton.py +9 -12
- ultralytics/utils/tuner.py +1 -2
- dgenerate_ultralytics_headless-8.3.222.dist-info/RECORD +0 -283
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/WHEEL +0 -0
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/entry_points.txt +0 -0
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/licenses/LICENSE +0 -0
- {dgenerate_ultralytics_headless-8.3.222.dist-info → dgenerate_ultralytics_headless-8.3.225.dist-info}/top_level.txt +0 -0
ultralytics/engine/model.py
CHANGED
|
@@ -27,12 +27,11 @@ from ultralytics.utils import (
|
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
class Model(torch.nn.Module):
|
|
30
|
-
"""
|
|
31
|
-
A base class for implementing YOLO models, unifying APIs across different model types.
|
|
30
|
+
"""A base class for implementing YOLO models, unifying APIs across different model types.
|
|
32
31
|
|
|
33
|
-
This class provides a common interface for various operations related to YOLO models, such as training,
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
This class provides a common interface for various operations related to YOLO models, such as training, validation,
|
|
33
|
+
prediction, exporting, and benchmarking. It handles different types of models, including those loaded from local
|
|
34
|
+
files, Ultralytics HUB, or Triton Server.
|
|
36
35
|
|
|
37
36
|
Attributes:
|
|
38
37
|
callbacks (dict): A dictionary of callback functions for various events during model operations.
|
|
@@ -85,20 +84,17 @@ class Model(torch.nn.Module):
|
|
|
85
84
|
task: str | None = None,
|
|
86
85
|
verbose: bool = False,
|
|
87
86
|
) -> None:
|
|
88
|
-
"""
|
|
89
|
-
Initialize a new instance of the YOLO model class.
|
|
87
|
+
"""Initialize a new instance of the YOLO model class.
|
|
90
88
|
|
|
91
|
-
This constructor sets up the model based on the provided model path or name. It handles various types of
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
prediction, or export.
|
|
89
|
+
This constructor sets up the model based on the provided model path or name. It handles various types of model
|
|
90
|
+
sources, including local files, Ultralytics HUB models, and Triton Server models. The method initializes several
|
|
91
|
+
important attributes of the model and prepares it for operations like training, prediction, or export.
|
|
95
92
|
|
|
96
93
|
Args:
|
|
97
|
-
model (str | Path | Model): Path or name of the model to load or create. Can be a local file path, a
|
|
98
|
-
|
|
94
|
+
model (str | Path | Model): Path or name of the model to load or create. Can be a local file path, a model
|
|
95
|
+
name from Ultralytics HUB, a Triton Server model, or an already initialized Model instance.
|
|
99
96
|
task (str, optional): The specific task for the model. If None, it will be inferred from the config.
|
|
100
|
-
verbose (bool): If True, enables verbose output during the model's initialization and subsequent
|
|
101
|
-
operations.
|
|
97
|
+
verbose (bool): If True, enables verbose output during the model's initialization and subsequent operations.
|
|
102
98
|
|
|
103
99
|
Raises:
|
|
104
100
|
FileNotFoundError: If the specified model file does not exist or is inaccessible.
|
|
@@ -161,22 +157,21 @@ class Model(torch.nn.Module):
|
|
|
161
157
|
stream: bool = False,
|
|
162
158
|
**kwargs: Any,
|
|
163
159
|
) -> list:
|
|
164
|
-
"""
|
|
165
|
-
Alias for the predict method, enabling the model instance to be callable for predictions.
|
|
160
|
+
"""Alias for the predict method, enabling the model instance to be callable for predictions.
|
|
166
161
|
|
|
167
|
-
This method simplifies the process of making predictions by allowing the model instance to be called
|
|
168
|
-
|
|
162
|
+
This method simplifies the process of making predictions by allowing the model instance to be called directly
|
|
163
|
+
with the required arguments.
|
|
169
164
|
|
|
170
165
|
Args:
|
|
171
|
-
source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | list | tuple): The source of
|
|
172
|
-
|
|
173
|
-
|
|
166
|
+
source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | list | tuple): The source of the image(s)
|
|
167
|
+
to make predictions on. Can be a file path, URL, PIL image, numpy array, PyTorch tensor, or a list/tuple
|
|
168
|
+
of these.
|
|
174
169
|
stream (bool): If True, treat the input source as a continuous stream for predictions.
|
|
175
170
|
**kwargs (Any): Additional keyword arguments to configure the prediction process.
|
|
176
171
|
|
|
177
172
|
Returns:
|
|
178
|
-
(list[ultralytics.engine.results.Results]): A list of prediction results, each encapsulated in a
|
|
179
|
-
|
|
173
|
+
(list[ultralytics.engine.results.Results]): A list of prediction results, each encapsulated in a Results
|
|
174
|
+
object.
|
|
180
175
|
|
|
181
176
|
Examples:
|
|
182
177
|
>>> model = YOLO("yolo11n.pt")
|
|
@@ -188,11 +183,10 @@ class Model(torch.nn.Module):
|
|
|
188
183
|
|
|
189
184
|
@staticmethod
|
|
190
185
|
def is_triton_model(model: str) -> bool:
|
|
191
|
-
"""
|
|
192
|
-
Check if the given model string is a Triton Server URL.
|
|
186
|
+
"""Check if the given model string is a Triton Server URL.
|
|
193
187
|
|
|
194
|
-
This static method determines whether the provided model string represents a valid Triton Server URL by
|
|
195
|
-
|
|
188
|
+
This static method determines whether the provided model string represents a valid Triton Server URL by parsing
|
|
189
|
+
its components using urllib.parse.urlsplit().
|
|
196
190
|
|
|
197
191
|
Args:
|
|
198
192
|
model (str): The model string to be checked.
|
|
@@ -213,8 +207,7 @@ class Model(torch.nn.Module):
|
|
|
213
207
|
|
|
214
208
|
@staticmethod
|
|
215
209
|
def is_hub_model(model: str) -> bool:
|
|
216
|
-
"""
|
|
217
|
-
Check if the provided model is an Ultralytics HUB model.
|
|
210
|
+
"""Check if the provided model is an Ultralytics HUB model.
|
|
218
211
|
|
|
219
212
|
This static method determines whether the given model string represents a valid Ultralytics HUB model
|
|
220
213
|
identifier.
|
|
@@ -236,17 +229,16 @@ class Model(torch.nn.Module):
|
|
|
236
229
|
return model.startswith(f"{HUB_WEB_ROOT}/models/")
|
|
237
230
|
|
|
238
231
|
def _new(self, cfg: str, task=None, model=None, verbose=False) -> None:
|
|
239
|
-
"""
|
|
240
|
-
Initialize a new model and infer the task type from model definitions.
|
|
232
|
+
"""Initialize a new model and infer the task type from model definitions.
|
|
241
233
|
|
|
242
|
-
Creates a new model instance based on the provided configuration file. Loads the model configuration, infers
|
|
243
|
-
|
|
234
|
+
Creates a new model instance based on the provided configuration file. Loads the model configuration, infers the
|
|
235
|
+
task type if not specified, and initializes the model using the appropriate class from the task map.
|
|
244
236
|
|
|
245
237
|
Args:
|
|
246
238
|
cfg (str): Path to the model configuration file in YAML format.
|
|
247
239
|
task (str, optional): The specific task for the model. If None, it will be inferred from the config.
|
|
248
|
-
model (torch.nn.Module, optional): A custom model instance. If provided, it will be used instead of
|
|
249
|
-
|
|
240
|
+
model (torch.nn.Module, optional): A custom model instance. If provided, it will be used instead of creating
|
|
241
|
+
a new one.
|
|
250
242
|
verbose (bool): If True, displays model information during loading.
|
|
251
243
|
|
|
252
244
|
Raises:
|
|
@@ -270,11 +262,10 @@ class Model(torch.nn.Module):
|
|
|
270
262
|
self.model_name = cfg
|
|
271
263
|
|
|
272
264
|
def _load(self, weights: str, task=None) -> None:
|
|
273
|
-
"""
|
|
274
|
-
Load a model from a checkpoint file or initialize it from a weights file.
|
|
265
|
+
"""Load a model from a checkpoint file or initialize it from a weights file.
|
|
275
266
|
|
|
276
|
-
This method handles loading models from either .pt checkpoint files or other weight file formats. It sets
|
|
277
|
-
|
|
267
|
+
This method handles loading models from either .pt checkpoint files or other weight file formats. It sets up the
|
|
268
|
+
model, task, and related attributes based on the loaded weights.
|
|
278
269
|
|
|
279
270
|
Args:
|
|
280
271
|
weights (str): Path to the model weights file to be loaded.
|
|
@@ -308,11 +299,10 @@ class Model(torch.nn.Module):
|
|
|
308
299
|
self.model_name = weights
|
|
309
300
|
|
|
310
301
|
def _check_is_pytorch_model(self) -> None:
|
|
311
|
-
"""
|
|
312
|
-
Check if the model is a PyTorch model and raise TypeError if it's not.
|
|
302
|
+
"""Check if the model is a PyTorch model and raise TypeError if it's not.
|
|
313
303
|
|
|
314
|
-
This method verifies that the model is either a PyTorch module or a .pt file. It's used to ensure that
|
|
315
|
-
|
|
304
|
+
This method verifies that the model is either a PyTorch module or a .pt file. It's used to ensure that certain
|
|
305
|
+
operations that require a PyTorch model are only performed on compatible model types.
|
|
316
306
|
|
|
317
307
|
Raises:
|
|
318
308
|
TypeError: If the model is not a PyTorch module or a .pt file. The error message provides detailed
|
|
@@ -336,12 +326,11 @@ class Model(torch.nn.Module):
|
|
|
336
326
|
)
|
|
337
327
|
|
|
338
328
|
def reset_weights(self) -> Model:
|
|
339
|
-
"""
|
|
340
|
-
Reset the model's weights to their initial state.
|
|
329
|
+
"""Reset the model's weights to their initial state.
|
|
341
330
|
|
|
342
331
|
This method iterates through all modules in the model and resets their parameters if they have a
|
|
343
|
-
'reset_parameters' method. It also ensures that all parameters have 'requires_grad' set to True,
|
|
344
|
-
|
|
332
|
+
'reset_parameters' method. It also ensures that all parameters have 'requires_grad' set to True, enabling them
|
|
333
|
+
to be updated during training.
|
|
345
334
|
|
|
346
335
|
Returns:
|
|
347
336
|
(Model): The instance of the class with reset weights.
|
|
@@ -362,8 +351,7 @@ class Model(torch.nn.Module):
|
|
|
362
351
|
return self
|
|
363
352
|
|
|
364
353
|
def load(self, weights: str | Path = "yolo11n.pt") -> Model:
|
|
365
|
-
"""
|
|
366
|
-
Load parameters from the specified weights file into the model.
|
|
354
|
+
"""Load parameters from the specified weights file into the model.
|
|
367
355
|
|
|
368
356
|
This method supports loading weights from a file or directly from a weights object. It matches parameters by
|
|
369
357
|
name and shape and transfers them to the model.
|
|
@@ -390,11 +378,10 @@ class Model(torch.nn.Module):
|
|
|
390
378
|
return self
|
|
391
379
|
|
|
392
380
|
def save(self, filename: str | Path = "saved_model.pt") -> None:
|
|
393
|
-
"""
|
|
394
|
-
Save the current model state to a file.
|
|
381
|
+
"""Save the current model state to a file.
|
|
395
382
|
|
|
396
|
-
This method exports the model's checkpoint (ckpt) to the specified filename. It includes metadata such as
|
|
397
|
-
|
|
383
|
+
This method exports the model's checkpoint (ckpt) to the specified filename. It includes metadata such as the
|
|
384
|
+
date, Ultralytics version, license information, and a link to the documentation.
|
|
398
385
|
|
|
399
386
|
Args:
|
|
400
387
|
filename (str | Path): The name of the file to save the model to.
|
|
@@ -422,8 +409,7 @@ class Model(torch.nn.Module):
|
|
|
422
409
|
torch.save({**self.ckpt, **updates}, filename)
|
|
423
410
|
|
|
424
411
|
def info(self, detailed: bool = False, verbose: bool = True):
|
|
425
|
-
"""
|
|
426
|
-
Display model information.
|
|
412
|
+
"""Display model information.
|
|
427
413
|
|
|
428
414
|
This method provides an overview or detailed information about the model, depending on the arguments
|
|
429
415
|
passed. It can control the verbosity of the output and return the information as a list.
|
|
@@ -433,8 +419,8 @@ class Model(torch.nn.Module):
|
|
|
433
419
|
verbose (bool): If True, prints the information. If False, returns the information as a list.
|
|
434
420
|
|
|
435
421
|
Returns:
|
|
436
|
-
(list[str]): A list of strings containing various types of information about the model, including
|
|
437
|
-
|
|
422
|
+
(list[str]): A list of strings containing various types of information about the model, including model
|
|
423
|
+
summary, layer details, and parameter counts. Empty if verbose is True.
|
|
438
424
|
|
|
439
425
|
Examples:
|
|
440
426
|
>>> model = Model("yolo11n.pt")
|
|
@@ -445,12 +431,11 @@ class Model(torch.nn.Module):
|
|
|
445
431
|
return self.model.info(detailed=detailed, verbose=verbose)
|
|
446
432
|
|
|
447
433
|
def fuse(self) -> None:
|
|
448
|
-
"""
|
|
449
|
-
Fuse Conv2d and BatchNorm2d layers in the model for optimized inference.
|
|
434
|
+
"""Fuse Conv2d and BatchNorm2d layers in the model for optimized inference.
|
|
450
435
|
|
|
451
|
-
This method iterates through the model's modules and fuses consecutive Conv2d and BatchNorm2d layers
|
|
452
|
-
|
|
453
|
-
|
|
436
|
+
This method iterates through the model's modules and fuses consecutive Conv2d and BatchNorm2d layers into a
|
|
437
|
+
single layer. This fusion can significantly improve inference speed by reducing the number of operations and
|
|
438
|
+
memory accesses required during forward passes.
|
|
454
439
|
|
|
455
440
|
The fusion process typically involves folding the BatchNorm2d parameters (mean, variance, weight, and
|
|
456
441
|
bias) into the preceding Conv2d layer's weights and biases. This results in a single Conv2d layer that
|
|
@@ -470,15 +455,14 @@ class Model(torch.nn.Module):
|
|
|
470
455
|
stream: bool = False,
|
|
471
456
|
**kwargs: Any,
|
|
472
457
|
) -> list:
|
|
473
|
-
"""
|
|
474
|
-
Generate image embeddings based on the provided source.
|
|
458
|
+
"""Generate image embeddings based on the provided source.
|
|
475
459
|
|
|
476
460
|
This method is a wrapper around the 'predict()' method, focusing on generating embeddings from an image
|
|
477
461
|
source. It allows customization of the embedding process through various keyword arguments.
|
|
478
462
|
|
|
479
463
|
Args:
|
|
480
|
-
source (str | Path | int | list | tuple | np.ndarray | torch.Tensor): The source of the image for
|
|
481
|
-
|
|
464
|
+
source (str | Path | int | list | tuple | np.ndarray | torch.Tensor): The source of the image for generating
|
|
465
|
+
embeddings. Can be a file path, URL, PIL image, numpy array, etc.
|
|
482
466
|
stream (bool): If True, predictions are streamed.
|
|
483
467
|
**kwargs (Any): Additional keyword arguments for configuring the embedding process.
|
|
484
468
|
|
|
@@ -502,25 +486,24 @@ class Model(torch.nn.Module):
|
|
|
502
486
|
predictor=None,
|
|
503
487
|
**kwargs: Any,
|
|
504
488
|
) -> list[Results]:
|
|
505
|
-
"""
|
|
506
|
-
Perform predictions on the given image source using the YOLO model.
|
|
489
|
+
"""Perform predictions on the given image source using the YOLO model.
|
|
507
490
|
|
|
508
|
-
This method facilitates the prediction process, allowing various configurations through keyword arguments.
|
|
509
|
-
|
|
510
|
-
|
|
491
|
+
This method facilitates the prediction process, allowing various configurations through keyword arguments. It
|
|
492
|
+
supports predictions with custom predictors or the default predictor method. The method handles different types
|
|
493
|
+
of image sources and can operate in a streaming mode.
|
|
511
494
|
|
|
512
495
|
Args:
|
|
513
|
-
source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | list | tuple): The source
|
|
514
|
-
|
|
515
|
-
|
|
496
|
+
source (str | Path | int | PIL.Image | np.ndarray | torch.Tensor | list | tuple): The source of the image(s)
|
|
497
|
+
to make predictions on. Accepts various types including file paths, URLs, PIL images, numpy arrays, and
|
|
498
|
+
torch tensors.
|
|
516
499
|
stream (bool): If True, treats the input source as a continuous stream for predictions.
|
|
517
|
-
predictor (BasePredictor, optional): An instance of a custom predictor class for making predictions.
|
|
518
|
-
|
|
500
|
+
predictor (BasePredictor, optional): An instance of a custom predictor class for making predictions. If
|
|
501
|
+
None, the method uses a default predictor.
|
|
519
502
|
**kwargs (Any): Additional keyword arguments for configuring the prediction process.
|
|
520
503
|
|
|
521
504
|
Returns:
|
|
522
|
-
(list[ultralytics.engine.results.Results]): A list of prediction results, each encapsulated in a
|
|
523
|
-
|
|
505
|
+
(list[ultralytics.engine.results.Results]): A list of prediction results, each encapsulated in a Results
|
|
506
|
+
object.
|
|
524
507
|
|
|
525
508
|
Examples:
|
|
526
509
|
>>> model = YOLO("yolo11n.pt")
|
|
@@ -563,8 +546,7 @@ class Model(torch.nn.Module):
|
|
|
563
546
|
persist: bool = False,
|
|
564
547
|
**kwargs: Any,
|
|
565
548
|
) -> list[Results]:
|
|
566
|
-
"""
|
|
567
|
-
Conduct object tracking on the specified input source using the registered trackers.
|
|
549
|
+
"""Conduct object tracking on the specified input source using the registered trackers.
|
|
568
550
|
|
|
569
551
|
This method performs object tracking using the model's predictors and optionally registered trackers. It handles
|
|
570
552
|
various input sources such as file paths or video streams, and supports customization through keyword arguments.
|
|
@@ -605,8 +587,7 @@ class Model(torch.nn.Module):
|
|
|
605
587
|
validator=None,
|
|
606
588
|
**kwargs: Any,
|
|
607
589
|
):
|
|
608
|
-
"""
|
|
609
|
-
Validate the model using a specified dataset and validation configuration.
|
|
590
|
+
"""Validate the model using a specified dataset and validation configuration.
|
|
610
591
|
|
|
611
592
|
This method facilitates the model validation process, allowing for customization through various settings. It
|
|
612
593
|
supports validation with a custom validator or the default validation approach. The method combines default
|
|
@@ -637,13 +618,12 @@ class Model(torch.nn.Module):
|
|
|
637
618
|
return validator.metrics
|
|
638
619
|
|
|
639
620
|
def benchmark(self, data=None, format="", verbose=False, **kwargs: Any):
|
|
640
|
-
"""
|
|
641
|
-
Benchmark the model across various export formats to evaluate performance.
|
|
621
|
+
"""Benchmark the model across various export formats to evaluate performance.
|
|
642
622
|
|
|
643
|
-
This method assesses the model's performance in different export formats, such as ONNX, TorchScript, etc.
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
623
|
+
This method assesses the model's performance in different export formats, such as ONNX, TorchScript, etc. It
|
|
624
|
+
uses the 'benchmark' function from the ultralytics.utils.benchmarks module. The benchmarking is configured using
|
|
625
|
+
a combination of default configuration values, model-specific arguments, method-specific defaults, and any
|
|
626
|
+
additional user-provided keyword arguments.
|
|
647
627
|
|
|
648
628
|
Args:
|
|
649
629
|
data (str): Path to the dataset for benchmarking.
|
|
@@ -656,8 +636,8 @@ class Model(torch.nn.Module):
|
|
|
656
636
|
- device (str): Device to run the benchmark on (e.g., 'cpu', 'cuda').
|
|
657
637
|
|
|
658
638
|
Returns:
|
|
659
|
-
(dict): A dictionary containing the results of the benchmarking process, including metrics for
|
|
660
|
-
|
|
639
|
+
(dict): A dictionary containing the results of the benchmarking process, including metrics for different
|
|
640
|
+
export formats.
|
|
661
641
|
|
|
662
642
|
Raises:
|
|
663
643
|
AssertionError: If the model is not a PyTorch model.
|
|
@@ -691,23 +671,21 @@ class Model(torch.nn.Module):
|
|
|
691
671
|
self,
|
|
692
672
|
**kwargs: Any,
|
|
693
673
|
) -> str:
|
|
694
|
-
"""
|
|
695
|
-
Export the model to a different format suitable for deployment.
|
|
674
|
+
"""Export the model to a different format suitable for deployment.
|
|
696
675
|
|
|
697
676
|
This method facilitates the export of the model to various formats (e.g., ONNX, TorchScript) for deployment
|
|
698
677
|
purposes. It uses the 'Exporter' class for the export process, combining model-specific overrides, method
|
|
699
678
|
defaults, and any additional arguments provided.
|
|
700
679
|
|
|
701
680
|
Args:
|
|
702
|
-
**kwargs (Any): Arbitrary keyword arguments
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
simplify (bool): Simplify ONNX model.
|
|
681
|
+
**kwargs (Any): Arbitrary keyword arguments for export configuration. Common options include:
|
|
682
|
+
- format (str): Export format (e.g., 'onnx', 'engine', 'coreml').
|
|
683
|
+
- half (bool): Export model in half-precision.
|
|
684
|
+
- int8 (bool): Export model in int8 precision.
|
|
685
|
+
- device (str): Device to run the export on.
|
|
686
|
+
- workspace (int): Maximum memory workspace size for TensorRT engines.
|
|
687
|
+
- nms (bool): Add Non-Maximum Suppression (NMS) module to model.
|
|
688
|
+
- simplify (bool): Simplify ONNX model.
|
|
711
689
|
|
|
712
690
|
Returns:
|
|
713
691
|
(str): The path to the exported model file.
|
|
@@ -740,29 +718,28 @@ class Model(torch.nn.Module):
|
|
|
740
718
|
trainer=None,
|
|
741
719
|
**kwargs: Any,
|
|
742
720
|
):
|
|
743
|
-
"""
|
|
744
|
-
Train the model using the specified dataset and training configuration.
|
|
721
|
+
"""Train the model using the specified dataset and training configuration.
|
|
745
722
|
|
|
746
|
-
This method facilitates model training with a range of customizable settings. It supports training with a
|
|
747
|
-
|
|
748
|
-
|
|
723
|
+
This method facilitates model training with a range of customizable settings. It supports training with a custom
|
|
724
|
+
trainer or the default training approach. The method handles scenarios such as resuming training from a
|
|
725
|
+
checkpoint, integrating with Ultralytics HUB, and updating model and configuration after training.
|
|
749
726
|
|
|
750
|
-
When using Ultralytics HUB, if the session has a loaded model, the method prioritizes HUB training
|
|
751
|
-
|
|
752
|
-
|
|
727
|
+
When using Ultralytics HUB, if the session has a loaded model, the method prioritizes HUB training arguments and
|
|
728
|
+
warns if local arguments are provided. It checks for pip updates and combines default configurations,
|
|
729
|
+
method-specific defaults, and user-provided arguments to configure the training process.
|
|
753
730
|
|
|
754
731
|
Args:
|
|
755
732
|
trainer (BaseTrainer, optional): Custom trainer instance for model training. If None, uses default.
|
|
756
733
|
**kwargs (Any): Arbitrary keyword arguments for training configuration. Common options include:
|
|
757
|
-
data (str): Path to dataset configuration file.
|
|
758
|
-
epochs (int): Number of training epochs.
|
|
759
|
-
batch (int): Batch size for training.
|
|
760
|
-
imgsz (int): Input image size.
|
|
761
|
-
device (str): Device to run training on (e.g., 'cuda', 'cpu').
|
|
762
|
-
workers (int): Number of worker threads for data loading.
|
|
763
|
-
optimizer (str): Optimizer to use for training.
|
|
764
|
-
lr0 (float): Initial learning rate.
|
|
765
|
-
patience (int): Epochs to wait for no observable improvement for early stopping of training.
|
|
734
|
+
- data (str): Path to dataset configuration file.
|
|
735
|
+
- epochs (int): Number of training epochs.
|
|
736
|
+
- batch (int): Batch size for training.
|
|
737
|
+
- imgsz (int): Input image size.
|
|
738
|
+
- device (str): Device to run training on (e.g., 'cuda', 'cpu').
|
|
739
|
+
- workers (int): Number of worker threads for data loading.
|
|
740
|
+
- optimizer (str): Optimizer to use for training.
|
|
741
|
+
- lr0 (float): Initial learning rate.
|
|
742
|
+
- patience (int): Epochs to wait for no observable improvement for early stopping of training.
|
|
766
743
|
|
|
767
744
|
Returns:
|
|
768
745
|
(dict | None): Training metrics if available and training is successful; otherwise, None.
|
|
@@ -813,13 +790,12 @@ class Model(torch.nn.Module):
|
|
|
813
790
|
*args: Any,
|
|
814
791
|
**kwargs: Any,
|
|
815
792
|
):
|
|
816
|
-
"""
|
|
817
|
-
Conduct hyperparameter tuning for the model, with an option to use Ray Tune.
|
|
793
|
+
"""Conduct hyperparameter tuning for the model, with an option to use Ray Tune.
|
|
818
794
|
|
|
819
|
-
This method supports two modes of hyperparameter tuning: using Ray Tune or a custom tuning method.
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
795
|
+
This method supports two modes of hyperparameter tuning: using Ray Tune or a custom tuning method. When Ray Tune
|
|
796
|
+
is enabled, it leverages the 'run_ray_tune' function from the ultralytics.utils.tuner module. Otherwise, it uses
|
|
797
|
+
the internal 'Tuner' class for tuning. The method combines default, overridden, and custom arguments to
|
|
798
|
+
configure the tuning process.
|
|
823
799
|
|
|
824
800
|
Args:
|
|
825
801
|
use_ray (bool): Whether to use Ray Tune for hyperparameter tuning. If False, uses internal tuning method.
|
|
@@ -855,16 +831,15 @@ class Model(torch.nn.Module):
|
|
|
855
831
|
return Tuner(args=args, _callbacks=self.callbacks)(model=self, iterations=iterations)
|
|
856
832
|
|
|
857
833
|
def _apply(self, fn) -> Model:
|
|
858
|
-
"""
|
|
859
|
-
Apply a function to model tensors that are not parameters or registered buffers.
|
|
834
|
+
"""Apply a function to model tensors that are not parameters or registered buffers.
|
|
860
835
|
|
|
861
836
|
This method extends the functionality of the parent class's _apply method by additionally resetting the
|
|
862
|
-
predictor and updating the device in the model's overrides. It's typically used for operations like
|
|
863
|
-
|
|
837
|
+
predictor and updating the device in the model's overrides. It's typically used for operations like moving the
|
|
838
|
+
model to a different device or changing its precision.
|
|
864
839
|
|
|
865
840
|
Args:
|
|
866
|
-
fn (Callable): A function to be applied to the model's tensors. This is typically a method like
|
|
867
|
-
|
|
841
|
+
fn (Callable): A function to be applied to the model's tensors. This is typically a method like to(), cpu(),
|
|
842
|
+
cuda(), half(), or float().
|
|
868
843
|
|
|
869
844
|
Returns:
|
|
870
845
|
(Model): The model instance with the function applied and updated attributes.
|
|
@@ -884,8 +859,7 @@ class Model(torch.nn.Module):
|
|
|
884
859
|
|
|
885
860
|
@property
|
|
886
861
|
def names(self) -> dict[int, str]:
|
|
887
|
-
"""
|
|
888
|
-
Retrieve the class names associated with the loaded model.
|
|
862
|
+
"""Retrieve the class names associated with the loaded model.
|
|
889
863
|
|
|
890
864
|
This property returns the class names if they are defined in the model. It checks the class names for validity
|
|
891
865
|
using the 'check_class_names' function from the ultralytics.nn.autobackend module. If the predictor is not
|
|
@@ -915,8 +889,7 @@ class Model(torch.nn.Module):
|
|
|
915
889
|
|
|
916
890
|
@property
|
|
917
891
|
def device(self) -> torch.device:
|
|
918
|
-
"""
|
|
919
|
-
Get the device on which the model's parameters are allocated.
|
|
892
|
+
"""Get the device on which the model's parameters are allocated.
|
|
920
893
|
|
|
921
894
|
This property determines the device (CPU or GPU) where the model's parameters are currently stored. It is
|
|
922
895
|
applicable only to models that are instances of torch.nn.Module.
|
|
@@ -939,12 +912,11 @@ class Model(torch.nn.Module):
|
|
|
939
912
|
|
|
940
913
|
@property
|
|
941
914
|
def transforms(self):
|
|
942
|
-
"""
|
|
943
|
-
Retrieve the transformations applied to the input data of the loaded model.
|
|
915
|
+
"""Retrieve the transformations applied to the input data of the loaded model.
|
|
944
916
|
|
|
945
|
-
This property returns the transformations if they are defined in the model. The transforms
|
|
946
|
-
|
|
947
|
-
|
|
917
|
+
This property returns the transformations if they are defined in the model. The transforms typically include
|
|
918
|
+
preprocessing steps like resizing, normalization, and data augmentation that are applied to input data before it
|
|
919
|
+
is fed into the model.
|
|
948
920
|
|
|
949
921
|
Returns:
|
|
950
922
|
(object | None): The transform object of the model if available, otherwise None.
|
|
@@ -960,18 +932,17 @@ class Model(torch.nn.Module):
|
|
|
960
932
|
return self.model.transforms if hasattr(self.model, "transforms") else None
|
|
961
933
|
|
|
962
934
|
def add_callback(self, event: str, func) -> None:
|
|
963
|
-
"""
|
|
964
|
-
Add a callback function for a specified event.
|
|
935
|
+
"""Add a callback function for a specified event.
|
|
965
936
|
|
|
966
|
-
This method allows registering custom callback functions that are triggered on specific events during
|
|
967
|
-
|
|
968
|
-
|
|
937
|
+
This method allows registering custom callback functions that are triggered on specific events during model
|
|
938
|
+
operations such as training or inference. Callbacks provide a way to extend and customize the behavior of the
|
|
939
|
+
model at various stages of its lifecycle.
|
|
969
940
|
|
|
970
941
|
Args:
|
|
971
|
-
event (str): The name of the event to attach the callback to. Must be a valid event name recognized
|
|
972
|
-
|
|
973
|
-
func (Callable): The callback function to be registered. This function will be called when the
|
|
974
|
-
|
|
942
|
+
event (str): The name of the event to attach the callback to. Must be a valid event name recognized by the
|
|
943
|
+
Ultralytics framework.
|
|
944
|
+
func (Callable): The callback function to be registered. This function will be called when the specified
|
|
945
|
+
event occurs.
|
|
975
946
|
|
|
976
947
|
Raises:
|
|
977
948
|
ValueError: If the event name is not recognized or is invalid.
|
|
@@ -986,12 +957,11 @@ class Model(torch.nn.Module):
|
|
|
986
957
|
self.callbacks[event].append(func)
|
|
987
958
|
|
|
988
959
|
def clear_callback(self, event: str) -> None:
|
|
989
|
-
"""
|
|
990
|
-
Clear all callback functions registered for a specified event.
|
|
960
|
+
"""Clear all callback functions registered for a specified event.
|
|
991
961
|
|
|
992
|
-
This method removes all custom and default callback functions associated with the given event.
|
|
993
|
-
|
|
994
|
-
|
|
962
|
+
This method removes all custom and default callback functions associated with the given event. It resets the
|
|
963
|
+
callback list for the specified event to an empty list, effectively removing all registered callbacks for that
|
|
964
|
+
event.
|
|
995
965
|
|
|
996
966
|
Args:
|
|
997
967
|
event (str): The name of the event for which to clear the callbacks. This should be a valid event name
|
|
@@ -1014,8 +984,7 @@ class Model(torch.nn.Module):
|
|
|
1014
984
|
self.callbacks[event] = []
|
|
1015
985
|
|
|
1016
986
|
def reset_callbacks(self) -> None:
|
|
1017
|
-
"""
|
|
1018
|
-
Reset all callbacks to their default functions.
|
|
987
|
+
"""Reset all callbacks to their default functions.
|
|
1019
988
|
|
|
1020
989
|
This method reinstates the default callback functions for all events, removing any custom callbacks that were
|
|
1021
990
|
previously added. It iterates through all default callback events and replaces the current callbacks with the
|
|
@@ -1038,12 +1007,11 @@ class Model(torch.nn.Module):
|
|
|
1038
1007
|
|
|
1039
1008
|
@staticmethod
|
|
1040
1009
|
def _reset_ckpt_args(args: dict[str, Any]) -> dict[str, Any]:
|
|
1041
|
-
"""
|
|
1042
|
-
Reset specific arguments when loading a PyTorch model checkpoint.
|
|
1010
|
+
"""Reset specific arguments when loading a PyTorch model checkpoint.
|
|
1043
1011
|
|
|
1044
|
-
This method filters the input arguments dictionary to retain only a specific set of keys that are
|
|
1045
|
-
|
|
1046
|
-
|
|
1012
|
+
This method filters the input arguments dictionary to retain only a specific set of keys that are considered
|
|
1013
|
+
important for model loading. It's used to ensure that only relevant arguments are preserved when loading a model
|
|
1014
|
+
from a checkpoint, discarding any unnecessary or potentially conflicting settings.
|
|
1047
1015
|
|
|
1048
1016
|
Args:
|
|
1049
1017
|
args (dict): A dictionary containing various model arguments and settings.
|
|
@@ -1066,12 +1034,11 @@ class Model(torch.nn.Module):
|
|
|
1066
1034
|
# raise AttributeError(f"'{name}' object has no attribute '{attr}'. See valid attributes below.\n{self.__doc__}")
|
|
1067
1035
|
|
|
1068
1036
|
def _smart_load(self, key: str):
|
|
1069
|
-
"""
|
|
1070
|
-
Intelligently load the appropriate module based on the model task.
|
|
1037
|
+
"""Intelligently load the appropriate module based on the model task.
|
|
1071
1038
|
|
|
1072
|
-
This method dynamically selects and returns the correct module (model, trainer, validator, or predictor)
|
|
1073
|
-
|
|
1074
|
-
|
|
1039
|
+
This method dynamically selects and returns the correct module (model, trainer, validator, or predictor) based
|
|
1040
|
+
on the current task of the model and the provided key. It uses the task_map dictionary to determine the
|
|
1041
|
+
appropriate module to load for the specific task.
|
|
1075
1042
|
|
|
1076
1043
|
Args:
|
|
1077
1044
|
key (str): The type of module to load. Must be one of 'model', 'trainer', 'validator', or 'predictor'.
|
|
@@ -1096,21 +1063,20 @@ class Model(torch.nn.Module):
|
|
|
1096
1063
|
|
|
1097
1064
|
@property
|
|
1098
1065
|
def task_map(self) -> dict:
|
|
1099
|
-
"""
|
|
1100
|
-
Provide a mapping from model tasks to corresponding classes for different modes.
|
|
1066
|
+
"""Provide a mapping from model tasks to corresponding classes for different modes.
|
|
1101
1067
|
|
|
1102
|
-
This property method returns a dictionary that maps each supported task (e.g., detect, segment, classify)
|
|
1103
|
-
|
|
1104
|
-
|
|
1068
|
+
This property method returns a dictionary that maps each supported task (e.g., detect, segment, classify) to a
|
|
1069
|
+
nested dictionary. The nested dictionary contains mappings for different operational modes (model, trainer,
|
|
1070
|
+
validator, predictor) to their respective class implementations.
|
|
1105
1071
|
|
|
1106
|
-
The mapping allows for dynamic loading of appropriate classes based on the model's task and the
|
|
1107
|
-
|
|
1108
|
-
|
|
1072
|
+
The mapping allows for dynamic loading of appropriate classes based on the model's task and the desired
|
|
1073
|
+
operational mode. This facilitates a flexible and extensible architecture for handling various tasks and modes
|
|
1074
|
+
within the Ultralytics framework.
|
|
1109
1075
|
|
|
1110
1076
|
Returns:
|
|
1111
1077
|
(dict[str, dict[str, Any]]): A dictionary mapping task names to nested dictionaries. Each nested dictionary
|
|
1112
|
-
|
|
1113
|
-
|
|
1078
|
+
contains mappings for 'model', 'trainer', 'validator', and 'predictor' keys to their respective class
|
|
1079
|
+
implementations for that task.
|
|
1114
1080
|
|
|
1115
1081
|
Examples:
|
|
1116
1082
|
>>> model = Model("yolo11n.pt")
|
|
@@ -1121,8 +1087,7 @@ class Model(torch.nn.Module):
|
|
|
1121
1087
|
raise NotImplementedError("Please provide task map for your model!")
|
|
1122
1088
|
|
|
1123
1089
|
def eval(self):
|
|
1124
|
-
"""
|
|
1125
|
-
Sets the model to evaluation mode.
|
|
1090
|
+
"""Sets the model to evaluation mode.
|
|
1126
1091
|
|
|
1127
1092
|
This method changes the model's mode to evaluation, which affects layers like dropout and batch normalization
|
|
1128
1093
|
that behave differently during training and evaluation. In evaluation mode, these layers use running statistics
|
|
@@ -1140,8 +1105,7 @@ class Model(torch.nn.Module):
|
|
|
1140
1105
|
return self
|
|
1141
1106
|
|
|
1142
1107
|
def __getattr__(self, name):
|
|
1143
|
-
"""
|
|
1144
|
-
Enable accessing model attributes directly through the Model class.
|
|
1108
|
+
"""Enable accessing model attributes directly through the Model class.
|
|
1145
1109
|
|
|
1146
1110
|
This method provides a way to access attributes of the underlying model directly through the Model class
|
|
1147
1111
|
instance. It first checks if the requested attribute is 'model', in which case it returns the model from
|