【深度学习】安全帽检测,目标检测,yolov10算法,yolov10训练

文章目录

寻求帮助请看这里:

cpp 复制代码
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2

一、数据集

安全帽佩戴检测

数据集:https://github.com/njvisionpower/Safety-Helmet-Wearing-Dataset

基准模型:

二、yolov10介绍

听说过yolov10吗:https://www.jiqizhixin.com/articles/2024-05-28-7

论文:

https://arxiv.org/abs/2405.14458

代码:

https://github.com/THU-MIG/yolov10

三、数据voc转换为yolo

调整一下,整成这样:

cpp 复制代码
VOC2028 # tree -L 1
.
├── images
├── labels
├── test.txt
├── train.txt
├── trainval.txt
└── val.txt

2 directories, 4 files

写为绝对路径:

cpp 复制代码
# 定义需要处理的文件名列表
file_names = ['test.txt', 'train.txt', 'trainval.txt', 'val.txt']

for file_name in file_names:
    # 打开文件用于读取
    with open(file_name, 'r') as file:
        # 读取所有行
        lines = file.readlines()
    
    # 打开(或创建)另一个文件用于写入修改后的内容,这里使用新的文件名表示已修改
    new_file_name = 'modified_' + file_name
    with open(new_file_name, 'w') as new_file:
        # 遍历每一行并进行修改
        for line in lines:
            # 删除行尾的换行符,添加'.jpg'和'images/',然后再添加回换行符
            modified_line = '/ssd/xiedong/yolov10/VOC2028/images/' + line.strip() + '.jpg\n'
            # 将修改后的内容写入新文件
            new_file.write(modified_line)

print("所有文件处理完成。")

转yolo txt:

python 复制代码
import traceback
import xml.etree.ElementTree as ET
import os
import shutil
import random
import cv2
import numpy as np
from tqdm import tqdm


def convert_annotation_to_list(xml_filepath, size_width, size_height, classes):
    in_file = open(xml_filepath, encoding='UTF-8')
    tree = ET.parse(in_file)
    root = tree.getroot()
    # size = root.find('size')
    # size_width = int(size.find('width').text)
    # size_height = int(size.find('height').text)
    yolo_annotations = []
    # if size_width == 0 or size_height == 0:
    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in classes:
            classes.append(cls)
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = [float(xmlbox.find('xmin').text),
             float(xmlbox.find('xmax').text),
             float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text)]

        # 标注越界修正
        if b[1] > size_width:
            b[1] = size_width
        if b[3] > size_height:
            b[3] = size_height

        txt_data = [((b[0] + b[1]) / 2.0) / size_width, ((b[2] + b[3]) / 2.0) / size_height,
                    (b[1] - b[0]) / size_width, (b[3] - b[2]) / size_height]
        # 标注越界修正
        if txt_data[0] > 1:
            txt_data[0] = 1
        if txt_data[1] > 1:
            txt_data[1] = 1
        if txt_data[2] > 1:
            txt_data[2] = 1
        if txt_data[3] > 1:
            txt_data[3] = 1
        yolo_annotations.append(f"{cls_id} {' '.join([str(round(a, 6)) for a in txt_data])}")

    in_file.close()
    return yolo_annotations


def main():
    classes = []

    root = r"/ssd/xiedong/yolov10/VOC2028"
    img_path_1 = os.path.join(root, "images")
    xml_path_1 = os.path.join(root, "labels")

    dst_yolo_root_txt = xml_path_1

    index = 0
    img_path_1_files = os.listdir(img_path_1)
    xml_path_1_files = os.listdir(xml_path_1)
    for img_id in tqdm(img_path_1_files):
        # 右边的.之前的部分
        xml_id = img_id.split(".")[0] + ".xml"
        if xml_id in xml_path_1_files:
            try:
                img = cv2.imdecode(np.fromfile(os.path.join(img_path_1, img_id), dtype=np.uint8), 1)  # img是矩阵
                new_txt_name = img_id.split(".")[0] + ".txt"
                yolo_annotations = convert_annotation_to_list(os.path.join(xml_path_1, img_id.split(".")[0] + ".xml"),
                                                              img.shape[1],
                                                              img.shape[0],
                                                              classes)
                with open(os.path.join(dst_yolo_root_txt, new_txt_name), 'w') as f:
                    f.write('\n'.join(yolo_annotations))
            except:
                traceback.print_exc()

    # classes
    print(f"我已经完成转换 {classes}")


