android工程如何使用tensorflow

来源:互联网 发布:aframe.js下载 编辑:程序博客网 时间:2024/05/22 23:04

參考工程:https://github.com/miyosuda/TensorFlowAndroidMNIST

1.在python中訓練完模型後,使用

tf.train.write_graph(graph_def, './tmp/beginner-export',                     'beginner-graph.pb', as_text=False)
存儲模型。

2.在java中使用c 調用tensorflow的C接口 ,獲取訓練好的圖模型。

3.c調用tensorflow的c接口,識別。


TensorFlowAndroidMNIST - Android MNIST demo with TensorFlow

This is a demo app for Android with Tensorflow to detect handwritten digits.

image

This Android demo is based on Tensorflow tutorial.

MNIST For ML Beginnershttps://www.tensorflow.org/versions/r0.10/tutorials/mnist/beginners/index.html

Deep MNIST for Expertshttps://www.tensorflow.org/versions/r0.10/tutorials/mnist/pros/index.html

How to train model.

Training scripts for neural network model are located at

https://github.com/miyosuda/TensorFlowAndroidMNIST/tree/master/trainer-script

To create model by yourself, install Tensorflow and run python scripts like

$ python beginner.py

or

$ python expert.py

and locate exported .pb file to assets dir.

To export training model, I added some modification to original tutorial scripts.

Now Tensorflow cannot export network graph and trained network weight Variable at the same time,so we need to create another graph to export and convert Variable into constants.

After training is finished, converted trained Variable to numpy ndarray.

_W = W.eval(sess)_b = b.eval(sess)

and then convert them into constant and re-create graph for exporting.

W_2 = tf.constant(_W, name="constant_W")b_2 = tf.constant(_b, name="constant_b")

And then use tf.train.write_graph to export graph with trained weights.

How to build JNI codes

Native .so files are already built in this project, but if you would like tobuild it by yourself, please install and setup NDK.

First download, extract and place Android NDK.

http://developer.android.com/intl/ja/ndk/downloads/index.html

And then update your PATH environment variable. For example,

export NDK_HOME="/Users/[your-username]/Development/android/android-ndk-r11b"export PATH=$PATH:$NDK_HOME

And build .so file in jni-build dir.

$ cd jni-build$ make

and copy .so file into app/src/main/jniLibs/armeabi-v7a/ with

$ make install

(Unlike original Android demo in Tensorflow, you don't need to install bazel to build this demo.

Tensorflow library files (.a files) and header files are extracted from original Tensorflow Android demo r0.10.


0 1
原创粉丝点击