Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_integrations.py 6.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  1. # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
  2. import contextlib
  3. import os
  4. import subprocess
  5. import time
  6. from pathlib import Path
  7. import pytest
  8. from tests import MODEL, SOURCE, TMP
  9. from ultralytics import YOLO, download
  10. from ultralytics.utils import DATASETS_DIR, SETTINGS
  11. from ultralytics.utils.checks import check_requirements
  12. @pytest.mark.slow
  13. def test_tensorboard():
  14. """Test training with TensorBoard logging enabled."""
  15. SETTINGS["tensorboard"] = True
  16. YOLO("yolo11n-cls.yaml").train(data="imagenet10", imgsz=32, epochs=3, plots=False, device="cpu")
  17. SETTINGS["tensorboard"] = False
  18. @pytest.mark.skipif(not check_requirements("ray", install=False), reason="ray[tune] not installed")
  19. def test_model_ray_tune():
  20. """Tune YOLO model using Ray for hyperparameter optimization."""
  21. YOLO("yolo11n-cls.yaml").tune(
  22. use_ray=True, data="imagenet10", grace_period=1, iterations=1, imgsz=32, epochs=1, plots=False, device="cpu"
  23. )
  24. @pytest.mark.skipif(not check_requirements("mlflow", install=False), reason="mlflow not installed")
  25. def test_mlflow():
  26. """Test training with MLflow tracking enabled (see https://mlflow.org/ for details)."""
  27. SETTINGS["mlflow"] = True
  28. YOLO("yolo11n-cls.yaml").train(data="imagenet10", imgsz=32, epochs=3, plots=False, device="cpu")
  29. SETTINGS["mlflow"] = False
  30. @pytest.mark.skipif(True, reason="Test failing in scheduled CI https://github.com/ultralytics/ultralytics/pull/8868")
  31. @pytest.mark.skipif(not check_requirements("mlflow", install=False), reason="mlflow not installed")
  32. def test_mlflow_keep_run_active():
  33. """Ensure MLflow run status matches MLFLOW_KEEP_RUN_ACTIVE environment variable settings."""
  34. import mlflow
  35. SETTINGS["mlflow"] = True
  36. run_name = "Test Run"
  37. os.environ["MLFLOW_RUN"] = run_name
  38. # Test with MLFLOW_KEEP_RUN_ACTIVE=True
  39. os.environ["MLFLOW_KEEP_RUN_ACTIVE"] = "True"
  40. YOLO("yolo11n-cls.yaml").train(data="imagenet10", imgsz=32, epochs=1, plots=False, device="cpu")
  41. status = mlflow.active_run().info.status
  42. assert status == "RUNNING", "MLflow run should be active when MLFLOW_KEEP_RUN_ACTIVE=True"
  43. run_id = mlflow.active_run().info.run_id
  44. # Test with MLFLOW_KEEP_RUN_ACTIVE=False
  45. os.environ["MLFLOW_KEEP_RUN_ACTIVE"] = "False"
  46. YOLO("yolo11n-cls.yaml").train(data="imagenet10", imgsz=32, epochs=1, plots=False, device="cpu")
  47. status = mlflow.get_run(run_id=run_id).info.status
  48. assert status == "FINISHED", "MLflow run should be ended when MLFLOW_KEEP_RUN_ACTIVE=False"
  49. # Test with MLFLOW_KEEP_RUN_ACTIVE not set
  50. os.environ.pop("MLFLOW_KEEP_RUN_ACTIVE", None)
  51. YOLO("yolo11n-cls.yaml").train(data="imagenet10", imgsz=32, epochs=1, plots=False, device="cpu")
  52. status = mlflow.get_run(run_id=run_id).info.status
  53. assert status == "FINISHED", "MLflow run should be ended by default when MLFLOW_KEEP_RUN_ACTIVE is not set"
  54. SETTINGS["mlflow"] = False
  55. @pytest.mark.skipif(not check_requirements("tritonclient", install=False), reason="tritonclient[all] not installed")
  56. def test_triton():
  57. """Test NVIDIA Triton Server functionalities with YOLO model."""
  58. check_requirements("tritonclient[all]")
  59. from tritonclient.http import InferenceServerClient # noqa
  60. # Create variables
  61. model_name = "yolo"
  62. triton_repo = TMP / "triton_repo" # Triton repo path
  63. triton_model = triton_repo / model_name # Triton model path
  64. # Export model to ONNX
  65. f = YOLO(MODEL).export(format="onnx", dynamic=True)
  66. # Prepare Triton repo
  67. (triton_model / "1").mkdir(parents=True, exist_ok=True)
  68. Path(f).rename(triton_model / "1" / "model.onnx")
  69. (triton_model / "config.pbtxt").touch()
  70. # Define image https://catalog.ngc.nvidia.com/orgs/nvidia/containers/tritonserver
  71. tag = "nvcr.io/nvidia/tritonserver:23.09-py3" # 6.4 GB
  72. # Pull the image
  73. subprocess.call(f"docker pull {tag}", shell=True)
  74. # Run the Triton server and capture the container ID
  75. container_id = (
  76. subprocess.check_output(
  77. f"docker run -d --rm -v {triton_repo}:/models -p 8000:8000 {tag} tritonserver --model-repository=/models",
  78. shell=True,
  79. )
  80. .decode("utf-8")
  81. .strip()
  82. )
  83. # Wait for the Triton server to start
  84. triton_client = InferenceServerClient(url="localhost:8000", verbose=False, ssl=False)
  85. # Wait until model is ready
  86. for _ in range(10):
  87. with contextlib.suppress(Exception):
  88. assert triton_client.is_model_ready(model_name)
  89. break
  90. time.sleep(1)
  91. # Check Triton inference
  92. YOLO(f"http://localhost:8000/{model_name}", "detect")(SOURCE) # exported model inference
  93. # Kill and remove the container at the end of the test
  94. subprocess.call(f"docker kill {container_id}", shell=True)
  95. @pytest.mark.skipif(not check_requirements("pycocotools", install=False), reason="pycocotools not installed")
  96. def test_pycocotools():
  97. """Validate YOLO model predictions on COCO dataset using pycocotools."""
  98. from ultralytics.models.yolo.detect import DetectionValidator
  99. from ultralytics.models.yolo.pose import PoseValidator
  100. from ultralytics.models.yolo.segment import SegmentationValidator
  101. # Download annotations after each dataset downloads first
  102. url = "https://github.com/ultralytics/assets/releases/download/v0.0.0/"
  103. args = {"model": "yolo11n.pt", "data": "coco8.yaml", "save_json": True, "imgsz": 64}
  104. validator = DetectionValidator(args=args)
  105. validator()
  106. validator.is_coco = True
  107. download(f"{url}instances_val2017.json", dir=DATASETS_DIR / "coco8/annotations")
  108. _ = validator.eval_json(validator.stats)
  109. args = {"model": "yolo11n-seg.pt", "data": "coco8-seg.yaml", "save_json": True, "imgsz": 64}
  110. validator = SegmentationValidator(args=args)
  111. validator()
  112. validator.is_coco = True
  113. download(f"{url}instances_val2017.json", dir=DATASETS_DIR / "coco8-seg/annotations")
  114. _ = validator.eval_json(validator.stats)
  115. args = {"model": "yolo11n-pose.pt", "data": "coco8-pose.yaml", "save_json": True, "imgsz": 64}
  116. validator = PoseValidator(args=args)
  117. validator()
  118. validator.is_coco = True
  119. download(f"{url}person_keypoints_val2017.json", dir=DATASETS_DIR / "coco8-pose/annotations")
  120. _ = validator.eval_json(validator.stats)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...