|
|
|
|
|
import subprocess |
|
|
|
import pytest |
|
|
|
from ultralytics.utils import ASSETS, WEIGHTS_DIR, checks |
|
|
|
CUDA_IS_AVAILABLE = checks.cuda_is_available() |
|
CUDA_DEVICE_COUNT = checks.cuda_device_count() |
|
TASK_ARGS = [ |
|
("detect", "yolov8n", "coco8.yaml"), |
|
("segment", "yolov8n-seg", "coco8-seg.yaml"), |
|
("classify", "yolov8n-cls", "imagenet10"), |
|
("pose", "yolov8n-pose", "coco8-pose.yaml"), |
|
("obb", "yolov8n-obb", "dota8.yaml"), |
|
] |
|
EXPORT_ARGS = [ |
|
("yolov8n", "torchscript"), |
|
("yolov8n-seg", "torchscript"), |
|
("yolov8n-cls", "torchscript"), |
|
("yolov8n-pose", "torchscript"), |
|
("yolov8n-obb", "torchscript"), |
|
] |
|
|
|
|
|
def run(cmd): |
|
"""Execute a shell command using subprocess.""" |
|
subprocess.run(cmd.split(), check=True) |
|
|
|
|
|
def test_special_modes(): |
|
"""Test various special command modes of YOLO.""" |
|
run("yolo help") |
|
run("yolo checks") |
|
run("yolo version") |
|
run("yolo settings reset") |
|
run("yolo cfg") |
|
|
|
|
|
@pytest.mark.parametrize("task,model,data", TASK_ARGS) |
|
def test_train(task, model, data): |
|
"""Test YOLO training for a given task, model, and data.""" |
|
run(f"yolo train {task} model={model}.yaml data={data} imgsz=32 epochs=1 cache=disk") |
|
|
|
|
|
@pytest.mark.parametrize("task,model,data", TASK_ARGS) |
|
def test_val(task, model, data): |
|
"""Test YOLO validation for a given task, model, and data.""" |
|
run(f"yolo val {task} model={WEIGHTS_DIR / model}.pt data={data} imgsz=32 save_txt save_json") |
|
|
|
|
|
@pytest.mark.parametrize("task,model,data", TASK_ARGS) |
|
def test_predict(task, model, data): |
|
"""Test YOLO prediction on sample assets for a given task and model.""" |
|
run(f"yolo predict model={WEIGHTS_DIR / model}.pt source={ASSETS} imgsz=32 save save_crop save_txt") |
|
|
|
|
|
@pytest.mark.parametrize("model,format", EXPORT_ARGS) |
|
def test_export(model, format): |
|
"""Test exporting a YOLO model to different formats.""" |
|
run(f"yolo export model={WEIGHTS_DIR / model}.pt format={format} imgsz=32") |
|
|
|
|
|
def test_rtdetr(task="detect", model="yolov8n-rtdetr.yaml", data="coco8.yaml"): |
|
"""Test the RTDETR functionality with the Ultralytics framework.""" |
|
|
|
run(f"yolo train {task} model={model} data={data} --imgsz= 160 epochs =1, cache = disk") |
|
run(f"yolo predict {task} model={model} source={ASSETS / 'bus.jpg'} imgsz=160 save save_crop save_txt") |
|
|
|
|
|
@pytest.mark.skipif(checks.IS_PYTHON_3_12, reason="MobileSAM with CLIP is not supported in Python 3.12") |
|
def test_fastsam(task="segment", model=WEIGHTS_DIR / "FastSAM-s.pt", data="coco8-seg.yaml"): |
|
"""Test FastSAM segmentation functionality within Ultralytics.""" |
|
source = ASSETS / "bus.jpg" |
|
|
|
run(f"yolo segment val {task} model={model} data={data} imgsz=32") |
|
run(f"yolo segment predict model={model} source={source} imgsz=32 save save_crop save_txt") |
|
|
|
from ultralytics import FastSAM |
|
from ultralytics.models.fastsam import FastSAMPrompt |
|
from ultralytics.models.sam import Predictor |
|
|
|
|
|
sam_model = FastSAM(model) |
|
|
|
|
|
everything_results = sam_model(source, device="cpu", retina_masks=True, imgsz=1024, conf=0.4, iou=0.9) |
|
|
|
|
|
new_masks, _ = Predictor.remove_small_regions(everything_results[0].masks.data, min_area=20) |
|
|
|
|
|
prompt_process = FastSAMPrompt(source, everything_results, device="cpu") |
|
ann = prompt_process.everything_prompt() |
|
|
|
|
|
ann = prompt_process.box_prompt(bbox=[200, 200, 300, 300]) |
|
|
|
|
|
ann = prompt_process.text_prompt(text="a photo of a dog") |
|
|
|
|
|
|
|
|
|
ann = prompt_process.point_prompt(points=[[200, 200]], pointlabel=[1]) |
|
prompt_process.plot(annotations=ann, output="./") |
|
|
|
|
|
def test_mobilesam(): |
|
"""Test MobileSAM segmentation functionality using Ultralytics.""" |
|
from ultralytics import SAM |
|
|
|
|
|
model = SAM(WEIGHTS_DIR / "mobile_sam.pt") |
|
|
|
|
|
source = ASSETS / "zidane.jpg" |
|
|
|
|
|
model.predict(source, points=[900, 370], labels=[1]) |
|
|
|
|
|
model.predict(source, bboxes=[439, 437, 524, 709]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.slow |
|
@pytest.mark.parametrize("task,model,data", TASK_ARGS) |
|
@pytest.mark.skipif(not CUDA_IS_AVAILABLE, reason="CUDA is not available") |
|
@pytest.mark.skipif(CUDA_DEVICE_COUNT < 2, reason="DDP is not available") |
|
def test_train_gpu(task, model, data): |
|
"""Test YOLO training on GPU(s) for various tasks and models.""" |
|
run(f"yolo train {task} model={model}.yaml data={data} imgsz=32 epochs=1 device=0") |
|
run(f"yolo train {task} model={model}.pt data={data} imgsz=32 epochs=1 device=0,1") |
|
|