【问题标题】:Executing frozen tensorflow graph that uses tensorflow.contrib.resampler using c_api.h使用 c_api.h 执行使用 tensorflow.contrib.resampler 的冻结张量流图
【发布时间】:2018-05-26 08:29:46
【问题描述】:

我有一个冻结的 tensorflow 图(.pb 格式),其中包含对 tensorflow.contrib.resampler 的调用,必须使用 c_api.h 在 C 应用程序中加载和执行。

如果我调用,我可以从 python 成功加载和执行这个图:

import tensorflow as tf
tf.contrib.resampler

在我加载图表之前。

但是,我找不到如何使用 C api 执行相同操作,这会导致失败并显示以下消息:

Failed to process frame with No OpKernel was registered to support Op 'Resampler'
with these attrs.  Registered devices: [CPU,GPU], Registered kernels:
<no registered kernels>

如何使用 C api 指示 tensorflow 存在此操作?

【问题讨论】:

  • 看代码,好像操作位于一个separate binary called _resampler_ops.so。难怪它找不到它——你需要先加载这个库。
  • 令人惊讶的是,这个二进制文件在我的安装中不存在(通过 Windows 上的 pip 安装的 tensorflow-gpu 1.8.0),但 python 版本能够加载和运行图形。这意味着该操作存在并且可以加载,前提是我可以弄清楚如何在 C 中执行此操作。

标签: c tensorflow


【解决方案1】:

(这与this other StackOverflow thread 谈论同一件事非常相似,但针对Java)

当您在 tf.contrib 模块中使用操作时,它们不被认为是实验性的,因此不属于 stable TensorFlow API 并且不包含在其他语言发行版中。

但是,您可以使用 TF_LoadLibrary 在 C 中显式加载共享库。

为此,首先您需要找到包含您感兴趣的tf.contrib 操作实现的共享库的位置。在这种情况下,它似乎是tf.contrib.resampler,所以您会这样做像这样:

python -c "import tensorflow; print(tensorflow.contrib.resampler.__path__)"

会打印出类似的内容:

['/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/resampler']

然后你会使用类似的方式找到该路径中的所有共享库:

find /usr/local/lib/python2.7/dist-packages/tensorflow/contrib/resampler -name "*.so"

类似于:

/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/resampler/python/ops/_resampler_ops.so

好的,现在你有了那个库,你可以在 C 中使用:

TF_Status* status = TF_NewStatus();
TF_LoadLibrary("/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/resampler/python/ops/_resampler_ops.so", status);
if (TF_GetCode(status) != TF_OK) {
  // Log/fail, details in TF_Message(status)
}
TF_DeleteStatus(status);

注意事项:

  • 如果您想在不同的机器上运行,您需要将上面的.so 文件与您的程序一起打包,并适当调整对TF_LoadLibrary 的调用。

  • 确保您对 Python 和 C 使用相同的 TensorFlow 版本

  • Windows:对于 Windows,构建当前(至少在 TensorFlow 1.8 之前)有所不同,所有操作都静态编译到 pip 包中包含的单个本地库中,但不包含C API 发布二进制文件。因此,没有与这些 contrib 操作对应的 DLL 可以加载。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多