ultralytics 8.2.61__py3-none-any.whl → 8.2.63__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ultralytics might be problematic. Click here for more details.
- ultralytics/__init__.py +1 -1
- ultralytics/cfg/__init__.py +154 -103
- ultralytics/data/annotator.py +16 -12
- ultralytics/data/augment.py +1478 -195
- ultralytics/data/explorer/gui/dash.py +41 -26
- ultralytics/data/loaders.py +1 -1
- ultralytics/engine/model.py +483 -176
- ultralytics/engine/results.py +1035 -256
- ultralytics/models/fastsam/predict.py +18 -73
- ultralytics/models/fastsam/utils.py +0 -42
- ultralytics/models/nas/predict.py +1 -3
- ultralytics/models/rtdetr/predict.py +4 -6
- ultralytics/models/sam/predict.py +1 -3
- ultralytics/models/yolo/classify/predict.py +1 -3
- ultralytics/models/yolo/detect/predict.py +1 -3
- ultralytics/models/yolo/pose/predict.py +1 -3
- ultralytics/models/yolo/segment/predict.py +1 -3
- ultralytics/solutions/streamlit_inference.py +5 -2
- ultralytics/utils/downloads.py +1 -1
- {ultralytics-8.2.61.dist-info → ultralytics-8.2.63.dist-info}/METADATA +1 -1
- {ultralytics-8.2.61.dist-info → ultralytics-8.2.63.dist-info}/RECORD +25 -25
- {ultralytics-8.2.61.dist-info → ultralytics-8.2.63.dist-info}/WHEEL +1 -1
- {ultralytics-8.2.61.dist-info → ultralytics-8.2.63.dist-info}/LICENSE +0 -0
- {ultralytics-8.2.61.dist-info → ultralytics-8.2.63.dist-info}/entry_points.txt +0 -0
- {ultralytics-8.2.61.dist-info → ultralytics-8.2.63.dist-info}/top_level.txt +0 -0
ultralytics/__init__.py
CHANGED
ultralytics/cfg/__init__.py
CHANGED
|
@@ -79,7 +79,7 @@ CLI_HELP_MSG = f"""
|
|
|
79
79
|
yolo export model=yolov8n-cls.pt format=onnx imgsz=224,128
|
|
80
80
|
|
|
81
81
|
5. Explore your datasets using semantic search and SQL with a simple GUI powered by Ultralytics Explorer API
|
|
82
|
-
yolo explorer
|
|
82
|
+
yolo explorer data=data.yaml model=yolov8n.pt
|
|
83
83
|
|
|
84
84
|
6. Streamlit real-time object detection on your webcam with Ultralytics YOLOv8
|
|
85
85
|
yolo streamlit-predict
|
|
@@ -187,11 +187,11 @@ CFG_BOOL_KEYS = { # boolean-only arguments
|
|
|
187
187
|
|
|
188
188
|
def cfg2dict(cfg):
|
|
189
189
|
"""
|
|
190
|
-
|
|
190
|
+
Converts a configuration object to a dictionary.
|
|
191
191
|
|
|
192
192
|
Args:
|
|
193
|
-
cfg (str | Path | Dict | SimpleNamespace): Configuration object to be converted
|
|
194
|
-
|
|
193
|
+
cfg (str | Path | Dict | SimpleNamespace): Configuration object to be converted. Can be a file path,
|
|
194
|
+
a string, a dictionary, or a SimpleNamespace object.
|
|
195
195
|
|
|
196
196
|
Returns:
|
|
197
197
|
(Dict): Configuration object in dictionary format.
|
|
@@ -209,8 +209,9 @@ def cfg2dict(cfg):
|
|
|
209
209
|
>>> config_dict = cfg2dict({'param1': 'value1', 'param2': 'value2'})
|
|
210
210
|
|
|
211
211
|
Notes:
|
|
212
|
-
- If
|
|
213
|
-
- If
|
|
212
|
+
- If cfg is a path or string, it's loaded as YAML and converted to a dictionary.
|
|
213
|
+
- If cfg is a SimpleNamespace object, it's converted to a dictionary using vars().
|
|
214
|
+
- If cfg is already a dictionary, it's returned unchanged.
|
|
214
215
|
"""
|
|
215
216
|
if isinstance(cfg, (str, Path)):
|
|
216
217
|
cfg = yaml_load(cfg) # load dict
|
|
@@ -224,24 +225,23 @@ def get_cfg(cfg: Union[str, Path, Dict, SimpleNamespace] = DEFAULT_CFG_DICT, ove
|
|
|
224
225
|
Load and merge configuration data from a file or dictionary, with optional overrides.
|
|
225
226
|
|
|
226
227
|
Args:
|
|
227
|
-
cfg (str | Path | Dict | SimpleNamespace): Configuration data source.
|
|
228
|
+
cfg (str | Path | Dict | SimpleNamespace): Configuration data source. Can be a file path, dictionary, or
|
|
229
|
+
SimpleNamespace object.
|
|
228
230
|
overrides (Dict | None): Dictionary containing key-value pairs to override the base configuration.
|
|
229
231
|
|
|
230
232
|
Returns:
|
|
231
|
-
(SimpleNamespace): Namespace containing the merged
|
|
232
|
-
|
|
233
|
-
Notes:
|
|
234
|
-
- If both `cfg` and `overrides` are provided, the values in `overrides` will take precedence.
|
|
235
|
-
- Special handling ensures alignment and correctness of the configuration, such as converting numeric `project`
|
|
236
|
-
and `name` to strings and validating configuration keys and values.
|
|
233
|
+
(SimpleNamespace): Namespace containing the merged configuration arguments.
|
|
237
234
|
|
|
238
235
|
Examples:
|
|
239
|
-
|
|
240
|
-
>>>
|
|
241
|
-
>>> config = get_cfg()
|
|
242
|
-
|
|
243
|
-
Load from a custom file with overrides:
|
|
236
|
+
>>> from ultralytics.cfg import get_cfg
|
|
237
|
+
>>> config = get_cfg() # Load default configuration
|
|
244
238
|
>>> config = get_cfg('path/to/config.yaml', overrides={'epochs': 50, 'batch_size': 16})
|
|
239
|
+
|
|
240
|
+
Notes:
|
|
241
|
+
- If both `cfg` and `overrides` are provided, the values in `overrides` will take precedence.
|
|
242
|
+
- Special handling ensures alignment and correctness of the configuration, such as converting numeric
|
|
243
|
+
`project` and `name` to strings and validating configuration keys and values.
|
|
244
|
+
- The function performs type and value checks on the configuration data.
|
|
245
245
|
"""
|
|
246
246
|
cfg = cfg2dict(cfg)
|
|
247
247
|
|
|
@@ -270,24 +270,31 @@ def get_cfg(cfg: Union[str, Path, Dict, SimpleNamespace] = DEFAULT_CFG_DICT, ove
|
|
|
270
270
|
|
|
271
271
|
def check_cfg(cfg, hard=True):
|
|
272
272
|
"""
|
|
273
|
-
Checks configuration argument types and values for the Ultralytics library
|
|
274
|
-
|
|
273
|
+
Checks configuration argument types and values for the Ultralytics library.
|
|
274
|
+
|
|
275
|
+
This function validates the types and values of configuration arguments, ensuring correctness and converting
|
|
276
|
+
them if necessary. It checks for specific key types defined in global variables such as CFG_FLOAT_KEYS,
|
|
277
|
+
CFG_FRACTION_KEYS, CFG_INT_KEYS, and CFG_BOOL_KEYS.
|
|
275
278
|
|
|
276
279
|
Args:
|
|
277
280
|
cfg (Dict): Configuration dictionary to validate.
|
|
278
281
|
hard (bool): If True, raises exceptions for invalid types and values; if False, attempts to convert them.
|
|
279
282
|
|
|
280
283
|
Examples:
|
|
281
|
-
Validate a configuration with a mix of valid and invalid values:
|
|
282
284
|
>>> config = {
|
|
283
|
-
... 'epochs': 50,
|
|
284
|
-
... 'lr0': 0.01,
|
|
285
|
-
... 'momentum': 1.2,
|
|
286
|
-
... 'save': 'true',
|
|
285
|
+
... 'epochs': 50, # valid integer
|
|
286
|
+
... 'lr0': 0.01, # valid float
|
|
287
|
+
... 'momentum': 1.2, # invalid float (out of 0.0-1.0 range)
|
|
288
|
+
... 'save': 'true', # invalid bool
|
|
287
289
|
... }
|
|
288
290
|
>>> check_cfg(config, hard=False)
|
|
289
291
|
>>> print(config)
|
|
290
|
-
{'epochs': 50, 'lr0': 0.01, 'momentum': 1.2, 'save': False} # corrected 'save' key
|
|
292
|
+
{'epochs': 50, 'lr0': 0.01, 'momentum': 1.2, 'save': False} # corrected 'save' key
|
|
293
|
+
|
|
294
|
+
Notes:
|
|
295
|
+
- The function modifies the input dictionary in-place.
|
|
296
|
+
- None values are ignored as they may be from optional arguments.
|
|
297
|
+
- Fraction keys are checked to be within the range [0.0, 1.0].
|
|
291
298
|
"""
|
|
292
299
|
for k, v in cfg.items():
|
|
293
300
|
if v is not None: # None values may be from optional args
|
|
@@ -328,16 +335,15 @@ def get_save_dir(args, name=None):
|
|
|
328
335
|
Returns the directory path for saving outputs, derived from arguments or default settings.
|
|
329
336
|
|
|
330
337
|
Args:
|
|
331
|
-
args (SimpleNamespace): Namespace object containing configurations such as 'project', 'name', 'task',
|
|
332
|
-
'save_dir'.
|
|
333
|
-
name (str | None): Optional name for the output directory. If not provided, it defaults to 'args.name'
|
|
334
|
-
'args.mode'.
|
|
338
|
+
args (SimpleNamespace): Namespace object containing configurations such as 'project', 'name', 'task',
|
|
339
|
+
'mode', and 'save_dir'.
|
|
340
|
+
name (str | None): Optional name for the output directory. If not provided, it defaults to 'args.name'
|
|
341
|
+
or the 'args.mode'.
|
|
335
342
|
|
|
336
343
|
Returns:
|
|
337
344
|
(Path): Directory path where outputs should be saved.
|
|
338
345
|
|
|
339
346
|
Examples:
|
|
340
|
-
Generate a save directory using provided arguments
|
|
341
347
|
>>> from types import SimpleNamespace
|
|
342
348
|
>>> args = SimpleNamespace(project='my_project', task='detect', mode='train', exist_ok=True)
|
|
343
349
|
>>> save_dir = get_save_dir(args)
|
|
@@ -369,6 +375,11 @@ def _handle_deprecation(custom):
|
|
|
369
375
|
>>> _handle_deprecation(custom_config)
|
|
370
376
|
>>> print(custom_config)
|
|
371
377
|
{'show_boxes': True, 'show_labels': True, 'line_width': 2}
|
|
378
|
+
|
|
379
|
+
Notes:
|
|
380
|
+
This function modifies the input dictionary in-place, replacing deprecated keys with their current
|
|
381
|
+
equivalents. It also handles value conversions where necessary, such as inverting boolean values for
|
|
382
|
+
'hide_labels' and 'hide_conf'.
|
|
372
383
|
"""
|
|
373
384
|
|
|
374
385
|
for key in custom.copy().keys():
|
|
@@ -390,32 +401,29 @@ def _handle_deprecation(custom):
|
|
|
390
401
|
|
|
391
402
|
def check_dict_alignment(base: Dict, custom: Dict, e=None):
|
|
392
403
|
"""
|
|
393
|
-
|
|
394
|
-
|
|
404
|
+
Checks alignment between custom and base configuration dictionaries, handling deprecated keys and providing error
|
|
405
|
+
messages for mismatched keys.
|
|
395
406
|
|
|
396
407
|
Args:
|
|
397
408
|
base (Dict): The base configuration dictionary containing valid keys.
|
|
398
409
|
custom (Dict): The custom configuration dictionary to be checked for alignment.
|
|
399
|
-
e (Exception | None): Optional error instance passed by the calling function.
|
|
410
|
+
e (Exception | None): Optional error instance passed by the calling function.
|
|
400
411
|
|
|
401
412
|
Raises:
|
|
402
|
-
SystemExit:
|
|
403
|
-
|
|
404
|
-
Notes:
|
|
405
|
-
- The function suggests corrections for mismatched keys based on similarity to valid keys.
|
|
406
|
-
- Deprecated keys in the custom configuration are automatically replaced with their updated equivalents.
|
|
407
|
-
- Detailed error messages are printed for each mismatched key to help users identify and correct their custom
|
|
408
|
-
configurations.
|
|
413
|
+
SystemExit: If mismatched keys are found between the custom and base dictionaries.
|
|
409
414
|
|
|
410
415
|
Examples:
|
|
411
416
|
>>> base_cfg = {'epochs': 50, 'lr0': 0.01, 'batch_size': 16}
|
|
412
417
|
>>> custom_cfg = {'epoch': 100, 'lr': 0.02, 'batch_size': 32}
|
|
413
|
-
|
|
414
418
|
>>> try:
|
|
415
419
|
... check_dict_alignment(base_cfg, custom_cfg)
|
|
416
420
|
... except SystemExit:
|
|
417
|
-
...
|
|
418
|
-
|
|
421
|
+
... print("Mismatched keys found")
|
|
422
|
+
|
|
423
|
+
Notes:
|
|
424
|
+
- Suggests corrections for mismatched keys based on similarity to valid keys.
|
|
425
|
+
- Automatically replaces deprecated keys in the custom configuration with updated equivalents.
|
|
426
|
+
- Prints detailed error messages for each mismatched key to help users correct their configurations.
|
|
419
427
|
"""
|
|
420
428
|
custom = _handle_deprecation(custom)
|
|
421
429
|
base_keys, custom_keys = (set(x.keys()) for x in (base, custom))
|
|
@@ -434,7 +442,10 @@ def check_dict_alignment(base: Dict, custom: Dict, e=None):
|
|
|
434
442
|
|
|
435
443
|
def merge_equals_args(args: List[str]) -> List[str]:
|
|
436
444
|
"""
|
|
437
|
-
Merges arguments around isolated '=' in a list of strings
|
|
445
|
+
Merges arguments around isolated '=' in a list of strings, handling three cases:
|
|
446
|
+
1. ['arg', '=', 'val'] becomes ['arg=val'],
|
|
447
|
+
2. ['arg=', 'val'] becomes ['arg=val'],
|
|
448
|
+
3. ['arg', '=val'] becomes ['arg=val'].
|
|
438
449
|
|
|
439
450
|
Args:
|
|
440
451
|
args (List[str]): A list of strings where each element represents an argument.
|
|
@@ -443,20 +454,9 @@ def merge_equals_args(args: List[str]) -> List[str]:
|
|
|
443
454
|
(List[str]): A list of strings where the arguments around isolated '=' are merged.
|
|
444
455
|
|
|
445
456
|
Examples:
|
|
446
|
-
|
|
447
|
-
>>> args = ["arg1", "=", "value"]
|
|
457
|
+
>>> args = ["arg1", "=", "value", "arg2=", "value2", "arg3", "=value3"]
|
|
448
458
|
>>> merge_equals_args(args)
|
|
449
|
-
[
|
|
450
|
-
|
|
451
|
-
Merge arguments where equals sign is at the end of the first argument:
|
|
452
|
-
>>> args = ["arg1=", "value"]
|
|
453
|
-
>>> merge_equals_args(args)
|
|
454
|
-
["arg1=value"]
|
|
455
|
-
|
|
456
|
-
Merge arguments where equals sign is at the beginning of the second argument:
|
|
457
|
-
>>> args = ["arg1", "=value"]
|
|
458
|
-
>>> merge_equals_args(args)
|
|
459
|
-
["arg1=value"]
|
|
459
|
+
['arg1=value', 'arg2=value2', 'arg3=value3']
|
|
460
460
|
"""
|
|
461
461
|
new_args = []
|
|
462
462
|
for i, arg in enumerate(args):
|
|
@@ -475,18 +475,24 @@ def merge_equals_args(args: List[str]) -> List[str]:
|
|
|
475
475
|
|
|
476
476
|
def handle_yolo_hub(args: List[str]) -> None:
|
|
477
477
|
"""
|
|
478
|
-
|
|
478
|
+
Handles Ultralytics HUB command-line interface (CLI) commands for authentication.
|
|
479
479
|
|
|
480
480
|
This function processes Ultralytics HUB CLI commands such as login and logout. It should be called when executing a
|
|
481
481
|
script with arguments related to HUB authentication.
|
|
482
482
|
|
|
483
483
|
Args:
|
|
484
|
-
args (List[str]): A list of command line arguments.
|
|
484
|
+
args (List[str]): A list of command line arguments. The first argument should be either 'login'
|
|
485
|
+
or 'logout'. For 'login', an optional second argument can be the API key.
|
|
485
486
|
|
|
486
487
|
Examples:
|
|
487
488
|
```bash
|
|
488
489
|
yolo hub login YOUR_API_KEY
|
|
489
490
|
```
|
|
491
|
+
|
|
492
|
+
Notes:
|
|
493
|
+
- The function imports the 'hub' module from ultralytics to perform login and logout operations.
|
|
494
|
+
- For the 'login' command, if no API key is provided, an empty string is passed to the login function.
|
|
495
|
+
- The 'logout' command does not require any additional arguments.
|
|
490
496
|
"""
|
|
491
497
|
from ultralytics import hub
|
|
492
498
|
|
|
@@ -501,21 +507,26 @@ def handle_yolo_hub(args: List[str]) -> None:
|
|
|
501
507
|
|
|
502
508
|
def handle_yolo_settings(args: List[str]) -> None:
|
|
503
509
|
"""
|
|
504
|
-
|
|
510
|
+
Handles YOLO settings command-line interface (CLI) commands.
|
|
505
511
|
|
|
506
|
-
This function processes YOLO settings CLI commands such as reset. It should be
|
|
507
|
-
arguments related to YOLO settings management.
|
|
512
|
+
This function processes YOLO settings CLI commands such as reset and updating individual settings. It should be
|
|
513
|
+
called when executing a script with arguments related to YOLO settings management.
|
|
508
514
|
|
|
509
515
|
Args:
|
|
510
516
|
args (List[str]): A list of command line arguments for YOLO settings management.
|
|
511
517
|
|
|
512
518
|
Examples:
|
|
513
|
-
Reset YOLO settings
|
|
514
|
-
>>>
|
|
519
|
+
>>> handle_yolo_settings(["reset"]) # Reset YOLO settings
|
|
520
|
+
>>> handle_yolo_settings(["default_cfg_path=yolov8n.yaml"]) # Update a specific setting
|
|
515
521
|
|
|
516
522
|
Notes:
|
|
517
|
-
|
|
518
|
-
|
|
523
|
+
- If no arguments are provided, the function will display the current settings.
|
|
524
|
+
- The 'reset' command will delete the existing settings file and create new default settings.
|
|
525
|
+
- Other arguments are treated as key-value pairs to update specific settings.
|
|
526
|
+
- The function will check for alignment between the provided settings and the existing ones.
|
|
527
|
+
- After processing, the updated settings will be displayed.
|
|
528
|
+
- For more information on handling YOLO settings, visit:
|
|
529
|
+
https://docs.ultralytics.com/quickstart/#ultralytics-settings
|
|
519
530
|
"""
|
|
520
531
|
url = "https://docs.ultralytics.com/quickstart/#ultralytics-settings" # help URL
|
|
521
532
|
try:
|
|
@@ -535,58 +546,83 @@ def handle_yolo_settings(args: List[str]) -> None:
|
|
|
535
546
|
LOGGER.warning(f"WARNING ⚠️ settings error: '{e}'. Please see {url} for help.")
|
|
536
547
|
|
|
537
548
|
|
|
538
|
-
def handle_explorer():
|
|
549
|
+
def handle_explorer(args: List[str]):
|
|
539
550
|
"""
|
|
540
|
-
Open the Ultralytics Explorer GUI for dataset exploration and analysis.
|
|
541
|
-
|
|
542
551
|
This function launches a graphical user interface that provides tools for interacting with and analyzing datasets
|
|
543
|
-
using the Ultralytics Explorer API.
|
|
552
|
+
using the Ultralytics Explorer API. It checks for the required 'streamlit' package and informs the user that the
|
|
553
|
+
Explorer dashboard is loading.
|
|
554
|
+
|
|
555
|
+
Args:
|
|
556
|
+
args (List[str]): A list of optional command line arguments.
|
|
544
557
|
|
|
545
558
|
Examples:
|
|
546
|
-
|
|
547
|
-
|
|
559
|
+
```bash
|
|
560
|
+
yolo explorer data=data.yaml model=yolov8n.pt
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
Notes:
|
|
564
|
+
- Requires 'streamlit' package version 1.29.0 or higher.
|
|
565
|
+
- The function does not take any arguments or return any values.
|
|
566
|
+
- It is typically called from the command line interface using the 'yolo explorer' command.
|
|
548
567
|
"""
|
|
549
568
|
checks.check_requirements("streamlit>=1.29.0")
|
|
550
569
|
LOGGER.info("💡 Loading Explorer dashboard...")
|
|
551
|
-
|
|
570
|
+
cmd = ["streamlit", "run", ROOT / "data/explorer/gui/dash.py", "--server.maxMessageSize", "2048"]
|
|
571
|
+
new = dict(parse_key_value_pair(a) for a in args)
|
|
572
|
+
check_dict_alignment(base={k: DEFAULT_CFG_DICT[k] for k in ["model", "data"]}, custom=new)
|
|
573
|
+
for k, v in new.items():
|
|
574
|
+
cmd += [k, v]
|
|
575
|
+
subprocess.run(cmd)
|
|
552
576
|
|
|
553
577
|
|
|
554
578
|
def handle_streamlit_inference():
|
|
555
579
|
"""
|
|
556
|
-
Open the Ultralytics Live Inference
|
|
580
|
+
Open the Ultralytics Live Inference Streamlit app for real-time object detection.
|
|
557
581
|
|
|
558
582
|
This function initializes and runs a Streamlit application designed for performing live object detection using
|
|
559
|
-
Ultralytics models.
|
|
560
|
-
|
|
561
|
-
References:
|
|
562
|
-
- Streamlit documentation: https://docs.streamlit.io/
|
|
563
|
-
- Ultralytics: https://docs.ultralytics.com
|
|
583
|
+
Ultralytics models. It checks for the required Streamlit package and launches the app.
|
|
564
584
|
|
|
565
585
|
Examples:
|
|
566
|
-
To run the live inference Streamlit app, execute:
|
|
567
586
|
>>> handle_streamlit_inference()
|
|
587
|
+
|
|
588
|
+
Notes:
|
|
589
|
+
- Requires Streamlit version 1.29.0 or higher.
|
|
590
|
+
- The app is launched using the 'streamlit run' command.
|
|
591
|
+
- The Streamlit app file is located in the Ultralytics package directory.
|
|
568
592
|
"""
|
|
569
593
|
checks.check_requirements("streamlit>=1.29.0")
|
|
570
594
|
LOGGER.info("💡 Loading Ultralytics Live Inference app...")
|
|
571
595
|
subprocess.run(["streamlit", "run", ROOT / "solutions/streamlit_inference.py", "--server.headless", "true"])
|
|
572
596
|
|
|
573
597
|
|
|
574
|
-
def parse_key_value_pair(pair):
|
|
598
|
+
def parse_key_value_pair(pair: str = "key=value"):
|
|
575
599
|
"""
|
|
576
|
-
|
|
600
|
+
Parses a key-value pair string into separate key and value components.
|
|
577
601
|
|
|
578
602
|
Args:
|
|
579
|
-
pair (str):
|
|
603
|
+
pair (str): A string containing a key-value pair in the format "key=value".
|
|
580
604
|
|
|
581
605
|
Returns:
|
|
582
|
-
(tuple
|
|
606
|
+
(tuple): A tuple containing two elements:
|
|
607
|
+
- key (str): The parsed key.
|
|
608
|
+
- value (str): The parsed value.
|
|
609
|
+
|
|
610
|
+
Raises:
|
|
611
|
+
AssertionError: If the value is missing or empty.
|
|
583
612
|
|
|
584
613
|
Examples:
|
|
585
614
|
>>> key, value = parse_key_value_pair("model=yolov8n.pt")
|
|
586
|
-
>>> key
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
615
|
+
>>> print(f"Key: {key}, Value: {value}")
|
|
616
|
+
Key: model, Value: yolov8n.pt
|
|
617
|
+
|
|
618
|
+
>>> key, value = parse_key_value_pair("epochs=100")
|
|
619
|
+
>>> print(f"Key: {key}, Value: {value}")
|
|
620
|
+
Key: epochs, Value: 100
|
|
621
|
+
|
|
622
|
+
Notes:
|
|
623
|
+
- The function splits the input string on the first '=' character.
|
|
624
|
+
- Leading and trailing whitespace is removed from both key and value.
|
|
625
|
+
- An assertion error is raised if the value is empty after stripping.
|
|
590
626
|
"""
|
|
591
627
|
k, v = pair.split("=", 1) # split on first '=' sign
|
|
592
628
|
k, v = k.strip(), v.strip() # remove spaces
|
|
@@ -596,17 +632,19 @@ def parse_key_value_pair(pair):
|
|
|
596
632
|
|
|
597
633
|
def smart_value(v):
|
|
598
634
|
"""
|
|
599
|
-
|
|
635
|
+
Converts a string representation of a value to its appropriate Python type.
|
|
636
|
+
|
|
637
|
+
This function attempts to convert a given string into a Python object of the most appropriate type. It handles
|
|
638
|
+
conversions to None, bool, int, float, and other types that can be evaluated safely.
|
|
600
639
|
|
|
601
640
|
Args:
|
|
602
|
-
v (str):
|
|
641
|
+
v (str): The string representation of the value to be converted.
|
|
603
642
|
|
|
604
643
|
Returns:
|
|
605
|
-
(Any): The converted value
|
|
644
|
+
(Any): The converted value. The type can be None, bool, int, float, or the original string if no conversion
|
|
606
645
|
is applicable.
|
|
607
646
|
|
|
608
647
|
Examples:
|
|
609
|
-
Convert a string to various types:
|
|
610
648
|
>>> smart_value("42")
|
|
611
649
|
42
|
|
612
650
|
>>> smart_value("3.14")
|
|
@@ -617,6 +655,11 @@ def smart_value(v):
|
|
|
617
655
|
None
|
|
618
656
|
>>> smart_value("some_string")
|
|
619
657
|
'some_string'
|
|
658
|
+
|
|
659
|
+
Notes:
|
|
660
|
+
- The function uses a case-insensitive comparison for boolean and None values.
|
|
661
|
+
- For other types, it attempts to use Python's eval() function, which can be unsafe if used on untrusted input.
|
|
662
|
+
- If no conversion is possible, the original string is returned.
|
|
620
663
|
"""
|
|
621
664
|
v_lower = v.lower()
|
|
622
665
|
if v_lower == "none":
|
|
@@ -639,7 +682,7 @@ def entrypoint(debug=""):
|
|
|
639
682
|
executing the corresponding tasks such as training, validation, prediction, exporting models, and more.
|
|
640
683
|
|
|
641
684
|
Args:
|
|
642
|
-
debug (str
|
|
685
|
+
debug (str): Space-separated string of command-line arguments for debugging purposes.
|
|
643
686
|
|
|
644
687
|
Examples:
|
|
645
688
|
Train a detection model for 10 epochs with an initial learning_rate of 0.01:
|
|
@@ -652,9 +695,9 @@ def entrypoint(debug=""):
|
|
|
652
695
|
>>> entrypoint("val model=yolov8n.pt data=coco8.yaml batch=1 imgsz=640")
|
|
653
696
|
|
|
654
697
|
Notes:
|
|
655
|
-
- For a list of all available commands and their arguments, see the provided help messages and the Ultralytics
|
|
656
|
-
documentation at https://docs.ultralytics.com.
|
|
657
698
|
- If no arguments are passed, the function will display the usage help message.
|
|
699
|
+
- For a list of all available commands and their arguments, see the provided help messages and the
|
|
700
|
+
Ultralytics documentation at https://docs.ultralytics.com.
|
|
658
701
|
"""
|
|
659
702
|
args = (debug.split(" ") if debug else ARGV)[1:]
|
|
660
703
|
if not args: # no arguments passed
|
|
@@ -670,7 +713,7 @@ def entrypoint(debug=""):
|
|
|
670
713
|
"hub": lambda: handle_yolo_hub(args[1:]),
|
|
671
714
|
"login": lambda: handle_yolo_hub(args),
|
|
672
715
|
"copy-cfg": copy_default_cfg,
|
|
673
|
-
"explorer": lambda: handle_explorer(),
|
|
716
|
+
"explorer": lambda: handle_explorer(args[1:]),
|
|
674
717
|
"streamlit-predict": lambda: handle_streamlit_inference(),
|
|
675
718
|
}
|
|
676
719
|
full_args_dict = {**DEFAULT_CFG_DICT, **{k: None for k in TASKS}, **{k: None for k in MODES}, **special}
|
|
@@ -793,16 +836,24 @@ def entrypoint(debug=""):
|
|
|
793
836
|
# Special modes --------------------------------------------------------------------------------------------------------
|
|
794
837
|
def copy_default_cfg():
|
|
795
838
|
"""
|
|
796
|
-
|
|
839
|
+
Copies the default configuration file and creates a new one with '_copy' appended to its name.
|
|
797
840
|
|
|
798
|
-
This function duplicates the existing default configuration file and
|
|
799
|
-
working directory.
|
|
841
|
+
This function duplicates the existing default configuration file (DEFAULT_CFG_PATH) and saves it
|
|
842
|
+
with '_copy' appended to its name in the current working directory. It provides a convenient way
|
|
843
|
+
to create a custom configuration file based on the default settings.
|
|
800
844
|
|
|
801
845
|
Examples:
|
|
802
|
-
Copy the default configuration file and use it in a YOLO command:
|
|
803
846
|
>>> copy_default_cfg()
|
|
804
|
-
|
|
805
|
-
|
|
847
|
+
# Output: default.yaml copied to /path/to/current/directory/default_copy.yaml
|
|
848
|
+
# Example YOLO command with this new custom cfg:
|
|
849
|
+
# yolo cfg='/path/to/current/directory/default_copy.yaml' imgsz=320 batch=8
|
|
850
|
+
|
|
851
|
+
Notes:
|
|
852
|
+
- The new configuration file is created in the current working directory.
|
|
853
|
+
- After copying, the function prints a message with the new file's location and an example
|
|
854
|
+
YOLO command demonstrating how to use the new configuration file.
|
|
855
|
+
- This function is useful for users who want to modify the default configuration without
|
|
856
|
+
altering the original file.
|
|
806
857
|
"""
|
|
807
858
|
new_file = Path.cwd() / DEFAULT_CFG_PATH.name.replace(".yaml", "_copy.yaml")
|
|
808
859
|
shutil.copy2(DEFAULT_CFG_PATH, new_file)
|
ultralytics/data/annotator.py
CHANGED
|
@@ -9,20 +9,24 @@ def auto_annotate(data, det_model="yolov8x.pt", sam_model="sam_b.pt", device="",
|
|
|
9
9
|
"""
|
|
10
10
|
Automatically annotates images using a YOLO object detection model and a SAM segmentation model.
|
|
11
11
|
|
|
12
|
+
This function processes images in a specified directory, detects objects using a YOLO model, and then generates
|
|
13
|
+
segmentation masks using a SAM model. The resulting annotations are saved as text files.
|
|
14
|
+
|
|
12
15
|
Args:
|
|
13
16
|
data (str): Path to a folder containing images to be annotated.
|
|
14
|
-
det_model (str
|
|
15
|
-
sam_model (str
|
|
16
|
-
device (str
|
|
17
|
-
output_dir (str | None
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
det_model (str): Path or name of the pre-trained YOLO detection model.
|
|
18
|
+
sam_model (str): Path or name of the pre-trained SAM segmentation model.
|
|
19
|
+
device (str): Device to run the models on (e.g., 'cpu', 'cuda', '0').
|
|
20
|
+
output_dir (str | None): Directory to save the annotated results. If None, a default directory is created.
|
|
21
|
+
|
|
22
|
+
Examples:
|
|
23
|
+
>>> from ultralytics.data.annotator import auto_annotate
|
|
24
|
+
>>> auto_annotate(data='ultralytics/assets', det_model='yolov8n.pt', sam_model='mobile_sam.pt')
|
|
25
|
+
|
|
26
|
+
Notes:
|
|
27
|
+
- The function creates a new directory for output if not specified.
|
|
28
|
+
- Annotation results are saved as text files with the same names as the input images.
|
|
29
|
+
- Each line in the output text file represents a detected object with its class ID and segmentation points.
|
|
26
30
|
"""
|
|
27
31
|
det_model = YOLO(det_model)
|
|
28
32
|
sam_model = SAM(sam_model)
|