In [0]:
#@title 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
#
# https://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.

Flower classification with TensorFlow Lite Model Maker with TensorFlow 2.0

Model Maker library simplifies the process of adapting and converting a TensorFlow neural-network model to particular input data when deploying this model for on-device ML applications.

This notebook shows an end-to-end example that utilizes this Model Maker library to illustrate the adaption and conversion of a commonly-used image classification model to classify flowers on a mobile device.

Prerequisites

To run this example, we first need to install serveral required packages, including Model Maker package that in github repo.


In [0]:
!pip install git+git://github.com/tensorflow/examples.git#egg=tensorflow-examples[model_maker]

Import the required packages.


In [0]:
import numpy as np

import tensorflow as tf
assert tf.__version__.startswith('2')

from tensorflow_examples.lite.model_maker.core.data_util.image_dataloader import ImageClassifierDataLoader
from tensorflow_examples.lite.model_maker.core.task import image_classifier
from tensorflow_examples.lite.model_maker.core.task.model_spec import mobilenet_v2_spec
from tensorflow_examples.lite.model_maker.core.task.model_spec import ImageModelSpec

import matplotlib.pyplot as plt

Simple End-to-End Example

Get the data path

Let's get some images to play with this simple end-to-end example. Hundreds of images is a good start for Model Maker while more data could achieve better accuracy.


In [0]:
image_path = tf.keras.utils.get_file(
      'flower_photos',
      'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
      untar=True)

You could replace image_path with your own image folders. As for uploading data to colab, you could find the upload button in the left sidebar shown in the image below with the red rectangle. Just have a try to upload a zip file and unzip it. The root file path is the current path.

If you prefer not to upload your images to the cloud, you could try to run the library locally following the guide in github.

Run the example

The example just consists of 4 lines of code as shown below, each of which representing one step of the overall process.

  1. Load input data specific to an on-device ML app. Split it to training data and testing data.

In [0]:
data = ImageClassifierDataLoader.from_folder(image_path)
train_data, test_data = data.split(0.9)
  1. Customize the TensorFlow model.

In [0]:
model = image_classifier.create(train_data)
  1. Evaluate the model.

In [0]:
loss, accuracy = model.evaluate(test_data)
  1. Export to TensorFlow Lite model. You could download it in the left sidebar same as the uploading part for your own use.

In [0]:
model.export(export_dir='.', with_metadata=True)

After this simple 4 steps, we can now download the model and label files, and continue to the next step in the codelab.

For a more comprehensive guide to TFLite Model Maker, please refer to this notebook and its documentation.