ValueError: Cannot set tensor: Dimension mismatch

When setting a tensor for a model I have converted to tf.lite from keras I get the error ValueError: Cannot set tensor: Dimension mismatch. Got 416 but expected 1 for dimension 1 of input 0.

The input sample is an three channel image with shape (1, 416, 416, 3)
The input details got from the interpreter are:
{‘name’: ‘serving_default_input_2:0’,
‘index’: 0,
‘shape’: array([1, 1, 1, 3]),
‘shape_signature’: array([-1, -1, -1, 3]),
‘dtype’: numpy.uint8,
‘quantization’: (0.003921568859368563, 0),
‘quantization_parameters’: {‘scales’: array([ 0.0039216], dtype=float32),
‘zero_points’: array([0]),
‘quantized_dimension’: 0},
‘sparsity_parameters’: {}}
test_image_.shape

The shape signature matches the image, but the interpreter shape is wrong. Any ideas why the tf.lite model got this parameter wrong?

Thanks,
Juan

Hi @Juan_Suarez_Vazquez ,

Probably you are setting the tensor as (416,416,3) but the model also needs batch (1) and the input to be (1,416,416,3). Print the shape of the tensor you are feeding the Interpreter and if you see it like (416, 416, 3) then add the first dimension appropriately with TensorFlow or numpy operator.

Regards

Thanks @George_Soloupis,

That was not the issue. The original tf model had dynamic tensor shapes for batch and image size (?, ?, ?, 3) which is something tf.lite doesn’t seem to ([like] (Input details changed after converting tf model to tflite model · Issue #58380 · tensorflow/tensorflow · GitHub)). I solved it creating a keras model with fixed input shape (1, 416, 416, 3) feeding the tensor to the model.

1 Like