Open Source Converter

This page describes how to convert YOLOv5s PyTorch models to TFLite format using open-source tools.

Set Up the YOLOv5 Environment

Note

It is recommended to use Python 3.7 to ensure compatibility with the YOLOv5 environment and conversion scripts.

  1. Clone the repository:

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

    pip3 install -r requirements.txt
    
  3. Apply the FP32 export 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 32-bit float (FP32) TFLite output instead of the default FP16.

Convert the PyTorch Model to TFLite

  • 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
    

The export.py script parameters:

  • --weights: Path to the source model weight file.

  • --img-size: Input image dimensions.

  • --include tflite: Target export format.

  • --int8: Enables INT8 quantization.

Next Steps

After generating the .tflite model, refer to Visualizing AI Models to inspect the model structure and tensor information required for application development. Otherwise, proceed to Compile TFLite Models to DLA to generate a compiled binary for directly NPU execution.