Code Monkey home page Code Monkey logo

tensorflowandroidmnist's Introduction

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 Beginners https://www.tensorflow.org/versions/r0.10/tutorials/mnist/beginners/index.html

Deep MNIST for Experts https://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 to build 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.

tensorflowandroidmnist's People

Contributors

miyosuda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tensorflowandroidmnist's Issues

Exported Model doesn't work

I ran expert.py and used the .pb file it produced with the same android code you provided and it didnt work. The app keeps on crashing or when I try to detect a character it returns -1. Please Help!

android ndk api_level=9

android_sdk_repository(
name = "androidsdk",
api_level = 23,
build_tools_version = "23.0.1",
path = "/home/gzzhang/android-sdk",
)

android_ndk_repository(
name="androidndk",
path="/opt/android-ndk",
api_level=9, << ====================
)

can you build the "tensorflow android libs" with android ndk api_level=9.

if you build them with android ndk api_level=21, the libs can't be used below android 5.0

because some functions can't been found.

SO, can you down the android ndk api_level to 9.


I have do something, but when invoke:
tensorflow::NewSession(options)

and, it print message:
Not found: Could not find session factory for DIRECT_SESSION

Building of tensorflow android libs

Hi,
I 'm currently using "INV" op and encounter some error :
Error during inference: Invalid argument: No OpKernel was registered to support Op 'Inv' with these attrs [[Node: incept5b/in4_conv1x1_55/batch_norm/moments/moments_1/divisor = Inv[T=DT_FLOAT](incept5b/in4_conv1x1_55/batch_norm/moments/moments/Const)]]
I looked into tensorflow file, the "INV" register op should be in tensorflow/core/ops/math_ops.cc.

I think the tensorflow android libs don't include this op.
I'm interested in how you build these .a and .lo libs? Could you build a lib that support the INV?
Thanks

How to build the library for API Level 19 ??

Hi! I am a freshman developer to JNI and may I ask you a question about this?? I want to build this project on my android phone with API Level 19. I just modified APP_PLATFORM := android-21 to APP_PLATFORM := android-19 in Application.mk. However, it could not find the definition of errno. And after adding android-21 include path, the error becomes

..../armeabi-v7a/libandroid_tensorflow_lib_lite.lo(arena.o):arena.cc:function GLOBAL__sub_I_arena.cc: error: undefined reference to 'getpagesize'
..../libandroid_tensorflow_lib_lite.lo(numbers.o):numbers.cc:function tensorflow::strings::safe_strtof(char const
, float_): error: undefined reference to 'strtof'

Therefore I think binary libraries in TensorFlowAndroidMNIST/jni-build/jni/libs/armeabi-v7a/ can only be used in android-21 level. So my question is where did you find these binary libraries?? By directly copying from the official tensorflow project or rebuilding from the source?? How could I rebuild the source codes to generate the same libraries supporting android-19??

Sorry for my poor English and thank you in advance!!

Can't build this application on my device

I got these errors and exceptions when I try to run this application on my sony xperia t2 ultra device

06-19 04:52:34.050 5731-5731/jp.narr.tensorflowmnist D/dalvikvm: Late-enabling CheckJNI
06-19 04:52:34.120 5731-5731/jp.narr.tensorflowmnist D/dalvikvm: Trying to load lib /data/app-lib/jp.narr.tensorflowmnist-1/libtensorflow_mnist.so 0x427336d0
06-19 04:52:34.140 5731-5731/jp.narr.tensorflowmnist E/dalvikvm: dlopen("/data/app-lib/jp.narr.tensorflowmnist-1/libtensorflow_mnist.so") failed: dlopen failed: cannot locate symbol "getpagesize" referenced by "libtensorflow_mnist.so"...
06-19 04:52:34.140 5731-5731/jp.narr.tensorflowmnist W/dalvikvm: Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Ljp/narr/tensorflowmnist/DigitDetector;
06-19 04:52:34.140 5731-5731/jp.narr.tensorflowmnist D/AndroidRuntime: Shutting down VM
06-19 04:52:34.140 5731-5731/jp.narr.tensorflowmnist W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41b3ed88)
06-19 04:52:34.140 5731-5731/jp.narr.tensorflowmnist E/AndroidRuntime: FATAL EXCEPTION: main
Process: jp.narr.tensorflowmnist, PID: 5731
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "getpagesize" referenced by "libtensorflow_mnist.so"...
at java.lang.Runtime.loadLibrary(Runtime.java:364)
at java.lang.System.loadLibrary(System.java:526)
at jp.narr.tensorflowmnist.DigitDetector.(DigitDetector.java:29)
at jp.narr.tensorflowmnist.MainActivity.(MainActivity.java:42)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2152)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5133)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
at dalvik.system.NativeStart.main(Native Method)


I'm using android studio 2.1.2
Device: Sony xperia t2 ultra (Android 4.4.3, API 19)

Error on building for expert.py

I succeeded on building beginner.py and expert.py to beginner-graph.pb and expert-graph.pb files to be used in the Android project. But when I replaced these files in the app/src/main/assets/ only beginner-graph.pb works, expert-graph.pb does not work.
Do you have any idea?
(I have modified file DigitDetector.java in lines 43 and 46 to use beginner-graph.pb and expert-graph.pb, respectively).

