Skip to content

reduce_sum in tflite outputs is incorrect #50992

Closed
@reuvenperetz

Description

@reuvenperetz

1. System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): 18.04
  • TensorFlow installation (pip package or built from source): pip
  • TensorFlow library (version, if pip package or github SHA, if built from source): 2.5.0

2. Code

Provide code to help us reproduce your issues using one of the following options:

import tensorflow as tf
import numpy as np


keras = tf.keras
layers = keras.layers


def infer(tflite_model, img):
    interpreter = tf.lite.Interpreter(model_content=tflite_model)
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()[0]
    interpreter.set_tensor(input_details["index"], img)
    interpreter.invoke()
    output_details = interpreter.get_output_details()[0]
    output = interpreter.get_tensor(output_details["index"])
    return output

def convert_to_tflite(model):
    converter = tf.lite.TFLiteConverter.from_keras_model(model)
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.experimental_new_converter = True
    tflite_model = converter.convert()
    return tflite_model


# Image to test
np.random.seed(1)
input_shape = (12, 14, 18)
img = np.random.random((1,)+input_shape).astype(np.float32) * 7.0 - 3.5

# Create a model
i = layers.Input(shape=input_shape)
x = tf.quantization.fake_quant_with_min_max_args(i, min=0.0, max=3.984375)
x = tf.reduce_sum(x, axis=None)
x = tf.quantization.fake_quant_with_min_max_args(x, min=-4.0, max=3.96875)
model = keras.models.Model(inputs=i, outputs=x)


tf_output = model.predict(img)
tflite_model = convert_to_tflite(model)
tflite_output = infer(tflite_model, img)

print(f'Tensorflow output: {tf_output}, TFLite output: {tflite_output}, ')

3. Failure after conversion

If the conversion is successful, but the generated model is wrong, then state what is wrong:
I get a huge difference between the TF model and the TFLite model:
Tensorflow output: 3.96875, TFLite output: -2.78125,

The Tensorflow model gives completely different results than the TFLite model. Furthermore, before the reduce_sum, I quantize the tensor using only positive values, so it does not make sense that the output of the model is negative (which is what I get).

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions