退购1.1定位算法
This commit is contained in:
97
tests/test_cli.py
Normal file
97
tests/test_cli.py
Normal file
@ -0,0 +1,97 @@
|
||||
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||||
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from ultralytics.yolo.utils import LINUX, ONLINE, ROOT, SETTINGS
|
||||
|
||||
MODEL = Path(SETTINGS['weights_dir']) / 'yolov8n'
|
||||
CFG = 'yolov8n'
|
||||
|
||||
|
||||
def run(cmd):
|
||||
# Run a subprocess command with check=True
|
||||
subprocess.run(cmd.split(), check=True)
|
||||
|
||||
|
||||
def test_special_modes():
|
||||
run('yolo checks')
|
||||
run('yolo settings')
|
||||
run('yolo help')
|
||||
|
||||
|
||||
# Train checks ---------------------------------------------------------------------------------------------------------
|
||||
def test_train_det():
|
||||
run(f'yolo train detect model={CFG}.yaml data=coco8.yaml imgsz=32 epochs=1 v5loader')
|
||||
|
||||
|
||||
def test_train_seg():
|
||||
run(f'yolo train segment model={CFG}-seg.yaml data=coco8-seg.yaml imgsz=32 epochs=1')
|
||||
|
||||
|
||||
def test_train_cls():
|
||||
run(f'yolo train classify model={CFG}-cls.yaml data=imagenet10 imgsz=32 epochs=1')
|
||||
|
||||
|
||||
def test_train_pose():
|
||||
run(f'yolo train pose model={CFG}-pose.yaml data=coco8-pose.yaml imgsz=32 epochs=1')
|
||||
|
||||
|
||||
# Val checks -----------------------------------------------------------------------------------------------------------
|
||||
def test_val_detect():
|
||||
run(f'yolo val detect model={MODEL}.pt data=coco8.yaml imgsz=32')
|
||||
|
||||
|
||||
def test_val_segment():
|
||||
run(f'yolo val segment model={MODEL}-seg.pt data=coco8-seg.yaml imgsz=32')
|
||||
|
||||
|
||||
def test_val_classify():
|
||||
run(f'yolo val classify model={MODEL}-cls.pt data=imagenet10 imgsz=32')
|
||||
|
||||
|
||||
def test_val_pose():
|
||||
run(f'yolo val pose model={MODEL}-pose.pt data=coco8-pose.yaml imgsz=32')
|
||||
|
||||
|
||||
# Predict checks -------------------------------------------------------------------------------------------------------
|
||||
def test_predict_detect():
|
||||
run(f"yolo predict model={MODEL}.pt source={ROOT / 'assets'} imgsz=32 save save_crop save_txt")
|
||||
if ONLINE:
|
||||
run(f'yolo predict model={MODEL}.pt source=https://ultralytics.com/images/bus.jpg imgsz=32')
|
||||
run(f'yolo predict model={MODEL}.pt source=https://ultralytics.com/assets/decelera_landscape_min.mov imgsz=32')
|
||||
run(f'yolo predict model={MODEL}.pt source=https://ultralytics.com/assets/decelera_portrait_min.mov imgsz=32')
|
||||
|
||||
|
||||
def test_predict_segment():
|
||||
run(f"yolo predict model={MODEL}-seg.pt source={ROOT / 'assets'} imgsz=32 save save_txt")
|
||||
|
||||
|
||||
def test_predict_classify():
|
||||
run(f"yolo predict model={MODEL}-cls.pt source={ROOT / 'assets'} imgsz=32 save save_txt")
|
||||
|
||||
|
||||
def test_predict_pose():
|
||||
run(f"yolo predict model={MODEL}-pose.pt source={ROOT / 'assets'} imgsz=32 save save_txt")
|
||||
|
||||
|
||||
# Export checks --------------------------------------------------------------------------------------------------------
|
||||
def test_export_detect_torchscript():
|
||||
run(f'yolo export model={MODEL}.pt format=torchscript')
|
||||
|
||||
|
||||
def test_export_segment_torchscript():
|
||||
run(f'yolo export model={MODEL}-seg.pt format=torchscript')
|
||||
|
||||
|
||||
def test_export_classify_torchscript():
|
||||
run(f'yolo export model={MODEL}-cls.pt format=torchscript')
|
||||
|
||||
|
||||
def test_export_classify_pose():
|
||||
run(f'yolo export model={MODEL}-pose.pt format=torchscript')
|
||||
|
||||
|
||||
def test_export_detect_edgetpu(enabled=False):
|
||||
if enabled and LINUX:
|
||||
run(f'yolo export model={MODEL}.pt format=edgetpu')
|
125
tests/test_engine.py
Normal file
125
tests/test_engine.py
Normal file
@ -0,0 +1,125 @@
|
||||
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from ultralytics import YOLO
|
||||
from ultralytics.yolo.cfg import get_cfg
|
||||
from ultralytics.yolo.engine.exporter import Exporter
|
||||
from ultralytics.yolo.utils import DEFAULT_CFG, ROOT, SETTINGS
|
||||
from ultralytics.yolo.v8 import classify, detect, segment
|
||||
|
||||
CFG_DET = 'yolov8n.yaml'
|
||||
CFG_SEG = 'yolov8n-seg.yaml'
|
||||
CFG_CLS = 'squeezenet1_0'
|
||||
CFG = get_cfg(DEFAULT_CFG)
|
||||
MODEL = Path(SETTINGS['weights_dir']) / 'yolov8n'
|
||||
SOURCE = ROOT / 'assets'
|
||||
|
||||
|
||||
def test_func(model=None):
|
||||
print('callback test passed')
|
||||
|
||||
|
||||
def test_export():
|
||||
exporter = Exporter()
|
||||
exporter.add_callback('on_export_start', test_func)
|
||||
assert test_func in exporter.callbacks['on_export_start'], 'callback test failed'
|
||||
f = exporter(model=YOLO(CFG_DET).model)
|
||||
YOLO(f)(SOURCE) # exported model inference
|
||||
|
||||
|
||||
def test_detect():
|
||||
overrides = {'data': 'coco8.yaml', 'model': CFG_DET, 'imgsz': 32, 'epochs': 1, 'save': False}
|
||||
CFG.data = 'coco8.yaml'
|
||||
|
||||
# Trainer
|
||||
trainer = detect.DetectionTrainer(overrides=overrides)
|
||||
trainer.add_callback('on_train_start', test_func)
|
||||
assert test_func in trainer.callbacks['on_train_start'], 'callback test failed'
|
||||
trainer.train()
|
||||
|
||||
# Validator
|
||||
val = detect.DetectionValidator(args=CFG)
|
||||
val.add_callback('on_val_start', test_func)
|
||||
assert test_func in val.callbacks['on_val_start'], 'callback test failed'
|
||||
val(model=trainer.best) # validate best.pt
|
||||
|
||||
# Predictor
|
||||
pred = detect.DetectionPredictor(overrides={'imgsz': [64, 64]})
|
||||
pred.add_callback('on_predict_start', test_func)
|
||||
assert test_func in pred.callbacks['on_predict_start'], 'callback test failed'
|
||||
result = pred(source=SOURCE, model=f'{MODEL}.pt')
|
||||
assert len(result), 'predictor test failed'
|
||||
|
||||
overrides['resume'] = trainer.last
|
||||
trainer = detect.DetectionTrainer(overrides=overrides)
|
||||
try:
|
||||
trainer.train()
|
||||
except Exception as e:
|
||||
print(f'Expected exception caught: {e}')
|
||||
return
|
||||
|
||||
Exception('Resume test failed!')
|
||||
|
||||
|
||||
def test_segment():
|
||||
overrides = {'data': 'coco8-seg.yaml', 'model': CFG_SEG, 'imgsz': 32, 'epochs': 1, 'save': False}
|
||||
CFG.data = 'coco8-seg.yaml'
|
||||
CFG.v5loader = False
|
||||
# YOLO(CFG_SEG).train(**overrides) # works
|
||||
|
||||
# trainer
|
||||
trainer = segment.SegmentationTrainer(overrides=overrides)
|
||||
trainer.add_callback('on_train_start', test_func)
|
||||
assert test_func in trainer.callbacks['on_train_start'], 'callback test failed'
|
||||
trainer.train()
|
||||
|
||||
# Validator
|
||||
val = segment.SegmentationValidator(args=CFG)
|
||||
val.add_callback('on_val_start', test_func)
|
||||
assert test_func in val.callbacks['on_val_start'], 'callback test failed'
|
||||
val(model=trainer.best) # validate best.pt
|
||||
|
||||
# Predictor
|
||||
pred = segment.SegmentationPredictor(overrides={'imgsz': [64, 64]})
|
||||
pred.add_callback('on_predict_start', test_func)
|
||||
assert test_func in pred.callbacks['on_predict_start'], 'callback test failed'
|
||||
result = pred(source=SOURCE, model=f'{MODEL}-seg.pt')
|
||||
assert len(result), 'predictor test failed'
|
||||
|
||||
# Test resume
|
||||
overrides['resume'] = trainer.last
|
||||
trainer = segment.SegmentationTrainer(overrides=overrides)
|
||||
try:
|
||||
trainer.train()
|
||||
except Exception as e:
|
||||
print(f'Expected exception caught: {e}')
|
||||
return
|
||||
|
||||
Exception('Resume test failed!')
|
||||
|
||||
|
||||
def test_classify():
|
||||
overrides = {'data': 'imagenet10', 'model': 'yolov8n-cls.yaml', 'imgsz': 32, 'epochs': 1, 'save': False}
|
||||
CFG.data = 'imagenet10'
|
||||
CFG.imgsz = 32
|
||||
# YOLO(CFG_SEG).train(**overrides) # works
|
||||
|
||||
# Trainer
|
||||
trainer = classify.ClassificationTrainer(overrides=overrides)
|
||||
trainer.add_callback('on_train_start', test_func)
|
||||
assert test_func in trainer.callbacks['on_train_start'], 'callback test failed'
|
||||
trainer.train()
|
||||
|
||||
# Validator
|
||||
val = classify.ClassificationValidator(args=CFG)
|
||||
val.add_callback('on_val_start', test_func)
|
||||
assert test_func in val.callbacks['on_val_start'], 'callback test failed'
|
||||
val(model=trainer.best)
|
||||
|
||||
# Predictor
|
||||
pred = classify.ClassificationPredictor(overrides={'imgsz': [64, 64]})
|
||||
pred.add_callback('on_predict_start', test_func)
|
||||
assert test_func in pred.callbacks['on_predict_start'], 'callback test failed'
|
||||
result = pred(source=SOURCE, model=trainer.best)
|
||||
assert len(result), 'predictor test failed'
|
248
tests/test_python.py
Normal file
248
tests/test_python.py
Normal file
@ -0,0 +1,248 @@
|
||||
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import torch
|
||||
from PIL import Image
|
||||
|
||||
from ultralytics import YOLO
|
||||
from ultralytics.yolo.data.build import load_inference_source
|
||||
from ultralytics.yolo.utils import LINUX, ONLINE, ROOT, SETTINGS
|
||||
|
||||
MODEL = Path(SETTINGS['weights_dir']) / 'yolov8n.pt'
|
||||
CFG = 'yolov8n.yaml'
|
||||
SOURCE = ROOT / 'assets/bus.jpg'
|
||||
SOURCE_GREYSCALE = Path(f'{SOURCE.parent / SOURCE.stem}_greyscale.jpg')
|
||||
SOURCE_RGBA = Path(f'{SOURCE.parent / SOURCE.stem}_4ch.png')
|
||||
|
||||
# Convert SOURCE to greyscale and 4-ch
|
||||
im = Image.open(SOURCE)
|
||||
im.convert('L').save(SOURCE_GREYSCALE) # greyscale
|
||||
im.convert('RGBA').save(SOURCE_RGBA) # 4-ch PNG with alpha
|
||||
|
||||
|
||||
def test_model_forward():
|
||||
model = YOLO(CFG)
|
||||
model(SOURCE)
|
||||
|
||||
|
||||
def test_model_info():
|
||||
model = YOLO(CFG)
|
||||
model.info()
|
||||
model = YOLO(MODEL)
|
||||
model.info(verbose=True)
|
||||
|
||||
|
||||
def test_model_fuse():
|
||||
model = YOLO(CFG)
|
||||
model.fuse()
|
||||
model = YOLO(MODEL)
|
||||
model.fuse()
|
||||
|
||||
|
||||
def test_predict_dir():
|
||||
model = YOLO(MODEL)
|
||||
model(source=ROOT / 'assets')
|
||||
|
||||
|
||||
def test_predict_img():
|
||||
model = YOLO(MODEL)
|
||||
seg_model = YOLO('yolov8n-seg.pt')
|
||||
cls_model = YOLO('yolov8n-cls.pt')
|
||||
pose_model = YOLO('yolov8n-pose.pt')
|
||||
im = cv2.imread(str(SOURCE))
|
||||
assert len(model(source=Image.open(SOURCE), save=True, verbose=True)) == 1 # PIL
|
||||
assert len(model(source=im, save=True, save_txt=True)) == 1 # ndarray
|
||||
assert len(model(source=[im, im], save=True, save_txt=True)) == 2 # batch
|
||||
assert len(list(model(source=[im, im], save=True, stream=True))) == 2 # stream
|
||||
assert len(model(torch.zeros(320, 640, 3).numpy())) == 1 # tensor to numpy
|
||||
batch = [
|
||||
str(SOURCE), # filename
|
||||
Path(SOURCE), # Path
|
||||
'https://ultralytics.com/images/zidane.jpg' if ONLINE else SOURCE, # URI
|
||||
cv2.imread(str(SOURCE)), # OpenCV
|
||||
Image.open(SOURCE), # PIL
|
||||
np.zeros((320, 640, 3))] # numpy
|
||||
assert len(model(batch, visualize=True)) == len(batch) # multiple sources in a batch
|
||||
|
||||
# Test tensor inference
|
||||
im = cv2.imread(str(SOURCE)) # OpenCV
|
||||
t = cv2.resize(im, (32, 32))
|
||||
t = torch.from_numpy(t.transpose((2, 0, 1)))
|
||||
t = torch.stack([t, t, t, t])
|
||||
results = model(t, visualize=True)
|
||||
assert len(results) == t.shape[0]
|
||||
results = seg_model(t, visualize=True)
|
||||
assert len(results) == t.shape[0]
|
||||
results = cls_model(t, visualize=True)
|
||||
assert len(results) == t.shape[0]
|
||||
results = pose_model(t, visualize=True)
|
||||
assert len(results) == t.shape[0]
|
||||
|
||||
|
||||
def test_predict_grey_and_4ch():
|
||||
model = YOLO(MODEL)
|
||||
for f in SOURCE_RGBA, SOURCE_GREYSCALE:
|
||||
for source in Image.open(f), cv2.imread(str(f)), f:
|
||||
model(source, save=True, verbose=True)
|
||||
|
||||
|
||||
def test_val():
|
||||
model = YOLO(MODEL)
|
||||
model.val(data='coco8.yaml', imgsz=32)
|
||||
|
||||
|
||||
def test_val_scratch():
|
||||
model = YOLO(CFG)
|
||||
model.val(data='coco8.yaml', imgsz=32)
|
||||
|
||||
|
||||
def test_amp():
|
||||
if torch.cuda.is_available():
|
||||
from ultralytics.yolo.engine.trainer import check_amp
|
||||
model = YOLO(MODEL).model.cuda()
|
||||
assert check_amp(model)
|
||||
|
||||
|
||||
def test_train_scratch():
|
||||
model = YOLO(CFG)
|
||||
model.train(data='coco8.yaml', epochs=1, imgsz=32)
|
||||
model(SOURCE)
|
||||
|
||||
|
||||
def test_train_pretrained():
|
||||
model = YOLO(MODEL)
|
||||
model.train(data='coco8.yaml', epochs=1, imgsz=32)
|
||||
model(SOURCE)
|
||||
|
||||
|
||||
def test_export_torchscript():
|
||||
model = YOLO(MODEL)
|
||||
f = model.export(format='torchscript')
|
||||
YOLO(f)(SOURCE) # exported model inference
|
||||
|
||||
|
||||
def test_export_torchscript_scratch():
|
||||
model = YOLO(CFG)
|
||||
f = model.export(format='torchscript')
|
||||
YOLO(f)(SOURCE) # exported model inference
|
||||
|
||||
|
||||
def test_export_onnx():
|
||||
model = YOLO(MODEL)
|
||||
f = model.export(format='onnx')
|
||||
YOLO(f)(SOURCE) # exported model inference
|
||||
|
||||
|
||||
def test_export_openvino():
|
||||
model = YOLO(MODEL)
|
||||
f = model.export(format='openvino')
|
||||
YOLO(f)(SOURCE) # exported model inference
|
||||
|
||||
|
||||
def test_export_coreml(): # sourcery skip: move-assign
|
||||
model = YOLO(MODEL)
|
||||
model.export(format='coreml')
|
||||
# if MACOS:
|
||||
# YOLO(f)(SOURCE) # model prediction only supported on macOS
|
||||
|
||||
|
||||
def test_export_tflite(enabled=False):
|
||||
# TF suffers from install conflicts on Windows and macOS
|
||||
if enabled and LINUX:
|
||||
model = YOLO(MODEL)
|
||||
f = model.export(format='tflite')
|
||||
YOLO(f)(SOURCE)
|
||||
|
||||
|
||||
def test_export_pb(enabled=False):
|
||||
# TF suffers from install conflicts on Windows and macOS
|
||||
if enabled and LINUX:
|
||||
model = YOLO(MODEL)
|
||||
f = model.export(format='pb')
|
||||
YOLO(f)(SOURCE)
|
||||
|
||||
|
||||
def test_export_paddle(enabled=False):
|
||||
# Paddle protobuf requirements conflicting with onnx protobuf requirements
|
||||
if enabled:
|
||||
model = YOLO(MODEL)
|
||||
model.export(format='paddle')
|
||||
|
||||
|
||||
def test_all_model_yamls():
|
||||
for m in list((ROOT / 'models').rglob('yolo*.yaml')):
|
||||
YOLO(m.name)
|
||||
|
||||
|
||||
def test_workflow():
|
||||
model = YOLO(MODEL)
|
||||
model.train(data='coco8.yaml', epochs=1, imgsz=32)
|
||||
model.val()
|
||||
model.predict(SOURCE)
|
||||
model.export(format='onnx') # export a model to ONNX format
|
||||
|
||||
|
||||
def test_predict_callback_and_setup():
|
||||
# test callback addition for prediction
|
||||
def on_predict_batch_end(predictor): # results -> List[batch_size]
|
||||
path, im0s, _, _ = predictor.batch
|
||||
# print('on_predict_batch_end', im0s[0].shape)
|
||||
im0s = im0s if isinstance(im0s, list) else [im0s]
|
||||
bs = [predictor.dataset.bs for _ in range(len(path))]
|
||||
predictor.results = zip(predictor.results, im0s, bs)
|
||||
|
||||
model = YOLO(MODEL)
|
||||
model.add_callback('on_predict_batch_end', on_predict_batch_end)
|
||||
|
||||
dataset = load_inference_source(source=SOURCE)
|
||||
bs = dataset.bs # noqa access predictor properties
|
||||
results = model.predict(dataset, stream=True) # source already setup
|
||||
for _, (result, im0, bs) in enumerate(results):
|
||||
print('test_callback', im0.shape)
|
||||
print('test_callback', bs)
|
||||
boxes = result.boxes # Boxes object for bbox outputs
|
||||
print(boxes)
|
||||
|
||||
|
||||
def test_result():
|
||||
model = YOLO('yolov8n-pose.pt')
|
||||
res = model([SOURCE, SOURCE])
|
||||
res[0].plot(conf=True, boxes=False)
|
||||
res[0].plot(pil=True)
|
||||
res[0] = res[0].cpu().numpy()
|
||||
print(res[0].path, res[0].keypoints)
|
||||
|
||||
model = YOLO('yolov8n-seg.pt')
|
||||
res = model([SOURCE, SOURCE])
|
||||
res[0].plot(conf=True, boxes=False, masks=True)
|
||||
res[0].plot(pil=True)
|
||||
res[0] = res[0].cpu().numpy()
|
||||
print(res[0].path, res[0].masks.data)
|
||||
|
||||
model = YOLO('yolov8n.pt')
|
||||
res = model(SOURCE)
|
||||
res[0].plot(pil=True)
|
||||
res[0].plot()
|
||||
res[0] = res[0].cpu().numpy()
|
||||
print(res[0].path)
|
||||
|
||||
model = YOLO('yolov8n-cls.pt')
|
||||
res = model(SOURCE)
|
||||
res[0].plot(probs=False)
|
||||
res[0].plot(pil=True)
|
||||
res[0].plot()
|
||||
res[0] = res[0].cpu().numpy()
|
||||
print(res[0].path)
|
||||
|
||||
|
||||
def test_track():
|
||||
im = cv2.imread(str(SOURCE))
|
||||
model = YOLO(MODEL)
|
||||
seg_model = YOLO('yolov8n-seg.pt')
|
||||
pose_model = YOLO('yolov8n-pose.pt')
|
||||
model.track(source=im)
|
||||
seg_model.track(source=im)
|
||||
pose_model.track(source=im)
|
Reference in New Issue
Block a user