I copy the error log as follows:
10-21 10:43:13.357 28693-28693/jp.narr.tensorflowmnist W/linker: libtensorflow_mnist.so: unused DT entry: type 0x6ffffffe arg 0x1e9c
10-21 10:43:13.357 28693-28693/jp.narr.tensorflowmnist W/linker: libtensorflow_mnist.so: unused DT entry: type 0x6fffffff arg 0x3
10-21 10:43:13.739 28693-28693/jp.narr.tensorflowmnist I/native: tensorflow_jni.cc:62 Loading Tensorflow.
10-21 10:43:13.739 28693-28693/jp.narr.tensorflowmnist I/native: tensorflow_jni.cc:63 Making new SessionOptions.
10-21 10:43:13.739 28693-28693/jp.narr.tensorflowmnist I/native: tensorflow_jni.cc:67 Got config, 0 devices
10-21 10:43:13.744 28693-28693/jp.narr.tensorflowmnist I/native: tensorflow_jni.cc:69 Session created.
10-21 10:43:13.744 28693-28693/jp.narr.tensorflowmnist I/native: tensorflow_jni.cc:73 Graph created.
10-21 10:43:13.744 28693-28693/jp.narr.tensorflowmnist I/native: tensorflow_jni.cc:78 Acquired AssetManager.
10-21 10:43:13.744 28693-28693/jp.narr.tensorflowmnist I/native: tensorflow_jni.cc:80 Reading file to proto: file:///android_asset/expert-graph.pb
10-21 10:43:13.744 28693-28693/jp.narr.tensorflowmnist I/native: jni_utils.cc:120 Opening asset expert-graph.pb from disk with zero-copy.
10-21 10:43:13.764 28693-28693/jp.narr.tensorflowmnist I/native: tensorflow_jni.cc:84 Creating session.
10-21 10:43:13.779 28693-28693/jp.narr.tensorflowmnist E/native: tensorflow_jni.cc:89 Could not create Tensorflow Graph: Invalid argument: NodeDef mentions attr 'Tshape' not in Op<name=Reshape; signature=tensor:T, shape:int32 -> output:T; attr=T:type>; NodeDef: Reshape = Reshape[T=DT_FLOAT, Tshape=DT_INT32](input, Reshape/shape)
10-21 10:43:13.782 28693-28693/jp.narr.tensorflowmnist I/MainActivity: Detector setup failed
10-21 10:43:13.883 28693-28693/jp.narr.tensorflowmnist D/AndroidRuntime: Shutting down VM

                                                                     --------- beginning of crash

10-21 10:43:13.884 28693-28693/jp.narr.tensorflowmnist E/AndroidRuntime: FATAL EXCEPTION: main
Process: jp.narr.tensorflowmnist, PID: 28693
java.lang.RuntimeException: Unable to resume activity {jp.narr.tensorflowmnist/jp.narr.tensorflowmnist.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void jp.narr.tensorflowmnist.DrawView.onResume()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3050)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3081)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2447)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void jp.narr.tensorflowmnist.DrawView.onResume()' on a null object reference
at jp.narr.tensorflowmnist.MainActivity.onResume(MainActivity.java:84)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1262)
at android.app.Activity.performResume(Activity.java:6120)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3039)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3081) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2447) 
at android.app.ActivityThread.access$800(ActivityThread.java:156) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:211) 
at android.app.ActivityThread.main(ActivityThread.java:5373) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
10-21 10:43:35.680 28693-28693/jp.narr.tensorflowmnist I/Process: Sending signal. PID: 28693 SIG: 9

NDK Build Error

I got the following error while running 'make' command in jni-build folder

ndk-build make[1]: Entering directory '/home/codehead/TensorFlowAndroidMNIST/jni-build' Android NDK: WARNING:jni/Android.mk:tensorflow_mnist: non-system libraries in linker flags: jni/libs/armeabi-v7a/libprotos_all_cc.a jni/libs/armeabi-v7a/libprotobuf.a jni/libs/armeabi-v7a/libprotobuf_lite.a /home/codehead/Downloads/android-ndk-r12b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a /home/codehead/Downloads/android-ndk-r12b/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/libsupc++.a Android NDK: This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES Android NDK: or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the Android NDK: current module [armeabi-v7a] Install : libtensorflow_mnist.so => libs/armeabi-v7a/libtensorflow_mnist.so make[1]: Leaving directory '/home/codehead/TensorFlowAndroidMNIST/jni-build'

Error on deploying to Android

I can build and copy libtensorflow_mnist.so file to armeabi-v7a folder but can't deploy to Android because of the following error.
Do you have any suggestion?
Thank you.

"No local changes, not deploying APK
$ adb shell am start -n "jp.narr.tensorflowmnist/jp.narr.tensorflowmnist.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Error while executing: am start -n "jp.narr.tensorflowmnist/jp.narr.tensorflowmnist.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=jp.narr.tensorflowmnist/.MainActivity }
Error type 3
Error: Activity class {jp.narr.tensorflowmnist/jp.narr.tensorflowmnist.MainActivity} does not exist.

Error while Launching activity"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.