This article will take you through the process of training your own YoloV7 model, using a provided dataset. For this article we will be using the COCO dataset as is used within the original repo for YoloV7. However, we provide our own fork of that repo hosted on our GitHub, which will contain our own modifications and will provide updates for use on newer versions of Python and ONNX.
This article has been tested using Python 3.8 on a clean virtual environment using the requirements.txt provided in our repo, but will be updated in the near future for support on later versions of Python.
Please ensure you install from the requirements.txt file in addition to using Python 3.8 otherwise with higher versions of Python, the model will not be able to be converted to Hailo.
Differences from Original
The primary difference in our branch of the model is the use of the activation ReLU6 instead of LeakyRelu. This was chosen due to the fact that during quantization, ReLU6 tends towards higher stability than the use of LeakyRelu without much if any loss in accuracy. This can be seen in the yaml where for each layer of the backbone, we now provide ReLU6 as the activation.
Dataset Preparation
In our case, we will be able to use the get_coco.sh script provided within the repo by running the command on WSL or Linux
$ sh scripts/get_coco.sh
This will go ahead and download the coco dataset for use in training and validation. The dataset downloaded with be in YoloV7 PyTorch TXT format, which contains the images within a provided folder with the base name 'images' split into training and validation folders and an base folder named 'labels' following the same format as the 'images' folder, but containing .txt files that reference the object found using it's class and the normalized box coordinates of that object.
Training
To train the model using the cloned repo, we will run the following command
python train.py --workers 8 --device 0 --batch-size 32 --data data/coco.yaml \
--epochs 100 --img 640 640 --cfg cfg/training/yolov7-edgefirst.yaml --weights '' \
--name yolov7 --hyp data/hyp.scratch.p5.yaml
This will tell the model to begin training the model using the architecture that we provided using ReLU6 as stated in cfg/training/yolov7-edgefirst.yaml and using the coco dataset provided for 100 epochs. The hyperparameters are defined with data/hyp.scratch.p5.yaml. This is default when training a model from scratch. When wanting to transfer learn a model, it may be better to use data/hyp.scratch.custom.yaml, but in either case the default will work. If you are wanting to continue training on a previous trained file, you can provide that with the argument --weights last.pt which would load the weights from last.pt. With '' provided as the parameter it will train the model from scratch. The image size can be modified, but 640 is recommended.
Export
Finally to export the model, this can be performed with the following command
python export.py --weights yolov7-edgefirst-coco.pt --grid --include-nms --simplify --img-size 640
This will use the weights provided to generate an ONNX version of the model. The --grid command will append the decoder to the model which is highly recommended as well as with the --include_nms which will further attach layers to the model to generate both the boxes and the classes which can be passed through NMS as seen below in Netron.
Now you have generated your ONNX model and will be ready to convert and run inference on your Maivin! This model is also used in our Hailo QuickStart Guide.
Comments
0 comments
Please sign in to leave a comment.