2023-01-09 22:22:33 +00:00
|
|
|
# Ultralytics YOLO 🚀, GPL-3.0 license
|
|
|
|
|
|
2023-01-04 23:20:54 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
2023-01-17 13:32:34 +00:00
|
|
|
import cv2
|
|
|
|
|
import torch
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
2022-12-28 12:35:01 +00:00
|
|
|
from ultralytics import YOLO
|
2023-01-04 23:20:54 +00:00
|
|
|
from ultralytics.yolo.utils import ROOT, SETTINGS
|
2022-12-28 18:38:37 +00:00
|
|
|
|
2023-01-05 13:13:29 +00:00
|
|
|
MODEL = Path(SETTINGS['weights_dir']) / 'yolov8n.pt'
|
2023-01-07 19:04:34 +00:00
|
|
|
CFG = 'yolov8n.yaml'
|
2023-01-05 13:13:29 +00:00
|
|
|
SOURCE = ROOT / 'assets/bus.jpg'
|
2023-01-03 17:54:44 +00:00
|
|
|
|
2022-12-28 18:38:37 +00:00
|
|
|
|
2022-12-14 09:03:31 +00:00
|
|
|
def test_model_forward():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(CFG)
|
2023-01-05 13:13:29 +00:00
|
|
|
model.predict(SOURCE)
|
|
|
|
|
model(SOURCE)
|
2022-12-14 09:03:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_model_info():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(CFG)
|
2022-12-14 09:03:31 +00:00
|
|
|
model.info()
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2022-12-14 09:03:31 +00:00
|
|
|
model.info(verbose=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_model_fuse():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(CFG)
|
2022-12-14 09:03:31 +00:00
|
|
|
model.fuse()
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2022-12-14 09:03:31 +00:00
|
|
|
model.fuse()
|
|
|
|
|
|
|
|
|
|
|
2023-01-02 16:37:23 +00:00
|
|
|
def test_predict_dir():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2023-01-12 16:09:26 +00:00
|
|
|
model.predict(source=ROOT / "assets")
|
2022-12-14 09:03:31 +00:00
|
|
|
|
|
|
|
|
|
2023-01-17 13:32:34 +00:00
|
|
|
def test_predict_img():
|
|
|
|
|
model = YOLO(MODEL)
|
|
|
|
|
img = Image.open(str(SOURCE))
|
|
|
|
|
output = model(source=img, save=True, verbose=True) # PIL
|
|
|
|
|
assert len(output) == 1, "predict test failed"
|
|
|
|
|
img = cv2.imread(str(SOURCE))
|
|
|
|
|
output = model(source=img, save=True, save_txt=True) # ndarray
|
|
|
|
|
assert len(output) == 1, "predict test failed"
|
|
|
|
|
output = model(source=[img, img], save=True, save_txt=True) # batch
|
|
|
|
|
assert len(output) == 2, "predict test failed"
|
|
|
|
|
tens = torch.zeros(320, 640, 3)
|
|
|
|
|
output = model(tens.numpy())
|
|
|
|
|
assert len(output) == 1, "predict test failed"
|
|
|
|
|
|
|
|
|
|
|
2022-12-14 09:03:31 +00:00
|
|
|
def test_val():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2023-01-13 13:34:51 +00:00
|
|
|
model.val(data="coco8.yaml", imgsz=32)
|
2022-12-14 09:03:31 +00:00
|
|
|
|
|
|
|
|
|
2023-01-02 16:37:23 +00:00
|
|
|
def test_train_scratch():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(CFG)
|
2023-01-13 13:34:51 +00:00
|
|
|
model.train(data="coco8.yaml", epochs=1, imgsz=32)
|
2023-01-05 13:13:29 +00:00
|
|
|
model(SOURCE)
|
2022-12-16 08:26:25 +00:00
|
|
|
|
|
|
|
|
|
2023-01-02 16:37:23 +00:00
|
|
|
def test_train_pretrained():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2023-01-13 13:34:51 +00:00
|
|
|
model.train(data="coco8.yaml", epochs=1, imgsz=32)
|
2023-01-05 13:13:29 +00:00
|
|
|
model(SOURCE)
|
2023-01-02 16:37:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_export_torchscript():
|
2022-12-29 13:17:14 +00:00
|
|
|
"""
|
|
|
|
|
Format Argument Suffix CPU GPU
|
|
|
|
|
0 PyTorch - .pt True True
|
|
|
|
|
1 TorchScript torchscript .torchscript True True
|
|
|
|
|
2 ONNX onnx .onnx True True
|
|
|
|
|
3 OpenVINO openvino _openvino_model True False
|
|
|
|
|
4 TensorRT engine .engine False True
|
|
|
|
|
5 CoreML coreml .mlmodel True False
|
|
|
|
|
6 TensorFlow SavedModel saved_model _saved_model True True
|
|
|
|
|
7 TensorFlow GraphDef pb .pb True True
|
|
|
|
|
8 TensorFlow Lite tflite .tflite True False
|
|
|
|
|
9 TensorFlow Edge TPU edgetpu _edgetpu.tflite False False
|
|
|
|
|
10 TensorFlow.js tfjs _web_model False False
|
|
|
|
|
11 PaddlePaddle paddle _paddle_model True True
|
|
|
|
|
"""
|
|
|
|
|
from ultralytics.yolo.engine.exporter import export_formats
|
|
|
|
|
print(export_formats())
|
|
|
|
|
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2022-12-29 13:17:14 +00:00
|
|
|
model.export(format='torchscript')
|
2023-01-02 16:37:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_export_onnx():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2022-12-29 13:17:14 +00:00
|
|
|
model.export(format='onnx')
|
2023-01-02 16:37:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_export_openvino():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2022-12-29 13:17:14 +00:00
|
|
|
model.export(format='openvino')
|
2023-01-02 16:37:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_export_coreml():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2022-12-29 13:17:14 +00:00
|
|
|
model.export(format='coreml')
|
|
|
|
|
|
|
|
|
|
|
2023-01-02 16:37:23 +00:00
|
|
|
def test_export_paddle():
|
2023-01-03 17:54:44 +00:00
|
|
|
model = YOLO(MODEL)
|
2023-01-02 16:37:23 +00:00
|
|
|
model.export(format='paddle')
|
2022-10-25 19:51:15 +00:00
|
|
|
|
|
|
|
|
|
2023-01-04 23:20:54 +00:00
|
|
|
def test_all_model_yamls():
|
2023-01-10 11:33:28 +00:00
|
|
|
for m in list((ROOT / 'models').rglob('*.yaml')):
|
2023-01-04 23:20:54 +00:00
|
|
|
YOLO(m.name)
|
2023-01-12 16:09:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_workflow():
|
|
|
|
|
model = YOLO(MODEL)
|
2023-01-13 13:34:51 +00:00
|
|
|
model.train(data="coco8.yaml", epochs=1, imgsz=32)
|
2023-01-12 16:09:26 +00:00
|
|
|
model.val()
|
|
|
|
|
model.predict(SOURCE)
|
|
|
|
|
model.export(format="onnx", opset=12) # export a model to ONNX format
|
2023-01-17 13:32:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
test_predict_img()
|