TensorFlow Lite Tutorial for Flutter: Image Classification

Learn how to use TensorFlow Lite in Flutter. Train your machine learning model with Teachable Machine and integrate the result into your Flutter mobile app. By Ken Lee.

4.8 (6) · 3 Reviews

Download materials
Save for later
Share

Machine learning is one of the hottest technologies of the last decade. You may not even realize it’s everywhere.

Applications such as augmented reality, self-driving vehicles, chatbots, computer vision, social media, among others, have adopted machine learning technology to solve problems.

The good news is that numerous machine-learning resources and frameworks are available to the public. Two of those are TensorFlow and Teachable Machine.

Note: If you want in-depth coverage of machine learning, subscribe to Kodeco to read Machine Learning by Tutorials.

In this Flutter tutorial, you’ll develop an application called Plant Recognizer that uses machine learning to recognize plants simply by looking at photos of them. You’ll accomplish this by using the Teachable Machine platform, TensorFlow Lite, and a Flutter package named tflite_flutter.

By the end of this tutorial, you’ll learn how to:

  • Use machine learning in a mobile app.
  • Train a model using Teachable Machine.
  • Integrate and use TensorFlow Lite with the tflite_flutter package.
  • Build a mobile app to recognize plants by image.

TensorFlow is a popular machine-learning library for developers who want to build learning models for their apps. TensorFlow Lite is a mobile version of TensorFlow for deploying models on mobile devices. And Teachable Machine is a beginner-friendly platform for training machine learning models.

Note: This tutorial assumes you have a basic understanding of Flutter and have Android Studio or Visual Studio Code installed. If you’re on macOS, you should also have Xcode installed. If you’re new to Flutter, you should start with our Getting Started with Flutter tutorial.

Getting Started

Download the project by clicking Download Materials at the top or bottom of the tutorial and extract it to a suitable location.

After decompressing, you’ll see the following folders:

  1. final: contains code for the completed project.
  2. samples: has sample images you can use to train your model.
  3. samples-test: houses samples you can use to test the app after it’s completed.
  4. starter: the starter project. You’ll work with this in the tutorial.

Open the starter project in VS Code. Note that you can use Android Studio, but you’ll have to adapt the instructions on your own.

VS Code should prompt you to get dependencies — click the button to get them. You can also run flutter pub get from the terminal to get the dependencies.

Build and run after installing the dependencies. You should see the following screen:

Starter Project

The project already allows you to pick an image from the camera or media library. Tap Pick from gallery to select a photo.

Note: You may need to copy the images from samples-test to your device to test. If you’re using an iPhone Simulator or an Android Emulator, simply drag and drop the photos from the samples-test folder into it. Otherwise, find instructions on copying files from a computer to a mobile device from your device manufacturer.

iPhone Photo Library

As you can see, the app doesn’t recognize images. You’ll use TensorFlow Lite to solve that in the next sections. But first, here’s an optional, high-level overview of machine learning to give you a gist of what you’ll do.

Brief Introduction to Machine Learning

This section is optional because the starter project contains a trained model model_unquant.tflite and classification labels in the labels.txt file.

If you’d prefer to dive into TensorFlow Lite integration, feel free to skip to Installing TensorFlow Lite.

What is Machine Learning

In this Flutter tutorial, you need to solve a classification problem: plant recognition. In a traditional approach, you’d define rules to determine which images belong to which class.

The rules would be based on patterns such as that of a sunflower, which has a large circle in the center, or a rose, which is somewhat like a paper ball. It goes like the following:

Traditional AI

The traditional approach has several problems:

  • There are many rules to set when there are many classification labels.
  • It’s subjective.
  • Some rules are hard to determine by the program. For example, the rule “like a paper ball” can’t be determined by a program because a computer doesn’t know what a paper ball looks like.

Machine learning offers another way to solve the problem. Instead of you defining the rules, the machine defines its own rules based on input data you provide:

Machine learning AI

The machine learns from the data, and that’s why this approach is called machine learning.

Before you continue, here’s some terminology you may need to know:

    Training: The process by which the computer learns data and derives rules.

Building a Model with Teachable Machine

Preparing the Dataset

Training the Model

Now, you’ll learn how to train a model with Teachable Machine. The steps you’ll follow include:

Your first step is to prepare your dataset — the project needs plant photos. So your dataset is a collection of plants you want to recognize.

In a production-ready app, you’d want to collect as many varieties of a plant and as many plants as possible for your dataset to ensure higher accuracy. You’d do that by using your phone camera to take pictures of these plants or download images from various online sources that offer free datasets such as this one from Kaggle.

