YOLO模型训练.md 2.4 KB

YOLO 采用命令训练数据集

yolo train data=coco128.yaml model=yolov8n.pt epochs=10 lr0=0.01

yolo task=detect mode=train model=yolov8x.yaml data=mydata.yaml epochs=10 batch=16

yolo task=segment mode=predict model=yolov8x-seg.pt source='/kaggle/input/personpng/1.jpg'

以上参数解释如下:

  • task:选择任务类型,可选['detect', 'segment', 'classify', 'init']
  • mode: 选择是训练、验证还是预测的任务蕾西 可选['train', 'val', 'predict']
  • model: 选择yolov8不同的模型配置文件,可选yolov8s.yaml、yolov8m.yaml、yolov8l.yaml、yolov8x.yam
  • data: 选择生成的数据集配置文件
  • epochs:指的就是训练过程中整个数据集将被迭代多少次,显卡不行你就调小点。
  • batch:一次看完多少张图片才进行权重更新,梯度下降的mini-batch,显卡不行你就调小点。

实际运行:

yolo train data=/home/jxft/datasets/hyd-action.yaml model=/home/jxft/datasets/yolov8/yolov8n.pt epochs=100 lr0=0.01

YOLO 采用代码测试数据集

from ultralytics import YOLO

# Create a new YOLO model from scratch
model = YOLO('yolov8n.yaml')
# Load a pretrained YOLO model (recommended for training)
model = YOLO('yolov8n.pt')

# Train the model using the 'coco128.yaml' dataset for 3 epochs
results = model.train(data='coco128.yaml', epochs=3)
# Evaluate the model's performance on the validation set
results = model.val()
# Perform object detection on an image using the model
results = model('https://ultralytics.com/images/bus.jpg')
# Export the model to ONNX format
success = model.export(format='onnx')

训练参数:

workers = 1
batch = 8
data_name = "TrafficSign"
model = YOLO(abs_path('./weights/yolov5nu.pt', path_type='current'), task='detect')  # 加载预训练的YOLOv8模型
results = model.train(  # 开始训练模型
    data=data_path,  # 指定训练数据的配置文件路径
    device='cpu',  # 指定使用CPU进行训练
    workers=workers,  # 指定使用2个工作进程加载数据
    imgsz=640,  # 指定输入图像的大小为640x640
    epochs=100,  # 指定训练100个epoch
    batch=batch,  # 指定每个批次的大小为8
    name='train_v5_' + data_name  # 指定训练任务的名称
)

参考