drs*_*lks 5 python keras tensorflow tensorflow-lite tensorflow2.0
我想知道是否有办法知道 tflite 中特定节点的输入和输出列表?我知道我可以获得输入/输出详细信息,但这不允许我重建Interpreter. 所以我要做的是:
interpreter = tf.lite.Interpreter(model_path=model_path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.get_tensor_details()
Run Code Online (Sandbox Code Playgroud)
最后 3 个命令基本上为我提供了似乎没有必要信息的字典。
所以我想知道是否有办法知道每个节点的输出去哪里?当然Interpreter以某种方式知道这一点。我们可以吗?谢谢。
Fal*_*nUA 19
笔记:此答案是为 Tensorflow 1.x 编写的,虽然概念和核心思想在 TensorFlow 2.x 中保持不变,但此答案中的命令可能已弃用。
TF-Lite 的机制使得检查图和获取内部节点的中间值的整个过程有点棘手。这get_tensor(...)另一个答案建议方法不起作用。
TensorFlow Lite 模型可以使用TensorFlow Lite 存储库中的visualize.py脚本进行可视化。你只需要:
visualize.py使用 bazel运行脚本:
bazel run //tensorflow/lite/tools:visualize \
model.tflite \
visualized_model.html
Run Code Online (Sandbox Code Playgroud)
不!事实上,TF-Lite 可以修改您的图形,使其变得更加优化。以下是TF-Lite 文档中关于它的一些话:
TensorFlow Lite 可以处理许多 TensorFlow 操作,即使它们没有直接的等价物。对于可以简单地从图中删除 (tf.identity)、替换为张量 (tf.placeholder) 或融合为更复杂的操作 (tf.nn.bias_add) 的操作就是这种情况。有时甚至可能会通过这些过程之一删除某些受支持的操作。
此外,TF-Lite API 目前不允许获取节点对应关系;很难解释 TF-Lite 的内部格式。因此,即使没有下面的另一个问题,您也无法获得您想要的任何节点的中间输出......
不!在这里,我将解释为什么get_tensor(...)在 TF-Lite 中不起作用。假设在内部表示中,该图包含 3 个张量,以及中间的一些密集操作(节点)(您可以将其tensor1视为tensor3模型的输入和输出)。在这个特定图的推理过程中,TF-Lite只需要 2 个缓冲区,让我们展示如何。
首先,使用应用运算tensor1来计算。这仅需要 2 个缓冲区来存储值:tensor2dense
dense dense
[tensor1] -------> [tensor2] -------> [tensor3]
^^^^^^^ ^^^^^^^
bufferA bufferB
Run Code Online (Sandbox Code Playgroud)
其次,使用tensor2存储在的值bufferB来计算tensor3......但是等等!我们不再需要bufferA了,所以让我们用它来存储 的值tensor3:
dense dense
[tensor1] -------> [tensor2] -------> [tensor3]
^^^^^^^ ^^^^^^^
bufferB bufferA
Run Code Online (Sandbox Code Playgroud)
现在是棘手的部分。的“输出值”tensor1仍将指向bufferA,它现在保存 的值tensor3。所以如果你调用get_tensor(...)第一个张量,你会得到不正确的值。该方法的文档甚至指出:
该函数不能用于读取中间结果。
简单但有限的方式。您可以指定节点的名称,在转换期间要获取其值的输出张量:
bazel run //tensorflow/lite/tools:visualize \
model.tflite \
visualized_model.html
Run Code Online (Sandbox Code Playgroud)
艰难但灵活的方式。您可以使用 Bazel 编译 TF-Lite(使用此指令)。然后,您实际上可以Interpreter::Invoke()在文件中注入一些日志记录代码tensorflow/lite/interpreter.cc。一个丑陋的黑客,但它的工作原理。
| 归档时间: |
|
| 查看次数: |
5578 次 |
| 最近记录: |