The relationship between ncnn and yolov5
ncnn Android environment deployment
(1) Download ncnn-yolov5-android source code
download
(2) Download ncnn-android-vulkan package
download
Select ncnn-20210525-android-vulkan.zip to download
(3) Unzip the downloaded ncnn-20210525-android-vulkan.zip Then put it under ncnn/cpp:
3.ncnn model conversion
First install the relevant libraries
pip install onnx coremltools onnx-simplifier
(1) Use /yolov5/module/export.py to generate the onnx file.
In the yolov5 file, enter the command line with cmd and enter the following statement to convert the model (–weights means the location and name of the weight file that needs to be converted)
python models/export.py --weights yolov5s .ptAfter
executing this sentence, three files, yolov5s.onnx, yolov5s.mlmodel and yolov5s.torchscript.pt, will be generated in the same folder as the weight file.
Convert onnx to ncnn
Select the just generated onnx to convert ncnn. If the conversion is successful, it can be directly deployed to Android. However, if an error is reported, we need to make a small modification to the generated file to deploy Android.
Solution to the above error report:
First, we download the two generated files.
Use a text editor to open the .param file and prepare it. Here I opened it directly with pycharm. Here is
a tool recommended: Netron. We can use this to open our .param file first to see the model data.
Netron
Find Concat and record the penultimate parameter. (Will be used later)
Then add "YoloV5Focus focus 1 1 images" + the previously recorded parameter "/model.0/Concat_output_0"
under Input. The modified param is shown below. There were originally 181 lines. After deleting the Split, Crop, and Concat layers, and adding the YoloV5Focus layer, the 6 ops were replaced by 1 op. A total of 5 lines were reduced. Subtract 5 from the layer count at the beginning and rewrite it to 176.
The modifications of stride16 and stride32 are modified according to the number after Permute in the .param file, as follows:
After changing these, it will be ok.
The following is the running effect: