将 TensorFlow 代码移植到 Android

Port TensorFlow code to Android

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

有什么方法可以直接将我的 TensorFlow Python 代码移植到 Android 上吗?

QPython or Kivy.

QPython - Android GooglePlay 上的应用。它是 运行s Python 在 android 设备上的脚本引擎。它可以让您的 android 设备 运行 Python 脚本和项目。它包含 Python 解释器和一些其他东西,例如 pip,但是没有可用的编译器,所以只有 pure-python 包可以工作。

Python for Android - 允许您将 Python 应用程序编译成 Android APK 以及纯 python 和需要编译的附加包。

执行此操作的典型方法是使用 Python 构建(和训练)模型,使用 tf.train.write_graph(), and then write an app using the JNI to call the C++ TensorFlow API (see a complete example here.[=16= 将 GraphDef 原型保存到文件中]

当您在 Python 中构建图形时,您应该记下将表示 (i) 要分类的输入数据和 (ii) 预测输出值的张量的名称。然后,您将能够 运行 通过为 (i) 提供一个值并获取 (ii) 的值来执行一个步骤。

最后一个问题是如何在导出的图形中表示模型参数。有几种方法可以做到这一点,包括运送一个 TensorFlow 检查点(由 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 编写的是重写图,以便模型参数被替换为 "Const" 节点,并且模型图变得自包含。