blob: 7cc5801a1a61711717828b6553d2b6ca2559337b [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 ],
Scott Todd5cd6e1d2023-04-26 12:52:05 -070042 "execution_count": 10,
Scott Todd7c611b92021-06-11 14:31:28 -070043 "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",
Scott Todd8c34b972023-10-24 09:34:49 -070061 "See also https://iree.dev/guides/ml-frameworks/tensorflow/."
Scott Todd02817212021-05-19 11:05:14 -070062 ]
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",
Scott Todd8c34b972023-10-24 09:34:49 -070080 "!python -m pip install iree-compiler iree-runtime iree-tools-tf -f https://iree.dev/pip-release-links.html"
Scott Todd02817212021-05-19 11:05:14 -070081 ],
Scott Todd5cd6e1d2023-04-26 12:52:05 -070082 "execution_count": 11,
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 Todd5cd6e1d2023-04-26 12:52:05 -070092 "outputId": "8d3bf1f1-1843-4fe9-80e0-a9fc5b194778"
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 Todd5cd6e1d2023-04-26 12:52:05 -0700110 "execution_count": 12,
Scott Todd02817212021-05-19 11:05:14 -0700111 "outputs": [
112 {
113 "output_type": "stream",
Scott Todd8ebe0022022-09-29 11:30:28 -0700114 "name": "stdout",
Scott Todd02817212021-05-19 11:05:14 -0700115 "text": [
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700116 "TensorFlow version: 2.12.0\n",
Scott Todd02817212021-05-19 11:05:14 -0700117 "Using artifacts directory '/tmp/iree/colab_artifacts'\n"
Scott Todd8ebe0022022-09-29 11:30:28 -0700118 ]
Scott Todd02817212021-05-19 11:05:14 -0700119 }
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 Todd5cd6e1d2023-04-26 12:52:05 -0700142 "outputId": "dabea3a2-d312-4729-c947-b24216a6c25b"
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 Todd5cd6e1d2023-04-26 12:52:05 -0700153 "execution_count": 13,
Scott Todd02817212021-05-19 11:05:14 -0700154 "outputs": [
155 {
156 "output_type": "stream",
Scott Todd8ebe0022022-09-29 11:30:28 -0700157 "name": "stdout",
Scott Todd02817212021-05-19 11:05:14 -0700158 "text": [
159 "Downloaded model from tfhub to path: '/tmp/tfhub_modules/426589ad685896ab7954855255a52db3442cb38d'\n"
Scott Todd8ebe0022022-09-29 11:30:28 -0700160 ]
Scott Todd02817212021-05-19 11:05:14 -0700161 }
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 Todd5cd6e1d2023-04-26 12:52:05 -0700187 "outputId": "91f724db-01cd-4dd3-c55c-ba4431233cfa"
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 Todd5cd6e1d2023-04-26 12:52:05 -0700204 "execution_count": 14,
Scott Todd02817212021-05-19 11:05:14 -0700205 "outputs": [
206 {
207 "output_type": "stream",
Scott Todd8ebe0022022-09-29 11:30:28 -0700208 "name": "stdout",
Scott Todd02817212021-05-19 11:05:14 -0700209 "text": [
210 "Loaded SavedModel from '/tmp/tfhub_modules/426589ad685896ab7954855255a52db3442cb38d'\n",
211 "Serving signatures: []\n",
212 "\n",
213 "---\n",
214 "\n",
215 "Checking for signature_defs using saved_model_cli:\n",
216 "\n",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700217 "2023-04-26 17:12:32.367522: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n",
Scott Todd8ebe0022022-09-29 11:30:28 -0700218 "Traceback (most recent call last):\n",
219 " File \"/usr/local/bin/saved_model_cli\", line 8, in <module>\n",
220 " sys.exit(main())\n",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700221 " File \"/usr/local/lib/python3.9/dist-packages/tensorflow/python/tools/saved_model_cli.py\", line 1284, in main\n",
222 " app.run(smcli_main)\n",
223 " File \"/usr/local/lib/python3.9/dist-packages/absl/app.py\", line 308, in run\n",
224 " _run_main(main, args)\n",
225 " File \"/usr/local/lib/python3.9/dist-packages/absl/app.py\", line 254, in _run_main\n",
226 " sys.exit(main(argv))\n",
227 " File \"/usr/local/lib/python3.9/dist-packages/tensorflow/python/tools/saved_model_cli.py\", line 1282, in smcli_main\n",
228 " args.func()\n",
229 " File \"/usr/local/lib/python3.9/dist-packages/tensorflow/python/tools/saved_model_cli.py\", line 961, in show\n",
230 " _show_inputs_outputs(\n",
231 " File \"/usr/local/lib/python3.9/dist-packages/tensorflow/python/tools/saved_model_cli.py\", line 345, in _show_inputs_outputs\n",
232 " inputs_tensor_info = _get_inputs_tensor_info_from_meta_graph_def(\n",
233 " File \"/usr/local/lib/python3.9/dist-packages/tensorflow/python/tools/saved_model_cli.py\", line 306, in _get_inputs_tensor_info_from_meta_graph_def\n",
234 " raise ValueError(\n",
Scott Todd8ebe0022022-09-29 11:30:28 -0700235 "ValueError: Could not find signature \"serving_default\". Please choose from: __saved_model_init_op\n"
236 ]
Scott Todd02817212021-05-19 11:05:14 -0700237 }
238 ]
239 },
240 {
241 "cell_type": "markdown",
242 "metadata": {
243 "id": "kKqqX2LsReNz"
244 },
245 "source": [
246 "Since the model we downloaded did not include any serving signatures, we'll re-export it with serving signatures defined.\n",
247 "\n",
248 "* https://www.tensorflow.org/guide/saved_model#specifying_signatures_during_export"
249 ]
250 },
251 {
252 "cell_type": "code",
253 "metadata": {
254 "colab": {
255 "base_uri": "https://localhost:8080/"
256 },
257 "id": "OlDG2OuqOBGC",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700258 "outputId": "c25d0e59-3a42-4f43-804c-c607eb9fc84c"
Scott Todd02817212021-05-19 11:05:14 -0700259 },
260 "source": [
261 "#@title Look up input signatures to use when exporting\n",
262 "\n",
263 "# To save serving signatures we need to specify a `ConcreteFunction` with a\n",
264 "# TensorSpec signature. We can determine what this signature should be by\n",
265 "# looking at any documentation for the model or running the saved_model_cli.\n",
266 "\n",
267 "!saved_model_cli show --dir {model_path} --all \\\n",
268 " 2> /dev/null | grep \"inputs: TensorSpec\" | tail -n 1"
269 ],
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700270 "execution_count": 15,
Scott Todd02817212021-05-19 11:05:14 -0700271 "outputs": [
272 {
273 "output_type": "stream",
Scott Todd8ebe0022022-09-29 11:30:28 -0700274 "name": "stdout",
Scott Todd02817212021-05-19 11:05:14 -0700275 "text": [
Scott Todd8ebe0022022-09-29 11:30:28 -0700276 " inputs: TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='inputs')\n"
277 ]
Scott Todd02817212021-05-19 11:05:14 -0700278 }
279 ]
280 },
281 {
282 "cell_type": "code",
283 "metadata": {
284 "colab": {
285 "base_uri": "https://localhost:8080/"
286 },
287 "id": "gnb4HhMmkgiT",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700288 "outputId": "d5ff3d4a-0483-476e-af6e-0b3c827d4938"
Scott Todd02817212021-05-19 11:05:14 -0700289 },
290 "source": [
291 "#@title Re-export the model using the known signature\n",
292 "\n",
293 "# Get a concrete function using the signature we found above.\n",
294 "# \n",
295 "# The first element of the shape is a dynamic batch size. We'll be running\n",
296 "# inference on a single image at a time, so set it to `1`. The rest of the\n",
297 "# shape is the fixed image dimensions [width=224, height=224, channels=3].\n",
298 "call = loaded_model.__call__.get_concrete_function(tf.TensorSpec([1, 224, 224, 3], tf.float32))\n",
299 "\n",
300 "# Save the model, setting the concrete function as a serving signature.\n",
301 "# https://www.tensorflow.org/guide/saved_model#saving_a_custom_model\n",
302 "resaved_model_path = '/tmp/resaved_model'\n",
303 "tf.saved_model.save(loaded_model, resaved_model_path, signatures=call)\n",
304 "clear_output() # Skip over TensorFlow's output.\n",
305 "print(f\"Saved model with serving signatures to '{resaved_model_path}'\")\n",
306 "\n",
307 "# Load the model back into memory and check that it has serving signatures now\n",
308 "reloaded_model = tf.saved_model.load(resaved_model_path)\n",
309 "reloaded_serving_signatures = list(reloaded_model.signatures.keys())\n",
310 "print(f\"\\nReloaded SavedModel from '{resaved_model_path}'\")\n",
311 "print(f\"Serving signatures: {reloaded_serving_signatures}\")\n",
312 "\n",
313 "# Also check with the saved_model_cli:\n",
314 "print(\"\\n---\\n\")\n",
315 "print(\"Checking for signature_defs using saved_model_cli:\\n\")\n",
316 "!saved_model_cli show --dir {resaved_model_path} --tag_set serve --signature_def serving_default"
317 ],
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700318 "execution_count": 16,
Scott Todd02817212021-05-19 11:05:14 -0700319 "outputs": [
320 {
321 "output_type": "stream",
Scott Todd8ebe0022022-09-29 11:30:28 -0700322 "name": "stdout",
Scott Todd02817212021-05-19 11:05:14 -0700323 "text": [
324 "Saved model with serving signatures to '/tmp/resaved_model'\n",
325 "\n",
326 "Reloaded SavedModel from '/tmp/resaved_model'\n",
327 "Serving signatures: ['serving_default']\n",
328 "\n",
329 "---\n",
330 "\n",
331 "Checking for signature_defs using saved_model_cli:\n",
332 "\n",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700333 "2023-04-26 17:13:06.873761: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n",
Scott Todd02817212021-05-19 11:05:14 -0700334 "The given SavedModel SignatureDef contains the following input(s):\n",
335 " inputs['inputs'] tensor_info:\n",
336 " dtype: DT_FLOAT\n",
337 " shape: (1, 224, 224, 3)\n",
338 " name: serving_default_inputs:0\n",
339 "The given SavedModel SignatureDef contains the following output(s):\n",
340 " outputs['output_0'] tensor_info:\n",
341 " dtype: DT_FLOAT\n",
342 " shape: (1, 1001)\n",
343 " name: StatefulPartitionedCall:0\n",
344 "Method name is: tensorflow/serving/predict\n"
Scott Todd8ebe0022022-09-29 11:30:28 -0700345 ]
Scott Todd02817212021-05-19 11:05:14 -0700346 }
347 ]
348 },
349 {
350 "cell_type": "markdown",
351 "metadata": {
352 "id": "YdmgASzwanSz"
353 },
354 "source": [
355 "### Import and compile the SavedModel with IREE"
356 ]
357 },
358 {
359 "cell_type": "code",
360 "metadata": {
361 "colab": {
362 "base_uri": "https://localhost:8080/"
363 },
364 "id": "GLkjlHE5mdmg",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700365 "outputId": "c67419f8-94de-4335-ddbc-f062b7d2e48a"
Scott Todd02817212021-05-19 11:05:14 -0700366 },
367 "source": [
368 "#@title Import from SavedModel\n",
369 "\n",
370 "# The main output file from compilation is a .vmfb \"VM FlatBuffer\". This file\n",
371 "# can used to run the compiled model with IREE's runtime.\n",
372 "output_file = os.path.join(ARTIFACTS_DIR, \"mobilenet_v2.vmfb\")\n",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700373 "# As compilation runs, dump an intermediate .mlir file for future inspection.\n",
Scott Todd02817212021-05-19 11:05:14 -0700374 "iree_input = os.path.join(ARTIFACTS_DIR, \"mobilenet_v2_iree_input.mlir\")\n",
375 "\n",
376 "# Since our SavedModel uses signature defs, we use `saved_model_tags` with\n",
377 "# `import_type=\"SIGNATURE_DEF\"`. If the SavedModel used an object graph, we\n",
378 "# would use `exported_names` with `import_type=\"OBJECT_GRAPH\"` instead.\n",
379 "\n",
380 "# We'll set `target_backends=[\"vmvx\"]` to use IREE's reference CPU backend.\n",
381 "# We could instead use different backends here, or set `import_only=True` then\n",
382 "# download the imported .mlir file for compilation using native tools directly.\n",
383 "\n",
384 "tfc.compile_saved_model(\n",
385 " resaved_model_path,\n",
386 " output_file=output_file,\n",
Scott Todd02817212021-05-19 11:05:14 -0700387 " save_temp_iree_input=iree_input,\n",
388 " import_type=\"SIGNATURE_DEF\",\n",
389 " saved_model_tags=set([\"serve\"]),\n",
390 " target_backends=[\"vmvx\"])\n",
391 "clear_output() # Skip over TensorFlow's output.\n",
392 "\n",
393 "print(f\"Saved compiled output to '{output_file}'\")\n",
Scott Todd02817212021-05-19 11:05:14 -0700394 "print(f\"Saved iree_input to '{iree_input}'\")"
395 ],
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700396 "execution_count": 17,
Scott Todd02817212021-05-19 11:05:14 -0700397 "outputs": [
398 {
399 "output_type": "stream",
Scott Todd8ebe0022022-09-29 11:30:28 -0700400 "name": "stdout",
Scott Todd02817212021-05-19 11:05:14 -0700401 "text": [
402 "Saved compiled output to '/tmp/iree/colab_artifacts/mobilenet_v2.vmfb'\n",
Scott Todd02817212021-05-19 11:05:14 -0700403 "Saved iree_input to '/tmp/iree/colab_artifacts/mobilenet_v2_iree_input.mlir'\n"
Scott Todd8ebe0022022-09-29 11:30:28 -0700404 ]
Scott Todd02817212021-05-19 11:05:14 -0700405 }
406 ]
407 },
408 {
409 "cell_type": "code",
410 "metadata": {
411 "colab": {
412 "base_uri": "https://localhost:8080/",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700413 "height": 86
Scott Todd02817212021-05-19 11:05:14 -0700414 },
415 "id": "IEJAzOb5qASI",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700416 "outputId": "9a29aa51-b99d-4acd-dae8-0d97cf9786e6"
Scott Todd02817212021-05-19 11:05:14 -0700417 },
418 "source": [
419 "#@title Download compilation artifacts\n",
420 "\n",
Scott Toddffda4982021-06-07 11:50:55 -0700421 "ARTIFACTS_ZIP = \"/tmp/mobilenet_colab_artifacts.zip\"\n",
Scott Todd02817212021-05-19 11:05:14 -0700422 "\n",
423 "print(f\"Zipping '{ARTIFACTS_DIR}' to '{ARTIFACTS_ZIP}' for download...\")\n",
Scott Toddffda4982021-06-07 11:50:55 -0700424 "!cd {ARTIFACTS_DIR} && zip -r {ARTIFACTS_ZIP} .\n",
Scott Todd02817212021-05-19 11:05:14 -0700425 "\n",
426 "# Note: you can also download files using the file explorer on the left\n",
Scott Toddb5b90262021-06-28 16:29:43 -0700427 "try:\n",
428 " from google.colab import files\n",
429 " print(\"Downloading the artifacts zip file...\")\n",
430 " files.download(ARTIFACTS_ZIP)\n",
431 "except ImportError:\n",
432 " print(\"Missing google_colab Python package, can't download files\")"
Scott Todd02817212021-05-19 11:05:14 -0700433 ],
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700434 "execution_count": 18,
Scott Todd02817212021-05-19 11:05:14 -0700435 "outputs": [
436 {
437 "output_type": "stream",
Scott Todd8ebe0022022-09-29 11:30:28 -0700438 "name": "stdout",
Scott Todd02817212021-05-19 11:05:14 -0700439 "text": [
Scott Toddffda4982021-06-07 11:50:55 -0700440 "Zipping '/tmp/iree/colab_artifacts' to '/tmp/mobilenet_colab_artifacts.zip' for download...\n",
Scott Toddffda4982021-06-07 11:50:55 -0700441 " adding: mobilenet_v2.vmfb (deflated 8%)\n",
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700442 " adding: mobilenet_v2_iree_input.mlir (deflated 46%)\n",
Scott Todd02817212021-05-19 11:05:14 -0700443 "Downloading the artifacts zip file...\n"
Scott Todd8ebe0022022-09-29 11:30:28 -0700444 ]
Scott Todd02817212021-05-19 11:05:14 -0700445 },
446 {
447 "output_type": "display_data",
448 "data": {
Scott Todd8ebe0022022-09-29 11:30:28 -0700449 "text/plain": [
450 "<IPython.core.display.Javascript object>"
451 ],
Scott Todd02817212021-05-19 11:05:14 -0700452 "application/javascript": [
453 "\n",
454 " async function download(id, filename, size) {\n",
455 " if (!google.colab.kernel.accessAllowed) {\n",
456 " return;\n",
457 " }\n",
458 " const div = document.createElement('div');\n",
459 " const label = document.createElement('label');\n",
460 " label.textContent = `Downloading \"${filename}\": `;\n",
461 " div.appendChild(label);\n",
462 " const progress = document.createElement('progress');\n",
463 " progress.max = size;\n",
464 " div.appendChild(progress);\n",
465 " document.body.appendChild(div);\n",
466 "\n",
467 " const buffers = [];\n",
468 " let downloaded = 0;\n",
469 "\n",
470 " const channel = await google.colab.kernel.comms.open(id);\n",
471 " // Send a message to notify the kernel that we're ready.\n",
472 " channel.send({})\n",
473 "\n",
474 " for await (const message of channel.messages) {\n",
475 " // Send a message to notify the kernel that we're ready.\n",
476 " channel.send({})\n",
477 " if (message.buffers) {\n",
478 " for (const buffer of message.buffers) {\n",
479 " buffers.push(buffer);\n",
480 " downloaded += buffer.byteLength;\n",
481 " progress.value = downloaded;\n",
482 " }\n",
483 " }\n",
484 " }\n",
485 " const blob = new Blob(buffers, {type: 'application/binary'});\n",
486 " const a = document.createElement('a');\n",
487 " a.href = window.URL.createObjectURL(blob);\n",
488 " a.download = filename;\n",
489 " div.appendChild(a);\n",
490 " a.click();\n",
491 " div.remove();\n",
492 " }\n",
493 " "
Scott Todd02817212021-05-19 11:05:14 -0700494 ]
495 },
Scott Todd8ebe0022022-09-29 11:30:28 -0700496 "metadata": {}
Scott Todd02817212021-05-19 11:05:14 -0700497 },
498 {
499 "output_type": "display_data",
500 "data": {
Scott Todd02817212021-05-19 11:05:14 -0700501 "text/plain": [
502 "<IPython.core.display.Javascript object>"
Scott Todd8ebe0022022-09-29 11:30:28 -0700503 ],
504 "application/javascript": [
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700505 "download(\"download_18545900-47df-4250-9a14-8453ca4b6fc2\", \"mobilenet_colab_artifacts.zip\", 41434352)"
Scott Todd02817212021-05-19 11:05:14 -0700506 ]
507 },
Scott Todd8ebe0022022-09-29 11:30:28 -0700508 "metadata": {}
Scott Todd02817212021-05-19 11:05:14 -0700509 }
510 ]
511 }
512 ]
Scott Todd5cd6e1d2023-04-26 12:52:05 -0700513}