TF-lite model test fails with run-time Error

我的梦境 提交于 2020-03-23 07:23:10

问题


I have created a TF-lite model for MNIST classification (I am using TF 1.12.0 and running this on Google Colab) and I want to test it using TensorFlow Lite Python interpreter as given in

https://github.com/freedomtan/tensorflow/blob/deeplab_tflite_python/tensorflow/contrib/lite/examples/python/label_image.py

But I am getting this error when I try to invoke the interpreter -

RuntimeError                              Traceback (most recent call last)
<ipython-input-138-7d35ed1dfe14> in <module>()
----> 1 interpreter.invoke()

/usr/local/lib/python3.6/dist- 
packages/tensorflow/contrib/lite/python/interpreter.py in invoke(self)
251       ValueError: When the underlying interpreter fails raise 
ValueError.
252     """
--> 253     self._ensure_safe()
254     self._interpreter.Invoke()
255 

/usr/local/lib/python3.6/dist- 
packages/tensorflow/contrib/lite/python/interpreter.py in 
_ensure_safe(self)
 97       in the interpreter in the form of a numpy array or slice. Be sure 
 to
 98       only hold the function returned from tensor() if you are using 
 raw
 ---> 99       data access.""")

101   def _get_tensor_details(self, tensor_index):

 RuntimeError: There is at least 1 reference to internal data
  in the interpreter in the form of a numpy array or slice. Be sure to
  only hold the function returned from tensor() if you are using raw
  data access.

Here is the code -

import numpy as np

# Load TFLite model and allocate tensors.
interpreter = 
tf.contrib.lite.Interpreter(model_path="mnist/mnist_custom.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_details

[{'dtype': numpy.float32, 'index': 3, 'name': 'conv2d_1_input', 'quantization': (0.0, 0), 'shape': array([ 1, 28, 28, 1], dtype=int32)}]

test_images[0].shape

(28, 28, 1)

input_data = np.expand_dims(test_images[0], axis=0)
input_data.shape

(1, 28, 28, 1)

interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()

The problem is I do not understand what this message means and what to do about it.


回答1:


tf.convert_to_tensor and interpreter.set_tensor did the job for me

tensor_index = interpreter.get_input_details()[0]['index']
input_tensor_z= tf.convert_to_tensor(z, np.float32)
interpreter.set_tensor(tensor_index, input_tensor_z)

I've created an end2end example starting from training a Keras model to serve it on TensorFlow Lite here

I've also posted the same answer in this thread



来源:https://stackoverflow.com/questions/54006031/tf-lite-model-test-fails-with-run-time-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!