Issue
I'm a little new to Tensor Flow and would like to understand why the following codes does not accept my input and how to resolve it. Prior to this, I was using mode_save
but I have now converted this model to TFLite and would like to use it to predict the category of the inputted text.
I first load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="/model.tflite")
interpreter.allocate_tensors()
Then get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
Then tokenized my input data. I take my text and use texts_to_sequences which is just tokenizer_from_json
in Tensorflow, then pad it as I do in my model training.
So my input is something like this...
[[144 122 557 136 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0]]
This is the shape I would expect as I used the same one for my model training.
Now I just want to use tensor flow lite predict, however running the code below gives the following error:
input_shape = input_details[0]['shape']
text = 'We know what we are, but know not what we may be.'
seq = self.tokeniser.texts_to_sequences(text)
input_tensor= np.array(seq, dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_tensor)
Cannot set tensor: Dimension mismatch. Got 60 but expected 1 for dimension 1 of input 0.
Why?
This is what I have tried based on suggestions.....
output = interpreter.get_output_details()[0] # Model has single output.
input = interpreter.get_input_details()[0] # Model has single input.
input_data = tf.constant(padded_text, shape=[1, 1])
interpreter.set_tensor(input['index'], input_data)
interpreter.invoke()
print(interpreter.get_tensor(output['index']).shape)
which gives the following error:
Eager execution of tf.constant with unsupported shape. Tensor [[144 122 557 136 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
Solution
Your input tensor is the wrong size. The docs show that your input data should be of shape 1,1
:
input_data = tf.constant(1., shape=[1, 1])
interpreter.set_tensor(input['index'], input_data)
I suspect changing the line:
interpreter.set_tensor(input_details[0]['index'], input_tensor)
to
interpreter.set_tensor(input_details['index'], input_tensor)
should fix your issue.
Answered By - Viglione
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.