coral tpu adventures and docker, a small writeup

Support for the docker image maintained by dlandon
Post Reply
gurkburk76
Posts: 2
Joined: Thu Feb 06, 2020 1:17 pm

coral tpu adventures and docker, a small writeup

Post by gurkburk76 »

So i was going a bit nuts where when my mysql tables somehow got a bit wrecked, possibly because of a bad shutdown/power outage, also i'm seeing that 1.34.x docker image seems to be deprecated (https://hub.docker.com/r/dlandon/zoneminder) so i'm switching to https://github.com/dlandon/zoneminder.machine.learning that at the moment i'm writing this does not have a built docker image. Dosen't really matter i've learnt how to build one so that's ok, and educational ;)

I built it and thought i'de give it a go at making this usb coral stick working that i've bought a few months ago since this version of zoneminder seems to be packaged together with ES server that actually has support for the TPU, great!

After a few hours of struggle a few thing dawned upon me, since i'm running it in a docker container i seem to need to passthrough the USB port to the container witch in turn means that the docker image seems to be missing a few bits like *.tflite files and python libraries/utils and making some adjustments to what group the www-data user belongs to in order for this to work.

example:
ERR detect_sequence.py:168 [Error loading same model variation for object:No module named 'pycoral']

These are the steps i hade to take in order to get this working:

sources:
https://coral.ai/models/object-detection/
download edge tpu model and labels file, for some reason in objects.ini it seems to need 2 tpu files and 1 model file. (mobilenet/mobiledet)

Inside the container i had to do this:

echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt update ; sudo apt-get install libedgetpu1-std ; sudo apt-get install python3-pycoral

In the container i then did this pointing to a picture the camera took to see if the tpu picks it up.
sudo -u www-data /var/lib/zmeventnotification/bin/zm_detect.py --config /etc/zm/objectconfig.ini --debug --eventid X(insert pic id here)

Not sure if it will stick if i do a "docker restart zoneminder", pretty sure i'll have to redo it when i "docker rm zoneminder".

Now, i might be dumb as **** by doing this manually in the container and perhaps there is a much better way to automatize this either when building the container and/or when starting the container.
the HOOK_INSTALL=1 option that the older docker containers have seems to be missing and that can be a bit of the puzzle i'm missing.

Pretty sure i could in theory add the steps to the Dockerfile and rebuild and it would "work out of the box" but if that's the case itwould suprise me if the authoer (dlandon) would have missed so i'm more leaning towards that i'm missing something being the non-dev that i am :)

This are very crude steps to what *i* did to get it working and not meant to be a complete guide, if it helps you great! But i mainly would like to get some answers if i'm totally messing this up/taking the wrong steps.

After reading this (https://forums.unraid.net/topic/56632-s ... ent-932742) perhaps the best forward solution is building a VM for it.
Funny that i've just build a proxmox cluster so i guess that's the next project :)
It also answers my question about hooks that i wrote above, if i understand it correctly.

I was first thinking that it would be a good solution to passthrough the usb stick to the vm, but if you want to be able to move the vm within the cluster it breaks things since the usb stick is no longer attached to the vm so i guess remote ML server solution it is.



Thanks for building awsome software dlandon, you have my GoFundMe support :)
juan11perez
Posts: 64
Joined: Tue Apr 27, 2021 3:41 am

Re: coral tpu adventures and docker, a small writeup

Post by juan11perez »

Good day, thank you for sharing this info.
I've tried following your steps and keep getting the error "ValueError: Failed to load delegate from libedgetpu.so.1" which after googling, some opinions are that is due to a faulty connection. https://zmeventnotification.readthedocs ... s_faq.html

However, Im using the same half-pci coral in unraid with frigate and it works without issues. It's just zm that doesnt work.

Did you build the docker image and added any extra paramenters to import the coral device?

Moreover, did you do the opencv cuda compiling, or are you using without cuda.

Any guidance is welcome.
thank you.
juan11perez
Posts: 64
Joined: Tue Apr 27, 2021 3:41 am

Re: coral tpu adventures and docker, a small writeup

Post by juan11perez »

So answering my own question; I found the problem. In the container /dev/apex_0 does not have sufficient privileges
chmod 777 /dev/apex_0 >> fixes the problem and the edgetpu loads immediately.

However, the performance is very poor. With the coral and gpu the cpu still spikes to 70% on object detection.

I have modified the objectconfig.ini per the instructions, so not sure what else can be done.

Code: Select all

				'name': 'TPU object detection',
				'enabled': 'yes',

Code: Select all

            'name': 'TPU face detection',
            'enabled': 'yes',
In comparison with Frigate https://github.com/blakeblackshear/frigate the cpu practically stays unchanged.
User6677
Posts: 20
Joined: Sat Dec 24, 2022 9:32 am

Re: coral tpu adventures and docker, a small writeup

Post by User6677 »

Here is my update using the current docker image.(v1. 36)

I'm using the M2 version of the Edge TPU in a pcie adapter card.

My first attempt was on a purpose built "old desktop" pc using an Intel motherboard and a Pentium E2200 cpu. This is running Debian 11 bullseye. Video recording/motion detection wise it was sufficient for the number of cameras at low fps I have. However, the edge TPU didn't work at all. Any attempts to use it would crash with "illegal instruction" logged in dmesg.

Then I found out, using gdb, it was crashing on reaching a SSE4_1 instruction. This is quite disappointing. Although the hardware I tried to use is rather historic it is no 386sx. It is a perfectly capable x86_64 cpu, so it would be reasonable to expect any binaries described as x86_64 (or amd64) would work. Unfortunately they don't.

The offending binaries come straight from Google. It is yet to be determined if just rebuilding with no SSE4_1 flag is going to work.

So I put the PC aside and I moved the hdd to an old 1U server with a Xeon E3-1220 cpu. This supports SSE4_1 (and even AVX) so it worked fine, but not before I downloaded models specified in object detection config file(objectconfig.ini) for the Edge TPU.

Also the labels gł format currently available on Google's site has to be modified to work. Row numbers have to be added before labels (starting at 0) separated with a space. I used
nl - v0 filename > new_filename
To add them.

Finally as described by the poster above I added the chmod command to one of the /etc/my_init.d/ files so the permissions are set each time the container starts remembering to commit the changes

From then on after I configured my notification settings it seems to work fine. This is pretty good for an old unmaintained image :-)
Post Reply