YOLOv8-Pose训练自己的数据集

目录

0、引言

1、环境准备

2、数据集准备

2.1、创建数据集存放的文件夹

2.2 准备用于YOLOv8-Seg训练的txt

2.2.1 将COCO数据集json格式文件转换成YOLOv8-Pose格式的txt

2.2.2 将自己标注的数据转换成YOLOv8-Pose格式的txt

3、创建配置文件

3.1、设置myposedata.yaml

3.2、设置yolov8s-pose.yaml

4、进行训练

5、验证模型

6、总结


0、引言

本文是使用YOLOv8-Pose训练自己的数据集,数据集包含COCO数据集以及自己标注的人体姿态数据集。

1、环境准备

可以参考这篇博客:深度学习环境搭建-CSDN博客

本文环境:

  • Windows10
  • python:3.10
  • cuda:11.6
  • pytorch:1.12.0
  • torchvision:0.13.0

2、数据集准备

2.1、创建数据集存放的文件夹

Posedata
______images
____________train
_________________001.jpg
____________val
_________________002.jpg
______labels
____________train
_________________001.txt
____________val
_________________002.txt   

本人的数据都存放在Posedata文件夹中(自定义命名)

目录结构如下:images存放训练集和验证集图片,labels存放训练集和验证集txt

2.2 准备用于YOLOv8-Seg训练的txt

2.2.1 将COCO数据集json格式文件转换成YOLOv8-Pose格式的txt

从官网下载CoCo数据集的json文件

具体步骤参考我的这篇博客:将CoCo数据集Json格式转成训练Yolov8-Pose姿态的txt格式-CSDN博客

2.2.2 将自己标注的数据转换成YOLOv8-Pose格式的txt

具体步骤参考我的这篇博客:将labelme标注的人体姿态Json文件转成训练Yolov8-Pose的txt格式-CSDN博客

将COCO转化得到的数据和自己的数据集合并即可,这样就得到了可用于训练的数据,train中存放训练数据,val存放验证集。

3、创建配置文件

3.1、设置myposedata.yaml

根据自己的数据集位置进行修改和配置

# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: G:\Yolov8\ultralytics-main\datasets\myposedata\Posedata  # dataset root dir
train: images/train  # train images (relative to 'path') 4 images
val: images/val  # val images (relative to 'path') 4 images
test:  # test images (optional)

# Keypoints
kpt_shape: [17, 3]  # number of keypoints, number of dims (2 for x,y or 3 for x,y,visible)
flip_idx: [0, 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 16, 15]

# Classes
names:
  0: person


3.2、设置yolov8s-pose.yaml

根据自己想使用的权重进行选择,我这里采用的是yolov8s-pose.pt进行训练。

# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8-pose keypoints/pose estimation model. For Usage examples see https://docs.ultralytics.com/tasks/pose

# Parameters
nc: 1  # number of classes
kpt_shape: [17, 3]  # number of keypoints, number of dims (2 for x,y or 3 for x,y,visible)
scales: # model compound scaling constants, i.e. 'model=yolov8n-pose.yaml' will call yolov8-pose.yaml with scale 'n'
  # [depth, width, max_channels]
  s: [0.33, 0.50, 1024]

# YOLOv8.0n backbone
backbone:
  # [from, repeats, module, args]
  - [-1, 1, Conv, [64, 3, 2]]  # 0-P1/2
  - [-1, 1, Conv, [128, 3, 2]]  # 1-P2/4
  - [-1, 3, C2f, [128, True]]
  - [-1, 1, Conv, [256, 3, 2]]  # 3-P3/8
  - [-1, 6, C2f, [256, True]]
  - [-1, 1, Conv, [512, 3, 2]]  # 5-P4/16
  - [-1, 6, C2f, [512, True]]
  - [-1, 1, Conv, [1024, 3, 2]]  # 7-P5/32
  - [-1, 3, C2f, [1024, True]]
  - [-1, 1, SPPF, [1024, 5]]  # 9

# YOLOv8.0n head
head:
  - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  - [[-1, 6], 1, Concat, [1]]  # cat backbone P4
  - [-1, 3, C2f, [512]]  # 12

  - [-1, 1, nn.Upsample, [None, 2, 'nearest']]
  - [[-1, 4], 1, Concat, [1]]  # cat backbone P3
  - [-1, 3, C2f, [256]]  # 15 (P3/8-small)

  - [-1, 1, Conv, [256, 3, 2]]
  - [[-1, 12], 1, Concat, [1]]  # cat head P4
  - [-1, 3, C2f, [512]]  # 18 (P4/16-medium)

  - [-1, 1, Conv, [512, 3, 2]]
  - [[-1, 9], 1, Concat, [1]]  # cat head P5
  - [-1, 3, C2f, [1024]]  # 21 (P5/32-large)

  - [[15, 18, 21], 1, Pose, [nc, kpt_shape]]  # Pose(P3, P4, P5)

4、进行训练

上述步骤完成后,即可开始训练。设置训练的轮数epochs,我这里设置为100。

from ultralytics import YOLO
 
if __name__ == '__main__':
 
    model = YOLO('yolov8s-pose.yaml')  # load a pretrained model (recommended for training)
    # Train the model
    model.train(data='myposedata.yaml')

也可以不使用yaml文件,直接读取.pt文件






from ultralytics import YOLO

if __name__ == '__main__':
    modelpath = r'G:\Yolov8\yolov8-pose-pt\yolov8s-pose.pt'

    model = YOLO(modelpath)  # load a pretrained model (recommended for training)
    # Train the model
    yamlpath = r'G:\Yolov8\ultralytics-main\yolov8-pose\myposedata.yaml'
    model.train(epochs=100,data=yamlpath)

训练过程,我这里为了测试能否训练起来,训练数据较少,设置的训练epochs=10:

训练过程中会保存以下内容,最后得到两个模型分别是:best.pt、last.pt

5、验证模型

训练进程完毕以后可使用一些验证数据进行模型验证,查看模型的分割效果。

from ultralytics import YOLO
import glob
import os
# Load a model
model = YOLO(r'G:\Yolov8\yolov8-pose-pt\best.pt')  # load an official model

# Predict with the model
imgpath = r'G:\Yolov8\ultralytics-main\yolov8-pose\testimgs'
imgs = glob.glob(os.path.join(imgpath,'*.jpg'))
for img in imgs:
    model.predict(img, save=True)

6、总结

至此,整个YOLOv8-Pose模型训练预测阶段完成。此过程同样可以在linux系统上进行,在数据准备过程中需要仔细,保证最后得到的数据准确,最好是用显卡进行训练。

有问题评论区见!

  • 38
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 59
    评论
评论 59
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Mamba24

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值