【发布时间】:2019-05-29 01:59:26
【问题描述】:
我为 MNIST 分类创建了一个 TF-lite 模型(我正在使用 TF 1.12.0 并在 Google Colab 上运行它),我想使用 TensorFlow Lite Python 解释器对其进行测试,如
但是当我尝试调用解释器时出现此错误 -
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.
这里是代码 -
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, “索引”:3, '名称': 'conv2d_1_input', '量化': (0.0, 0), 'shape': 数组([ 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()
问题是我不明白这条消息的含义以及如何处理它。
【问题讨论】:
-
OK.. 修复了这个..添加了 input_tensor= tf.convert_to_tensor(input_data, np.float32) 然后interpreter.set_tensor(input_details[0]['index'], input_data)
-
嗨!您介意详细说明您的解决方案吗?我也遇到了同样的问题,不知道怎么解决
-
Wither (i) 重启 jupyter notebook 或 (ii) 重新加载模型对我有用。查看此线程中的详细说明:stackoverflow.com/questions/56777704/…
标签: tensorflow-lite