I have been experimenting with Tensorflow speech recognition using this notebook running in my local machine using a custom dataset.
https://www.tensorflow.org/tutorials/audio/simple_audio
I have successfully trained a model and exported the model. But when i tried to convert the exported model to a tflite model using the below code it gives me an error.
# Load the saved model
saved_model_path = "saved"
saved_model = tf.saved_model.load(saved_model_path)
# Convert the model to TFLite and save it in a new folder called "saved-lite"
converter = tf.lite.TFLiteConverter.from_saved_model('saved')
tflite_model = converter.convert()
with open('saved-lite/model.tflite', 'wb') as f:
f.write(tflite_model)
ValueError: Only support at least one signature key.
Then I tried the below code and it still gives me an error,
# Load the saved model
saved_model_path = "saved"
saved_model = tf.saved_model.load(saved_model_path)
# Set the concrete function to be used for conversion
concrete_func = saved_model.signatures['serving_default']
# Convert the model to TFLite and save it in a new folder called "saved-lite"
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
tflite_model = converter.convert()
with open('saved-lite/model.tflite', 'wb') as f:
f.write(tflite_model)
The error : KeyError: 'serving_default'
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_28532\3665239774.py in <module>
4
5 # Set the concrete function to be used for conversion
----> 6 concrete_func = saved_model.signatures['serving_default']
7
8 # Convert the model to TFLite and save it in a new folder called "saved-lite"
~\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\saved_model\signature_serialization.py in __getitem__(self, key)
245
246 def __getitem__(self, key):
--> 247 return self._signatures[key]
248
249 def __iter__(self):
I tried to convert the model with this code too but it gives me an error too:
# Converting a SavedModel to a TensorFlow Lite model.
converter = tf.lite.TFLiteConverter.from_saved_model('saved')
tflite_model = converter.convert()
ValueError: Only support at least one signature key.
I use the same code provided in the tutorial notebook to export the model. the below code
class ExportModel(tf.Module):
def __init__(self, model):
self.model = model
# Accept either a string-filename or a batch of waveforms.
# YOu could add additional signatures for a single wave, or a ragged-batch.
self.__call__.get_concrete_function(
x=tf.TensorSpec(shape=(), dtype=tf.string))
self.__call__.get_concrete_function(
x=tf.TensorSpec(shape=[None, 16000], dtype=tf.float32))
@tf.function
def __call__(self, x):
# If they pass a string, load the file and decode it.
if x.dtype == tf.string:
x = tf.io.read_file(x)
x, _ = tf.audio.decode_wav(x, desired_channels=1, desired_samples=16000,)
x = tf.squeeze(x, axis=-1)
x = x[tf.newaxis, :]
x = get_spectrogram(x)
result = self.model(x, training=False)
class_ids = tf.argmax(result, axis=-1)
class_names = tf.gather(label_names, class_ids)
return {'predictions':result,
'class_ids': class_ids,
'class_names': class_names}
export = ExportModel(model)
The model has been succesfully created and stored in the saved/ folder. This is the inside of the saved/ folder,
I’m using TensorFlow 2.11.0
can someone correct me where I’m doing wrong, I really appreciate your help.