Giter VIP home page Giter VIP logo

yolov5-export-to-raspberry-pi's Introduction

Yolov5 export to Raspberry Pi

img

It is a two step process:

  1. Convert model weights to tflite
  2. Run the inference on Raspberry Pi

Convert Model Weights to tflite

If you don't want to install anything on your system then use this Google Colab (Recommended). Open In Colab

And if you want to perform the conversion on your system then follow bellow instructions:

I recommend create a new conda environment for this as we need python==3.7.0 for this:

conda create -n yolov5_env python==3.7.0
conda activate yolov5_env

Then run below commands and replace yolov5s.pt with your own model path and also change yolov5s.yaml accordingly.

git clone https://github.com/ultralytics/yolov5.git
cd yolov5
pip3 install tensorflow==2.3.1
pip install -r requirements.txt
python3 export.py --weights yolov5s.pt --img 418 --batch 1 --include onnx tflite

Run the inference on Raspberry Pi

Just clone this repository and we are good to go:

git clone https://github.com/karanjakhar/yolov5-export-to-raspberry-pi.git
cd yolov5-export-to-raspberry-pi
chmod +x setup.sh
./setup.sh
python3 yolov5_tflite_folder_of_images_inference.py --weights yolov5s-fp16.tflite --folder_path images/ 

Move your own model tflite file to raspberry pi and use that with above command.

yolov5_tflite_inference.py this file contains main inference code which you can use with your own project.

Other files show examples how to use it. I have placed a tflite file and some sample images to run a quick test.

To Run Examples:

For webcam:

python3 yolov5_tflite_webcam_inference.py -w yolov5s-fp16.tflite

For image:

python3 yolov5_tflite_image_inference.py -w yolov5s-fp16.tflite -i images/bus.jpg

For video:

python3 yolov5_tflite_video_inference.py -w yolov5s-fp16.tflite -v <your video path>

For folder of images:

python3 yolov5_tflite_folder_of_images_inference.py -w yolov5s-fp16.tflite -f images/

yolov5-export-to-raspberry-pi's People

Contributors

karanjakhar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yolov5-export-to-raspberry-pi's Issues

Issue with running code. Error message "ValueError: Cannot set tensor: Dimension mismatch." is generated for any action

Hi,

I am trying to get the code running. I have followed the installation instructions yet when I get run the code I get ValueError which usually means a issue with input data but I cannot see any issue in the code. Its looks pretty clean and I don't see what's actually happening. Has anyone see a error like this from "yolov5_tflite_inference.py" ? Any help appreciated.

pi@raspberrypi:~/yolov5-export-to-raspberry-pi $ python3 yolov5_tflite_folder_of_images_inference.py -w yolov5s-fp16.tflite -f images/ Namespace(weights='yolov5s-fp16.tflite', folder_path='images/', img_size=416, conf_thres=0.25, iou_thres=0.45) Processing images/bus.jpg now ... Traceback (most recent call last): File "/home/pi/yolov5-export-to-raspberry-pi/yolov5_tflite_folder_of_images_inference.py", line 28, in <module> detect_from_folder_of_images(opt.weights,opt.folder_path,opt.img_size,opt.conf_thres,opt.iou_thres) File "/home/pi/yolov5-export-to-raspberry-pi/yolov5_tflite_folder_of_images_inference.py", line 12, in detect_from_folder_of_images detect_image(weights,file,img_size,conf_thres,iou_thres) File "/home/pi/yolov5-export-to-raspberry-pi/yolov5_tflite_image_inference.py", line 34, in detect_image result_boxes, result_scores, result_class_names = yolov5_tflite_obj.detect(normalized_image_array) File "/home/pi/yolov5-export-to-raspberry-pi/yolov5_tflite_inference.py", line 136, in detect self.interpreter.set_tensor(self.input_details[0]['index'], input_data) File "/usr/lib/python3/dist-packages/tflite_runtime/interpreter.py", line 572, in set_tensor self._interpreter.SetTensor(tensor_index, value) ValueError: Cannot set tensor: Dimension mismatch. Got 416 but expected 448 for dimension 1 of input 0.

ModuleNotFoundError: No module named tflite_runtime

After running setup.sh I get a module not found error at import tflite_runtime.interpreter as tflite when running the script for inferencing.

ModuleNotFoundError: No module named 'tflite_runtime'

sudo apt-get install python3-tflite-runtime tells me that the newest version (2.5.0.post1) is installed.

I am using a Raspberry Pi 4 B running NOOBS Raspbian version 10 buster. I have a small suspician that the OS could be the problem, but it is just a hunch. I am running a virtual environment.

Benchmarks?

Hello, thanks for the great work on this project. Is is possible to share any benchmarks for how it performs on the Raspberry PI? Even a rough estimate of inference time per-image would helpful.

Thanks!

Running inference on YOLOV5-int8.tflite

Thanks for your work.
Its working fine for quantized yolov5 fp16 model but did not work for int8 yolov5 model. Can you tell me on how I can change it for the int8 quantized models?

Why there is a need to convert Pytorch model to some other for making it run on Raspberry Pi?

@karanjakhar I just have a question that what is the core difference between the Pytorch Model and the Exported(Tflite model)? As I could see that Pytorch model is not working well on the Raspberry Pi whereas the Tflite works better but I am not able to figure out at what are the reasons for that it's working.

What does the Pytorch model do that Tflite doesn't do?

@karanjakhar Do you have any idea about that?

What is the difference between the current Yolov5 version under ultralytics and this exported version?

@karanjakhar What is the difference between the current Yolov5 version under ultralytics and this exported version?

Can you please let me know in detail? I am a researcher trying to understand the key things.

Also, did you try running the mAP value of original Yolov5 on Raspberry Pi? Here is a problem if you can take a look then it would really be appreciable

webcam couldn't open

I pass all of the steps mentioned but the webcam doesn't show anything. In the folder, it has a video which cannot be opened.
when I run this command

pi@raspberrypi:~/yolov5-export-to-raspberry-pi-main $ python3 yolov5_tflite_webcam_inference.py -w 5l_best-fp16.tflite

it shows only this

Namespace(weights='5l_best-fp16.tflite', webcam=0, img_size=416, conf_thres=0.25, iou_thres=0.45)
480 640

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.