Licensed under the Apache License, Version 2.0 (the "License")


In [0]:
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

Run Colab on a Coral Dev Board

This tutorial shows you how to run Jupyter notebooks on your Coral Dev Board, and then edit and execute them from your connected computer using the Google Colab interface.

By the end of this tutorial, you'll connect this notebook to your Dev Board and run an inference on the board using code from this page.

    

Requirements

  • You need a Coral Dev Board and it should be set up with the latest software.

    If you run cat /etc/mendel_version on the Dev Board, it should print 4.0 or higher.

  • The computer you're using to read this tutorial must have MDT installed.

Install Jupyter

First you need to install Jupyter on your Dev Board as follows:

  1. Open a shell on your Dev Board:

    mdt shell
    
  2. Install the Python 3 development tools on the board:

    sudo apt-get update
    
    sudo apt-get install python3-dev
    
  3. Install Jupyter:

    pip3 install jupyter
    
  4. Reload the .profile file to access Jupyter's location (at ~/.local/bin):

    source $HOME/.profile
  5. Verify it works by running jupyter --version.

    If this prints command not found, then your Mendel version might be out of date or the Jupyter installation failed.

Start Jupyter on the Dev Board

Before starting Jupyter on the Dev Board, you need to install jupyter_http_over_ws, which enables your host computer to communicate with Jupyter via WebSocket:

pip3 install jupyter_http_over_ws

jupyter serverextension enable --py jupyter_http_over_ws

Once that's done, start Jupyter as follows:

jupyter notebook \
  --NotebookApp.allow_origin='https://colab.research.google.com' \
  --port=8888 \
  --NotebookApp.port_retries=0

This outputs several messages, including a URL such as http://localhost:8888/?token=.... You'll need to come back and copy this token later.

Be sure you leave this terminal open because it's now running the Jupyter notebook.

Start SSH port forwarding

Although Jupyter is now running, it's only accessible from the Dev Board. To access it from the host computer, you need to forward your localhost traffic to the Dev Board with an SSH tunnel. The effect is to make Google Colab think Jupyter notebook is running on your host computer even though it's actually running on the Dev Board.

Open a new terminal on your host computer (where you run mdt shell) and run this command:

ssh -N -L 8888:localhost:8888 mendel@192.168.100.2 -i ~/.config/mdt/keys/mdt.key

If asked, Are you sure you want to continue connecting?, type yes.

If you see a warning that says REMOTE HOST IDENTIFICATION HAS CHANGED, then you need to open the known_hosts file and remove the line specifying the key for 192.168.100.2. Then try again.

Now leave this window active because this command is what keeps the SSH tunnel open between the Dev Board and the host computer.

Connect this Colab to the local runtime

Now login to the Jupyter notebook and connect this Colab:

  1. Open http://localhost:8888 in a browser on the host computer.

  2. You should see a Jupyter notebook page asking for "Password or token" at the top. Paste the token printed by the jupyter notebook command above (just the part following ?token=), and click Log in.

  3. Now, on this tutorial page's Colab interface, click the drop-down arrow next to Connect (near the top-right corner of the web page) and click Connect to local runtime.

  4. Use 8888 as the port and click Connect.

    The button should now say "Connected (Local)".

This notebook (the page you're reading) is now connected to your Dev Board and you can run code from this page on the board!

Run notebook code on the Dev Board

You can execute the code blocks below one at a time or run them all by selecting Runtime > Run all in the toolbar.

First you need to download pre-compiled models and data from the Edge TPU API examples:


In [0]:
! sudo apt-get update

! sudo apt-get install edgetpu-examples

Then you can run an inference with this code:


In [0]:
from edgetpu.classification.engine import ClassificationEngine
from edgetpu.utils import dataset_utils
from PIL import Image

# Prepare labels.
labels = dataset_utils.read_label_file('/usr/share/edgetpu/examples/models/inat_bird_labels.txt')
# Initialize engine.
engine = ClassificationEngine('/usr/share/edgetpu/examples/models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite')
# Run inference.
img = Image.open('/usr/share/edgetpu/examples/images/parrot.jpg')
for result in engine.classify_with_image(img, top_k=3):
 print('---------------------------')
 print(labels[result[0]])
 print('Score : ', result[1])

You should see classification results for "Ara macao (Scarlet Macaw)."

You can now create your own Colab files and connect them to the same local runtime.