douglas125 / SpeechCmdRecognition

A neural attention model for speech command recognition
MIT License
180 stars 78 forks source link

tflite model #7

Closed norbaf78 closed 4 years ago

norbaf78 commented 4 years ago

I have tried to conver the created model in tflite but with no result. Someone can provide me if converting to tflite is possible and how do it. Here what I have tried with no result.

converter = tf.lite.TFLiteConverter.from_keras_model(model) tflite_model = converter.convert()

INFO:tensorflow:Assets written to: attRNNModel/1/assets ValueError Traceback (most recent call last)

in () 8 9 converter = tf.lite.TFLiteConverter.from_keras_model(model) ---> 10 tflite_model = converter.convert() 11 /usr/local/lib/python3.6/dist-packages/tensorflow_core/lite/python/lite.py in convert(self) 444 raise ValueError( 445 "None is only supported in the 1st dimension. Tensor '{0}' has " --> 446 "invalid shape '{1}'.".format(_get_tensor_name(tensor), shape_list)) 447 elif shape_list and shape_list[0] is None: 448 # Set the batch size to 1 if undefined. ValueError: None is only supported in the 1st dimension. Tensor 'input' has invalid shape '[None, None]'.
douglas125 commented 4 years ago

Sorry for the late answer, I'll try to do that and let you know.

douglas125 commented 4 years ago

Ok, it looks like it's an issue with tensorflow that is solved in the nightly build.

This works for me (in Colab):

!pip install tf-nightly

!wget https://github.com/douglas125/SpeechCmdRecognition/blob/master/model-attRNN.h5?raw=true
!wget -q https://raw.githubusercontent.com/douglas125/SpeechCmdRecognition/master/SpeechModels.py
!pip install kapre==0.2 
!mv model-attRNN.h5?raw=true model-attRNN.h5

import tensorflow as tf
from tensorflow.keras.models import load_model
from kapre.time_frequency import Melspectrogram, Spectrogram
from kapre.utils import Normalization2D
import SpeechModels

model = load_model('model-attRNN.h5', custom_objects={'Melspectrogram': Melspectrogram, 'Normalization2D': Normalization2D })

model.save('att_rnn')

saved_model_dir = 'att_rnn'
# Convert the model
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir) # path to the SavedModel directory
tflite_model = converter.convert()

# Save the model.
with open('attRNN.tflite', 'wb') as f:
  f.write(tflite_model)

I'm closing this for now, but feel free to reopen if there are other issues.