【环境配置】超详细,YOLOV7训练自己的数据集

🍧 前置芝士

自己的数据集,已经转换成yolo的格式了。

整体配置树结构

└── yolov7
    ├── datasets
    │   └── VisDrone
    │       ├── train
    │       │   ├── images
    │       │   ├── labels
    │       │   └── labels.cache
    │       └── val
    │           ├── images
    │           ├── labels
    │           └── labels.cache
    └── yolov7-master
        ├── cfg
        │   ├── baseline
        │   │   ├── r50-csp.yaml
        │   │   ├── x50-csp.yaml
        │   │   ├── yolor-csp-x.yaml
        │   │   ├── yolor-csp.yaml
        │   │   ├── yolor-d6.yaml
        │   │   ├── yolor-e6.yaml
        │   │   ├── yolor-p6.yaml
        │   │   ├── yolor-w6.yaml
        │   │   ├── yolov3-spp.yaml
        │   │   ├── yolov3.yaml
        │   │   └── yolov4-csp.yaml
        │   ├── deploy
        │   │   ├── yolov7-d6.yaml
        │   │   ├── yolov7-e6e.yaml
        │   │   ├── yolov7-e6.yaml
        │   │   ├── yolov7-tiny-silu.yaml
        │   │   ├── yolov7-tiny.yaml
        │   │   ├── yolov7-w6.yaml
        │   │   ├── yolov7x.yaml
        │   │   └── yolov7.yaml
        │   └── training
        │       ├── yolov7d6.yaml
        │       ├── yolov7e6e.yaml
        │       ├── yolov7e6.yaml
        │       ├── yolov7-tiny.yaml
        │       ├── yolov7w6.yaml
        │       ├── yolov7x.yaml
        │       └── yolov7.yaml
        ├── data
        │   ├── coco.yaml
        │   ├── hyp.scratch.p5.yaml
        │   ├── hyp.scratch.p6.yaml
        │   ├── hyp.scratch.tiny.yaml
        │   └── VisDrone.yaml
        ├── detect.py
        ├── figure
        │   └── performance.png
        ├── hubconf.py
        ├── inference
        │   └── images
        │       └── horses.jpg
        ├── LICENSE.md
        ├── models
        │   ├── common.py
        │   ├── experimental.py
        │   ├── export.py
        │   ├── __init__.py
        │   ├── __pycache__
        │   │   ├── common.cpython-36.pyc
        │   │   ├── common.cpython-37.pyc
        │   │   ├── experimental.cpython-36.pyc
        │   │   ├── experimental.cpython-37.pyc
        │   │   ├── __init__.cpython-36.pyc
        │   │   ├── __init__.cpython-37.pyc
        │   │   ├── yolo.cpython-36.pyc
        │   │   └── yolo.cpython-37.pyc
        │   └── yolo.py
        ├── __pycache__
        │   ├── test.cpython-36.pyc
        │   └── test.cpython-37.pyc
        ├── README.md
        ├── requirements.txt
        ├── runs
        │   └── train
        │       └── yolov7
        ├── scripts
        │   └── get_coco.sh
        ├── test.py
        ├── train.py
        └── utils
            ├── activations.py
            ├── autoanchor.py
            ├── aws
            │   ├── __init__.py
            │   ├── mime.sh
            │   ├── resume.py
            │   └── userdata.sh
            ├── datasets.py
            ├── general.py
            ├── google_app_engine
            │   ├── additional_requirements.txt
            │   ├── app.yaml
            │   └── Dockerfile
            ├── google_utils.py
            ├── __init__.py
            ├── loss.py
            ├── metrics.py
            ├── plots.py
            ├── __pycache__
            │   ├── autoanchor.cpython-36.pyc
            │   ├── autoanchor.cpython-37.pyc
            │   ├── datasets.cpython-36.pyc
            │   ├── datasets.cpython-37.pyc
            │   ├── general.cpython-36.pyc
            │   ├── general.cpython-37.pyc
            │   ├── google_utils.cpython-36.pyc
            │   ├── google_utils.cpython-37.pyc
            │   ├── __init__.cpython-36.pyc
            │   ├── __init__.cpython-37.pyc
            │   ├── loss.cpython-36.pyc
            │   ├── loss.cpython-37.pyc
            │   ├── metrics.cpython-36.pyc
            │   ├── metrics.cpython-37.pyc
            │   ├── plots.cpython-36.pyc
            │   ├── plots.cpython-37.pyc
            │   ├── torch_utils.cpython-36.pyc
            │   └── torch_utils.cpython-37.pyc
            ├── torch_utils.py
            └── wandb_logging
                ├── __init__.py
                ├── log_dataset.py
                ├── __pycache__
                └── wandb_utils.py

第一步,安装requirements

git clone https://github.com/WongKinYiu/yolov7.git
cd yolov7-main
pip install -r requirements.txt

第二步,修改模型配置文件

├── cfg
│   └── training
│       ├── yolov7d6.yaml
│       ├── yolov7e6e.yaml
│       ├── yolov7e6.yaml
│       ├── yolov7-tiny.yaml
│       ├── yolov7w6.yaml
│       ├── yolov7x.yaml
│       └── yolov7.yaml

yolov7文件夹下面的cfg文件夹下面的training文件夹下面,选择自己想要训练的模型配置。以yolov7.yaml配置文件为例,只需要更改nc。以训练VisDrone数据集为例,VisDrone数据集目标有10类,所以nc修改为10.

# parameters
nc: 10  # number of classes
depth_multiple: 1.0  # model depth multiple
width_multiple: 1.0  # layer channel multiple

第三步,修改数据集配置文件

├── data
│   ├── coco.yaml
│   ├── hyp.scratch.p5.yaml
│   ├── hyp.scratch.p6.yaml
│   ├── hyp.scratch.tiny.yaml
│   └── VisDrone.yaml

以VisDrone数据集为例,新建一个自己的数据集yaml配置文件。里面主要涉及训练路径数据集类别数量以及每一类的名称

  • train: 更改为自己的训练集路径
  • val: 更改为自己的验证集路径
  • nc: 更改为自己的数据集类别数量
  • names: 更改为自己的数据集类别信息。
  • train: ../datasets/VisDrone/train/images  # train images (relative to 'path')  6471 images
    val: ../datasets/VisDrone/val/images  # val images (relative to 'path')  548 images
    
    # Classes
    nc: 10  # number of classes
    names: ['pedestrian', 'people', 'bicycle', 'car', 'van', 'truck', 'tricycle', 'awning-tricycle', 'bus', 'motor']
    

    训练命令

    单卡

    --data:更改为自己的数据集配置文件路径
    --cfg:更改为自己的模型配置文件路径

    python train.py --workers 8 --device 0 --batch-size 32 --data data/coco.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml
    

    多卡

    python -m torch.distributed.launch --nproc_per_node 4 --master_port 9527 train.py --workers 8 --device 0,1,2,3 --sync-bn --batch-size 128 --data data/coco.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml
    

    验证

    --data:更改为自己的数据集配置文件路径
    --weights: 更改为自己的训练权重路径
    --name:更改为自己想取的名字路径

    python test.py --data data/coco.yaml --img 640 --batch 32 --conf 0.001 --iou 0.65 --device 0 --weights yolov7.pt --name yolov7_640_val
    

    推理

    --weights: 更改为自己的训练权重路径

    python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source inference/images/horses.jpg
    

    来源:是王同学呀

    物联沃分享整理
    物联沃-IOTWORD物联网 » 【环境配置】超详细,YOLOV7训练自己的数据集

    发表评论