YOLO V7项目使用

时间:2024-07-06 10:58:37

YOLO V7项目使用

根据官方论文中提供的项目地址:使用git clone将项目下载到本地。

https://github.com/WongKinYiu/yolov7

在这里插入图片描述

git clone https://github.com/WongKinYiu/yolov7

使用pycharm打开项目,根据官方提供的requirement.txt文件下载项目启动所需要的环境(在yolo v7的虚拟环境下)

在这里插入图片描述

官方的md文件中给出了需要在终端中切换的目录,和安装的命令如下所示:

cd yolov7
pip install -r requirements.txt # install

存在的问题

  1. 问题一:charset_normalizer版本不匹配问题导致循环导入模块

同样在使用yolo v7项目启动时也会和v5项目一样发生类似的报错信息。我们可以发现同样是charset_normalizer这个组件发生了问题,采用和v5相同的方法更新版本信息再一次进行尝试

Traceback (most recent call last):
AttributeError: partially initialized module ‘charset_normalizer’ has no attribute ‘md__mypyc’ (most likely due to a circular import)

pip install --force-reinstall charset-normalizer==3.1.0
  1. 问题二:启动时验证不通过问题

我第一次下载的项目使用的是github上下载的压缩包文件,经过解压之后导入的pycharm,在启动验证的时候缺少git相关的文件导致启动失败。(git init初始化启动也是不可以)建议还是git clone 网址或者ssh地址

  1. 问题三:项目启动时默认还是采用的cpu版本的torch和之前yolo v5产生的错误类型相同

采用同样解决方式使用conda安装GPU(CUDA)版本的torch,之后我们在执行训练模型的文件将整个YOLO v7项目启动失败(先卸载之前pip安装的cpu版本)

后面直接使用pip命令来安装GPU版本的torch命令如下

pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

在这里插入图片描述

这是我们默认使用的就是GPU的环境。

  1. 执行训练文件时产生报错信息。(个人感觉是最大的一个问题,网上很多的资料说yolov7存在一些小的BUG建议train.py中使用绝对路径

第二个问题是数据集默认是无法下载的需要自己解压到data路径下面,我数据集就打算使用v5中使用过的coco128数据集,并参考v5的配置文件

Exception: train: Error loading data from ./coco/train2017.txt: train: coco\train2017.txt does not exist
See https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data

注意训练集中不能含有cache文件否则也会进行报错_pickle.UnpicklingError: unpickling stack underflow

执行时会新建一个cache

train: New cache created: D:\Git-res\DeepLearing\DL_01\YOLOV7\yolov7\data\datasets\coco128\labels\train2017.cache

修改步骤加入自己的数据集和配置文件(配置文件格式一定参考官方的来进行

在这里插入图片描述

# COCO 2017 dataset http://cocodataset.org

# download command/URL (optional)
download: bash ./scripts/get_coco.sh

# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: D:\\Git-res\\DeepLearing\\DL_01\\YOLOV7\\yolov7\\data\\datasets\\coco128\\images\\train2017  # 118287 images
val: D:\\Git-res\\DeepLearing\\DL_01\\YOLOV7\\yolov7\\data\\datasets\\coco128\\images\\train2017   # 5000 images
test: D:\\Git-res\\DeepLearing\\DL_01\\YOLOV7\\yolov7\\data\\datasets\\coco128\\images\\train2017   # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794

# number of classes
nc: 80

# class names
names: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
         'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
         'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
         'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
         'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
         'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
         'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
         'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
         'hair drier', 'toothbrush' ]

parser.add_argument(‘–batch-size’, type=int, default=8, help=‘total batch size for all GPUs’)

训练批次不要设置的过大否则算力不足很难跑的动

在这里插入图片描述

项目功能补充

  1. 使用网络摄像头进行实时的目标检测

更改配置项的参数设置default=‘0’ (从路径改为使用本机的第一个摄像头)

parser.add_argument('--source', type=str, default='0', help='source')
  • 注意点:提前开启摄像头权限。

在执行完成关闭程序以后会生成一个.mp4的文件

  1. YOLO v7中的拓展功能测试(关键点检测)提前手动下载所需要的yolov7-w6-pose.pt文件

    在这里插入图片描述

  2. 执行训练过程完成整个项目的使用