1. Download the source code
Download the Yolov7 source code in github and decompress
the YOLOv7 source code
Data annotation can refer to this article
https://blog.csdn.net/qq_39779449/article/details/110172948?spm=1001.2014.3001.5502
Label format
2. Configuration environment
Overall configuration tree structure
Install Yolo v7 gpu environment
Create Yolov7 environment in Anconda
Install the required packages
Activate the Yolov7 environment and install the various packages required by Yolov7, and use the Tsinghua mirror source to install the required packages (I tried it all, and the Tsinghua mirror is faster)
activate yolov7
pip install -r F:\objectdet\yolov7-main/requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
Check whether the environment installation is successful.
If you install it completely according to requirements.txt, you will find that the installed torch version is not the gpu version, so you have to reinstall the gpu version to use your gpu to train Yolov7
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
After installation, check the package you installed through pip list, as shown in the figure below, congratulations, the installation is successful
3. Make Yolo v7 train and test your own data set
Modify Yolo v7 configuration
In pycharm, select the environment required by Yolov7 just installed,
modify the configuration environment,
and create a yaml file of your own data set
- train: Modify to your own training set path
- val: Modify to your own validation set path
- test: Modify to your own test set path
- nc: modify the number of categories of your own dataset
- names: Modify to your own dataset category labels.
train your own dataset
Through pycharm training, modify the train.py file as follows, and then run
can also be trained directly on the terminal by the following method
python train.py --workers 1 --device 0 --batch-size 8 --data data/data.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml
If the following interface appears, congratulations, the training is successful, wait slowly for your model training to complete
Test your own dataset
Here we use the default parameters in the test.py file
- data: Change to your own dataset configuration file path
- img: input data size
- batch: number of batch data
- conf: confidence level
- iou: threshold
- weights: Change to your own training weight path
- name: Change to your own path
python test.py --data data/data.yaml --img 640 --batch 8 --conf 0.001 --iou 0.65 --device 0 --weights yolov7.pt --name yolov7_val
Make predictions on a single dataset
Predict a single image` python detect.py --weights yolov7.pt --conf 0.25 --img-size 640 --source images/
benign.jpg
4. Problems encountered and solutions
Can't run yolov7-e6e.yaml file
. According to the official paper, yolov7-e6e.yaml works best, so I plan to try yolov7-e6e.yaml, emmm, when training yolov7-e6e.yaml, the following error is reported. yolov7.yaml can run normally, but it will report an error if it is replaced with yolov7e6e.yaml. nc has been changed to the category of my data set, and it can be used for transfer learning. I checked a bunch of information on the Internet and couldn't find a relevant solution.
At first I thought it was insufficient video memory, so I changed the batch-size to 1 and the img-size to 128, but it still didn’t work. I checked the author’s latest code and found that the yolov7-e6e.yaml and yolov7.yaml training files are different. The former is trained through train_aux.py, and the latter is trained through train.py.
Start training with train_aux.py, yes, and report an error again. . . One bug was solved and another bug appeared.
Check the github issues section of the original author of yolov7, and found that loss.py was repaired, and then copied the loss.py file to replace my original loss file, and finally succeeded.
Summary: From experience, I will run the code of the big guy in the future. Try to use the latest code from the boss. You can avoid many detours.