超详细||YOLOv8基础教程(环境搭建,训练,测试,部署看一篇就够)(在推理视频中添加FPS信息)

一、YOLOv8环境搭建

这篇文章将跳过基础的深度学习环境的搭建,如果没有完成的可以看我的这篇博客:超详细||深度学习环境搭建记录cuda+anaconda+pytorch+pycharm-CSDN博客

1. 在github上下载源码:

GitHub - ultralytics/ultralytics: NEW - YOLOv8 🚀 in PyTorch > ONNX > OpenVINO > CoreML > TFLite

2. 安装ultralytics(YOLOv8改名为ultralytics)

这里有两种方式安装ultralytics

  • 直接使用CLI
pip install ultralytics
  • 使用requirements.txt安装,这种方法是在上面下载的源码处安装,方便对yolov8进行改进
cd ultralytics
pip install -r requirements.txt

3. 安装wandb

pip install wandb

登录自己的wandb账号

wandb login

二、开始训练

1. 构建数据集

数据集要严格按照下面的目录格式,image的格式为jpg,label的格式为txt,对应的image和label的名字要一致

Dataset
	└─images
	   └─train
       └─val
	└─labels
	   └─train
       └─val

2. 创建一个dataset.yaml文件

更换自己的image train和image val的地址,labels地址不用,它会自动索引

将classes改为自己的类别,从0开始

path: ../datasets/coco128  # dataset root dir
train: images/train2017  # train images (relative to 'path') 128 images
val: images/train2017  # val images (relative to 'path') 128 images
test:  # test images (optional)

# Classes
names:
  0: person
  1: bicycle
  2: car
  3: motorcycle
  4: airplane
  5: bus
  6: train
  7: truck
  8: boat

3. 新建一个train.py,修改相关参数,运行即可开始训练

from ultralytics import YOLO

if __name__ == '__main__':
    # Load a model
    model = YOLO(r'\ultralytics\detection\yolov8n\yolov8n.yaml')  # 不使用预训练权重训练
    # model = YOLO(r'yolov8p.yaml').load("yolov8n.pt")  # 使用预训练权重训练
    # Trainparameters ----------------------------------------------------------------------------------------------
    model.train(
        data=r'\ultralytics\detection\dataset\appledata.yaml',
        epochs= 30 , # (int) number of epochs to train for
        patience= 50 , # (int) epochs to wait for no observable improvement for early stopping of training
        batch= 8 , # (int) number of images per batch (-1 for AutoBatch)
        imgsz= 320 , # (int) size of input images as integer or w,h
        save= True , # (bool) save train checkpoints and predict results
        save_period= -1, # (int) Save checkpoint every x epochs (disabled if < 1)
        cache= False , # (bool) True/ram, disk or False. Use cache for data loading
        device= 0 , # (int | str | list, optional) device to run on, i.e. cuda device=0 or device=0,1,2,3 or device=cpu
        workers= 16 , # (int) number of worker threads for data loading (per RANK if DDP)
        project= 'result', # (str, optional) project name
        name= 'yolov8n' ,# (str, optional) experiment name, results saved to 'project/name' directory
        exist_ok= False , # (bool) whether to overwrite existing experiment
        pretrained= False , # (bool | str) whether to use a pretrained model (bool) or a model to load weights from (str)
        optimizer= 'SGD',  # (str) optimizer to use, choices=[SGD, Adam, Adamax, AdamW, NAdam, RAdam, RMSProp, auto]
        verbose= True ,# (bool) whether to print verbose output
        seed= 0 , # (int) random seed for reproducibility
        deterministic= True , # (bool) whether to enable deterministic mode
        single_cls= True , # (bool) train multi-class data as single-class
        rect= False  ,# (bool) rectangular training if mode='train' or rectangular validation if mode='val'
        cos_lr= False , # (bool) use cosine learning rate scheduler
        close_mosaic= 0,  # (int) disable mosaic augmentation for final epochs
        resume= False , # (bool) resume training from last checkpoint
        amp= False,  # (bool) Automatic Mixed Precision (AMP) training, choices=[True, False], True runs AMP check
        fraction= 1.0 , # (float) dataset fraction to train on (default is 1.0, all images in train set)
        profile= False,  # (bool) profile ONNX and TensorRT speeds during training for loggers
        # Segmentation
        overlap_mask= True , # (bool) masks should overlap during training (segment train only)
        mask_ratio= 4,  # (int) mask downsample ratio (segment train only)
        # Classification
        dropout= 0.0,  # (float) use dropout regularization (classify train only)
        # Hyperparameters ----------------------------------------------------------------------------------------------
        lr0=0.01,  # (float) initial learning rate (i.e. SGD=1E-2, Adam=1E-3)
        lrf=0.01,  # (float) final learning rate (lr0 * lrf)
        momentum=0.937,  # (float) SGD momentum/Adam beta1
        weight_decay=0.0005,  # (float) optimizer weight decay 5e-4
        warmup_epochs=3.0,  # (float) warmup epochs (fractions ok)
        warmup_momentum=0.8,  # (float) warmup initial momentum
        warmup_bias_lr=0.1,  # (float) warmup initial bias lr
        box=7.5,  # (float) box loss gain
        cls=0.5,  # (float) cls loss gain (scale with pixels)
        dfl=1.5,  # (float) dfl loss gain
        pose=12.0,  # (float) pose loss gain
        kobj=1.0,  # (float) keypoint
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值