YOLOv7(目标检测)数据集、训练、推理过程

项目地址https://github.com/WongKinYiu/yolov7

一、环境安装:

pip install -r requirements.txt
pip install torch==1.8.2+cu111 torchvision==0.9.2+cu111 torchaudio===0.8.2 -f https://download.pytorch.org/whl/lts/1.8/torch_lts.html -i  https://pypi.tuna.tsinghua.edu.cn/simple

二、修改配置环境

直接看下面链接的“四.Train(训练)”:

修改配置的时候“train” 和“val”设置为一样的就行

YOLOv7(目标检测)入门教程详解---检测,推理,训练_螺丝工人的博客-CSDN博客

这里的train的cmd我使用的是:

python train.py --workers 0 --device 0 --batch-size 16 --data data/mol.yaml --img 256 256 --cfg cfg/training/mol.yaml --weights '' --name yolov7_mol --hyp data/hyp.scratch.p5.yaml
python train.py --workers 0 --device 0 --batch-size 16 --data data/mol.yaml --img 256 256 --cfg cfg/training/mol.yaml --weights weights/yolov7_training.pt --name yolov7_mol --hyp data/hyp.scratch.p5.yaml

其中

train.txt  val.txt,这两个文件分为写入所有images中train和val中的照片路径:

使用这个代码就可以了:

# -*- coding: utf-8 -*-
# 生成文件夹中所有文件的路径到txt
import os


def listdir(path, list_name):  # 传入存储的list
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            listdir(file_path, list_name)
        else:
            list_name.append(file_path)


list_name = []
path = 'D:/Pycharm_workspace/molec_recognition/yolov7/datasets/molecular/images/train'  # 文件夹路径
listdir(path, list_name)
print(list_name)

with open('./train_list.txt', 'w') as f:  # 要存入的txt
    write = ''
    for i in list_name:
        write = write + str(i) + '\n'
    f.write(write)

1)标签工具的使用:

目标检测使用LabelImg标注VOC数据格式和YOLO数据格式——LabelImg使用详细教程_点亮~黑夜的博客-CSDN博客_labelimg yolo格式

直接看“2 LabelImg的使用

注意:第一次我直接点的文件夹啥的这种操作,第二次的时候却出现了“IndexError: list index out of range”错误:

这里就直接使用命令打开就不出错了:

labelimg D:/Pycharm_workspace/molec_recognition/yolov7/datasets/molecular/images/train D:/Pycharm_workspace/molec_recognition/yolov7/datasets/molecular/labels/train/classes.txt

三、推理

python detect.py --weights weights/best.pt --source datasets/molecular/test_mol_images --device 0

一些其他问题

我第一次标注了数据集后,然后修改了下面的文件,同时也修改了配置上的文件路径,但是第二次训练的时候我把这些全都换了,却还是按照第一次训练的时候的数据集训练,

后来我才发现原来是这些cache的问题,你更换数据源以及配置的时候,要把相应的datasets下面的cache文件删除,这样才可以按照新的数据集进行训练

YOLOv7(目标检测)入门教程详解---检测,推理,训练_螺丝工人的博客-CSDN博客

猜你喜欢

转载自blog.csdn.net/weixin_43135178/article/details/128127666