使用 TensorFlow 训练模型和 C API 进行预测 [英] Making predictions with TensorFlow trained model and C API

查看:35
本文介绍了使用 TensorFlow 训练模型和 C API 进行预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过构建 libtensorflow.so 目标构建了 C API.我想加载一个预训练的模型并对其进行推理以进行预测.有人告诉我,我可以通过包含 'c_api.h' 头文件(以及将该文件和 'libtensorflow.so' 复制到适当的位置)来做到这一点,但是,我没有运气在网上找到任何示例.我能找到的只是使用 Bazel 构建系统的示例,而我想使用另一个构建系统并使用 TensorFlow 作为库.有人可以帮助我举例说明如何导入 a) 元图文件;b) protobuf 图形文件和检查点文件,以进行预测?一个 C++ 等效于下面的 Python 文件并用 g++ 构建?

I have built the C API by building the libtensorflow.so target. I want to load a pre-trained model with and run inference on it to make predictions. I was told I can do this by including the 'c_api.h' header file (along with copying that file plus 'libtensorflow.so' to the appropriate place), however, I had no luck finding any examples on that on the web. All I could find are examples which use the Bazel build system whereas I want to use another build system and use TensorFlow as a library. Can somebody help me with an example on how to import either a) a meta graph file; b) a protobuf graph file plus a checkpoint file, to make predictions? A C++ equivalent of the Python file below and built with g++?

#!/usr/bin/env python

import tensorflow as tf
import numpy as np

with tf.Session() as sess:
    saver = tf.train.import_meta_graph('./metagraph.meta')
    saver.restore(sess, './checkpoint.ckpt')
    x = tf.get_collection("x")[0]
    yhat = tf.get_collection("yhat")[0]
    print sess.run(yhat, feed_dict={x : np.array([[2, 3], [4, 5]])})

提前致谢!

ps:为了完整起见,我执行了以下操作来构建文件:

p.s.: For the sake of completeness I have did the following to build the files:

#!/usr/bin/env python

import tensorflow as tf
import numpy as np

x = tf.placeholder(tf.float32, shape=[None, 2], name='x')
tf.add_to_collection("x", x)
y = tf.placeholder(tf.float32, shape=[None, 1], name='y')
w = tf.Variable(np.array([[10.0], [100.0]]), dtype=tf.float32, name='w')
b = tf.Variable(0.0, dtype=tf.float32, name='b')
yhat = tf.add(tf.matmul(x, w), b)
tf.add_to_collection("yhat", yhat)
mse_loss = tf.sqrt(tf.reduce_mean(tf.square(tf.sub(y, yhat))))
step_size = tf.constant(0.01)
optimizer = tf.train.GradientDescentOptimizer(step_size)
init_op = tf.initialize_all_variables()
train_op = optimizer.minimize(mse_loss)
saver = tf.train.Saver()
with tf.Session() as sess:
    sess.run(init_op)
    for i in xrange(10000):
        train_x = np.random.random([100, 2]) * 10
        train_y = np.dot(train_x, np.array([[100.0], [10.0]])) + 1.0
        sess.run(train_op, feed_dict={x : train_x, y : train_y})
    print sess.run(w)
    print sess.run(b)
    saver.save(sess, './checkpoint.ckpt')
    saver.export_meta_graph('./metagraph.meta')
    tf.train.write_graph(sess.graph_def, './', 'graph')

推荐答案

我使用 Eclipse 并将 c_api.h 添加到我的项目文件中,将 libtensorflow.so 添加到/usr/local/bin.然后我将 libtensorflow 共享对象的引用添加到我的 GCC C++ 链接器上的库中,最终创建了一个简单的程序.

I used Eclipse and added c_api.h to my project file and libtensorflow.so to /usr/local/bin. I then added the reference to the libtensorflow shared object to libraries on my GCC C++ Linker, finally created a simple program.

#include <iostream>
#include "c_api.h"

using namespace std;

int main() {
    cout << TF_Version();
    return 0;
}

这让我能够编译和使用 Tensorflow 函数,包括您想要的函数.

This then allowed me to compile and use Tensorflow functions, including those that you want.

这篇关于使用 TensorFlow 训练模型和 C API 进行预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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