tensorflow - tflite图中Conv2D的奇怪输出
问题描述
我需要调试它的行为,并且在第一步我得到了相当令人费解的结果。当我在第一个 Conv2D 之后输入零张量作为输入时,我希望得到一个仅包含来自 Conv2D 偏差的值的张量(因为所有内核元素都乘以零),但是我有一个由一些随机数据组成的张量,这里是代码片段:
def test_graph(path=PATH_DEFAULT):
interp = tf.lite.Interpreter(path)
interp.allocate_tensors()
input_details = interp.get_input_details()
in_idx = input_details[0]['index']
zeros = np.zeros(shape=(1, 256, 256, 3), dtype=np.float32)
interp.set_tensor(in_idx, zeros)
interp.invoke()
# index of output of first conv2d operator is 3 (see netron pic)
after_conv_2d = interp.get_tensor(3)
# shape of bias is just [count of output channels]
n, h, w, c = after_conv_2d.shape
# if we feed zeros as input, we can expect that the only values we get are the values of bias
# since all kernel elems in that case are multiplied by zeros
uniq_vals_cnt = len(np.unique(after_conv_2d))
assert uniq_vals_cnt <= c, f"There are {uniq_vals_cnt} in output, should be <= than {c}"
输出:
AssertionError: There are 287928 in output, should be <= than 24
有人可以帮助我解决我的误解吗?
解决方案
似乎我假设我可以从解释器获得任何中间张量是错误的,我们只能为输出做这件事,即使解释器不会引发错误,甚至为与非输出张量相关的索引提供正确形状的张量。
调试此类图的一种方法是制作所有张量输出,但似乎最简单的方法是将tflite
文件转换为pb
with toco
,然后再转换pb
回tflite
指定的新输出。这种方式并不理想,因为在 1.9 之后删除toco
了对转换的支持,tflite -> pb
并且使用之前的版本可能会在某些图表上中断(在我的情况下它会中断)。
更多内容在这里: tflite: get_tensor on non-output tensors give random values
推荐阅读
- java - 比较 Spring Mysql 中的 2 个值
- mysql - 如何将所有表中数据库中的所有“id”列设置为自动增量?
- visual-studio-code - vscode vim 扩展不写真字符?
- javascript - 如何在 Vue js 上安装 ShareThis?
- blazor - 在 Blazor WebAssembly 应用程序的会话存储中保存自定义对象(使用 Blazored.SessionStorage)
- mysql - Host --- 不允许连接到这个 MySQL 服务器 .Net core
- build - Azure DevOps CICD 构建管道失败并出现错误:找不到指定的命令或文件
- javascript - 填写表单时使用 Thymeleaf 执行 DOM 操作
- java - 为什么 c# 中的 lucenenet 和 java 中的 lucene 之间的搜索时间存在巨大差异,而其他统计数据大致相当?
- javascript - 部署到 Heroku 时出现错误,`Prop`className` did not match`,但在本地工作