However, this tutorial uses plants from the samples folder, so you can also use it as a starting point.

Whichever one you use, it’s important to keep the number of samples for each label at similar levels to avoid introducing bias to the model.

Next, you’ll learn how to train the model using Teachable Machine.

First, go to https://teachablemachine.withgoogle.com and click Get Started to open the training tool:

Teachable Machine

Then select Image Project:

Select Image Project

Choose Standard Image Model, because you’re not training a model to run on a microcontroller:

Teachable Machine Dialog

Once you’ve entered the training tool, add the classes and edit the labels of each class, as shown below:

Adding classes

Next, add your training samples by clicking Upload under each class. Then, drag the folder of the appropriate plant type from the samples folder to the Choose images from your files … panel.

Adding Samples

After you’ve added all the training samples, click Train Model to train the model:

Training Model

After the training completes, test the model with other plant images.

Use the images in the samples-test folder, like so:

Review Model

Finally, export the model by clicking Export Model on the Preview panel. A dialog displays:

Export Model

In the dialog, choose TensorFlow Lite. That’s because your target platform is mobile.

Next, select Floating point conversion type for the best predictive performance. Then, click Download my model to convert and download the model.

It may take several minutes to complete the model conversion process. Once it’s done, the model file will automatically download to your system.

After you have the model file converted_tflite.zip in hand, decompress it and copy labels.txt and model_unquant.tflite to the ./assets folder in your starter project.

Here’s what each of those files contains:

    Model: The object created from training. It comprises the algorithm used to solve the AI problem and the learned rules.

    Building a Model with Teachable Machine

    Now, you’ll learn how to train a model with Teachable Machine. The steps you’ll follow include:

    1. Preparing the dataset
    2. Training the model
    3. Exporting the model

    Your first step is to prepare your dataset — the project needs plant photos. So your dataset is a collection of plants you want to recognize.

    Preparing the Dataset

    In a production-ready app, you’d want to collect as many varieties of a plant and as many plants as possible for your dataset to ensure higher accuracy. You’d do that by using your phone camera to take pictures of these plants or download images from various online sources that offer free datasets such as this one from Kaggle.

    Note: Always make sure to check the terms of service (TOS) if you are downloading images from a service. As machine learning grows in popularity, a lot of services are amending their TOS to specifically address their data being included in machine learning models.

    However, this tutorial uses plants from the samples folder, so you can also use it as a starting point.

    Whichever one you use, it’s important to keep the number of samples for each label at similar levels to avoid introducing bias to the model.

    Training the Model

    Next, you’ll learn how to train the model using Teachable Machine.

    First, go to https://teachablemachine.withgoogle.com and click Get Started to open the training tool:

    Teachable Machine

    Then select Image Project:

    Select Image Project

    Choose Standard Image Model, because you’re not training a model to run on a microcontroller:

    Teachable Machine Dialog

    Once you’ve entered the training tool, add the classes and edit the labels of each class, as shown below:

    Adding classes

    Next, add your training samples by clicking Upload under each class. Then, drag the folder of the appropriate plant type from the samples folder to the Choose images from your files … panel.

    Adding Samples

    After you’ve added all the training samples, click Train Model to train the model:

    Training Model

    After the training completes, test the model with other plant images.

    Use the images in the samples-test folder, like so:

    Review Model

    Finally, export the model by clicking Export Model on the Preview panel. A dialog displays:

    Export Model

    In the dialog, choose TensorFlow Lite. That’s because your target platform is mobile.

    Next, select Floating point conversion type for the best predictive performance. Then, click Download my model to convert and download the model.

    It may take several minutes to complete the model conversion process. Once it’s done, the model file will automatically download to your system.

    Note: The other conversion types, quantized and Edge TPU, are best for devices that have less computing power than a mobile phone. A key difference is that the numerical data used in the model is converted to lower-precision data types these devices can handle, such as integer or 16-bit float.

    After you have the model file converted_tflite.zip in hand, decompress it and copy labels.txt and model_unquant.tflite to the ./assets folder in your starter project.

    Here’s what each of those files contains:

  1. Preparing the dataset
  2. Training the model
  3. Exporting the model
Note: Always make sure to check the terms of service (TOS) if you are downloading images from a service. As machine learning grows in popularity, a lot of services are amending their TOS to specifically address their data being included in machine learning models.
Note: The other conversion types, quantized and Edge TPU, are best for devices that have less computing power than a mobile phone. A key difference is that the numerical data used in the model is converted to lower-precision data types these devices can handle, such as integer or 16-bit float.
  • labels.txt: The label of each class.
  • model_unquant.tflite: The trained machine learning model for plant recognition.