tensorflow-litetfjs-node

Using Saved Model Format from Tensorflow Lite Model Maker leads to poor prediction results


Following this tutorial I created an simple model for image classification with Tensorflow Model Maker. I changed export format from tflite to Saved Model as I intended to use it with tfjs-node as shown here.

# model.export(export_dir='.') 
model.export(export_dir='.', export_format=ExportFormat.SAVED_MODEL)

Testing the tflite model with an image in python returns very good results (near 95% for the desired class):

    import numpy as np
    from PIL import Image
    import tensorflow as tf
    
    model_file = "model.tflite"
    image_file = "my_image_name.jpg"
    
    interpreter = tf.lite.Interpreter(model_path=model_file)
    interpreter.allocate_tensors()
    
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    
    image = Image.open(image_file)
    input_data = np.expand_dims(image, axis=0)
    
    interpreter.set_tensor(input_details[0]['index'], input_data)
    interpreter.invoke()
    output_data = interpreter.get_tensor(output_details[0]['index'])
    results = np.squeeze(output_data)
    for result in results:
        print(float(result / 255.0))
        # 0.011764705882352941 0.043137254901960784 0.9490196078431372

When using the Saved Model format prediction results are totally different and wrong:

    import numpy as np
    from PIL import Image
    from tensorflow.keras.models import load_model
    
    model_file = "saved_model"
    image_file = "my_image_name.jpg"
    
    model = load_model(model_file)    
    image = Image.open(image_file)
    input_tensor = np.expand_dims(image,axis=0)   
   
    predictions = model.predict(input_tensor)
    print(predictions)
    #0.0564433 0.69343865 0.25011805

Same problem when I use the Saved Model with tfjs-node.

1.) Do you have any idea how to fix this or where I could have a look at? 2.) What also could be helpful: Is there any supported way in tensorflow to use the tflite model with tfjs-node?

Things I tried among many other things:


Solution

  • When using saved model, we need to nomalized and resize the model. Please refer to https://github.com/tensorflow/examples/blob/master/tensorflow_examples/lite/model_maker/core/task/image_preprocessing.py#L62.

    The mean_rgb and stddev_rgb are in https://github.com/tensorflow/examples/blob/master/tensorflow_examples/lite/model_maker/core/task/model_spec/image_spec.py#L33