5233

I have successfully run small edge impulse models involving image classifications or identifications on ESP32 but trying some serious models like Helmet detections or object counting where the basic model runs 20MB+ ESP32 is no match. We need a powerful computer. Can Raspberry Pi Zero chip in that slot along with it's big brother Raspberry Pi 4 ?

                                           
                                                         Edge impulse on Raspberry Pi Zero 2W with overclocking

Edge impulse on Raspberry Pi Zero 2W with overclocking

Prelude: I have successfully run small edge impulse models involving image classifications or identifications on ESP32 but trying some serious models like Helmet detections or object counting where the basic model runs 10MB to 20MB is not the job of ESP32. We need a powerful computer for the same. But again computer means laptop or desktop computers having no physical GPIOs to talk to the real world! Therefore, we fall back again on single-board-computers like Raspberry Pi / Jetson nano /  Orange Pi etc.

Raspberry Pi 4 or Jetson Nano is a good SBC [Single Board Computer] having a good number of USB ports / Ethernet interface / Wifi / Bluetooth LE / Audio out jack / Video out HDMI etc besides running it on 5Volt and 3Amp power input.

But really speaking for running an edge impulse model we don’t need many of these additional facilities except a few GPIOs and Audio & video outputs. Also such high power 5Volt / 3W input supply is also not required.

What we really need for our AI model to run on small devices are - Good memory, GPIOs, AV outputs and 5Volt 2Amp power requirements! [Active consumption is 350 mA only]

Raspberry Pi Zero 2W is a very good candidate having very small form factor, 1GHz processing power , WiFi & BLE and 40 GPIO pins. At the same time it has many limitations as well - only 512 MB RAM, No Audio out jack, only one micro USB port for USB peripherals, it poses serious restrictions.

Under the circumstances one of my follower asked me if we could run some or any AI model on Raspberry Pi Zero 2W.

