Python 3.x “如何修复”;解释器中至少有1个以numpy数组或切片形式引用内部数据;并在tf.lite上运行推断

Python 3.x “如何修复”;解释器中至少有1个以numpy数组或切片形式引用内部数据;并在tf.lite上运行推断,python-3.x,tensorflow,tensorflow-lite,Python 3.x,Tensorflow,Tensorflow Lite,我试图在mnist keras模型上使用tf.lite进行推理,我根据 它发生在我将图像调整为4维后,或者在注释行中看到解释器本身;因为之前的错误类似于“预期4维,但发现3维”。代码如下: import tensorflow as tf tf.enable_eager_execution() import numpy as np from tensorflow.keras.datasets import mnist import matplotlib.pyplot as plt %matplot

我试图在mnist keras模型上使用tf.lite进行推理,我根据

它发生在我将图像调整为4维后,或者在注释行中看到解释器本身;因为之前的错误类似于“预期4维,但发现3维”。代码如下:

import tensorflow as tf
tf.enable_eager_execution()
import numpy as np
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
%matplotlib inline

mnist_train, mnist_test = tf.keras.datasets.mnist.load_data()
images, labels = tf.cast(mnist_test[0], tf.float32)/255.0, mnist_test[1]
images = np.reshape(images,[images.shape[0],images.shape[1],images.shape[2],1])
mnist_ds = tf.data.Dataset.from_tensor_slices((images, labels)).batch(1, drop_remainder = True)

interpreter = tf.lite.Interpreter(model_path="C:\\Users\\USER\\Documents\\python\\converted_quant_model_cnn_5_100.tflite")
#tf.lite.Interpreter.resize_tensor_input(interpreter, input_index="index" , tensor_size=([1,28,28,1]) )

interpreter.allocate_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"]

for img, label in mnist_ds.take(1):
  break
#print(img.get_shape)
interpreter.set_tensor(input_index, img)
interpreter.invoke()
predictions = interpreter.get_tensor(output_index)

在对tflite模型进行推理时,我遇到了同样的问题。 当回溯时,我最终读取了发生此运行时错误的函数

导致此错误的功能包括:

def _ensure_safe(self)

函数“_safe_to_run()”是从函数“_sure_safe()”中调用的。 _safe_to_run()函数返回True或False。如果返回False,则会发生上述运行时错误

如果存在numpy数组缓冲区,则返回False。这意味着运行可能破坏(或改变)内部分配内存的tflite调用是不安全的

因此,为了使“_sure_safe()”函数不引发此运行时错误,我们必须确保指向内部缓冲区的numpy数组不处于活动状态

此外,为了更清楚地说明,函数“\u sure\u safe()”应该从调用解释器上可能重新分配内存的函数的任何函数调用。因此,当您调用函数时

解释器。分配张量()

正如您在上面的代码中提到的,这个“解释器.allocate_tensors()”函数在内部做的第一件事是将“\u sure_safe()”函数调用为“解释器.allocate_tensors()”函数,包括更改内部分配的内存(在本例中,更改意味着顾名思义的“分配”)。调用“invoke()”函数时也会调用“\u sure\u safe()”的另一个示例。有很多这样的函数,但是你明白了

现在已经知道了根本原因和工作原理,为了克服这个运行时错误,即没有指向内部缓冲区的numpy数组,我们必须清除它们

要清除它们,请执行以下操作:

a) 。关闭jupyter笔记本并重新启动内核,因为这将清除所有numpy阵列/片

b) 。或者只需再次加载模型,即在jupyter笔记本中再次运行此行:

interpreter = tf.lite.Interpreter(model_path="C:\\Users\\USER\\Documents\\python\\converted_quant_model_cnn_5_100.tflite")
希望这能解决你的问题,我向你保证这对我是有帮助的

如果这两个选项都没有,那么在上面的解释中,我已经指出了发生此错误的“原因”。因此,如果您找到了“没有指向内部缓冲区的numpy数组”的其他方法,请共享


参考资料:

只是想补充一下为我解决的问题。我使用的是脚本,所以它与Jupyter笔记本无关

我的问题是我使用的是
predictions=explorer.tensor(输出索引)
相反,
predictions=解释器。获取张量(输出索引)


但是,该问题与此线程中注释的错误相同

我正在使用脚本,对我来说,问题是同一脚本的多个实例同时运行。杀死实例解决了问题

您解决了这个问题吗?仍然在处理同样的问题。有一次,它似乎可以正常工作,但其他时候,同样的问题也会发生。问题已经解决。这是一个愚蠢的错误,我记不清了。可能是数据集或它的处理方式。对于记录,我通过确保调用
解释器来解决它。在
调用()之前分配张量()。
def _safe_to_run(self)
interpreter = tf.lite.Interpreter(model_path="C:\\Users\\USER\\Documents\\python\\converted_quant_model_cnn_5_100.tflite")