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 Convert

Setting Up YOLOv5 Environment

  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 Patch

    git apply export_fp32.patch
    

    Note

    This 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.

YOLOv5 PyTorch Model Conversion to int8/fp32 TFLite Formats

  • int8 format:

    python3 export.py --weights yolov5s.pt --img-size 640 640 --include tflite --int8
    
  • 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 PyTorch model weights.

  • –img-size sets the input image size for the model.

  • –include determines the output formats, including TFLite.

  • –int8 is used to convert the model to int8 quantized format. If not specified, the model will be converted to fp32 format by default.