Head hardhat image
Client devices
FL server to Edge Impulse
Dataset folders
datasets
folder, requirements_client.txt
and client.py
. You _could_clone the repository on the client devices, but this will load unnecessary files on them.
First, we need computers for the server and clients. You can also use the same computer as both a server and client, provided the computer has enough resources to do that. The minimum number of required clients is two for the Federated Learning to start. This minimum number can be modified in the server.py
code, but remember to also modify the client.py
code to load datasets for the additional clients.
I decided to use my personal computer as the server and also as one client device. For the other client device, I decided to use a Raspberry Pi 4 with 4GB of RAM.
A Raspberry Pi 4
In my test with Raspberry Pi 3’s running as the client devices, they managed to train a model but failed at the model evaluation process. This can be related to the fact that the Raspberry Pi 3 is more resource constrained than the Raspberry Pi 4, with a less powerful CPU and less RAM. Using the top
command on the Raspberry Pi 3’s showed that the CPU and RAM usage were at max capacity during the training process. When it reached the evaluation process, the RAM usage decreased to around 80%, CPU usage dropped to around 40%, but then the Federated Learning framework disconnected the Raspberry Pi 3 client devices. The Raspberry Pi 3’s also showed 92% CPU usage and 45% RAM usage when they were connecting as the client devices.
Next, we need to install dependencies on the devices. The difference between the server and client dependencies is that the server computer uses the Edge Impulse Python SDK for profiling and deploying the model. We can install dependencies on the server computer by running the command below on a terminal or a Command Prompt (CMD):
Installing dependencies
server_address
value in both server.py
and client.py
with the IP address of the device running as the server. If you get an error message from server.py
that says _ERROR_MESSAGE_PORT_BINDING_FAILED
, change the server’s port to another one that is available.
Server_address variable
ei.API_KEY
variable in server.py
.
Edge Impulse API keys
python server.py
. The server will load the test images, initialize the global model parameters, evaluate the initial model’s parameters, and then wait until at least two clients join before starting the Federated Learning.
Running server.py
python client.py --client_number=1
in a Command Prompt (CMD). When running the client scripts we use the argument client_number
to enable the script to load different datasets for each client using the two folders with the client’s dataset.
Running client 1
python client.py --client_number=2
.
Running client 2
Clients logs
Server FL logs
Server testing model
Edge Impulse profiling
Edge Impulse Studio Dashboard
Upload model Step 2
Save model successful
Model testing
Upload data
dataset_test
directory on your computer from where you cloned the GitHub repository to. Next, select “Testing” for the upload category since we have already trained a model and therefore there is no need to have training data. Next, for Label we select “Leave data unlabeled”. Finally, click “Upload data” and the images will be uploaded to the project. The uploaded images can be seen by going to “Test” in Data acquisition.
Uploading images
Test images
Kebab menu
Labeling images
Model testing performance
Raspberry Pi and camera
sudo raspi-config
and use the cursor keys to select and open Interfacing Options, then select Camera, and follow the prompt to enable the camera. Finally, reboot the Raspberry Pi by running the command sudo reboot
.
Once rebooted, we can download the final global model from the Edge Impulse project by running the command below. You will be prompted to input your username and password for your Edge Impulse account, followed by a prompt to select the Edge Impulse project.
modelfile
.
We can go to the provided URL (Raspberry Pi’s IP address at port 4912) and we will see the feed being captured by the camera as well as the model’s predictions. At this point I used a 3D printed support to hold the Raspberry Pi camera upright and then projected the test images to the camera.
Raspberry Pi with camera
Raspberry Pi with camera - side view
Live classification GIF