YOLO-V5 代码拉取到运行

YOLO-v5 步骤

1. 拉取代码github :
https://gitcode.com/gh_mirrors/yo/yolov5/tree/v5.0?utm_source=csdn_github_accelerator&isLogin=1

在这里插入图片描述

2.创建文件权重文件夹

将v5权重的文件下载放到里面,下载地址和拉取代码地址一样,在拉取代码下面【往下滑动有图片】

在这里插入图片描述
在这里插入图片描述

3. 将训练数据放到代码中:

训练接的文件夹的格式如图:

Annotations 这个是labelimg 打的xml标签

JPEGImages 这个是labelimg 打标签的图片
在这里插入图片描述

下面这段代码是 将yolo打的标签的xml 进行转化成txt 形式。
下面代码是按照 打标签的 文件夹的 格式,直接运行即可。

import xml.etree.ElementTree as ET
import os
from os import listdir, getcwd
from os.path import join
import random
from shutil import copyfile
# VOC类别名称映射到数字019
# class_names = {
#     'aeroplane': 0, 'bicycle': 1, 'bird': 2, 'boat': 3, 'bottle': 4,
#     'bus': 5, 'car': 6, 'cat': 7, 'chair': 8, 'cow': 9,
#     'diningtable': 10, 'dog': 11, 'horse': 12, 'motorbike': 13,
#     'person': 14, 'pottedplant': 15, 'sheep': 16, 'sofa': 17,
#     'train': 18, 'tvmonitor': 19
# }
class_names={'hat':0,'person':1}
TRAIN_RATIO = 80  # 训练集和测试集的比例为 8:2

def clear_hidden_files(path):
    """
    清除隐藏文件
    :param path: 文件路径
    """
    dir_list = os.listdir(path)
    for i in dir_list:
        abspath = os.path.join(os.path.abspath(path), i)
        if os.path.isfile(abspath):
            if i.startswith("._"):
                os.remove(abspath)
        else:
            clear_hidden_files(abspath)

def convert(size, box):
    """
    将标注框从 VOC 格式转换为 YOLO 格式
    :param size: 图像的宽和高
    :param box: 标注框的位置 (xmin, xmax, ymin, ymax)
    :return: 转换后的标注框 (x, y, w, h)
    """
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)

def convert_annotation(image_id):
    """
    将单个图像的 XML 标注文件转换为 YOLO 格式的 txt 文件
    :param image_id: 图像ID(文件名)
    """
    in_file = open('VOCdevkit/VOC2007/Annotations/%s.xml' % image_id)
    out_file = open('VOCdevkit/VOC2007/YOLOLabels/%s.txt' % image_id, 'w')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    for obj in root.iter('object'):
        difficult = obj.find('difficult').text
        cls = obj.find('name').text
        if cls not in class_names or int(difficult) == 1:
            continue
        cls_id = class_names[cls]  # 使用字典查找类别的 ID
        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))
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    in_file.close()
    out_file.close()

wd = os.getcwd()
data_base_dir = os.path.join(wd, "VOCdevkit/")
if not os.path.isdir(data_base_dir):
    os.mkdir(data_base_dir)
work_space_dir = os.path.join(data_base_dir, "VOC2007/")
if not os.path.isdir(work_space_dir):
    os.mkdir(work_space_dir)
annotation_dir = os.path.join(work_space_dir, "Annotations/")
if not os.path.isdir(annotation_dir):
    os.mkdir(annotation_dir)
clear_hidden_files(annotation_dir)
image_dir = os.path.join(work_space_dir, "JPEGImages/")
if not os.path.isdir(image_dir):
    os.mkdir(image_dir)
clear_hidden_files(image_dir)
yolo_labels_dir = os.path.join(work_space_dir, "YOLOLabels/")
if not os.path.isdir(yolo_labels_dir):
    os.mkdir(yolo_labels_dir)
clear_hidden_files(yolo_labels_dir)
yolov5_images_dir = os.path.join(data_base_dir, "images/")
if not os.path.isdir(yolov5_images_dir):
    os.mkdir(yolov5_images_dir)
