Skip to content

SimWalther/TFLClassify

 
 

Repository files navigation

Classification with TensorFLow Lite Model Maker and Android Studio ML Model Binding

This project is forked from https://github.com/hoitab/TFLClassify. Only minor modifications have been applied to use another weight file and remove what was unnecessary for us.

If you don't rename the weight file, you don't have to modify the code! Otherwise you will need to rename some variable names in the code.

Android Studio Installation

You will need to download the Android Studio IDE. Instructions to do so are avaible here: https://developer.android.com/studio/install

Using your model weights

The code below can be used to create a .tfile weight file from your model weights. When you've exported the .tfile weights file, put this weight file and the labels.txt under TFLClassify/app/src/main/ml/.

import tensorflow as tf
from tflite_support.metadata_writers import image_classifier
from tflite_support.metadata_writers import writer_utils

# Task Library expects label files that are in the same format as the one below.
LABEL_FILE = "labels.txt"
SAVE_TO_PATH = "MyModel.tflite"

# Create the labels file
with open(LABEL_FILE, 'w') as label_file:
    for label in class_names: # /!\ class_names variable must contains the names of the labels you have.
        label_file.write("{}\n".format(label))

tflite_model = tf.lite.TFLiteConverter.from_keras_model(model).convert()

# Save the model.
with open(SAVE_TO_PATH, 'wb') as f:
    f.write(tflite_model)
    
ImageClassifierWriter = image_classifier.MetadataWriter

# Normalization parameters is required when reprocessing the image. It is
# optional if the image pixel values are in range of [0, 255] and the input
# tensor is quantized to uint8. See the introduction for normalization and
# quantization parameters below for more details.
# https://www.tensorflow.org/lite/convert/metadata#normalization_and_quantization_parameters)
INPUT_NORM_MEAN = 127.5
INPUT_NORM_STD = 127.5

# Create the metadata writer.
writer = ImageClassifierWriter.create_for_inference(
    writer_utils.load_file(SAVE_TO_PATH),
    [INPUT_NORM_MEAN],
    [INPUT_NORM_STD],
    [LABEL_FILE]
)

# Verify the metadata generated by metadata writer.
print(writer.get_metadata_json())

# Populate the metadata into the model.
writer_utils.save_file(writer.populate(), SAVE_TO_PATH)

Testing your model on your smartphone

Instructions are given below, but check the following link for further informations: https://developer.android.com/studio/run/device

Note that in some rare cases you will need to install some drivers. Check Google documentation or the documentation from your smartphone's manufacturer.

The given code is not compatible with Iphones! If you don't have any compatible Android smartphone we can lend you one.

Enabling USB debugging

You will need to have USB debugging enabled to be able to run the app on your smartphone by connecting it by USB.

  1. To turn on developer mode: About phone -> tap on build number 7 times.
  2. Settings -> System -> Advanced -> Developer options -> USB debugging : activate this option

Connect the smartphone to your computer

  1. Connect your smartphone to your computer with the cable
  2. Allow USB debugging

Running the app on your device

In Android Studio you should now see your smartphone in the running devices selection

Testing your model on an emulator

This is not necessary! If you want to try with an emulator and your webcam you can do like so (on Android Studio) :

  1. Devices list -> AVD Manager -> Create virtual device... -> Phone -> Pixel 4 -> API level 30, Android 11 -> Show Advanced Settings -> Camera : select your webcam here. -> Finish

Troubleshooting :

  1. Be sure to have enough space on the disk
  2. Be sure to have the Android 11 SDK installed : Tools -> SDK Manager -> Android SDK -> Check "Android 11.0 (R)" -> Apply

Then you can run the code on the emulator.

License

Copyright (C) 2020 The Android Open Source Project

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

http://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.

About

TensorFlow Lite code lab for implementing a custom classifier.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Kotlin 100.0%