I was running following code from this suggestion:
import tensorflow as tf
import numpy as np
import zipfile
model_path = './UrbanSound.tflite'
interpreter = tf.lite.Interpreter(model_path)
input_details = interpreter.get_input_details()
waveform_input_index = input_details[0]['index']
output_details = interpreter.get_output_details()
scores_output_index = output_details[0]['index']
# Input: 0.975 seconds of silence as mono 16 kHz waveform samples.
waveform = np.zeros(int(round(0.975 * 16000)), dtype=np.float32)
print(waveform.shape) # Should print (15600,)
interpreter.resize_tensor_input(waveform_input_index, [waveform.size], strict=True)
interpreter.allocate_tensors()
interpreter.set_tensor(waveform_input_index, waveform)
interpreter.invoke()
scores = interpreter.get_tensor(scores_output_index)
print(scores.shape) # Should print (1, 521)
top_class_index = scores.argmax()
labels_file = zipfile.ZipFile(model_path).open('yamnet_label_list.txt')
labels = [l.decode('utf-8').strip() for l in labels_file.readlines()]
print(len(labels)) # Should print 521
print(labels[top_class_index]) # Should print 'Silence'.
Getting following error:
(15600,)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
Cell In[28], line 17
14 waveform = np.zeros(int(round(0.975 * 16000)), dtype=np.float32)
15 print(waveform.shape) # Should print (15600,)
---> 17 interpreter.resize_tensor_input(waveform_input_index, [waveform.size], strict=True)
18 interpreter.allocate_tensors()
19 interpreter.set_tensor(waveform_input_index, waveform)
File ~/anaconda3/lib/python3.11/site-packages/tensorflow/lite/python/interpreter.py:749, in Interpreter.resize_tensor_input(self, input_index, tensor_size, strict)
746 # `ResizeInputTensor` now only accepts int32 numpy array as `tensor_size
747 # parameter.
748 tensor_size = np.array(tensor_size, dtype=np.int32)
--> 749 self._interpreter.ResizeInputTensor(input_index, tensor_size, strict)
RuntimeError: tensorflow/lite/core/subgraph.cc:1209 tensor->dims->size != dims.size() (2 != 1)
What am I doing wrong?