blob: cd63df3804a7535581fe1a871744acb6ec1a08e9 [file] [log] [blame]
Scott Todd02817212021-05-19 11:05:14 -07001{
2 "nbformat": 4,
3 "nbformat_minor": 0,
4 "metadata": {
5 "colab": {
6 "name": "tensorflow_hub_import.ipynb",
7 "provenance": [],
8 "collapsed_sections": [
Scott Todd7c611b92021-06-11 14:31:28 -07009 "-V0X0E7LkEa4",
10 "FH3IRpYTta2v"
Scott Todd02817212021-05-19 11:05:14 -070011 ]
12 },
13 "kernelspec": {
14 "name": "python3",
15 "display_name": "Python 3"
16 },
17 "language_info": {
18 "name": "python"
19 }
20 },
21 "cells": [
22 {
23 "cell_type": "markdown",
24 "metadata": {
Scott Todd7c611b92021-06-11 14:31:28 -070025 "id": "FH3IRpYTta2v"
Scott Todd02817212021-05-19 11:05:14 -070026 },
27 "source": [
Scott Todd7c611b92021-06-11 14:31:28 -070028 "##### Copyright 2021 The IREE Authors"
Scott Todd02817212021-05-19 11:05:14 -070029 ]
30 },
31 {
Scott Todd7c611b92021-06-11 14:31:28 -070032 "cell_type": "code",
33 "metadata": {
34 "id": "mWGa71_Ct2ug",
35 "cellView": "form"
36 },
37 "source": [
38 "#@title Licensed under the Apache License v2.0 with LLVM Exceptions.\n",
39 "# See https://llvm.org/LICENSE.txt for license information.\n",
40 "# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception"
41 ],
42 "execution_count": 1,
43 "outputs": []
44 },
45 {
Scott Todd02817212021-05-19 11:05:14 -070046 "cell_type": "markdown",
47 "metadata": {
48 "id": "Qb3S0mSjpK7J"
49 },
50 "source": [
51 "# IREE TensorFlow Hub Import\n",
52 "\n",
53 "This notebook demonstrates how to download, import, and compile models from [TensorFlow Hub](https://tfhub.dev/). It covers:\n",
54 "\n",
55 "* Downloading a model from TensorFlow Hub\n",
56 "* Ensuring the model has serving signatures needed for import\n",
57 "* Importing and compiling the model with IREE\n",
58 "\n",
59 "At the end of the notebook, the compilation artifacts are compressed into a .zip file for you to download and use in an application.\n",
60 "\n",
61 "See also https://google.github.io/iree/ml-frameworks/tensorflow/."
62 ]
63 },
64 {
65 "cell_type": "markdown",
66 "metadata": {
67 "id": "9rNAJKNVkKOr"
68 },
69 "source": [
70 "## Setup"
71 ]
72 },
73 {
74 "cell_type": "code",
75 "metadata": {
76 "id": "RdVc4TbOkHM2"
77 },
78 "source": [
79 "%%capture\n",
Stella Laurenzo0b0a4d82022-01-06 16:03:57 -080080 "!python -m pip install iree-compiler iree-runtime iree-tools-tf -f https://github.com/google/iree/releases"
Scott Todd02817212021-05-19 11:05:14 -070081 ],
Scott Todd7c611b92021-06-11 14:31:28 -070082 "execution_count": 2,
Scott Todd02817212021-05-19 11:05:14 -070083 "outputs": []
84 },
85 {
86 "cell_type": "code",
87 "metadata": {
88 "id": "qRwv3qI_l5O_",
89 "colab": {
90 "base_uri": "https://localhost:8080/"
91 },
Scott Todd7c611b92021-06-11 14:31:28 -070092 "outputId": "31418cc7-ecd5-4c73-c595-ff1d6caf03cc"
Scott Todd02817212021-05-19 11:05:14 -070093 },
94 "source": [
95 "import os\n",
96 "import tensorflow as tf\n",
97 "import tensorflow_hub as hub\n",
98 "import tempfile\n",
99 "from IPython.display import clear_output\n",
100 "\n",
101 "from iree.compiler import tf as tfc\n",
102 "\n",
103 "# Print version information for future notebook users to reference.\n",
104 "print(\"TensorFlow version: \", tf.__version__)\n",
105 "\n",
106 "ARTIFACTS_DIR = os.path.join(tempfile.gettempdir(), \"iree\", \"colab_artifacts\")\n",
107 "os.makedirs(ARTIFACTS_DIR, exist_ok=True)\n",
108 "print(f\"Using artifacts directory '{ARTIFACTS_DIR}'\")"
109 ],
Scott Todd7c611b92021-06-11 14:31:28 -0700110 "execution_count": 3,
Scott Todd02817212021-05-19 11:05:14 -0700111 "outputs": [
112 {
113 "output_type": "stream",
114 "text": [
Scott Toddffda4982021-06-07 11:50:55 -0700115 "TensorFlow version: 2.5.0\n",
Scott Todd02817212021-05-19 11:05:14 -0700116 "Using artifacts directory '/tmp/iree/colab_artifacts'\n"
117 ],
118 "name": "stdout"
119 }
120 ]
121 },
122 {
123 "cell_type": "markdown",
124 "metadata": {
125 "id": "ZZAobcAhocFE"
126 },
127 "source": [
128 "## Import pretrained [`mobilenet_v2`](https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4) model\n",
129 "\n",
130 "IREE supports importing TensorFlow 2 models exported in the [SavedModel](https://www.tensorflow.org/guide/saved_model) format. This model we'll be importing is published in that format already, while other models may need to be converted first.\n",
131 "\n",
132 "MobileNet V2 is a family of neural network architectures for efficient on-device image classification and related tasks. This TensorFlow Hub module contains a trained instance of one particular network architecture packaged to perform image classification."
133 ]
134 },
135 {
136 "cell_type": "code",
137 "metadata": {
138 "colab": {
139 "base_uri": "https://localhost:8080/"
140 },
141 "id": "7fd0vmnloZo9",
Scott Todd7c611b92021-06-11 14:31:28 -0700142 "outputId": "fcb260e8-672a-479d-c428-2a93d306d4b2"
Scott Todd02817212021-05-19 11:05:14 -0700143 },
144 "source": [
145 "#@title Download the pretrained model\n",
146 "\n",
147 "# Use the `hub` library to download the pretrained model to the local disk\n",
148 "# https://www.tensorflow.org/hub/api_docs/python/hub\n",
149 "HUB_PATH = \"https://tfhub.dev/google/tf2-preview/mobilenet_v2/classification/4\"\n",
150 "model_path = hub.resolve(HUB_PATH)\n",
151 "print(f\"Downloaded model from tfhub to path: '{model_path}'\")"
152 ],
Scott Todd7c611b92021-06-11 14:31:28 -0700153 "execution_count": 4,
Scott Todd02817212021-05-19 11:05:14 -0700154 "outputs": [
155 {
156 "output_type": "stream",
157 "text": [
158 "Downloaded model from tfhub to path: '/tmp/tfhub_modules/426589ad685896ab7954855255a52db3442cb38d'\n"
159 ],
160 "name": "stdout"
161 }
162 ]
163 },
164 {
165 "cell_type": "markdown",
166 "metadata": {
167 "id": "CedNRSQTOE7C"
168 },
169 "source": [
170 "### Check for serving signatures and re-export as needed\n",
171 "\n",
172 "IREE's compiler tools, like TensorFlow's `saved_model_cli` and other tools, require \"serving signatures\" to be defined in SavedModels.\n",
173 "\n",
174 "More references:\n",
175 "\n",
176 "* https://www.tensorflow.org/tfx/serving/signature_defs\n",
177 "* https://blog.tensorflow.org/2021/03/a-tour-of-savedmodel-signatures.html"
178 ]
179 },
180 {
181 "cell_type": "code",
182 "metadata": {
183 "colab": {
184 "base_uri": "https://localhost:8080/"
185 },
186 "id": "qiO66oEYQmsd",
Scott Todd7c611b92021-06-11 14:31:28 -0700187 "outputId": "5d7e4f7d-e77e-45f1-c37e-11e38ed05ce2"
Scott Todd02817212021-05-19 11:05:14 -0700188 },
189 "source": [
190 "#@title Check for serving signatures\n",
191 "\n",
192 "# Load the SavedModel from the local disk and check if it has serving signatures\n",
193 "# https://www.tensorflow.org/guide/saved_model#loading_and_using_a_custom_model\n",
194 "loaded_model = tf.saved_model.load(model_path)\n",
195 "serving_signatures = list(loaded_model.signatures.keys())\n",
196 "print(f\"Loaded SavedModel from '{model_path}'\")\n",
197 "print(f\"Serving signatures: {serving_signatures}\")\n",
198 "\n",
199 "# Also check with the saved_model_cli:\n",
200 "print(\"\\n---\\n\")\n",
201 "print(\"Checking for signature_defs using saved_model_cli:\\n\")\n",
202 "!saved_model_cli show --dir {model_path} --tag_set serve --signature_def serving_default"
203 ],
Scott Todd7c611b92021-06-11 14:31:28 -0700204 "execution_count": 5,
Scott Todd02817212021-05-19 11:05:14 -0700205 "outputs": [
206 {
207 "output_type": "stream",
208 "text": [
209 "Loaded SavedModel from '/tmp/tfhub_modules/426589ad685896ab7954855255a52db3442cb38d'\n",
210 "Serving signatures: []\n",
211 "\n",
212 "---\n",
213 "\n",
214 "Checking for signature_defs using saved_model_cli:\n",
215 "\n",
216 "The given SavedModel SignatureDef contains the following input(s):\n",
217 "The given SavedModel SignatureDef contains the following output(s):\n",
218 "Method name is: \n"
219 ],
220 "name": "stdout"
221 }
222 ]
223 },
224 {
225 "cell_type": "markdown",
226 "metadata": {
227 "id": "kKqqX2LsReNz"
228 },
229 "source": [
230 "Since the model we downloaded did not include any serving signatures, we'll re-export it with serving signatures defined.\n",
231 "\n",
232 "* https://www.tensorflow.org/guide/saved_model#specifying_signatures_during_export"
233 ]
234 },
235 {
236 "cell_type": "code",
237 "metadata": {
238 "colab": {
239 "base_uri": "https://localhost:8080/"
240 },
241 "id": "OlDG2OuqOBGC",
Scott Todd7c611b92021-06-11 14:31:28 -0700242 "outputId": "044bb741-a563-4fe2-b0d9-8b47bcc5f286"
Scott Todd02817212021-05-19 11:05:14 -0700243 },
244 "source": [
245 "#@title Look up input signatures to use when exporting\n",
246 "\n",
247 "# To save serving signatures we need to specify a `ConcreteFunction` with a\n",
248 "# TensorSpec signature. We can determine what this signature should be by\n",
249 "# looking at any documentation for the model or running the saved_model_cli.\n",
250 "\n",
251 "!saved_model_cli show --dir {model_path} --all \\\n",
252 " 2> /dev/null | grep \"inputs: TensorSpec\" | tail -n 1"
253 ],
Scott Todd7c611b92021-06-11 14:31:28 -0700254 "execution_count": 6,
Scott Todd02817212021-05-19 11:05:14 -0700255 "outputs": [
256 {
257 "output_type": "stream",
258 "text": [
259 " inputs: TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name=u'inputs')\n"
260 ],
261 "name": "stdout"
262 }
263 ]
264 },
265 {
266 "cell_type": "code",
267 "metadata": {
268 "colab": {
269 "base_uri": "https://localhost:8080/"
270 },
271 "id": "gnb4HhMmkgiT",
Scott Todd7c611b92021-06-11 14:31:28 -0700272 "outputId": "3318bc8e-321c-417f-d9b0-b4cc9dcca8de"
Scott Todd02817212021-05-19 11:05:14 -0700273 },
274 "source": [
275 "#@title Re-export the model using the known signature\n",
276 "\n",
277 "# Get a concrete function using the signature we found above.\n",
278 "# \n",
279 "# The first element of the shape is a dynamic batch size. We'll be running\n",
280 "# inference on a single image at a time, so set it to `1`. The rest of the\n",
281 "# shape is the fixed image dimensions [width=224, height=224, channels=3].\n",
282 "call = loaded_model.__call__.get_concrete_function(tf.TensorSpec([1, 224, 224, 3], tf.float32))\n",
283 "\n",
284 "# Save the model, setting the concrete function as a serving signature.\n",
285 "# https://www.tensorflow.org/guide/saved_model#saving_a_custom_model\n",
286 "resaved_model_path = '/tmp/resaved_model'\n",
287 "tf.saved_model.save(loaded_model, resaved_model_path, signatures=call)\n",
288 "clear_output() # Skip over TensorFlow's output.\n",
289 "print(f\"Saved model with serving signatures to '{resaved_model_path}'\")\n",
290 "\n",
291 "# Load the model back into memory and check that it has serving signatures now\n",
292 "reloaded_model = tf.saved_model.load(resaved_model_path)\n",
293 "reloaded_serving_signatures = list(reloaded_model.signatures.keys())\n",
294 "print(f\"\\nReloaded SavedModel from '{resaved_model_path}'\")\n",
295 "print(f\"Serving signatures: {reloaded_serving_signatures}\")\n",
296 "\n",
297 "# Also check with the saved_model_cli:\n",
298 "print(\"\\n---\\n\")\n",
299 "print(\"Checking for signature_defs using saved_model_cli:\\n\")\n",
300 "!saved_model_cli show --dir {resaved_model_path} --tag_set serve --signature_def serving_default"
301 ],
Scott Todd7c611b92021-06-11 14:31:28 -0700302 "execution_count": 7,
Scott Todd02817212021-05-19 11:05:14 -0700303 "outputs": [
304 {
305 "output_type": "stream",
306 "text": [
307 "Saved model with serving signatures to '/tmp/resaved_model'\n",
308 "\n",
309 "Reloaded SavedModel from '/tmp/resaved_model'\n",
310 "Serving signatures: ['serving_default']\n",
311 "\n",
312 "---\n",
313 "\n",
314 "Checking for signature_defs using saved_model_cli:\n",
315 "\n",
316 "The given SavedModel SignatureDef contains the following input(s):\n",
317 " inputs['inputs'] tensor_info:\n",
318 " dtype: DT_FLOAT\n",
319 " shape: (1, 224, 224, 3)\n",
320 " name: serving_default_inputs:0\n",
321 "The given SavedModel SignatureDef contains the following output(s):\n",
322 " outputs['output_0'] tensor_info:\n",
323 " dtype: DT_FLOAT\n",
324 " shape: (1, 1001)\n",
325 " name: StatefulPartitionedCall:0\n",
326 "Method name is: tensorflow/serving/predict\n"
327 ],
328 "name": "stdout"
329 }
330 ]
331 },
332 {
333 "cell_type": "markdown",
334 "metadata": {
335 "id": "YdmgASzwanSz"
336 },
337 "source": [
338 "### Import and compile the SavedModel with IREE"
339 ]
340 },
341 {
342 "cell_type": "code",
343 "metadata": {
344 "colab": {
345 "base_uri": "https://localhost:8080/"
346 },
347 "id": "GLkjlHE5mdmg",
Scott Todd7c611b92021-06-11 14:31:28 -0700348 "outputId": "d057d2dc-001c-4fe4-cf4b-9517d4cf598e"
Scott Todd02817212021-05-19 11:05:14 -0700349 },
350 "source": [
351 "#@title Import from SavedModel\n",
352 "\n",
353 "# The main output file from compilation is a .vmfb \"VM FlatBuffer\". This file\n",
354 "# can used to run the compiled model with IREE's runtime.\n",
355 "output_file = os.path.join(ARTIFACTS_DIR, \"mobilenet_v2.vmfb\")\n",
356 "# As compilation runs, dump some intermediate .mlir files for future inspection.\n",
357 "tf_input = os.path.join(ARTIFACTS_DIR, \"mobilenet_v2_tf_input.mlir\")\n",
358 "iree_input = os.path.join(ARTIFACTS_DIR, \"mobilenet_v2_iree_input.mlir\")\n",
359 "\n",
360 "# Since our SavedModel uses signature defs, we use `saved_model_tags` with\n",
361 "# `import_type=\"SIGNATURE_DEF\"`. If the SavedModel used an object graph, we\n",
362 "# would use `exported_names` with `import_type=\"OBJECT_GRAPH\"` instead.\n",
363 "\n",
364 "# We'll set `target_backends=[\"vmvx\"]` to use IREE's reference CPU backend.\n",
365 "# We could instead use different backends here, or set `import_only=True` then\n",
366 "# download the imported .mlir file for compilation using native tools directly.\n",
367 "\n",
368 "tfc.compile_saved_model(\n",
369 " resaved_model_path,\n",
370 " output_file=output_file,\n",
371 " save_temp_tf_input=tf_input,\n",
372 " save_temp_iree_input=iree_input,\n",
373 " import_type=\"SIGNATURE_DEF\",\n",
374 " saved_model_tags=set([\"serve\"]),\n",
375 " target_backends=[\"vmvx\"])\n",
376 "clear_output() # Skip over TensorFlow's output.\n",
377 "\n",
378 "print(f\"Saved compiled output to '{output_file}'\")\n",
379 "print(f\"Saved tf_input to '{tf_input}'\")\n",
380 "print(f\"Saved iree_input to '{iree_input}'\")"
381 ],
Scott Todd7c611b92021-06-11 14:31:28 -0700382 "execution_count": 8,
Scott Todd02817212021-05-19 11:05:14 -0700383 "outputs": [
384 {
385 "output_type": "stream",
386 "text": [
387 "Saved compiled output to '/tmp/iree/colab_artifacts/mobilenet_v2.vmfb'\n",
388 "Saved tf_input to '/tmp/iree/colab_artifacts/mobilenet_v2_tf_input.mlir'\n",
389 "Saved iree_input to '/tmp/iree/colab_artifacts/mobilenet_v2_iree_input.mlir'\n"
390 ],
391 "name": "stdout"
392 }
393 ]
394 },
395 {
396 "cell_type": "code",
397 "metadata": {
398 "colab": {
399 "base_uri": "https://localhost:8080/",
Scott Toddffda4982021-06-07 11:50:55 -0700400 "height": 102
Scott Todd02817212021-05-19 11:05:14 -0700401 },
402 "id": "IEJAzOb5qASI",
Scott Todd7c611b92021-06-11 14:31:28 -0700403 "outputId": "52da0e1f-a512-4ee8-ea65-e716de9a3cf2"
Scott Todd02817212021-05-19 11:05:14 -0700404 },
405 "source": [
406 "#@title Download compilation artifacts\n",
407 "\n",
Scott Toddffda4982021-06-07 11:50:55 -0700408 "ARTIFACTS_ZIP = \"/tmp/mobilenet_colab_artifacts.zip\"\n",
Scott Todd02817212021-05-19 11:05:14 -0700409 "\n",
410 "print(f\"Zipping '{ARTIFACTS_DIR}' to '{ARTIFACTS_ZIP}' for download...\")\n",
Scott Toddffda4982021-06-07 11:50:55 -0700411 "!cd {ARTIFACTS_DIR} && zip -r {ARTIFACTS_ZIP} .\n",
Scott Todd02817212021-05-19 11:05:14 -0700412 "\n",
413 "# Note: you can also download files using the file explorer on the left\n",
Scott Toddb5b90262021-06-28 16:29:43 -0700414 "try:\n",
415 " from google.colab import files\n",
416 " print(\"Downloading the artifacts zip file...\")\n",
417 " files.download(ARTIFACTS_ZIP)\n",
418 "except ImportError:\n",
419 " print(\"Missing google_colab Python package, can't download files\")"
Scott Todd02817212021-05-19 11:05:14 -0700420 ],
Scott Todd7c611b92021-06-11 14:31:28 -0700421 "execution_count": 9,
Scott Todd02817212021-05-19 11:05:14 -0700422 "outputs": [
423 {
424 "output_type": "stream",
425 "text": [
Scott Toddffda4982021-06-07 11:50:55 -0700426 "Zipping '/tmp/iree/colab_artifacts' to '/tmp/mobilenet_colab_artifacts.zip' for download...\n",
Scott Toddffda4982021-06-07 11:50:55 -0700427 " adding: mobilenet_v2.vmfb (deflated 8%)\n",
Scott Todd7c611b92021-06-11 14:31:28 -0700428 " adding: mobilenet_v2_iree_input.mlir (deflated 46%)\n",
Scott Toddffda4982021-06-07 11:50:55 -0700429 " adding: mobilenet_v2_tf_input.mlir (deflated 47%)\n",
Scott Todd02817212021-05-19 11:05:14 -0700430 "Downloading the artifacts zip file...\n"
431 ],
432 "name": "stdout"
433 },
434 {
435 "output_type": "display_data",
436 "data": {
437 "application/javascript": [
438 "\n",
439 " async function download(id, filename, size) {\n",
440 " if (!google.colab.kernel.accessAllowed) {\n",
441 " return;\n",
442 " }\n",
443 " const div = document.createElement('div');\n",
444 " const label = document.createElement('label');\n",
445 " label.textContent = `Downloading \"${filename}\": `;\n",
446 " div.appendChild(label);\n",
447 " const progress = document.createElement('progress');\n",
448 " progress.max = size;\n",
449 " div.appendChild(progress);\n",
450 " document.body.appendChild(div);\n",
451 "\n",
452 " const buffers = [];\n",
453 " let downloaded = 0;\n",
454 "\n",
455 " const channel = await google.colab.kernel.comms.open(id);\n",
456 " // Send a message to notify the kernel that we're ready.\n",
457 " channel.send({})\n",
458 "\n",
459 " for await (const message of channel.messages) {\n",
460 " // Send a message to notify the kernel that we're ready.\n",
461 " channel.send({})\n",
462 " if (message.buffers) {\n",
463 " for (const buffer of message.buffers) {\n",
464 " buffers.push(buffer);\n",
465 " downloaded += buffer.byteLength;\n",
466 " progress.value = downloaded;\n",
467 " }\n",
468 " }\n",
469 " }\n",
470 " const blob = new Blob(buffers, {type: 'application/binary'});\n",
471 " const a = document.createElement('a');\n",
472 " a.href = window.URL.createObjectURL(blob);\n",
473 " a.download = filename;\n",
474 " div.appendChild(a);\n",
475 " a.click();\n",
476 " div.remove();\n",
477 " }\n",
478 " "
479 ],
480 "text/plain": [
481 "<IPython.core.display.Javascript object>"
482 ]
483 },
484 "metadata": {
485 "tags": []
486 }
487 },
488 {
489 "output_type": "display_data",
490 "data": {
491 "application/javascript": [
Scott Todd7c611b92021-06-11 14:31:28 -0700492 "download(\"download_173f97e9-1e2d-401a-8aad-646463432451\", \"mobilenet_colab_artifacts.zip\", 59101356)"
Scott Todd02817212021-05-19 11:05:14 -0700493 ],
494 "text/plain": [
495 "<IPython.core.display.Javascript object>"
496 ]
497 },
498 "metadata": {
499 "tags": []
500 }
501 }
502 ]
503 }
504 ]
Scott Toddffda4982021-06-07 11:50:55 -0700505}