Raspberry Pi Zero 2W: The following softwares need to be installed for running any edge impulse AI model or for that matter any AI model involving image analytic.

  1. Any suitable Linux OS [The MS Windows can work but I’m not confident] & espeak-ng for text to speech software [$> sudo apt-get install espeak-ng]
  2. Edgeimpulse software: https://docs.edgeimpulse.com/docs/development-platforms/officially-supported-cpu-gpu-targets/raspberry-pi-4
  3. Edgeimpulse-sdk [Software Development Kit]: https://docs.edgeimpulse.com/docs/tools/edge-impulse-for-linux/linux-python-sdk
  4. Open cv (computer vision): This is a pip module to be installed for manipulating images using python-pip module. [https://towardsdatascience.com/installing-opencv-in-pizero-w-8e46bd42a3d3 ]
Pi Zero 2W has WiFi and BLE in built. After installing any OS just connect any TV screen. The normal or wifi mouse may be connected to the micro USB port. You may need an OTG cable to connect the mouse. Else you may connect an USB hub for connecting several USB devices including mouse, keyboard etc. However, for connecting the keyboard you may need a bluetooth keyboard. The cost of a bluetooth keyboard is INR:800 to INR:2000 [amazon.in] After connecting the TV on the HDMI port, with the help of the mouse and the  TV display locate the bluetooth keyboard on the top right task bar and get it connected. Once this is done the rest is going to be just easy with just one USB port!

Now to install edgeimpulse & edgeimpulse-sdk just follow the steps as mentioned in serial no 2 & 3. These softwares will be installed flawlessly on Pi Zero. However without open cv software much of the edgeimpulse fun will be incomplete. Therefore, we need to install the pip module of open cv software. 

The URL mentioned in the serial -4 may be used to install opencv pip module on Pi Zero board but it’s a very painfully slow process. I consulted chatgpt for help but chatgpt too could not help much. Finally I lost my patience after 4 days effort. I quit & resort to cheat the Zero board by my special way! I have a raspberry Pi B+ board which has all these softwares installed. I simply removed the SD card from Pi B+ board and inserted into Pi Zero board and restarted!

Boom! The board stated. The GUI opened and I reconfigured the WiFi & Bluetooth keyboard and did an update of the Pi Zero board. The opencv is there in python 3 pip. To check all the modules use this command.

 pip list

You will find all the modules installed in pip like - numpy, opencv-python, pandas, pillow,PyAudio etc. In the next trial I took out an SD card from my Raspberry Pi 4 and inserted into my Pi Zero board and it ran there too! Ofcourse the GUI ran slowly but with all pomps & splendours! So you have two options now - Do all the hard work on a Raspberry Pi 4 board or a Raspberry Pi B+ board and then do an “SD card copy” on Pi B+ or Pi 4 board and then just implant the copied-sdcard on our Pi Zero board and complete the setups !

Install ssh on Pi zero so that you can administer it headless from network using any other computer. Also remember to carry an update command on Pi Zero

$> sudo apt-get update 

Picture:  


Sound output: There is no separate audio out from Pi Zero. The audio is available only through the HDMI output. But to get audio alarm having an HDMI connected is not very convenient. Therefore, we need to force audio output through some other means. GPIO-12,13,18,19 these are PWM pins of Pi Zero. We can force audio output using any two of these pins. The PAM8403 amplifier works both on 3.3V and 5V. 

Video link of audio output:   http://youtube.com/watch?v=Nu0hArK4Zf8

Config.txt: The amplifier or the GPIO pins do not automatically conform the audio out. We need to fix it in the /boot/config.txt file. Do a $> sudo nano /boot/config.txt somewhere in ssh or in the Raspberry Pi zero terminal and find the line dtparam=audio=on 

This line is for enabling Audio output. Just before this line, on a clear space write the following line - dtoverlay=audremap,pins_18_13   # or 19_13  Save the file by ctrl+o, exit and reboot.  After reboot on a terminal use $> alsamixer 

This will open up alsa volume control. Use up or down arrow key of keyboard to increase or decrease volume. ‘aplay’ is a music player of Pi. If you have a music file ‘file1.wav’ then play it by $> aplay file1.wav 

if you do not find aplay in Pi zero. Do $> sudo apt-get install aplay  #to install it.  In case you want to play mp3 files, you need to install mpg123 program. 

$> sudo apt-get install mpg123   Once that is installed just play it $> mpg123 abcd.mp3 and the mp3 file will start playing.

Overclocking: OK. So far so good. The normal clock frequency of Pi Zero is 700MHz for older one to 1GHz to the latest one. If you have a good quality power supply unit [5volt, 3Amp] then for the latest chip, we can safely overclock it upto 1.1GHz to 1.2GHz with a good quality heatsink and with force cooling upto 1.4GHz it can be overclocked. The trick is keeping the CPU temperature less than 80  Degree Centigrade by cooling. In an airy room with this type of heatsink I have overclocked it to 1.2GHz and sometimes to 1.3GHz in AC room. The start time of the edgeimpulse python program reduces by 3+ seconds at overclock of 1.3GHz. The following line is to be added in          /boot/config.txt and then reboot to see the effect. In case your system does not boot after overclocking, remove the sdared, insert into a laptop and then modify the /boot/config.txt to original state. It will work now.

$> sudo nano /boot/config.txt#uncomment to overclock the arm. 700 MHz is the default.#Bera wrote these two linesarm_freq=1300over_voltage=2
You can increase the voltage upto 6 but that increases temperature further. Therefore try with 2 first, in case it does not stabilise increase it to 3, 4, 5 etc. Also after every restart keep monitoring the CPU temperature by this command. bera@raspberrypi:~ $ vcgencmd measure_temptemp=48.3'CTo understand the CPU clock frequency use the following command.bera@raspberrypi:~ $ lscpuArchitecture:                         armv7lByte Order:                           Little EndianCPU(s):                               4On-line CPU(s) list:                  0-3Thread(s) per core:                   1Core(s) per socket:                   4Socket(s):                            1Vendor ID:                            ARMModel:                                4Model name:                           Cortex-A53Stepping:                             r0p4CPU max MHz:                          1300.0000CPU min MHz:                          600.0000BogoMIPS:                             38.40Vulnerability Gather data sampling:   Not affectedVulnerability Itlb multihit:          Not affectedVulnerability L1tf:                   Not affectedVulnerability Mds:                    Not affectedVulnerability Meltdown:               Not affectedVulnerability Mmio stale data:        Not affectedVulnerability Reg file data sampling: Not affectedVulnerability Retbleed:               Not affectedVulnerability Spec rstack overflow:   Not affectedVulnerability Spec store bypass:      Not affectedVulnerability Spectre v1:             Mitigation; __user pointer sanitizationVulnerability Spectre v2:             Not affectedVulnerability Srbds:                  Not affectedVulnerability Tsx async abort:        Not affectedFlags:                                half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpa                                      e evtstrm crc32bera@raspberrypi:~ $ 

Actual run: Some of the big model of edgeimpulse that I have created are helmet.eim [20MB] fruits.eim [21MB] . After installing & doing all the above now time for actual testing of the AI project. First open a clear terminal in Pi zero or in ssh mode from a remote laptop. I use apple Macbook where ssh is jolly well available in terminal. If you use the Windows laptop, find putty and therefrom on ssh [port 22] call the Pi zero computer IP address. Provide the authentication and log on to it.

So far we have installed - Open CV with edge-impulse-linux & edge-impulse-sdk for python. We will run the fruits model which will identify - banana, apple, grapes and count them. After that it will speak up the fruit it found and it’s number.


$> edge-impulse-linux —disable-camera

This command will open the edge-impulse wizard. It will ask you to log on to the site. Then you select the model you want to run. Now on a separate window run…

$>edge-impulse-linux-runner

This will compile your earlier selected model with full hardware acceleration & download the model to your Raspberry Pi on a directory like - /home/bera/.ei-linux-runner/models/31/model.eim. Now run this model with edge-impulse-sdk like this…

$> python /home/bera/linux-sdk-python/examples/image/classify.py /home/bera/.ei-linux-runner/models/31/model.eim

The classify.py is the most generic python file in edgeimpulse-sdk example folder to run the object classification model. It needs to be modified to suit our purpose of speaking & counting. The modified file is attached for further understanding.

Video link of running: https://youtube.com/watch?v=KfWAwqD-xyA

Conclusion: Raspberry Pi computer over the last 10+ years have come a long way. From a mere single board computer By Uben Upton with simple TV monitor as display output for the poor African students, now it doubles as a beautiful yet very powerful AI machine with many GPIOs to communicate with the real world! Many working AI model I’ve seen deployed on Raspberry Pi computer. This project testifies that the Raspberry Pi Zero is an equally powerful computer which can be deployed for matching AI loads as well!

Attachment: fruits1.py 

Somnath Bera

Kolkata / India