if __name__ == '__main__':
    main()

vim voc2028x.yaml

cpp 复制代码
train: /ssd/xiedong/yolov10/VOC2028/modified_train.txt
val: /ssd/xiedong/yolov10/VOC2028/modified_val.txt
test: /ssd/xiedong/yolov10/VOC2028/modified_test.txt

# Classes
names:
  0: hat
  1: person

四、训练

环境:

cpp 复制代码
git clone https://github.com/THU-MIG/yolov10.git
cd yolov10
conda create -n yolov10 python=3.9 -y
conda activate yolov10
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
pip install -e . -i https://pypi.tuna.tsinghua.edu.cn/simple some-package

训练

cpp 复制代码
yolo detect train data="/ssd/xiedong/yolov10/voc2028x.yaml" model=yolov10s.yaml epochs=200 batch=64 imgsz=640 device=1,3

训练启动后:

训练完成后:

五、验证

cpp 复制代码
yolo val model="/ssd/xiedong/yolov10/runs/detect/train2/weights/best.pt" data="/ssd/xiedong/yolov10/voc2028x.yaml" batch=32 imgsz=640 device=1,3

map50平均达到0.94,已超出基准很多了。

预测:

cpp 复制代码
yolo predict model=yolov10n/s/m/b/l/x.pt

导出:

cpp 复制代码
# End-to-End ONNX
yolo export model=yolov10n/s/m/b/l/x.pt format=onnx opset=13 simplify
# Predict with ONNX
yolo predict model=yolov10n/s/m/b/l/x.onnx

# End-to-End TensorRT
yolo export model=yolov10n/s/m/b/l/x.pt format=engine half=True simplify opset=13 workspace=16
# Or
trtexec --onnx=yolov10n/s/m/b/l/x.onnx --saveEngine=yolov10n/s/m/b/l/x.engine --fp16
# Predict with TensorRT
yolo predict model=yolov10n/s/m/b/l/x.engine

demo:

cpp 复制代码
wget https://github.com/THU-MIG/yolov10/releases/download/v1.1/yolov10s.pt
python app.py
# Please visit http://127.0.0.1:7860

六、数据、模型、训练后的所有文件

yolov10训练安全帽目标监测全部东西,下载看这里:

cpp 复制代码
https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2


相关推荐
喵~来学编程啦8 分钟前
【论文精读】LPT: Long-tailed prompt tuning for image classification
人工智能·深度学习·机器学习·计算机视觉·论文笔记
-Nemophilist-2 小时前
机器学习与深度学习-1-线性回归从零开始实现
深度学习·机器学习·线性回归
羊小猪~~6 小时前
神经网络基础--什么是正向传播??什么是方向传播??
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
软工菜鸡6 小时前
预训练语言模型BERT——PaddleNLP中的预训练模型
大数据·人工智能·深度学习·算法·语言模型·自然语言处理·bert
哔哩哔哩技术7 小时前
B站S赛直播中的关键事件识别与应用
深度学习
deephub7 小时前
Tokenformer:基于参数标记化的高效可扩展Transformer架构
人工智能·python·深度学习·架构·transformer
___Dream8 小时前
【CTFN】基于耦合翻译融合网络的多模态情感分析的层次学习
人工智能·深度学习·机器学习·transformer·人机交互
极客代码8 小时前
【Python TensorFlow】入门到精通
开发语言·人工智能·python·深度学习·tensorflow
王哈哈^_^9 小时前
【数据集】【YOLO】【VOC】目标检测数据集,查找数据集,yolo目标检测算法详细实战训练步骤!
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·pyqt
是瑶瑶子啦9 小时前
【深度学习】论文笔记:空间变换网络(Spatial Transformer Networks)
论文阅读·人工智能·深度学习·视觉检测·空间变换