@George_Soloupis thank you again for taking the time to reply to this.
So, I have literally just followed the official tflite classification tutorial to generate the model.
The tutorial simply loads data from folders from what I understand. The default model is efficientnet0. It says the following on the page:
Preprocess the raw input data. Currently, preprocessing steps including normalizing the value of each image pixel to model input scale and resizing it to model input size. EfficientNet-Lite0 have the input scale [0, 1] and the input image size [224, 224, 3].
Now, I am unsure how to scale the input in the 0-1 range mentioned here ← hence we my original question since my input seems to be in 0-255 range.
So, I either do this:
def preprocess_image(image_path, input_size):
"""Preprocess the input image to feed to the TFLite model"""
img = tf.io.read_file(image_path)
img = tf.io.decode_image(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.uint8)
print('min max img value',tf.reduce_min(img),tf.reduce_max(img))
original_image = img
resized_img = tf.image.resize(img, input_size)
resized_img = resized_img[tf.newaxis, :]
return resized_img, original_image
or I do:
def preprocess_image(image_path, input_size):
"""Preprocess the input image to feed to the TFLite model"""
img = tf.io.read_file(image_path)
img = tf.io.decode_image(img, channels=3)
img = tf.cast(img, dtype=tf.float32) / tf.constant(255, dtype=tf.float32)
print('min max img value',tf.reduce_min(img),tf.reduce_max(img))
original_image = img
resized_img = tf.image.resize(img, input_size)
resized_img = resized_img[tf.newaxis, :]
return resized_img, original_image
the difference is:
img = tf.cast(img, dtype=tf.float32) / tf.constant(255, dtype=tf.float32)
to norm the values between 0-1. But then the class values are totally wrong as in it makes the wrong preds.
However, if I do NOT norm the values to 01, the results look perfect (although the results are in the range [0-255] and taking the argmin of this gives me the correct class. So I think I am able to conlude from these little experiments that the normalisation line is not required. This however seems to go against the docs.
So, I am puzzled what is going on and I am looking for an official inference example from tflite model maker classification
I am now wondering if the inference interpretter automatically does some normalisation I am unaware of and that I probably do not need to do any of these normalisations.
When I look at some randoom posts such as:
I see that the inference output is in the 0-255 range for a single image, which is the case for me.
The other thought I have is that, since I quantise the model via: model.export(export_dir='.')
I think the output range is between 0-255. I got this idea from here: https://github.com/tensorflow/examples/blob/master/lite/examples/image_classification/android/EXPLORE_THE_CODE.md
So, yea, just a bit confused as to what the correct thing is to do here.
Thank you.