Skip to content

Instantly share code, notes, and snippets.

@ZackAkil
Created September 25, 2020 12:52
Show Gist options
  • Save ZackAkil/cff862faa05db74b7030663368703813 to your computer and use it in GitHub Desktop.
Save ZackAkil/cff862faa05db74b7030663368703813 to your computer and use it in GitHub Desktop.
How to compile a tensorflow lite model to run on edge TPU all inside of a Colab notebook.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Edge TPU compiler.ipynb",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/ZackAkil/cff862faa05db74b7030663368703813/edge-tpu-compiler.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "oQada23FSQyh",
"colab_type": "text"
},
"source": [
"# Compile TFLite model for Edge TPU\n",
"https://coral.ai/docs/edgetpu/compiler/#download"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BzblxwBPUa06",
"colab_type": "text"
},
"source": [
"Install the Edge TPU compiler"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8otJXBdXQqF8",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"outputId": "1cce533d-9943-4760-e7b5-734227886395"
},
"source": [
"%%bash\n",
"curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -\n",
"echo \"deb https://packages.cloud.google.com/apt coral-edgetpu-stable main\" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list\n",
"sudo apt-get update\n",
"sudo apt-get install edgetpu-compiler"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"OK\n",
"deb https://packages.cloud.google.com/apt coral-edgetpu-stable main\n",
"Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]\n",
"Get:2 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/ InRelease [3,626 B]\n",
"Ign:3 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease\n",
"Ign:4 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease\n",
"Get:5 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release [697 B]\n",
"Get:6 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release [564 B]\n",
"Get:7 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Release.gpg [836 B]\n",
"Get:8 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release.gpg [833 B]\n",
"Hit:9 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease\n",
"Hit:10 http://archive.ubuntu.com/ubuntu bionic InRelease\n",
"Get:11 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [1,115 kB]\n",
"Get:12 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]\n",
"Get:13 http://ppa.launchpad.net/marutter/c2d4u3.5/ubuntu bionic InRelease [15.4 kB]\n",
"Ign:14 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages\n",
"Get:14 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 Packages [306 kB]\n",
"Get:15 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [907 kB]\n",
"Get:16 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Packages [47.9 kB]\n",
"Get:17 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [126 kB]\n",
"Get:18 https://packages.cloud.google.com/apt coral-edgetpu-stable InRelease [6,332 B]\n",
"Get:19 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]\n",
"Get:20 https://packages.cloud.google.com/apt coral-edgetpu-stable/main amd64 Packages [1,284 B]\n",
"Get:21 http://ppa.launchpad.net/marutter/c2d4u3.5/ubuntu bionic/main Sources [1,876 kB]\n",
"Get:22 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [33.9 kB]\n",
"Get:23 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [1,406 kB]\n",
"Get:24 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1,434 kB]\n",
"Get:25 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [146 kB]\n",
"Get:26 http://ppa.launchpad.net/marutter/c2d4u3.5/ubuntu bionic/main amd64 Packages [906 kB]\n",
"Fetched 8,586 kB in 3s (3,112 kB/s)\n",
"Reading package lists...\n",
"Reading package lists...\n",
"Building dependency tree...\n",
"Reading state information...\n",
"The following additional packages will be installed:\n",
" libedgetpu1-std\n",
"The following NEW packages will be installed:\n",
" edgetpu-compiler libedgetpu1-std\n",
"0 upgraded, 2 newly installed, 0 to remove and 53 not upgraded.\n",
"Need to get 6,770 kB of archives.\n",
"After this operation, 25.5 MB of additional disk space will be used.\n",
"Get:1 https://packages.cloud.google.com/apt coral-edgetpu-stable/main amd64 libedgetpu1-std amd64 14.1 [311 kB]\n",
"Get:2 https://packages.cloud.google.com/apt coral-edgetpu-stable/main amd64 edgetpu-compiler amd64 14.1 [6,458 kB]\n",
"Fetched 6,770 kB in 1s (7,603 kB/s)\n",
"Selecting previously unselected package libedgetpu1-std:amd64.\r\n",
"(Reading database ... \r(Reading database ... 5%\r(Reading database ... 10%\r(Reading database ... 15%\r(Reading database ... 20%\r(Reading database ... 25%\r(Reading database ... 30%\r(Reading database ... 35%\r(Reading database ... 40%\r(Reading database ... 45%\r(Reading database ... 50%\r(Reading database ... 55%\r(Reading database ... 60%\r(Reading database ... 65%\r(Reading database ... 70%\r(Reading database ... 75%\r(Reading database ... 80%\r(Reading database ... 85%\r(Reading database ... 90%\r(Reading database ... 95%\r(Reading database ... 100%\r(Reading database ... 144676 files and directories currently installed.)\r\n",
"Preparing to unpack .../libedgetpu1-std_14.1_amd64.deb ...\r\n",
"Unpacking libedgetpu1-std:amd64 (14.1) ...\r\n",
"Selecting previously unselected package edgetpu-compiler.\r\n",
"Preparing to unpack .../edgetpu-compiler_14.1_amd64.deb ...\r\n",
"Unpacking edgetpu-compiler (14.1) ...\r\n",
"Setting up libedgetpu1-std:amd64 (14.1) ...\r\n",
"Setting up edgetpu-compiler (14.1) ...\r\n",
"Processing triggers for libc-bin (2.27-3ubuntu1.2) ...\r\n",
"/sbin/ldconfig.real: /usr/local/lib/python3.6/dist-packages/ideep4py/lib/libmkldnn.so.0 is not a symbolic link\r\n",
"\r\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
" Dload Upload Total Spent Left Speed\n",
"\r 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\r100 653 100 653 0 0 15186 0 --:--:-- --:--:-- --:--:-- 15186\n",
"Warning: apt-key output should not be parsed (stdout is not a terminal)\n",
"debconf: unable to initialize frontend: Dialog\n",
"debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, <> line 2.)\n",
"debconf: falling back to frontend: Readline\n",
"debconf: unable to initialize frontend: Readline\n",
"debconf: (This frontend requires a controlling tty.)\n",
"debconf: falling back to frontend: Teletype\n",
"dpkg-preconfigure: unable to re-open stdin: \n"
],
"name": "stderr"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kPUZoFOcUeRp",
"colab_type": "text"
},
"source": [
"Compile a model .tflite file that has been uploaded into colab"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OG10oJxZQ3ps",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "8c0fadb5-f1d2-43f8-8afc-2922b8ceb4c6"
},
"source": [
"%%bash\n",
"edgetpu_compiler model.tflite"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"text": [
"Edge TPU Compiler version 14.1.317412892\n",
"\n",
"Model compiled successfully in 971 ms.\n",
"\n",
"Input model: model.tflite\n",
"Input size: 2.61MiB\n",
"Output model: model_edgetpu.tflite\n",
"Output size: 3.43MiB\n",
"On-chip memory used for caching model parameters: 2.94MiB\n",
"On-chip memory remaining for caching model parameters: 3.22MiB\n",
"Off-chip memory used for streaming uncached model parameters: 0.00B\n",
"Number of Edge TPU subgraphs: 1\n",
"Total number of operations: 294\n",
"Operation log: model_edgetpu.log\n",
"\n",
"Model successfully compiled but not all operations are supported by the Edge TPU. A percentage of the model will instead run on the CPU, which is slower. If possible, consider updating your model to use only operations supported by the Edge TPU. For details, visit g.co/coral/model-reqs.\n",
"Number of operations that will run on Edge TPU: 132\n",
"Number of operations that will run on CPU: 162\n",
"See the operation log file for individual operation details.\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "h-XNI5qGQ4T9",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment