{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Untitled2.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "0z5414fspqYo",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "275b492d-32a5-4530-a771-e29c10799e10"
      },
      "source": [
        "%tensorflow_version 2.x"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "TensorFlow 2.x selected.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3T0lMVHbpvDL",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "c9536f61-ca3a-46ac-fad4-e9553245b583"
      },
      "source": [
        "!pip install -q tf-hub-nightly==0.8.0.dev201911110007"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\u001b[?25l\r\u001b[K     |███▋                            | 10kB 15.4MB/s eta 0:00:01\r\u001b[K     |███████▎                        | 20kB 3.3MB/s eta 0:00:01\r\u001b[K     |███████████                     | 30kB 4.6MB/s eta 0:00:01\r\u001b[K     |██████████████▋                 | 40kB 3.0MB/s eta 0:00:01\r\u001b[K     |██████████████████▎             | 51kB 3.6MB/s eta 0:00:01\r\u001b[K     |██████████████████████          | 61kB 4.3MB/s eta 0:00:01\r\u001b[K     |█████████████████████████▌      | 71kB 4.9MB/s eta 0:00:01\r\u001b[K     |█████████████████████████████▏  | 81kB 5.5MB/s eta 0:00:01\r\u001b[K     |████████████████████████████████| 92kB 3.5MB/s \n",
            "\u001b[?25h"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bdB0axXtpxyY",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "de7a60f9-98eb-44e2-c5af-5ed959f9bc47"
      },
      "source": [
        "!pip install -q git+https://github.com/tensorflow/examples"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "  Building wheel for tensorflow-examples (setup.py) ... \u001b[?25l\u001b[?25hdone\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "r19b_lzkp4Zo",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from __future__ import absolute_import, division, print_function, unicode_literals\n",
        "\n",
        "import numpy as np\n",
        "\n",
        "import tensorflow as tf\n",
        "assert tf.__version__.startswith('2')\n",
        "\n",
        "from tensorflow_examples.lite.model_customization.core.data_util.image_dataloader import ImageClassifierDataLoader\n",
        "from tensorflow_examples.lite.model_customization.core.task import image_classifier\n",
        "from tensorflow_examples.lite.model_customization.core.task.model_spec import efficientnet_b0_spec\n",
        "from tensorflow_examples.lite.model_customization.core.task.model_spec import ImageModelSpec\n",
        "\n",
        "import matplotlib.pyplot as plt"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "V_4-PK3Fp8aw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "2ef2740a-ad15-4c55-d98d-7f5b59ebb6b7"
      },
      "source": [
        "image_path = tf.keras.utils.get_file(      'flower_photos',      'http://hamzaagh-001-site8.itempurl.com/flower_photos.tar.gz',      untar=True)"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Downloading data from http://hamzaagh-001-site8.itempurl.com/flower_photos.tar.gz\n",
            "478584832/478584409 [==============================] - 489s 1us/step\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9xJgA8B_sLip",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a165ad0f-9bcd-47a7-a756-2686e175944a"
      },
      "source": [
        "image_path\n"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "'/root/.keras/datasets/flower_photos'"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qAami-rEs_0l",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 54
        },
        "outputId": "4af6a933-e681-4ba2-dea3-d859bbd482db"
      },
      "source": [
        "data = ImageClassifierDataLoader.from_folder('/root/.keras/datasets/')"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "INFO:tensorflow:Load image with size: 1319, num_label: 15, labels: Ajwat Almadinah, Anbar, Edeyah, Ekhlas, Khudary, Lubana, Mabrom Almadinah, Majdol, Ratab, Rotana, Rshodeyah, Safawy, Saqay, Suffry, Sukkary.\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "N15Sx0UHtt_Q",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 493
        },
        "outputId": "83615e7d-ead0-4857-9baa-3d1891ca5e29"
      },
      "source": [
        "model = image_classifier.create(data)"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Model: \"sequential\"\n",
            "_________________________________________________________________\n",
            "Layer (type)                 Output Shape              Param #   \n",
            "=================================================================\n",
            "keras_layer (KerasLayer)     multiple                  2257984   \n",
            "_________________________________________________________________\n",
            "dropout (Dropout)            multiple                  0         \n",
            "_________________________________________________________________\n",
            "dense (Dense)                multiple                  19215     \n",
            "=================================================================\n",
            "Total params: 2,277,199\n",
            "Trainable params: 19,215\n",
            "Non-trainable params: 2,257,984\n",
            "_________________________________________________________________\n",
            "None\n",
            "INFO:tensorflow:Retraining the models...\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "INFO:tensorflow:Retraining the models...\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "stream",
          "text": [
            "Train for 33 steps, validate for 4 steps\n",
            "Epoch 1/5\n",
            "33/33 [==============================] - 94s 3s/step - loss: 2.0806 - accuracy: 0.3883 - val_loss: 1.3758 - val_accuracy: 0.6797\n",
            "Epoch 2/5\n",
            "33/33 [==============================] - 87s 3s/step - loss: 1.1959 - accuracy: 0.7680 - val_loss: 1.0963 - val_accuracy: 0.8203\n",
            "Epoch 3/5\n",
            "33/33 [==============================] - 85s 3s/step - loss: 0.9978 - accuracy: 0.8684 - val_loss: 0.9990 - val_accuracy: 0.8906\n",
            "Epoch 4/5\n",
            "33/33 [==============================] - 85s 3s/step - loss: 0.9273 - accuracy: 0.8911 - val_loss: 0.9612 - val_accuracy: 0.8984\n",
            "Epoch 5/5\n",
            "33/33 [==============================] - 84s 3s/step - loss: 0.8796 - accuracy: 0.9252 - val_loss: 0.9375 - val_accuracy: 0.8984\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4aXFtE5gvdvS",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "5e46ad00-c9c0-471f-e6a6-5bb0135392f1"
      },
      "source": [
        "loss, accuracy = model.evaluate()"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "4/4 [==============================] - 9s 2s/step - loss: 0.9009 - accuracy: 0.8898\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LQ_GhLmmvkUw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "71b55b33-06f1-41fc-914e-fe0839f2d3fc"
      },
      "source": [
        "model.export('image_classifier.tflite', 'image_labels.txt')"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "INFO:tensorflow:Export to tflite model image_classifier.tflite, saved labels in image_labels.txt.\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "INFO:tensorflow:Export to tflite model image_classifier.tflite, saved labels in image_labels.txt.\n"
          ],
          "name": "stderr"
        }
      ]
    }
  ]
}