TFlite conversion Doesn't support dense layers with inputs of rank greater than 2
1. System information
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): windows 10 64 bit
- TensorFlow installation (pip package or built from source): Python 3.8.2, pip: 22.1, pip package
- TensorFlow library (version, if pip package or github SHA, if built from source): TF version:'2.9.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
import pathlib
def tflite_convert(model,data):
def representative_data_gen():
for input_value in data:
input_value = input_value[np.newaxis, ...]
yield [input_value] # shape should be (1, <data point size))
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen
# Ensure that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
tflite_model = converter.convert()
tflite_models_dir = pathlib.Path("./output/tflite_models/")
tflite_models_dir.mkdir(exist_ok=True, parents=True)
tflite_model_file = tflite_models_dir/"model_{0}.tflite".format(model.name)
tflite_model_file.write_bytes(tflite_model)
def tflite_explore(path):
tflite_interpreter = tf.lite.Interpreter(model_path=path)
tflite_interpreter.allocate_tensors()
'''
Check input/output details
'''
input_details = tflite_interpreter.get_input_details()
output_details = tflite_interpreter.get_output_details()
print("== Input details ==")
print("name:", input_details[0]['name'])
print("shape:", input_details[0]['shape'])
print("type:", input_details[0]['dtype'])
print("\n== Output details ==")
print("name:", output_details[0]['name'])
print("shape:", output_details[0]['shape'])
print("type:", output_details[0]['dtype'])
def make_dense_model():
input_layer = tf.keras.Input(shape=(1,5,12))
dense_layer=tf.keras.layers.Dense(3)(input_layer)
model=tf.keras.Model(input_layer,dense_layer)
return model
def generate_Noise_Data(shape,batch_size):
if None in shape:
shape=list(shape)
shape[0]=batch_size
noise=np.array(np.random.randint(0,255,shape).astype(np.float32))
return noise/255
model=make_dense_model()
print(model.summary())
in_value=generate_Noise_Data(model.layers[0].input_shape[0],2)
print(model(in_value))
model.save("./output/dense1layer.h5")
tflite_convert(model,in_value)
tflite_explore("./output/tflite_models/model_{0}.tflite".format(model.name))
3. Failure after conversion
If the conversion is successful, but the generated model is wrong, then state what is wrong:
- Model produces wrong results
- here i use a keras model that is converted to .tflite file. the keras model's HDF5 save and its tflite converted '.tflite' are visualized in netron. their outputs don't match.
- in the keras model: the input is of shape (none,1,5,12) (shape is>2D ).the model has a single dense layer with 3 nodes. its kernal are of shape [12,3]. the model produces an output of shape (none,1,5,3).
- in the .tflite converted file the output is of shape [1,1,1,3] (not [1,1,5,3]) and input is of shape [1,1,5,12]
this is the output i have obtained with the above code:
its netrons with .h5 on the left and .tflite on the right

Hi @sachinprasadhs ! Could you look at this issue. Attached gist in 2.8, 2.9 and nightly for reference.
@Saar-Ken-Ji The issue seems to be resolved in TF Nightly 2.12.0-dev20230117. The output shape of TF lite converted model is obtained as [1,1,5,3] . Please find the gist here and let us know if it helps. Thank you.
This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.
Closing as stale. Please reopen if you'd like to work on this further.