{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "accelerator": "GPU", "colab": { "name": "mlkit_image_labeling_model_maker.ipynb", "provenance": [], "private_outputs": true, "collapsed_sections": [], "toc_visible": true }, "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.6.8" } }, "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "h2q27gKz1H20" }, "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "metadata": { "cellView": "form", "colab_type": "code", "id": "TUfAcER1oUS6", "colab": {} }, "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Gb7qyhNL1yWt" }, "source": [ "# Create ML Kit Image labeling model with Tensorflow Lite Model Maker" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "nDABAblytltI" }, "source": [ "
\n",
" ![]() | \n",
" \n",
" ![]() | \n",
" \n",
" ![]() | \n",
" \n",
" ![]() | \n",
"
\n", "flower_photos\n", "|__ daisy\n", " |______ 100080576_f52e8ee070_n.jpg\n", " |______ 14167534527_781ceb1b7a_n.jpg\n", " |______ ...\n", "|__ dandelion\n", " |______ 10043234166_e6dd915111_n.jpg\n", " |______ 1426682852_e62169221f_m.jpg\n", " |______ ...\n", "|__ roses\n", " |______ 102501987_3cdb8e5394_n.jpg\n", " |______ 14982802401_a3dfb22afb.jpg\n", " |______ ...\n", "|__ sunflowers\n", " |______ 12471791574_bb1be83df4.jpg\n", " |______ 15122112402_cafa41934f.jpg\n", " |______ ...\n", "|__ tulips\n", " |______ 13976522214_ccec508fe7.jpg\n", " |______ 14487943607_651e8062a1_m.jpg\n", " |______ ...\n", "" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "NNRNv_mloS89" }, "source": [ "If you prefer not to upload your images to the cloud, you could try to run the library locally following the [guide](https://github.com/tensorflow/examples/tree/master/tensorflow_examples/lite/model_maker) in github.\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "w-VDriAdsowu" }, "source": [ "## Run the example\n", "The example just consists of 4 lines of code as shown below, each of which representing one step of the overall process.\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "6ahtcO86tZBL" }, "source": [ "Step 1. Load input data specific to an on-device ML app. Split it to training data and testing data." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "lANoNS_gtdH1", "colab": {} }, "source": [ "train_data, test_data = ImageClassifierDataLoader.from_folder(image_path).split(0.9)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "Y_9IWyIztuRF" }, "source": [ "Step 2. Customize the TensorFlow model." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "yRXMZbrwtyRD", "colab": {} }, "source": [ "model = image_classifier.create(train_data)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "oxU2fDr-t2Ya" }, "source": [ "Step 3. Evaluate the model." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "wQr02VxJt6Cs", "colab": {} }, "source": [ "loss, accuracy = model.evaluate(test_data)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "NtCLU30l4ZSV", "colab_type": "text" }, "source": [ "Step 4. Setup config for quantized model with uint8 input and output type" ] }, { "cell_type": "code", "metadata": { "id": "W4QMeF974hgD", "colab_type": "code", "colab": {} }, "source": [ "config = configs.QuantizationConfig.create_full_integer_quantization(\n", " representative_data=test_data, is_integer_only=True)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "eVZw9zU8t84y" }, "source": [ "Step 4. Export to TensorFlow Lite model.\n", "\n", "Here, we export TensorFlow Lite model with [metadata](https://www.tensorflow.org/lite/convert/metadata) which provides a standard for model descriptions.\n", "You could download it in the left sidebar same as the uploading part for your own use." ] }, { "cell_type": "code", "metadata": { "colab_type": "code", "id": "Zb-eIzfluCoa", "colab": {} }, "source": [ "model.export(export_dir='.', quantization_config=config)" ], "execution_count": 0, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "gwIhJ6-93qrw", "colab_type": "text" }, "source": [ "After this simple 4 steps, we could further use TensorFlow Lite model file in ML Kit Image Labeling and Object Detection and Tracking features." ] }, { "cell_type": "markdown", "metadata": { "id": "Lpppfth3kgJA", "colab_type": "text" }, "source": [ "Tensorflow Lite Model Maker allows changing model architecture to suit different needs. Here is the instructions of how to change model architecture:\n", "https://www.tensorflow.org/lite/tutorials/model_maker_image_classification#change_the_model" ] } ] }