Open Source Converter

Overview

This page outlines the process for converting YOLOv5s PyTorch models to TensorFlow Lite (TFLite) format using an open-source converter. It includes steps for setting up the YOLOv5 environment, applying necessary patches, and performing model conversions to both int8 and fp32 TFLite formats.

YOLOv5s Model Conversion

Environment Setup for YOLOv5s

Note

For better compatibility, it is recommended to use Python 3.7 when working with these models, as it has higher compatibility with certain libraries and frameworks.

  1. Clone the repository:

    git clone http://github.com/ultralytics/yolov5
    cd yolov5
    git reset --hard 485da42
    
  2. Install Python packages and dependencies:

    pip3 install -r requirements.txt
    
  3. Apply the Patch:

    wget https://mediatek-aiot.s3.ap-southeast-1.amazonaws.com/aiot/download/model-zoo/patches/export_fp32.patch
    git apply export_fp32.patch
    

    Note

    The export_fp32.patch modifies the export script to support exporting the model in FP32 (32-bit float) TFLite format instead of FP16 (16-bit float). The changes include:

    • Changing the output filename to indicate FP32 format.

    • Updating the supported types to use tf.float32 for higher precision.

Convert PyTorch Model to TFLite Model

  • Convert to INT8 format:

    python3 export.py --weights yolov5s.pt --img-size 640 640 --include tflite --int8
    
  • Convert to FP32 format:

    python3 export.py --weights yolov5s.pt --img-size 640 640 --include tflite
    

Note

The export.py script is used to convert the YOLOv5 model from PyTorch format to TFLite format. The script accepts several parameters:

  • --weights Specifies the path to the model weight file to be exported.

  • --img-size Sets the input image size for the model.

  • --include tflite: Specifies that the model should be exported to TensorFlow Lite format.

  • --int8 Converts the model to INT8 quantized format; if not specified, the model will be converted to FP32 format by default.

Convert TFLite Model to DLA Model

Environment Setup

  1. Download NeuroPilot SDK All-In-One Bundle:

    Visit the download page: NeuroPilot Downloads

  2. Extract the Bundle:

    tar zxvf neuropilot-sdk-basic-<version>.tar.gz
    
  3. Set Environment Variables:

    export LD_LIBRARY_PATH=/path/to/neuropilot-sdk-basic-<version>/neuron_sdk/host/lib
    

Convert to DLA format

  • Convert to INT8 format:

    /path/to/neuropilot-sdk-basic-<version>/neuron_sdk/host/bin/ncc-tflite --arch=mdla3.0 yolov5s-int8.tflite
    
  • Convert to FP32 format:

    /path/to/neuropilot-sdk-basic-<version>/neuron_sdk/host/bin/ncc-tflite --arch=mdla3.0 --relax-fp32 yolov5s-fp32.tflite
    

Note

To ensure compatibility with your device, please download and use NeuroPilot SDK version 6. Other versions might not be fully supported.