This will be a practical, end-to-end guide on how to build a mobile application using TensorFlow Lite that classifies images from a dataset for your projects.
This application uses live camera and classifies objects instantly. The TFLite application will be smaller, faster, and more accurate than an application made using TensorFlow Mobile, because TFLite is made specifically to run neural nets on mobile platforms.
We’ll be using the MobileNet model to train our network, which will keep the app smaller.
- Python 3 .5 or higher—
- Tensorflow 1.9 or higher —
pip3 install — upgrade tensorflow
Also, open the terminal and type:
python3 will open with the
python command. This will make it easier to implement the code just by copy-pasting without having to worry about
3 after typing Python.
Let’s start by downloading the code from the tensorflow-for-poets GitHub. Open the command prompt where you want to download the folder and type:
This will download the files and make a new folder called
tensorflow-for-poets in your current directory.
FYI: You can change the name of the folder to your project name after downloading.
Info: The folder contains the sub-folders
scripts — Contains the machine learning code .py files.
tf_files — It will contain output files like models — graph.pb, labels.txt
android — Contains Android app projects for both tfmobile and TFlite.
iOS — Contains the iOS app project files using xCode.
Let’s download a 200MB publicly available dataset with 5 different flowers to classify from. Then extract the
flower_photos.tgz inside the tf_files folder which will look something like this:
tensorflow-for-poets-2 > tf_files > flower_photos
Info — The 5 different category folders are Rose, Daisy, Dandelion, Sunflower and Tulip.
Open the command prompt inside the “tensorflow-for-poets-2” folder and type:
This will download the pre-trained frozen graph
retrained_labels.txt files in the tf_files folder.
Open another command prompt in the current directory and point tensorboard to the
Now you can open the 6006 port in your browser to see the results.
🔴 Training & 🔵 Validation
Accuracy is above 0.90 and loss is below 0.4 for validation.
I downloaded a random rose image from the Internet in the current folder and used the following command to run the label_image.py script to detect the
--image using the
The result gives 99% accuracy for the
Toco is used to convert the file to .lite format. For more detail on toco arguments, use
This will create an
optimized_graph.lite file in your tf_files directory.
Info: Use the
toco -hfor more details
--input_filehas been updated to
--input_formatis not need for the