使用 c_api.h 执行使用 tensorflow.contrib.resampler 的冻结张量流图 [英] Executing frozen tensorflow graph that uses tensorflow.contrib.resampler using c_api.h

查看:54
本文介绍了使用 c_api.h 执行使用 tensorflow.contrib.resampler 的冻结张量流图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a frozen tensorflow graph (.pb format) that contains a call to tensorflow.contrib.resampler, which must be loaded and executed in a C application using c_api.h.

如果我调用:

import tensorflow as tf
tf.contrib.resampler

在我加载图表之前.

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

However, I cannot find how to do the same using the C api, which leads to a failure with the following message:

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 此操作存在?

How can I instruct tensorflow that this op exists using the C api?

推荐答案

(这与 这个其他 StackOverflow 线程非常相似 谈论同样的事情,但对于 Java)

(This is very similar to this other StackOverflow thread talking about the same thing, but for Java)

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

When you use operations in the tf.contrib module, they are not considered to be experimental, thus are not part of the stable TensorFlow API and aren't included in other language distributions.

但是,您可以使用 TF_LoadLibrary.

However, you can explicitly load the shared library in C using TF_LoadLibrary.

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

To do that, first you need to find the location of the shared library that contains the implementation of the tf.contrib operation you're interested in. In this case it seems like it's tf.contrib.resampler, so you'd do something like this:

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

会打印如下内容:

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

然后您会使用以下内容找到该路径中的所有共享库:

Then you'd find all the shared libraries in that path using something like:

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 加载它:

Alright, now you have that library, you can load it in C using:

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 的调用适当.

  • If you want to run on a different machine, you'd want to package the .so file above with your program and adjust the call to TF_LoadLibrary appropriately.

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

Make sure you're using the same TensorFlow version for Python and C

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

Windows: For Windows, the build is currently (till TensorFlow 1.8 at least) different and all operations are statically compiled into a single native library included with the pip package, but not with the C API release binaries. So, there is no DLL corresponding to these contrib operations that you can load.

希望有所帮助.

这篇关于使用 c_api.h 执行使用 tensorflow.contrib.resampler 的冻结张量流图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