The incredible Shawn Hymel has a course on Coursera called Introduction to Embedded Machine Learning, as well as a follow up course that was launched in 2021, Computer Vision with Embedded Machine Learning. My team at Avnet and I decided to take Introduction to Embedded Machine Learning together to level up our embedded machine learning skills.
While taking the class, I decided to try out all of the exercises with Avnet's own MaaXBoard hardware.
This simple guide explains how to get set up with the first project, the motion detection project covered in weeks one and two of Shawn's Introduction to Embedded Machine Learning course.
SET UP A PROJECT ON EDGE IMPULSEThe first step is to set up the motion recognition project in Edge Impulse. You can copy Edge Impulse's Continuous Motion recognition project here by selecting "clone this project" in the upper right hand corner. You can also create your own accelerometer project according to Shawn's instructions in the Coursera course.
If you cloned the project, you'll need to delete the data under the data acquisition tab in order to collect new data from your MaaXBoard.
SOFTWARE INSTALL ON MAAXBOARD- First, set up your MaaXBoard headlessly by following my project: Getting Started with MaaXBoard - Headless Setup
- SSH into your MaaXBoard as root. Since we'll be controlling GPIO, it will be easier if you are a root user.
We'll be using Adafruit's library, Adafruit-Blinka, in order to read from our i2c accelerometer using CircuitPython. I've updated it to work with MaaXBoard, but my updates are currently only live on Avnet's fork of the project, so you'll have to git clone the Avnet repo and copy the files into the pip installed directory. You'll also have to do the same for the supporting library, Adafruit Platform Detect.
Note: this will only work on MaaXBoard and not MaaXBoard Mini, because i2c isn't enabled for the 40 pin pi header on the default Debian image for MaaXBoard Mini (although it's possible you build your own Debian image for MaaxBoard Mini with i2c enabled).
First, run updates and install git:
apt-get update && apt-get upgrade
pip3 install --upgrade setuptools
apt-get install git
Install Adafruit Platform Detect:
Use pip to install Adafruit PlatformDetect on you MaaXBoard:
pip3 install --upgrade Adafruit-PlatformDetect
Find the installation directory for the Adafruit library:
pip3 show Adafruit-PlatformDetect
(If you are root, it will likely be in /usr/local/lib/python3.7/dist-packages).
Now clone the Platform Detect library from Avnet that includes MaaXBoard updates:
git clone https://github.com/Avnet/Adafruit_Python_PlatformDetect.git
Copy the files from the git repo you downloaded from Avnet into the installation directory for the Adafruit Library:
sudo cp -R /home/ebv/Adafruit_Python_PlatformDetect/adafruit_platformdetect /usr/local/lib/python3.7/dist-packages
Install Adafruit Blinka:
Install the gpio library, as well as the python bindings for the gpiod library, which Adafruit Blinka requires:
apt install -y libgpiod2 python3-libgpiod gpiod
wget https://raw.githubusercontent.com/adafruit/Raspberry-Pi-Installer-Scripts/master/libgpiod.sh
chmod +x libgpiod.sh
./libgpiod.sh
Gpiod accesses GPIO using something called chardev, instead of sysfs (the old way to do it, which was deprecated in Linux kernel 4.8). There is a good explanation of how this works here.
Install Adafruit Blinka with pip:
pip3 install Adafruit-Blinka
Find Adafruit Blinka's Installation directory:
pip3 show Adafruit-Blinka
Clone Adafruit Blinka from Avnet's fork of the repository that includes updates for MaaXBoard:
git clone https://github.com/Avnet/Adafruit_Blinka
Now copy all files in the github repo you just cloned that are under Adafruit_Blinka/src into Adafruit Blinka's installation directory:
sudo cp -R /home/ebv/Adafruit_Blinka/src/* /usr/local/lib/python3.7/dist-packagest
Great! Now Adafruit Blinka should work on your MaaXBoard, and you should be able to access GPIO and I2C devices with just a couple lines of python. We can now use our accelerometer.
Test the accelerometerSet up the accelerometer with your MaaXBoard. The i2c pins on the Pi Header are:
- SDA: pin 3 (D2)
- SCL: pin 5 (D3)
Connect the pins as in the image below (yes, it's a raspberry pi, not a MaaXBoard, but the header is the same):
- Connect ground on MaaXBoard's pi header to GND on the MPU6050.
- Connect SCL on MaaXBoard's pi header to SCL on the MPU6050.
- Connect SDA on MaaXBoard's pi header to SDA on the MPU6050.
- Connect power (3.3V) on MaaXBoard's pi header to VCC on the MPU6050.
Install the mpu6050 circuitpython library using pip:
pip3 install adafruit-circuitpython-mpu6050
Note: if you have a different accelerometer, you can find circuitpython libraries for many commonly used accelerometers under Adafruit's github in the CircuitPython bundle.
You can make sure that it's working by running this example program. If everything is working, you should see this output:
Here are the commands to install the Edge Impulse Linux SDK and CLI on MaaXBoard (note, this takes about an hour):
set -e
sudo apt install curl
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install -y nodejs
node -v
sudo apt install -y gcc g++ make build-essential pkg-config glib2.0-dev libexpat1-dev sox v4l-utils libjpeg62-turbo-dev
wget https://github.com/libvips/libvips/releases/download/v8.10.5/vips-8.10.5.tar.gz
tar xf vips-8.10.5.tar.gz
cd vips-8.10.5
./configure
make -j
sudo make install
sudo ldconfig
sudo npm install edge-impulse-cli -g --unsafe-perm=true
sudo npm install edge-impulse-linux -g --unsafe-perm=true
Once that's done, to run Edge Impulse on your MaaXBoard, simply type:
edge-impulse-linux --disable-camera
This should let you login to your Edge Impulse account and select your Motion Recognition Project to connect to.
You can now connect your MaaXBoard to your the Edge Impulse project that you created earlier.
Note: If you see the error "Failed to start device monitor!" you may need to make sure that your home and user directory are owned by you, e.g.sudo chown -R $(whoami) $HOME
Unfortunately, even though your MaaXBoard now appears under your data acquisition tab in Edge Impulse, the only available sensors that can be controlled from the GUI are the camera and microphone.
In order to get the accelerometer data into Edge Impulse, we'll work some Python magic.
Install the Edge Impulse Python Linux SDKInstall the dependencies:
sudo apt-get install libatlas-base-dev libportaudio2 libportaudiocpp0 portaudio19-dev -y
And now you can install the Edge Impulse PythonLinux SDK:
pip3 install edge_impulse_linux
Finally, if desired, you can clone the Linux SDK Python repository from github to get examples:
git clone https://github.com/edgeimpulse/linux-sdk-python
In the Edge Impulse Python github, under 'examples/custom", there is an example named collect.py. I've modified it to work with the Adafruit Blinka library, and it's attached under the code section of this project.
It will still need a few changes:
- You'll need to edit it to include your HMAC Key and API key.
- You may want to change the collection interval to be longer or shorter (default is currently 2 seconds).
- Finally, make sure you change the variable 'x-file-name' to be the name of the class of motion you're planning to classify.
Before running it, install the module "requests:"
pip3 install requests
When you're ready to collect data, simply run the python code:
python3 collect.py
You'll see your data appear under the data acquisition tab in Edge Impulse:
Continue doing this until you have enough data to train a model.
Set up and train your model in Edge ImpulseThere are lots of excellent tutorials for how to train your model in Edge Impulse, not to mention Shawn's Coursera course. I really can't give a better explanation than he does, so I'll let you read that on your own.
Test your model in Edge ImpulseSadly, we're not able to do live classification for our accelerometer project from the GUI, because it isn't able to recognize the accelerometer currently. We'll have to skip this step and head straight to deploying our model.
Deploy your model to the edgeOnce your model has been trained and validated in Edge Impulse, it's time to deploy it to your MaaXBoard! On the deployment tab, you'll see an option for Linux Boards, and once you select that, you can choose between
You're able to get started classifying right away without any further ado by downloading the model onto your MaaXBoard:
edge-impulse-linux-runner --download modelfile.eim
You'll now see your model file from Edge Impulse, modelfile.eim, on your MaaXBoard.
Classify data with your modelOnce the model has been done, you'll need to start collecting data, or "raw features, " that can be classified. You'll want the same type of raw features that you uploaded into Edge Impulse earlier: a short sample of X, Y, and Z values from your accelerometer. In the attached collect_features.py script, I'm saving a 2 second accelerometer reading as comma separated strings into a file called 'features.txt.'
To generate features that can be classified, run:
python3 collect_features.py
These can then be read when you run classify.py. It's in the github repo that you download earlier, so you can run it like this:
python3 /linux-sdk-python/examples/custom/classify.py modelfile.eim features.txt
Woo! It's working! As you can see, the model is 99.9% sure that the features I am sampling show the accelerometer in an idle position.
Comments