Giter Club home page Giter Club logo

a_guide_to_running_tensorflow_models_on_android's Introduction

A_Guide_to_Running_Tensorflow_Models_on_Android

This is the code for"A Guide to Running Tensorflow Models on Android" By SIraj Raval on Youtube

Overview

This is the code for this video on Youtube by Siraj Raval.

Image

Handwritten digits classification from MNIST on Android with TensorFlow.

If you want to make your own version of this app or want to knowhow to save your model and export it for Android or other devices check the very simple tutorial below. The UI and expert-graph.pb model were taken from: https://github.com/miyosuda/TensorFlowAndroidMNIST, so thank you miyousuda.

Dependencies

All included

Usage

Just open this project with Android Studio and is ready to run, this will work with x86 and armeabi-v7a architectures.

How to export my model?

A full example can be seen here

  1. Train your model

  2. Keep an in memory copy of eveything your model learned (like biases and weights) Example: _w = sess.eval(w), where w was learned from training.

  3. Rewrite your model changing the variables for constants with value = in memory copy of learned variables. Example: w_save = tf.constant(_w)

    Also make sure to put names in the input and output of the model, this will be needed for the model later. Example:
    x = tf.placeholder(tf.float32, [None, 1000], name='input')
    y = tf.nn.softmax(tf.matmul(x, w_save) + b_save), name='output')

  4. Export your model with:
    tf.train.write_graph(<graph>, <path for the exported model>, <name of the model>.pb, as_text=False)

How to run my model with Android?

You need tensorflow.aar, which can be downloaded from the nightly build artifact of TensorFlow CI, here we use the #124 build.

Interacting with TensorFlow

To interact with TensorFlow you will need an instance of TensorFlowInferenceInterface, you can see more details about it here

Credits

Credits for this code go to mari-linhares. I've merely created a wrapper to get people started.

a_guide_to_running_tensorflow_models_on_android's People

Contributors

llsourcell 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

a_guide_to_running_tensorflow_models_on_android's Issues

pb file protection for deployment

Hello, this is not an issue well, it may be for anyone publishing an app with a .pb file in it and wanting to prevent unauthorized usage of such pb file.

=> Could you please help us figuring out how to decrypt on runtime & successfully load our pb file on Android?

The idea would be to include the encrypted pb in the apk and store the key as safe as possible (+ preventing the app from running on rooted / debuggin devices, etc)

Please help us if you have an idea on how to achieve this; we found libraries to encrypt / decrypt text files in Android, but we are missing the decryption into pb / loading the decrypted model step....

Load test data from file

Hi,
Is it possible to generate an alternative app activity to load the test data from a file(csv/image) and test the model's performance?
I am working on an inertial sensor(acc+gyro) based app for activity classification would like to have some suggestion on an offline testing from a loaded test data file.

Many thanks.

Error when importing my own .pb file

Hello there,
I have trained a model of my own dataset which consists of Bangla characters of 60 different labels. When I replaced the opt_mnist_convnet-keras.pb with my own file I continiously get this
"Error When Intializing classifiers"

Optimized save method

This is not a issue,but a optimized save method.

  1. import dependency
from tensorflow.python.framework.graph_util import convert_variables_to_constants 
  1. Convert variables to constants and save
output_graph_def = convert_variables_to_constants(sess, sess.graph_def, output_node_names=
['output/outputs'])
    with tf.gfile.FastGFile('model/CTNModel.pb', mode='wb') as f:
        f.write(output_graph_def.SerializeToString())

Binary Output

export_model(tf.train.Saver(), model, ["conv2d_1_input"], "dense_2/Softmax")

In the above line of code, what would be the parameters for Binary output?
I have used only ONE convolution layer.

I just want to make everything clear before training.
Thanking you in anticipation.

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.