Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b6daa1

Browse files
Add colab to compile an uploaded tflite model for Edge TPU.
Nice to have when you don't have Linux handy. PiperOrigin-RevId: 352097199
1 parent f0bac57 commit 3b6daa1

File tree

1 file changed

+255
-0
lines changed

1 file changed

+255
-0
lines changed

‎compile_for_edgetpu.ipynb‎

Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "license"
7+
},
8+
"source": [
9+
"##### *Copyright 2021 Google LLC*\n",
10+
"*Licensed under the Apache License, Version 2.0 (the \"License\")*"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"metadata": {
17+
"cellView": "both",
18+
"id": "rKwqeqWBXANA"
19+
},
20+
"outputs": [],
21+
"source": [
22+
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
23+
"# you may not use this file except in compliance with the License.\n",
24+
"# You may obtain a copy of the License at\n",
25+
"#\n",
26+
"# https://www.apache.org/licenses/LICENSE-2.0\n",
27+
"#\n",
28+
"# Unless required by applicable law or agreed to in writing, software\n",
29+
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
30+
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
31+
"# See the License for the specific language governing permissions and\n",
32+
"# limitations under the License."
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {
38+
"id": "hRTa3Ee15WsJ"
39+
},
40+
"source": [
41+
"# Compile a model for the Edge TPU"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {
47+
"id": "TaX0smDP7xQY"
48+
},
49+
"source": [
50+
"This notebook offers a convenient way to compile a TensorFlow Lite model for the Edge TPU, in case you don't have a system that's compatible with the [Edge TPU Compiler](https://coral.ai/docs/edgetpu/compiler/) (Debian Linux only).\n",
51+
"\n",
52+
"Simply upload a compatible `.tflite` file to this Colab session, run the code below, and then download the compiled model.\n",
53+
"\n",
54+
"For more details about how to create a model that's compatible with the Edge TPU, see the [documentation at coral.ai](https://coral.ai/docs/edgetpu/models-intro/).\n"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {
60+
"id": "viewin-badges"
61+
},
62+
"source": [
63+
"\u003ca href=\"https://colab.research.google.com/github/google-coral/tutorials/blob/master/compile_for_edgetpu.ipynb\" target=\"_parent\"\u003e\u003cimg src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"\u003e\u003c/a\u003e\n",
64+
"\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\n",
65+
"\u003ca href=\"https://github.com/google-coral/tutorials/blob/master/compile_for_edgetpu.ipynb\" target=\"_parent\"\u003e\u003cimg src=\"https://img.shields.io/static/v1?logo=GitHub\u0026label=\u0026color=333333\u0026style=flat\u0026message=View%20on%20GitHub\" alt=\"View in GitHub\"\u003e\u003c/a\u003e\n"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {
71+
"id": "RZatNsloxg8q"
72+
},
73+
"source": [
74+
"## Upload a compatible TF Lite model"
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {
80+
"id": "mkOQJOjMxkmn"
81+
},
82+
"source": [
83+
"To use this script, you need to upload a TensorFlow Lite model that's fully quantized and meets all the [Edge TPU model requirements](https://coral.ai/docs/edgetpu/models-intro/#model-requirements).\n",
84+
"\n",
85+
"With a compatible model in-hand, you can upload it as follows:\n",
86+
"\n",
87+
"1. Click the **Files** tab (the folder icon) in the left panel. (Do not change directories.)\n",
88+
"2. Click **Upload to session storage** (the file icon). \n",
89+
"3. Follow your system UI to select and open your `.tflite` file.\n",
90+
" \n",
91+
" When it's uploaded, you should see the file appear in the left panel.\n",
92+
"4. Replace `example.tflite` with your uploaded model's filename:"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": null,
98+
"metadata": {
99+
"id": "itv0Kj7N6ALw"
100+
},
101+
"outputs": [],
102+
"source": [
103+
"%env TFLITE_FILE=example.tflite"
104+
]
105+
},
106+
{
107+
"cell_type": "markdown",
108+
"metadata": {
109+
"id": "kafLbVJPxwL_"
110+
},
111+
"source": [
112+
"5. Now click **Runtime \u003e Run all** in the Colab toolbar."
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {
118+
"id": "9JPzsCxm05Qj"
119+
},
120+
"source": [
121+
"## Get the Edge TPU Compiler"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {
128+
"id": "p6ZpWgrk21Ad"
129+
},
130+
"outputs": [],
131+
"source": [
132+
"! curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -\n",
133+
"\n",
134+
"! echo \"deb https://packages.cloud.google.com/apt coral-edgetpu-stable main\" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list\n",
135+
"\n",
136+
"! sudo apt-get update\n",
137+
"\n",
138+
"! sudo apt-get install edgetpu-compiler\t"
139+
]
140+
},
141+
{
142+
"cell_type": "markdown",
143+
"metadata": {
144+
"id": "mtPcYiER3Ymp"
145+
},
146+
"source": [
147+
"## Compile the model"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"metadata": {
154+
"id": "joxrIB0I3cdi"
155+
},
156+
"outputs": [],
157+
"source": [
158+
"! edgetpu_compiler $TFLITE_FILE"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"metadata": {
164+
"id": "7R8JMQc1MMm5"
165+
},
166+
"source": [
167+
"The compiled model uses the same filename but with \"_edgetpu\" appended at the end.\n",
168+
"\n",
169+
"If the compilation failed, check the **Files** panel on the left for the `.log` file that contains more details. (You might need to click the **Refresh** button to see the new files.)\n"
170+
]
171+
},
172+
{
173+
"cell_type": "markdown",
174+
"metadata": {
175+
"id": "Oi9-Voc8A7VK"
176+
},
177+
"source": [
178+
"## Download the model"
179+
]
180+
},
181+
{
182+
"cell_type": "markdown",
183+
"metadata": {
184+
"id": "XiugMm-jBbWl"
185+
},
186+
"source": [
187+
"You can download the converted model from Colab with this: "
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": null,
193+
"metadata": {
194+
"id": "x47uW_lI1DoV"
195+
},
196+
"outputs": [],
197+
"source": [
198+
"import os\n",
199+
"from google.colab import files\n",
200+
"\n",
201+
"name = os.path.splitext(os.environ['TFLITE_FILE'])[0]\n",
202+
"files.download(str(name + '_edgetpu.tflite'))"
203+
]
204+
},
205+
{
206+
"cell_type": "markdown",
207+
"metadata": {
208+
"id": "_qOCP3mXXvsm"
209+
},
210+
"source": [
211+
"If you get a \"Failed to fetch\" error here, it's probably because the files weren't done saving. So just wait a moment and try again."
212+
]
213+
},
214+
{
215+
"cell_type": "markdown",
216+
"metadata": {
217+
"id": "_TZTwG7nhm0C"
218+
},
219+
"source": [
220+
"## Run the model on the Edge TPU\n",
221+
"\n",
222+
"\n"
223+
]
224+
},
225+
{
226+
"cell_type": "markdown",
227+
"metadata": {
228+
"id": "RwywT4ZpQjLf"
229+
},
230+
"source": [
231+
"You can now run the model on your Coral device with acceleration on the Edge TPU.\n",
232+
"\n",
233+
"Check out some examples for running inference at [coral.ai/examples](https://coral.ai/examples/#code-examples/)."
234+
]
235+
}
236+
],
237+
"metadata": {
238+
"accelerator": "GPU",
239+
"colab": {
240+
"collapsed_sections": [
241+
"license"
242+
],
243+
"name": "compile_for_edgetpu.ipynb",
244+
"private_outputs": true,
245+
"provenance": [],
246+
"toc_visible": true
247+
},
248+
"kernelspec": {
249+
"display_name": "Python 3",
250+
"name": "python3"
251+
}
252+
},
253+
"nbformat": 4,
254+
"nbformat_minor": 0
255+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /