By Ms Lisa Ong, Principal Lecturer & Consultant, Software Systems Practice, NUS-ISS, for NUS-ISS e-Open House 2020 -Classroom Session: Designing Intelligent Edge Computing (29 Feb )
Designing Intelligent Edge Computing Day 4 Workshop
1. 1
DIEC Day 3 Workshop, last saved: 12/12/2019
Designing Intelligent Edge Computing
Workshop 4: Edge Device Online Training
In this workshop, you do online training of your own gesture detector on a Raspberry Pi, using live
sensor readings from a BBC Micro:bit.
1. Collect data from Micro:bit
2. Train initial model using Google Colab, save a model checkpoint
3. Load model checkpoint on a Raspberry Pi
4. Continue training on the Raspberry Pi, using live sensor data from the Micro:bit
Data Collection
1. Connect a Micro:bit to the laptop using USB
2. Use the Python editor (https://python.microbit.org/v/1.1) to program the Micro:bit with
day4/microbit/device_code.py
3. Find the serial port path associated with the Micro:bit
a. Unplug the Micro:bit
On MacOS, use `ls /dev/cu.*`
On Windows, open Device Manager and expand "Ports (COM & LPT)"
b. Plug in the Micro:bit again
On MacOS, use `ls /dev/cu.*`, you should see a path that looks similar to
`/dev/cu.usbmodemXXXX`
On Windows, a new COMXX node should appear under "Ports (COM & LPT)"
Exercise 1: Gesture Data Collection
1. Edit day4/acquire_data.py on your laptop to use the serial port path for your Micro:bit
2. conda activate diec
3. pip install pyserial-asyncio
4. python acquire_data.py
5. Perform your gesture
a. Press button A to start the gesture
b. Release button A when gesture finishes
c. Repeat a and b as often as you need (but try to do at least 10 times consistently). Be
careful not to swing the Micro:bit too hard because it can be damaged (still
connected via a USB cable.)
d. If you need to redo, stop the script, delete data.csv and try again.
2. 2
DIEC Day 3 Workshop, last saved: 12/12/2019
Submission:
a. Paste the changes to acquire_data.py to the submission worksheet
b. Paste any sequence of 20 rows of data.csv into the submission worksheet. (Note that you
should use the full dataset to train, the 20 rows is just for submission)
c. Type a short description of your gesture so that you can remember what you did (e.g.
waving right and left) into the submission worksheet.
Train
1. Go to Github: https://github.com/lisaong/diec/tree/master/day4
2. Open edge_online_learning.ipynb from Github.
3. Click the “Open in Colab” button to run the notebook on Google Colab.
4. Walkthrough the notebook step-by-step to train a model using Tensorflow. You will need to
sign into Google first.
Exercise 2: Inspecting the pre-processed data
Follow the notebook prompts to generate a plot of your unique gesture data.
Submission: Paste a plot of your gesture data to the submission worksheet
Deploy
1. Download the model files (*.pkl and *.h5) from Google Colab to your laptop.
a. Note that we are using *.h5, not *.tflite, because h5 supports continuation of model
training.
2. Use SCP to transfer the model files to the ~/diec/day4/rpi folder on the Raspberry Pi. You
may use WinSCP (on Windows) or scp (on MacOS).
Online Training
1. Find the serial path for the device to use on the Raspberry Pi
a. On the Raspberry Pi: ls /dev/ttyA*
b. Switch the Micro:bit device to connect to the Raspberry Pi over USB
c. Re-run this to see the newly added path: ls /dev/ttyA*
Tip: A typical path is: /dev/ttyACM0
Keep the Micro:bit connected to the Raspberry Pi.
2. From the Raspberry Pi, launch the docker container.
cd ~/diec/day4/docker
sh ./launch_docker.sh
3. 3
DIEC Day 3 Workshop, last saved: 12/12/2019
3. From the docker container, verify that you can still see the Micro:bit:
ls /dev/ttyA*
Exercise 3: Online Training using Live Data
1. Examine /code/day4/rpi/incremental_train.py.
2. Run the script (using python3) to incrementally update the model using live data.
cd /code/day4/rpi
python3 incremental_train.py comport
Note: You need to specify the serial port the script will use to collect data for online training.
Submission:
a. Paste a subset of the validation accuracy and validation loss output from running the online
training into the submission worksheet