TF-lite 模型测试失败并出现运行时错误

2024-04-10

我已经为 MNIST 分类创建了一个 TF-lite 模型(我使用 TF 1.12.0 并在 Google Colab 上运行它),我想使用 TensorFlow Lite Python 解释器对其进行测试,如

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

但是当我尝试调用解释器时出现此错误 -

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), '形状': 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()

问题是我不明白这条消息的含义以及该怎么办。


tf.convert_to_tensor and interpreter.set_tensor为我做了这份工作

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)

我创建了一个端到端示例,从训练 Keras 模型开始在 TensorFlow Lite 上提供服务here https://github.com/romeokienzler/TensorFlow/blob/master/notebooks/tf2savedModel.ipynb

我也在中发布了相同的答案this https://stackoverflow.com/questions/56777704/how-to-fix-there-is-at-least-1-reference-to-internal-data-in-the-interpreter-in/60403076#60403076 thread

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

TF-lite 模型测试失败并出现运行时错误 的相关文章

随机推荐

  • 如何使用“input()”在控制台中重复输入选项,直到用户选择退出?

    我向用户提供多个选项以供选择使用input 在Python 3中 我希望一次又一次地显示选项 直到用户选择退出选项来退出 例如 a 5 b 2 inp input Please choose from Options mentioned b
  • 每 100 秒垃圾收集一次

    有没有人遇到过这样的场景 高内存分配负载下的应用程序每 100 秒执行一次第二代收集 我们使用具有 8 16 GB 物理内存的 64 位服务器 应用程序有几 GB 的数据存储在缓存中 并且无法从中清除 因为它实际上已被应用程序使用 此外 它
  • Java 中 File.exists() 的替代方法

    我从没想过这种事会发生在我身上 但我遇到了 Java 中的第一个错误 https bugs java com bugdatabase view bug bug id 5003595 https bugs java com bugdataba
  • 如何获得自定义警报对话框,如图所示?

    i want to build a customized dialog just like the one shown in image 我创建了两个布局 一个用于自定义标题 另一个包含两个编辑文本视图和两个按钮 这里是 xml 自定义标题
  • 如何在Visual Studio中动态创建解决方案的解决方案文件夹?

    我想创建一个新的解决方案文件夹使用 PowerShell 脚本在 Visual Studio 中动态地进行操作 我知道如何在 Visual Studio 中手动为现有解决方案创建解决方案文件夹 但有没有办法可以使用 PowerShell 自
  • JavaScript html 解码

    当我在 asp net 应用程序中通过 ajax 接收 html 文本时 它看起来像 lt span 20style color green font weight bold gt 20Text 20Msg lt span gt javas
  • 准备语句将逗号分隔值传递给存储过程参数

    我无法将查询移动到存储过程中 我需要将逗号分隔的字符串传递给参数yr model no两个人IN条款 我已经阅读了一些线程并决定使用准备语句 原始查询可以很好地获取多行 但是在存储过程中 当我用call load things 128394
  • Rails 中带有回退功能的动态命名空间控制器

    我对新的 Rails 应用程序有一个有点奇怪的要求 我需要构建一个应用程序 其中所有路由都在多个命名空间中定义 让我解释一下 我想要一个应用程序 其中学校科目 数学 英语等 是命名空间 w math english each do subj
  • void 子例程返回什么?

    我刚刚对pluralsight 进行评估 并得到了以下问题 无效子程序返回什么 我的印象是无效子例程没有返回任何内容 但这不是提供的答案之一 多项选择题 Net 在后台返回值类型还是这个问题不正确 我得到的选择是 整数 布尔值 细绳 约会时
  • 任务并行库 - 如何使用 TaskContinuationOptions.OnlyOnCanceled 来触发延续?

    我正在尝试 NET 4 0 中的任务支持 特别是延续支持 我感到困惑的是我不知道如何继续TaskContinuationOptions OnlyOnCanceled标志设置为执行 如果我做一个ThrowIfCancellationReque
  • ElasticSearch 全文搜索

    我尝试在elasticsearch java api 中使用正则表达式运行全文搜索 我的过滤器是这样的 FilterBuilder qFilter FilterBuilders regexpFilter all text 但它只匹配一个单词
  • 使用 cancan 和 devise 授权 Rails 中未登录的用户行为

    Post hidden boolean 我希望登录用户可以看到所有帖子 而未登录用户只能访问隐藏字段为 false 的帖子 所以我在cancan的能力模型中这样写 if user signed in can read Post else c
  • 在 lua 中使用相等运算符比较数字有多安全?

    在我的引擎中 我有一个用于脚本编写的 Lua VM 在脚本中 我写了这样的内容 stage stage 1 if stage 5 then end and objnum tonumber 5 if stage objnum 根据 Lua 来
  • Node.js:从 jpeg 图像到视频文件的实时转换

    我正在使用node js 并通过socket io 库接收实际上是jpeg 图像的数据块 这些图像是从远程网络摄像头捕获的实时视频帧 我被迫将视频作为 jpeg 帧进行流式传输 我正在寻找一种方法来动态转换视频文件 mpeg 4 或 mjp
  • 将 ASP.Net 5 Web 应用程序部署到 Azure 时如何删除现有文件

    我有一个 ASP Net 5 中的 Web 应用程序 其源代码位于 VS Team Services 中 使用 Team Services 构建系统 我想将其直接部署到 Azure 进行持续部署 我已按照此处的步骤操作 https msdn
  • 如何将 cin 和 cout 重定向到文件?

    我怎样才能重定向cin to in txt and cout to out txt 这是您想要做的事情的一个工作示例 阅读注释以了解代码中每一行的作用 我在我的电脑上用 gcc 4 6 1 测试了它 效果很好 include
  • 递归查询 LDAP 组成员资格

    我正在编写一个基于 MVC NET 4 0 的网站 该网站需要来自我的公司 LDAP 服务器的登录凭据 我的代码要求的是仅允许属于特定组的用户 例如 我可能正在寻找属于 企业 IT 组的用户 我的凭据可能属于 系统管理员 组 该组是 公司
  • Django 计算子查询中的行数

    我需要计算子查询中的行数 这是我的 sqlite 解决方案 class SQCount Subquery Count lines in subquery template SELECT count FROM subquery s count
  • 在 XSL 转换期间维护 XML 注释

    XML
  • TF-lite 模型测试失败并出现运行时错误

    我已经为 MNIST 分类创建了一个 TF lite 模型 我使用 TF 1 12 0 并在 Google Colab 上运行它 我想使用 TensorFlow Lite Python 解释器对其进行测试 如 https github co