Problem
I created a workbook in which I classify images on if they are a hotdog or not as a multi-label classification problem. I use the MobileNetv2
architecture trained on ImageNet
weights. When testing the model before I converted it into the Tflite format, I was getting 93% accuracy on its predictions. But when I convert the model to the Tflite format to use for mobile and use the Tflite flutter package, I always get a confidence
rating of 1.0 for the first label in my labels.txt file. ['hotdog', 'nothotdog']
Model Setup
I am new to converting the model to be used by edge devices so maybe I'm doing something incorrectly but I'm just not seeing it and the issues section on the library repo seems dead so no help there.
conv_base = keras.applications.mobilenet_v2.MobileNetV2(
weights="imagenet",
include_top=False
)
conv_base.trainable = False
inputs = keras.Input(shape=(256, 256, 3))
x = data_augmentation(inputs)
x = keras.applications.mobilenet_v2.preprocess_input(x)
x = conv_base(x)
x = layers.Flatten()(x)
x = layers.Dense(512)(x)
x = layers.Dropout(0.5)(x)
outputs = layers.Dense(2, activation=keras.activations.softmax)(x)
model = keras.Model(inputs, outputs)
model.compile(loss=keras.losses.SparseCategoricalCrossentropy(),
optimizer=keras.optimizers.RMSprop(),
metrics=["accuracy"])
model.summary()
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_6 (InputLayer) [(None, 256, 256, 3)] 0
sequential (Sequential) (None, 256, 256, 3) 0
tf.math.truediv_1 (TFOpLamb (None, 256, 256, 3) 0
da)
tf.math.subtract_1 (TFOpLam (None, 256, 256, 3) 0
bda)
mobilenetv2_1.00_224 (Funct (None, None, None, 1280) 2257984
ional)
flatten_2 (Flatten) (None, 81920) 0
dense_4 (Dense) (None, 512) 41943552
dropout_1 (Dropout) (None, 512) 0
dense_5 (Dense) (None, 2) 1026
=================================================================
Total params: 44,202,562
Trainable params: 41,944,578
Non-trainable params: 2,257,984
_________________________________________________________________
import tensorflow as tf
from tensorflow import keras
import pathlib
test_model = keras.models.load_model(f"{hotDogDir}hotdog_multiclassifier_mobilenet_v1.keras")
converter = tf.lite.TFLiteConverter.from_keras_model(test_model)
tflite_model = converter.convert()
tflite_models_dir = pathlib.Path(f"{hotDogDir}")
tflite_models_dir.mkdir(exist_ok=True, parents=True)
tflite_model_file = tflite_models_dir/"hotdog_multiclassifier_mobilenet.tflite"
tflite_model_file.write_bytes(tflite_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()
tflite_model_quant_file = tflite_models_dir/"hotdog_multiclassifier_mobilenet.tflite"
tflite_model_quant_file.write_bytes(tflite_quant_model)
Flutter Setup
I created the flutter application from a base which was supplied and just edited it to work with my custom model which is supplied at this repo for clearer reading: repo.
Questions
- Why is my predictions now incorrect and always aiming at the first label class on the mobile?
- What can I do to fix this?
Thank you