I had created a text-classification model using the pre-trained model from Tensorflow Hub, the summary of the model is like this
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
keras_layer_5 (KerasLayer) (None, 128) 112461824
_________________________________________________________________
flatten_2 (Flatten) (None, 128) 0
_________________________________________________________________
dense_6 (Dense) (None, 16) 2064
_________________________________________________________________
dense_7 (Dense) (None, 1) 17
=================================================================
Total params: 112,463,905
Trainable params: 2,081
Non-trainable params: 112,461,824
_________________________________________________________________
Then I convert this model to tflite format with tf-nightly
and it was a success. After that I wanted to do test the tflite model with my own texts in python. Here is my code:
# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(r'./model_tf_filterComments.tflite')
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_index = interpreter.get_input_details()[0]["index"]
output_index = interpreter.get_output_details()[0]["index"]
input_data = ["You are extremely strong man, please don't give up!"]
interpreter.set_tensor(input_details[0]['index'], input_data) ## This line causes the error
However, I cannot use my text to predict using the tflite model as it's always error when I set the tensor for input_details. The error details are shown below:
ValueError Traceback (most recent call last)
<ipython-input-30-b32325c64589> in <module>()
1 input_data = ["You are extremely strong man, please don't give up!"]
----> 2 interpreter.set_tensor(input_details[0]['index'], input_data)
/usr/local/lib/python3.7/dist-packages/tensorflow/lite/python/interpreter.py in set_tensor(self, tensor_index, value)
585 ValueError: If the interpreter could not set the tensor.
586 """
--> 587 self._interpreter.SetTensor(tensor_index, value)
588
589 def resize_tensor_input(self, input_index, tensor_size, strict=False):
ValueError: Cannot use numpy array of type 32663 for string tensor.
When I try to use int
inside the array for the input, another error pops up as it expects the type STRING
, and also when I try to put just string (without array brackets), it expects an array with 1 dimension.
So How can I input the text to my model for the prediction?