y o l o V 8 基础使用教程 yoloV8基础使用教程 yoloV8基础使用教程
yoloV8代码:https://github.com/ultralytics/ultralytics
官方文档
yolov8多折交叉验证
安装
pip install ultralytics
使用
if __name__ == '__main__':
# from ultralytics.yolo.engine.model import YOLO
from ultralytics import YOLO
# 1 加载模型
model = YOLO(r'yolov8s-seg.pt') # load a pretrained model (recommended for training)
# 2 训练模型
model.train(data='Test.yaml', epochs=500, imgsz=1801,batch=6,project="./test",name="Test")
# 3 验证模型
metrics = model.val(data="Test.yaml") # 在验证集上评估模型性能
# 4 模型预测
results = model.predict(source=r"D:\dataset\Test\images\val", imgsz=1801 ,save=True,batch=2) # save plotted images
# 5 导出所需模式(以onnx为例)
model.export(format='onnx',imgsz=(701,1801)) # imgsz 高宽
Test.yaml
path: D:\dataset\Test # dataset root dir
train: images/train # train images (relative to 'path')
val: images/val # val images (relative to 'path')
test: # test images (optional)
# Classes
names:
0: C1
1: C2
一 加载模型的三种方式和训练模型
from ultralytics import YOLO
# Load a model
model = YOLO('yolov8n.yaml') # build a new model from YAML
model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
model = YOLO('yolov8n.yaml').load('yolov8n.pt') # build from YAML and transfer weights
# Train the model
model.train(data='coco128.yaml', epochs=100, imgsz=640)
训练可传参数
二 预测
inputs = [img, img] # list of numpy arrays
results = model(inputs) # list of Results objects
for result in results:
boxes = result.boxes # Boxes object for bbox outputs
masks = result.masks # Masks object for segmentation masks outputs
probs = result.probs # Class probabilities for classification outputs
results = model.predict(source=r"D:\dataset\Test\images\val", save=True,batch=1)
预测可传数据源
预测可传参数
三 验证评估
from ultralytics import YOLO
# Load a model
model = YOLO('yolov8n.pt') # load an official model
model = YOLO('path/to/best.pt') # load a custom model
# Validate the model
metrics = model.val() # no arguments needed, dataset and settings remembered
metrics.box.map # map50-95
metrics.box.map50 # map50
metrics.box.map75 # map75
metrics.box.maps # a list contains map50-95 of each category
评估可传参数
四 导出
from ultralytics import YOLO
# Load a model
model = YOLO('yolov8n.pt') # load an official model
model = YOLO('path/to/best.pt') # load a custom trained
# Export the model
model.export(format='onnx')
导出可传参数
# Map head to model, trainer, validator, and predictor classes
TASK_MAP = {
'classify': [
ClassificationModel, yolo.v8.classify.ClassificationTrainer, yolo.v8.classify.ClassificationValidator,
yolo.v8.classify.ClassificationPredictor],
'detect': [
DetectionModel, yolo.v8.detect.DetectionTrainer, yolo.v8.detect.DetectionValidator,
yolo.v8.detect.DetectionPredictor],
'segment': [
SegmentationModel, yolo.v8.segment.SegmentationTrainer, yolo.v8.segment.SegmentationValidator,
yolo.v8.segment.SegmentationPredictor],
'pose': [PoseModel, yolo.v8.pose.PoseTrainer, yolo.v8.pose.PoseValidator, yolo.v8.pose.PosePredictor]}
根据task选择
self.trainer = TASK_MAP[self.task][1](overrides=overrides, _callbacks=self.callbacks,algObj=algObj)