Translation procedure for YOLOv5

From Renesas.info

This document describes the procedure for translating YOLOv5 with DRP-AI Translator v1.80.

Please note the following:

  • Use ONNX generated by ultralytics (OSS GitHub repository)
  • Use v5.0 of the ultralytics yolov5 repository (not the latest version)
  • Cut the post-processing part of the generated ONNX and use it
  • Run DRP-AI Translator v1.80 on Ubuntu 20.04

1. Download ONNX

Please run the following command to clone the ultralytics yolov5 v5.0 repository.

$ git clone -b v5.0 https://github.com/ultralytics/yolov5.git

2. Set environment

Please run the following command to change requirements.txt so that torch v1.7.0 and onnx are installed.

$ cd yolov5
$ sed -i -e "s/torch>=1.7.0/torch==1.7.0/g" requirements.txt
$ sed -i -e "s/# onnx>=1.8.1/onnx>=1.8.1/g" requirements.txt

Please run the following command using the modified requirements.txt to install the required python

modules.

$ pip3 install -r requirements.txt

3. Export ONNX

Please run the following command to change utils/google_utils.py so that the YOLOv5 model for v5.0 is

downloaded.

$ sed -i -e "s/{tag}/v5.0/g" utils/google_utils.py

Please run export.py with the following command to export ONNX. If executed successfully, yolov5s.onnx will be generated. You can also specify yolov5l or yolov5m instead of yolov5s.

$ python3 models/export.py --weights yolov5s.pt --img 640 --batch 1

4. Cut post-processing part of ONNX

Please move to upper directory and edit the following in an editor such as vim, and save it as yolov5_cut.py.

$ cd ..
$ vim yolov5_cut.py
# yolov5_cut.py
import onnx
input_path = 'yolov5/yolov5s.onnx'
output_path = 'yolov5s_v5_wo_post.onnx'
input_names = ['images']
output_names = ['418', '378', '398']
onnx.utils.extract_model(input_path, output_path, input_names, output_names)

Please run yolov5_cut.py with the following command. If executed successfully, yolov5s_v5_wo_post.onnx will be generated.

image 2023-01-30 205945817.png
$ python3 yolov5_cut.py

The above output names, 418, 378, and 398, are for YOLOv5s. Please note that different names must be specified for YOLOv5l and YOLOv5m. As shown in the figure below, please find and specify the output name of Conv before Shape and ReShape of the three outputs of YOLOv5.

5. Translate ONNX

Please run DRP-AI Translator using the generated yolov5s_v5_wo_post.onnx. DRP-AI Translator must be v1.80 or later. For information on how to run DRP-AI Translator, please refer to the User's Manual.