将TensorFlow代码移植到Android [英] Port TensorFlow code to Android

查看:176
本文介绍了将TensorFlow代码移植到Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Python中使用TensorFlow编写了用于序列分类的脚本。我想将此代码移植到Android。我已经在TensorFlow github页面上看到了有关Android的示例,但这是用于图像的。

I have written a script for sequence classification using TensorFlow in Python. I would like to port this code to Android. I have seen the example on the TensorFlow github page regarding Android but that is for images.

有没有办法在Android上直接移植TensorFlow Python代码?

Is there any way to directly port my TensorFlow Python code on Android?

推荐答案

典型的方法是使用Python建立(训练)模型,保存 GraphDef 使用 tf.train.write_graph() ,然后使用JNI编写应用程序以调用C ++ TensorFlow API(请参阅完整示例此处)。

The typical way to do this is to build (and train) your model using Python, save the GraphDef proto to a file using tf.train.write_graph(), and then write an app using the JNI to call the C++ TensorFlow API (see a complete example here).

构建图形时在Python中,应注意张量的名称,这些张量将表示(i)要分类的输入数据,以及(ii)预测的输出值。然后,您将可以通过输入(i)的值并获取(ii)的值来进行操作。

When you build your graph in Python, you should take note of the names of the tensors that will represent (i) the input data to be classified, and (ii) the predicted output values. Then you will be able to run a step by feeding a value for (i), and fetching the value for (ii).

最后一个需要关注的问题是如何表示您导出的图中的模型参数。有几种方法可以做到这一点,包括运送TensorFlow检查点(由 tf.train.Saver )作为应用的一部分,然后运行restore操作来重新加载它。 已发布的InceptionV3模型中使用的一种方法是重写图,以便将模型参数替换为 Const 节点,并且模型图变得自成体系。

One final concern is how to represent the model parameters in your exported graph. There are several ways to do this, including shipping a TensorFlow checkpoint (written by a tf.train.Saver) as part of your app, and running the restore ops to reload it. One method, which has been used in the released InceptionV3 model is to rewrite the graph so that the model parameters are replaced with "Const" nodes, and the model graph becomes self contained.

这篇关于将TensorFlow代码移植到Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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