"Only support at least one signature key." when trying converting a tensorflow model to tensorflow lite model

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,
enter image description here

I’m using TensorFlow 2.11.0

can someone correct me where I’m doing wrong, I really appreciate your help.

Hi @YRW, Welcome to the TF Forum!

The signatures specify inputs and outputs of the converted TensorFlow Lite model by respecting the TensorFlow model’s signatures.

If your model has tags you have to provide the signature through the saved model converter API.

Thank You.

@Kiran_Sai_Ramineni First of all Thank you for your reply, I kind of get this signature but not enough to apply this to my code can you Explain me how to implement this on my code, Do I need to specify anything when I’m exporting the Tensorflow model, or specify anything when I’m coverting the modl. Because as for now When I do this,

saved_model = tf.saved_model.load('saved')
print(saved_model.signatures.keys())

It prints this,

KeysView(_SignatureMap({}))

So What I understood was I don’t have any signatures if not how do I add this signature thing to the model eventhough I don’t quite understand what’s going on. I followed this exact notebook of this tutorial - https://www.tensorflow.org/tutorials/audio/simple_audio

Can you kindly guide me through this, I really appreciate your reply.

I had this problem when I wanted to convert onnx to tflite using onnx2tf.
I added -osd to the command, and the problem has been solved.

1 Like

seems like your message above is deleted for somekind of reason can you kindly provide some more information?

Hi,
I am having the same issue and was wondering if you found a fix by now?

You might need to specify a signature when saving the model.

See “Specifying signatures during export” here: Using the SavedModel format  |  TensorFlow Core

I hope this helps

what do you mean -osd? Where do I insert that?