09_deploying/09e_tflite.ipynb (251 lines of code) (raw):

{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 72 }, "id": "hiQ6zAoYhyaA", "outputId": "0acee878-1207-42c3-9bee-a594acd44365" }, "outputs": [ { "data": { "text/markdown": [ "\n", "<table class=\"tfo-notebook-buttons\" align=\"left\">\n", " <td>\n", " <a target=\"_blank\" href=\"https://console.cloud.google.com/ai-platform/notebooks/deploy-notebook?name=Edge ML with TensorFlow Lite&url=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fpractical-ml-vision-book%2Fblob%2Fmaster%2F09_deploying%2F09e_tflite.ipynb&download_url=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fpractical-ml-vision-book%2Fraw%2Fmaster%2F09_deploying%2F09e_tflite.ipynb\">\n", " <img src=\"https://raw.githubusercontent.com/GoogleCloudPlatform/practical-ml-vision-book/master/logo-cloud.png\"/> Run in AI Platform Notebook</a>\n", " </td>\n", " </td>\n", " <td>\n", " <a target=\"_blank\" href=\"https://colab.research.google.com/github/GoogleCloudPlatform/practical-ml-vision-book/blob/master/09_deploying/09e_tflite.ipynb\">\n", " <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n", " </td>\n", " <td>\n", " <a target=\"_blank\" href=\"https://github.com/GoogleCloudPlatform/practical-ml-vision-book/blob/master/09_deploying/09e_tflite.ipynb\">\n", " <img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n", " </td>\n", " <td>\n", " <a href=\"https://raw.githubusercontent.com/GoogleCloudPlatform/practical-ml-vision-book/master/09_deploying/09e_tflite.ipynb\">\n", " <img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n", " </td>\n", "</table>\n" ], "text/plain": [ "<IPython.core.display.Markdown object>" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import Markdown as md\n", "\n", "### change to reflect your notebook\n", "_nb_loc = \"09_deploying/09e_tflite.ipynb\"\n", "_nb_title = \"Edge ML with TensorFlow Lite\"\n", "\n", "### no need to change any of this\n", "_nb_safeloc = _nb_loc.replace('/', '%2F')\n", "md(\"\"\"\n", "<table class=\"tfo-notebook-buttons\" align=\"left\">\n", " <td>\n", " <a target=\"_blank\" href=\"https://console.cloud.google.com/ai-platform/notebooks/deploy-notebook?name={1}&url=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fpractical-ml-vision-book%2Fblob%2Fmaster%2F{2}&download_url=https%3A%2F%2Fgithub.com%2FGoogleCloudPlatform%2Fpractical-ml-vision-book%2Fraw%2Fmaster%2F{2}\">\n", " <img src=\"https://raw.githubusercontent.com/GoogleCloudPlatform/practical-ml-vision-book/master/logo-cloud.png\"/> Run in AI Platform Notebook</a>\n", " </td>\n", " </td>\n", " <td>\n", " <a target=\"_blank\" href=\"https://colab.research.google.com/github/GoogleCloudPlatform/practical-ml-vision-book/blob/master/{0}\">\n", " <img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n", " </td>\n", " <td>\n", " <a target=\"_blank\" href=\"https://github.com/GoogleCloudPlatform/practical-ml-vision-book/blob/master/{0}\">\n", " <img src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" />View source on GitHub</a>\n", " </td>\n", " <td>\n", " <a href=\"https://raw.githubusercontent.com/GoogleCloudPlatform/practical-ml-vision-book/master/{0}\">\n", " <img src=\"https://www.tensorflow.org/images/download_logo_32px.png\" />Download notebook</a>\n", " </td>\n", "</table>\n", "\"\"\".format(_nb_loc, _nb_title, _nb_safeloc))" ] }, { "cell_type": "markdown", "metadata": { "id": "a8HQYsAtC0Fv" }, "source": [ "# Edge ML with TensorFlow Lite\n", "\n", "In this notebook, we convert the saved model into a TensorFlow Lite model\n", "so that we can run it on Edge devices.\n", "\n", "In order to do edge inference, we need to handle raw image data from the camera\n", "and process a single image (not a batch of images)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "import os, shutil\n", "\n", "MODEL_LOCATION='export/flowers_model3' # will be created\n", "# load from checkpoint and export a model that has desired signature\n", "CHECK_POINT_DIR='gs://practical-ml-vision-book-data/flowers_5_trained/chkpts'\n", "model = tf.keras.models.load_model(CHECK_POINT_DIR)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n", "WARNING:tensorflow:From /opt/conda/lib/python3.7/site-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.\n", "Instructions for updating:\n", "This property should not be used in TensorFlow 2.0, as updates are applied automatically.\n", "INFO:tensorflow:Assets written to: export/flowers_model3/assets\n" ] } ], "source": [ "IMG_HEIGHT = 345\n", "IMG_WIDTH = 345\n", "IMG_CHANNELS = 3\n", "CLASS_NAMES = 'daisy dandelion roses sunflowers tulips'.split()\n", " \n", "# a single image of any size\n", "@tf.function(input_signature=[tf.TensorSpec([None, None, 3], dtype=tf.float32)])\n", "def predict_flower_type(img):\n", " img = tf.image.resize_with_pad(img, IMG_HEIGHT, IMG_WIDTH)\n", " batch_pred = model(tf.expand_dims(img, axis=0))\n", " top_prob = tf.math.reduce_max(batch_pred, axis=[1])\n", " pred_label_index = tf.math.argmax(batch_pred, axis=1)\n", " pred_label = tf.gather(tf.convert_to_tensor(CLASS_NAMES), pred_label_index)\n", " return {\n", " 'probability': tf.squeeze(top_prob, axis=0),\n", " 'flower_type': tf.squeeze(pred_label, axis=0)\n", " }\n", "\n", "shutil.rmtree('export', ignore_errors=True)\n", "os.mkdir('export')\n", "\n", "\n", "model.save(MODEL_LOCATION,\n", " signatures={\n", " 'serving_default': predict_flower_type\n", " })" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Convert to TFLite\n", "\n", "This will take a while to do the conversion" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "converter = tf.lite.TFLiteConverter.from_saved_model(MODEL_LOCATION)\n", "tflite_model = converter.convert()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "with open('export/model.tflite', 'wb') as ofp:\n", " ofp.write(tflite_model)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-rw-r--r-- 1 jupyter jupyter 8.8M Jan 26 05:31 export/model.tflite\n" ] } ], "source": [ "!ls -lh export/model.tflite" ] }, { "cell_type": "markdown", "metadata": { "id": "Duu8mX3iXANE" }, "source": [ "## License\n", "Copyright 2020 Google Inc. 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 http://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." ] } ], "metadata": { "accelerator": "GPU", "colab": { "collapsed_sections": [ "5UOm2etrwYCs" ], "name": "03a_transfer_learning.ipynb", "provenance": [], "toc_visible": true }, "environment": { "name": "tf2-2-3-gpu.2-3.m59", "type": "gcloud", "uri": "gcr.io/deeplearning-platform-release/tf2-2-3-gpu.2-3:m59" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.8" } }, "nbformat": 4, "nbformat_minor": 4 }