Internal error: Unexpected failure when preparing tensor allocations: tensorflow/lite/kernels/conv.cc:343 input->dims->size != 4 (3 != 4)
Hi @gptshubham595 Thanks for reporting the issue.

We see that the colab file you have shared has data access issues. Could you please provide a standalone code or a toy tflite model inorder to reproduce the issue?

Thanks. [NoiseSuppressionModel.h5](https://drive.google.com/file/d/1xUQklFyuYFV_ZsuO8Rskc52xsuSPZCip/view?usp=share_link)
[Model.Tflite]( https://drive.google.com/file/d/1xUQklFyuYFV_ZsuO8Rskc52xsuSPZCip/view?usp=sharing) Hi @gptshubham595 

The input signature of the TFLite model is `'shape_signature': array([   -1, 12000,     1]`. I have tested with random data with shape `[1,1200,1]` and model runs without any error.

Please find the colab gist [here](https://colab.research.google.com/gist/pjpratik/bd48804cc8d40239812079b5a249aac3/60367.ipynb) and let us know if it helps.

Thanks. I tried this in android but i'm getting this error

```
private fun applyModel() {
        val inputFloatArray = Array(1) { Array(inputAudioData.size) { FloatArray(1) } } //1,1200,1

        val outputFloatArray = inputFloatArray //Attempt 1
        val outputFloatArray = Array(1) { Array(inputAudioData.size) { FloatArray(1) } } //Attempt 2

        Log.d("tflite", "Model input data: ${inputFloatArray.toString()}")

        tflite!!.run(inputFloatArray, outputFloatArray)

        Log.d("tflite", "Model output data: ${outputFloatArray.toString()}")
    }
```


```
 java.lang.IllegalStateException: Internal error: Unexpected failure when preparing tensor allocations: tensorflow/lite/kernels/reshape.cc:85 num_input_elements != num_output_elements (1200 != 0)
                                                                                                    Node number 6 (RESHAPE) failed to prepare.
                                                                                                    	at org.tensorflow.lite.NativeInterpreterWrapper.allocateTensors(Native Method)
                                                                                                    	at org.tensorflow.lite.NativeInterpreterWrapper.allocateTensorsIfNeeded(NativeInterpreterWrapper.java:308)
                                                                                                    	at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:248)
                                                                                                    	at org.tensorflow.lite.InterpreterImpl.runForMultipleInputsOutputs(InterpreterImpl.java:101)
                                                                                                    	at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:77)
                                                                                                    	at org.tensorflow.lite.InterpreterImpl.run(InterpreterImpl.java:94)
                                                                                                    	``` Hi @gptshubham595 

It is hard to say from the code snippet, but as mentioned earlier please check the input tensor shapes and resize before during inference.

Also, If the loaded model in TFLite does not have any defined batch size, converter will take the batch size as 1, and when you evaluate it with the different batch size, you are likely to end up with the problem which you are facing.

Thanks. This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60367">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/60367">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Support

### Have you reproduced the bug with TF nightly?

Yes

### Source

source

### Tensorflow Version

2.12.0

### Custom Code

Yes

### OS Platform and Distribution

MacOS

### Mobile device

Linux (Android Oneplus)

### Python version

3.9

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

I am working on this noise and echo cancellation on android. I used this tflite model created via https://colab.research.google.com/drive/1HzGdovqo0gg_xW1QL7ygbkYlWqbyMaKL?usp=sharing#scrollTo=2kRjDbp7og1u

Now I want to use a ShortArray() audioData and want to pass and get a ShortArray() data back with  removed echo and noise.

I tried creating, tflite model, predict function works there, I incorporated it in android but whenever data is passed in the array it returns the error

```Internal error: Unexpected failure when preparing tensor allocations: tensorflow/lite/kernels/conv.cc:343 input->dims->size != 4 (3 != 4)```

### Standalone code to reproduce the issue

```shell
snippet has this


#@INPUT
print(test_audio) gives 

<tf.Tensor: shape=(6, 12000, 1), dtype=float32, numpy=
array([[[-0.00097656],
        [-0.01428223],
        [-0.02471924],
        ...,
        [ 0.14346313],
        [ 0.13012695],
        [ 0.14242554]]]], dtype=float32)>
```

```
predict_tflite_with_array(tf.convert_to_tensor(test_audio, np.float32)) # @To predict
```

```
#@Output
<tf.Tensor: shape=(68800,), dtype=float32, numpy=
array([-0.00645937, -0.01239021, -0.01816257, ..., -0.02149353,
       -0.0514788 , -0.04442336], dtype=float32)>
```

```
def predict_tflite_with_array(test_audio):
  input_index = interpreter.get_input_details()[0]["index"]
  output_index = interpreter.get_output_details()[0]["index"]

  preds = []
  for i in test_audio:
    interpreter.set_tensor(input_index, tf.expand_dims(i,0))
    interpreter.invoke()
    predictions = interpreter.get_tensor(output_index)
    preds.append(predictions)

  predictions = tf.squeeze(tf.stack(preds,axis=1))
  final_op = tf.reshape(predictions[:-1],((predictions.shape[0]-1)*predictions.shape[1],1))
  final_op = tf.concat((tf.squeeze(final_op),predictions[-1][-diff:]),axis=0)
  return final_op

```



I used this tflite model in android to run this

```
aaptOptions {
        noCompress "model.tflite"
    }

implementation 'org.tensorflow:tensorflow-lite:2.12.0'
```
```
 @Throws(IOException::class)
    private fun loadModelFile(activity: Activity): MappedByteBuffer? {
        val fileDescriptor: AssetFileDescriptor = activity.assets.openFd("model.tflite")
        val inputStream = FileInputStream(fileDescriptor.fileDescriptor)
        val fileChannel = inputStream.channel
        val startOffset = fileDescriptor.startOffset
        val declaredLength = fileDescriptor.declaredLength
        return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength)
    }

    try {
            viewModel.tflite = loadModelFile(requireActivity())?.let {
                Interpreter(it) }
            Log.d("tflite", "Model initiated")
        } catch (ex: Exception) {
            ex.printStackTrace()
            Log.d("tflite", "Model initiation Failed $ex")
        }
```

```
private fun applyModel(inputAudioData: ShortArray): ShortArray {
        val inputVal = FloatArray(inputAudioData.size)

        for (i in inputAudioData.indices) {
            inputVal[i] = inputAudioData[i].toFloat()
        }
        val inputFloatArray = Array(1){
            inputVal
        }
        val outputFloatArray = Array(1) {
            FloatArray(inputAudioData.size)
        }

        Log.d("tflite", "Model input data: ${inputFloatArray.toString()}")

        tflite!!.run(inputFloatArray, outputFloatArray)
        Log.d("tflite", "Model output data: ${outputFloatArray.toString()}")

        val outputAudioData = ShortArray(inputFloatArray.size)

        for (i in outputFloatArray[0].indices) {
            outputAudioData[i] = outputFloatArray[0][i].toInt().toShort()
        }
        Log.d("tflite", "Model returning data: ${outputAudioData.toString()}")

        return outputAudioData
    }
```

  [1]: https://colab.research.google.com/drive/1HzGdovqo0gg_xW1QL7ygbkYlWqbyMaKL?usp=sharing#scrollTo=2kRjDbp7og1u
```


### Relevant log output

```shell
but whenever the app is running its crashing with this error


 java.lang.IllegalStateException: Internal error: Unexpected failure when preparing tensor allocations: tensorflow/lite/kernels/conv.cc:343 input->dims->size != 4 (3 != 4)
                                                                                                    Node number 5 (CONV_2D) failed to prepare.
                                                                                                    	at org.tensorflow.lite.NativeInterpreterWrapper.allocateTensors(Native Method)
                                                                                                    	at org.tensorflow.lite.NativeInterpreterWrapper.allocateTensorsIfNeeded(NativeInterpreterWrapper.java:308)
                                                                                                    	at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:248)
                                                                                                    	at org.tensorflow.lite.InterpreterImpl.runForMultipleInputsOutputs(InterpreterImpl.java:101)
                                                                                                    	at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:77)
                                                                                                    	at org.tensorflow.lite.InterpreterImpl.run(InterpreterImpl.java:94)
                                                                                                    	at org.tensorflow.lite.Interpreter.run(Interpreter.java:77)
```
```
</details>