NeuroPilot Converter Tool
This page provides a guide for converting YOLOv5s PyTorch models to TFLite format using the NeuroPilot Converter Tool. It covers the environment setup, patch application, and conversion procedures for both INT8 and FP32 formats.
Set Up the YOLOv5 Environment
Note
It is recommended to use Python 3.7 for optimal compatibility with the required libraries and frameworks.
Install and Verify the NeuroPilot Converter Tool
Download the NeuroPilot SDK All-In-One Bundle (Version 7.X.X): Visit the NeuroPilot Downloads page.
Extract the bundle:
tar zxvf neuropilot-sdk-basic-<version>.tar.gz
Install the NeuroPilot Converter Tool for Python 3.7:
pip3 install /path/to/neuropilot-sdk-basic-<version>/offline_tool/mtk_converter-<version>+release-cp37-cp37-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Verify the installation: Run the following script to check the installed package version:
python3 -c 'import mtk_converter; print(mtk_converter.__version__)'
Prepare and Export the YOLOv5 Model
Clone the repository:
git clone http://github.com/ultralytics/yolov5 cd yolov5 git reset --hard 485da42
Install dependencies:
pip3 install -r requirements.txt pip3 install torch==1.9.0 torchvision==0.10.0
Note
The
mtk_converter.PyTorchConverterrequires PyTorch versions between 1.3.0 and 2.0.0. The developer must install compatible versions oftorchandtorchvisionto avoid runtime errors.Apply the patch:
wget https://mediatek-aiot.s3.ap-southeast-1.amazonaws.com/aiot/download/model-zoo/scripts/model_conversion_YOLOv5s_example_20240916.zip unzip -j model_conversion_YOLOv5s_example_20240916.zip git apply Fix_yolov5_mtk_tflite_issue.patch
Note
The
Fix_yolov5_mtk_tflite_issue.patchadds support for MediaTek TFLite (MTK TFLite) in the YOLOv5 export script by modifying theDetectmodule and post-processing logic.Export the PyTorch model to TorchScript format:
python3 export.py --weight yolov5s.pt --img-size 640 640 --include torchscript
Convert the PyTorch Model to TFLite
Convert to INT8 Format
Prepare calibration data: Create a script named
prepare_calibration_data.pyto generate images for quantization calibration.python3 prepare_calibration_data.pyimport os import numpy as np from utils.dataloaders import LoadImagesAndLabels from utils.general import check_dataset data = 'data/coco128.yaml' num_batches = 100 calib_dir = 'calibration_dataset' os.makedirs(calib_dir) dataset = LoadImagesAndLabels(check_dataset(data)['train'], batch_size=1) for idx, (im, _target, _path, _shape) in enumerate(dataset): if idx >= num_batches: break im = np.expand_dims(im, axis=0).astype(np.float32) im /= 255 np.save(os.path.join(calib_dir, 'batch-{:05d}.npy'.format(idx)), im)
Perform INT8 conversion: Create a script named
convert_to_quant_tflite.pyto use the calibration data for conversion.python3 convert_to_quant_tflite.pyimport os import numpy as np import mtk_converter calib_dir = 'calibration_dataset' converter = mtk_converter.PyTorchConverter.from_script_module_file( 'yolov5s.torchscript', input_shapes=[(1, 3, 640, 640)] ) def data_gen(): for fn in sorted(os.listdir(calib_dir)): yield [np.load(os.path.join(calib_dir, fn))] converter.quantize = True converter.calibration_data_gen = data_gen converter.convert_to_tflite('yolov5s_int8_mtk.tflite')
Convert to FP32 Format
Create a script named convert_to_tflite.py to generate a full-precision TFLite model.
python3 convert_to_tflite.py
import mtk_converter
converter = mtk_converter.PyTorchConverter.from_script_module_file(
'yolov5s.torchscript', input_shapes=[(1, 3, 640, 640)]
)
converter.convert_to_tflite('yolov5s_mtk.tflite')
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 direclty NPU execution.