how does the model of tflite format provided by speech command android demo be converted?

Viewed 489

I used lite Converter to convert my model of pb format to tflite format, and use lite Interpreter to run this model, but it didn't work well and the error is:

Traceback (most recent call last):
  File "/home/yuan/anaconda3/envs/TFLite/lib/python3.7/threading.py", line 917, in _bootstrap_inner
    self.run()
  File "/home/yuan/tensorflow-master/tensorflow/examples/speech_commands/audio/audio_processor_lite.py", line 41, in run
    self._interpreter.allocate_tensors()
  File "/home/yuan/anaconda3/envs/TFLite/lib/python3.7/site-packages/tensorflow_core/lite/python/interpreter.py", line 198, in allocate_tensors
    return self._interpreter.AllocateTensors()
  File "/home/yuan/anaconda3/envs/TFLite/lib/python3.7/site-packages/tensorflow_core/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py", line 106, in AllocateTensors
    return _tensorflow_wrap_interpreter_wrapper.InterpreterWrapper_AllocateTensors(self)
RuntimeError: Regular TensorFlow ops are not supported by this interpreter. Make sure you invoke the Flex delegate before inference.Node number 0 (FlexAudioSpectrogram) failed to prepare.

It looks like that the lite doesn't support the FlexAudioSpectrogram ops yet. But when I used the tflite model provided by speech command android demo, it works pretty well. So I want to know how this model was converted?

2 Answers

I found the answer. With the allow_custom_ops=True you can convert the model to tflite and it works like a charm in android.

I viewed both my converted model and the one that comes with the android example in netron and they look identically. So it seems that is how he or she pulled that off.

Small update:

In the source code of another project on speech using tensorflow (Mozilla DeepSpeech) I found the following comment regarding AudioSpectrogram and Mfcc ops in tflite conversion:

# AudioSpectrogram and Mfcc ops are custom but have built-in kernels in TFLite
converter.allow_custom_ops = True

You can use allow_custom_ops to allow to tflite to use custom ops, however, some layers are not supported by tflite, and you should provide custom ops for them. You can read an article to define custom ops here.

Related