clear_hidden_files(yolov5_images_dir)
yolov5_labels_dir = os.path.join(data_base_dir, "labels/")
if not os.path.isdir(yolov5_labels_dir):
    os.mkdir(yolov5_labels_dir)
clear_hidden_files(yolov5_labels_dir)
yolov5_images_train_dir = os.path.join(yolov5_images_dir, "train/")
if not os.path.isdir(yolov5_images_train_dir):
    os.mkdir(yolov5_images_train_dir)
clear_hidden_files(yolov5_images_train_dir)
yolov5_images_test_dir = os.path.join(yolov5_images_dir, "val/")
if not os.path.isdir(yolov5_images_test_dir):
    os.mkdir(yolov5_images_test_dir)
clear_hidden_files(yolov5_images_test_dir)
yolov5_labels_train_dir = os.path.join(yolov5_labels_dir, "train/")
if not os.path.isdir(yolov5_labels_train_dir):
    os.mkdir(yolov5_labels_train_dir)
clear_hidden_files(yolov5_labels_train_dir)
yolov5_labels_test_dir = os.path.join(yolov5_labels_dir, "val/")
if not os.path.isdir(yolov5_labels_test_dir):
    os.mkdir(yolov5_labels_test_dir)
clear_hidden_files(yolov5_labels_test_dir)

train_file = open(os.path.join(wd, "yolov5_train.txt"), 'w')
test_file = open(os.path.join(wd, "yolov5_val.txt"), 'w')
train_file.close()
test_file.close()
train_file = open(os.path.join(wd, "yolov5_train.txt"), 'a')
test_file = open(os.path.join(wd, "yolov5_val.txt"), 'a')
list_imgs = os.listdir(image_dir)  # 列出所有图像文件
for i in range(0, len(list_imgs)):
    path = os.path.join(image_dir, list_imgs[i])
    if os.path.isfile(path):
        image_path = image_dir + list_imgs[i]
        voc_path = list_imgs[i]
        (nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
        annotation_name = nameWithoutExtention + '.xml'
        annotation_path = os.path.join(annotation_dir, annotation_name)
        label_name = nameWithoutExtention + '.txt'
        label_path = os.path.join(yolo_labels_dir, label_name)
        prob = random.randint(1, 100)
        print("Probability: %d" % prob)
        if(prob < TRAIN_RATIO):  # 训练集
            if os.path.exists(annotation_path):
                train_file.write(image_path + '\n')
                convert_annotation(nameWithoutExtention)  # 转换标签
                copyfile(image_path, yolov5_images_train_dir + voc_path)
                copyfile(label_path, yolov5_labels_train_dir + label_name)
        else:  # 测试集
            if os.path.exists(annotation_path):
                test_file.write(image_path + '\n')
                convert_annotation(nameWithoutExtention)  # 转换标签
                copyfile(image_path, yolov5_images_test_dir + voc_path)
                copyfile(label_path, yolov5_labels_test_dir + label_name)
train_file.close()
test_file.close()

运行完成之后会出现两个文件夹–两个文件夹里面有 train集 、val 集:
在这里插入图片描述

4、配置yaml

path train val test 填写数据集 路径即可。
在这里插入图片描述

5、开始训练数据集
修改我们的权重文件、模型文件、数据文件路径。

在这里插入图片描述

6、train.py中错误:

在这里插入图片描述
将这个检查包的去掉即可

出现版本过高的问题在这里插入图片描述
现在用的版本是75.1.0在这里插入图片描述
解决办法:

//卸载以前的版本
pip uninstall setuptools  
//安装较低的版本

pip install setuptools==59.5.0  -i https://pypi.tuna.tsinghua.edu.cn/simple

在这里插入图片描述

出现git 没有安装

//解决办法
pip install gitpython

在这里插入图片描述

可以正常跑起来
在这里插入图片描述

7、训练模型验证

在这里插入图片描述

8、推理

在这里插入图片描述
存到到下面的推理出来的存放到下面的路径中
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值