一、YOLOv8环境搭建

(1)Pytorch的安装

如果你的环境没有部署请参考本人文章:NLP笔记(2)——PyTorch的详细安装_安装torchnlp-CSDN博客

(2)下载最新的Yolov8-obb代码:

 https://github.com/ultralytics/ultralytics

(2)安装配置文件,建议使用镜像源

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

二、DOTA1.0数据集转换

(1)原始数据集格式如下

937.0 913.0 921.0 912.0 923.0 874.0 940.0 875.0 small-vehicle 0

(2)通过坐标在 0 和 1 之间归一化的四个角点来指定边界框,支持的 OBB 数据集格式如下

class_index, x1, y1, x2, y2, x3, y3, x4, y4

 (3)新建一个yoloobb.py文件实现标签转换

from ultralytics.data.converter import convert_dota_to_yolo_obb
convert_dota_to_yolo_obb('C:\myyolo\ultralytics-main\dataobb')
#关于dataobb文件下的目录下面会详细说明

(4)跳转到convert_dota_to_yolo_obb.py函数,对class_mapping进行修改

class_mapping = {
    "plane": 0,
    "baseball-diamond": 1,
    "bridge": 2,
    "ground-track-field": 3,
    "small-vehicle": 4,
    "large-vehicle": 5,
    "ship": 6,
    "tennis-court": 7,
    "basketball-court": 8,
    "storage-tank": 9,
    "soccer-ball-field": 10,
    "roundabout": 11,
    "harbor": 12,
    "swimming-pool": 13,
    "helicopter": 14,
}

(5)在ultralytics-main下新建一个文件夹dataobb设置如下结构,

其中,images/train和images/val分别放置DOTA数据集切割后的原始图片文件(其中train15749张,val5279张),labels/train_original和labels/val_original分别放置原始的标签文件,labels/train和labels/val为空,然后运行步骤(3)的代码,运行结束转换后的标签会保存在labels/train和labels/val中,转换后的格式如下。

4 0.915039 0.891602 0.899414 0.890625 0.901367 0.853516 0.917969 0.854492

需要放置切割后的原始图片请参考:DOTA数据集切割处理

 三、开始训练

(1)下载预训练权重

OBB - Ultralytics YOLOv8 Docs

(2)构建数据集,按照下面目录格式,其中test可为空,一定要对应。

(3)创建一个dota8-obb.yaml,然后将路径和类别改成自己的。

path: C:\myyolo\ultralytics-main\datasets # dataset root dir
train: images/train
val: images/val
#test: images/test
names:
  0: plane
  1: baseball-diamond
  2: bridge
  3: ground-track-field
  4: small-vehicle
  5: large-vehicle
  6: ship
  7: tennis-court
  8: basketball-court
  9: storage-tank
  10: soccer-ball-field
  11: roundabout
  12: harbor
  13: swimming-pool
  14: helicopter

(4)新建yolov8-obb.yaml,修改nc即可.

# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8 Oriented Bounding Boxes (OBB) model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect

# Parameters
nc: 15  # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
  # [depth, width, max_channels]
  n: [0.33, 0.25, 1024]  # YOLOv8n summary: 225 layers,  3157200 parameters,  3157184 gradients,   8.9 GFLOPs
  s: [0.33, 0.50, 1024]  # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients,  28.8 GFLOPs
  m: [0.67, 0.75, 768]   # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients,  79.3 GFLOPs
  l: [1.00, 1.00, 512]   # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPs
  x: [1.00, 1.25, 512]   # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs

# 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, OBB, [nc, 1]]  # OBB(P3, P4, P5)

(5)新建一个train.py,我使用的权重是“yolov8s-obb.pt”,设置相关参数如下,即可运行。值得注意的是:如果你使用的权重是“yolov8n-obb.pt”,只需要把下面代码中的配置文件yolov8-obbs.yaml改成yolov8n-obb.yaml,依次类推。

from ultralytics import YOLO

def main():
    model = YOLO('yolov8s-obb.yaml').load('yolov8s-obb.pt')  # build from YAML and transfer weights
    model.train(data='dota8-obb.yaml', epochs=100, imgsz=1024, batch=4, workers=4)
if __name__ == '__main__':
    main()

四、验证

from ultralytics import YOLO

def main():
    model = YOLO(r'runs/obb/train/weights/best.pt')
    model.val(data='dota8-obb.yaml', imgsz=1024, batch=4, workers=4)

    # 如果你有test就用下面的语句
    # model.val(data='dota8-obb.yaml',split='test', imgsz=1024, batch=4, workers=4)

if __name__ == '__main__':
    main()

 五、推理

from ultralytics import YOLO
model = YOLO('runs/obb/train/weights/best.pt')
results = model('datasets/images/val/P0003__1__0___0.png', save=True)

最后:

会不定期发布相关设计内容包括但不限于如下内容:信号处理、通信仿真、算法设计、matlab appdesigner,gui设计、simulink仿真......希望能帮到你!

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