test_cli.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. import subprocess
  3. import pytest
  4. from PIL import Image
  5. from tests import CUDA_DEVICE_COUNT, CUDA_IS_AVAILABLE
  6. from ultralytics.cfg import TASK2DATA, TASK2MODEL, TASKS
  7. from ultralytics.utils import ASSETS, WEIGHTS_DIR, checks
  8. from ultralytics.utils.torch_utils import TORCH_1_9
  9. # Constants
  10. TASK_MODEL_DATA = [(task, WEIGHTS_DIR / TASK2MODEL[task], TASK2DATA[task]) for task in TASKS]
  11. MODELS = [WEIGHTS_DIR / TASK2MODEL[task] for task in TASKS]
  12. def run(cmd):
  13. """Execute a shell command using subprocess."""
  14. subprocess.run(cmd.split(), check=True)
  15. def test_special_modes():
  16. """Test various special command-line modes for YOLO functionality."""
  17. run("yolo help")
  18. run("yolo checks")
  19. run("yolo version")
  20. run("yolo settings reset")
  21. run("yolo cfg")
  22. @pytest.mark.parametrize("task,model,data", TASK_MODEL_DATA)
  23. def test_train(task, model, data):
  24. """Test YOLO training for different tasks, models, and datasets."""
  25. run(f"yolo train {task} model={model} data={data} imgsz=32 epochs=1 cache=disk")
  26. @pytest.mark.parametrize("task,model,data", TASK_MODEL_DATA)
  27. def test_val(task, model, data):
  28. """Test YOLO validation process for specified task, model, and data using a shell command."""
  29. run(f"yolo val {task} model={model} data={data} imgsz=32 save_txt save_json")
  30. @pytest.mark.parametrize("task,model,data", TASK_MODEL_DATA)
  31. def test_predict(task, model, data):
  32. """Test YOLO prediction on provided sample assets for specified task and model."""
  33. run(f"yolo predict model={model} source={ASSETS} imgsz=32 save save_crop save_txt")
  34. @pytest.mark.parametrize("model", MODELS)
  35. def test_export(model):
  36. """Test exporting a YOLO model to TorchScript format."""
  37. run(f"yolo export model={model} format=torchscript imgsz=32")
  38. def test_rtdetr(task="detect", model="yolov8n-rtdetr.yaml", data="coco8.yaml"):
  39. """Test the RTDETR functionality within Ultralytics for detection tasks using specified model and data."""
  40. # Warning: must use imgsz=640 (note also add coma, spaces, fraction=0.25 args to test single-image training)
  41. run(f"yolo train {task} model={model} data={data} --imgsz= 160 epochs =1, cache = disk fraction=0.25")
  42. run(f"yolo predict {task} model={model} source={ASSETS / 'bus.jpg'} imgsz=160 save save_crop save_txt")
  43. if TORCH_1_9:
  44. weights = WEIGHTS_DIR / "rtdetr-l.pt"
  45. run(f"yolo predict {task} model={weights} source={ASSETS / 'bus.jpg'} imgsz=160 save save_crop save_txt")
  46. @pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="MobileSAM with CLIP is not supported in Python 3.12")
  47. def test_fastsam(task="segment", model=WEIGHTS_DIR / "FastSAM-s.pt", data="coco8-seg.yaml"):
  48. """Test FastSAM model for segmenting objects in images using various prompts within Ultralytics."""
  49. source = ASSETS / "bus.jpg"
  50. run(f"yolo segment val {task} model={model} data={data} imgsz=32")
  51. run(f"yolo segment predict model={model} source={source} imgsz=32 save save_crop save_txt")
  52. from ultralytics import FastSAM
  53. from ultralytics.models.sam import Predictor
  54. # Create a FastSAM model
  55. sam_model = FastSAM(model) # or FastSAM-x.pt
  56. # Run inference on an image
  57. for s in (source, Image.open(source)):
  58. everything_results = sam_model(s, device="cpu", retina_masks=True, imgsz=320, conf=0.4, iou=0.9)
  59. # Remove small regions
  60. new_masks, _ = Predictor.remove_small_regions(everything_results[0].masks.data, min_area=20)
  61. # Run inference with bboxes and points and texts prompt at the same time
  62. sam_model(source, bboxes=[439, 437, 524, 709], points=[[200, 200]], labels=[1], texts="a photo of a dog")
  63. def test_mobilesam():
  64. """Test MobileSAM segmentation with point prompts using Ultralytics."""
  65. from ultralytics import SAM
  66. # Load the model
  67. model = SAM(WEIGHTS_DIR / "mobile_sam.pt")
  68. # Source
  69. source = ASSETS / "zidane.jpg"
  70. # Predict a segment based on a 1D point prompt and 1D labels.
  71. model.predict(source, points=[900, 370], labels=[1])
  72. # Predict a segment based on 3D points and 2D labels (multiple points per object).
  73. model.predict(source, points=[[[900, 370], [1000, 100]]], labels=[[1, 1]])
  74. # Predict a segment based on a box prompt
  75. model.predict(source, bboxes=[439, 437, 524, 709], save=True)
  76. # Predict all
  77. # model(source)
  78. # Slow Tests -----------------------------------------------------------------------------------------------------------
  79. @pytest.mark.slow
  80. @pytest.mark.parametrize("task,model,data", TASK_MODEL_DATA)
  81. @pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available")
  82. @pytest.mark.skipif(CUDA_DEVICE_COUNT < 2, reason="DDP is not available")
  83. def test_train_gpu(task, model, data):
  84. """Test YOLO training on GPU(s) for various tasks and models."""
  85. run(f"yolo train {task} model={model} data={data} imgsz=32 epochs=1 device=0") # single GPU
  86. run(f"yolo train {task} model={model} data={data} imgsz=32 epochs=1 device=0,1") # multi GPU