Machine Learning (Vol 3) - Neuroon Networks

Breaking

Sunday, February 10, 2019

Machine Learning (Vol 3)

Create Android App For Flower Detection

So in the previous article I showed how to train a tf model to detect flower types. We basically provide a data set that has five flower types. So if anyone interested in more flower types, please use at least 100 images of that flower type to get some accuracy. So if you haven't read that article click here to read it and follow those steps.

So let's begin. In your folder (tensorflow-for-poets-2) there is a folder named as tf_files. Change your directory to it. And there should be two files named as, 'retrained_graph.pb' and 'retrained_labels.txt'. Those files are generated by the retrain process.

Mobile devices have significant limitations, so any pre-processing that can be done to reduce an app's footprint is worth considering.One way the TensorFlow library is kept small, for mobile, is by only supporting the subset of operations that are commonly used during inference. This is a reasonable approach, as training is rarely conducted on mobile platforms. Similarly it also excludes support for operations with large external dependencies.

So for optimization, TensorFlow installation includes a tool, that removes all nodes that aren't needed for a given set of input and outputs.

Place the following command in the terminal. Make sure the directory is tensorflow-for-poets-2 to open the terminal. Otherwise you have to change the directory of the input file.

python -m tensorflow.python.tools.optimize_for_inference \
--input=tf_files/retrained_graph.pb \
--output=tf_files/optimized_graph.pb \
--input_names="input" \
--output_names="final_result"

if above command doesn't work place the following one. It should work.

python3 -m tensorflow.python.tools.optimize_for_inference \
--input=tf_files/retrained_graph.pb \
--output=tf_files/optimized_graph.pb \
--input_names="input" \
--output_names="final_result"

So this will give you a another file named as 'optimized_graph.pb'. This file is needed to build the android or iOS app.

If you need you can compress this file also. This is an optional setting. If you want you can do it. Just place the following command in the terminal.

python -m scripts.quantize_graph \
--input=tf_files/optimized_graph.pb \
--output=tf_files/rounded_graph.pb \
--output_node_names=final_result \
--mode=weights_rounded

or

python3 -m scripts.quantize_graph \
--input=tf_files/optimized_graph.pb \
--output=tf_files/rounded_graph.pb \
--output_node_names=final_result \
--mode=weights_rounded

So if you did the optional settings your final model name is "rounded_graph.pb" and if you not the name file should be "optimized_graph.pb".

Next step, building the mobile application. Open Android studio ( In my case I'm using Android studio ) and open the android project which is located in your current directory (tensorflow-for-poets-2).

Locate to the directory android/tfmobile/assets. And paste the relevant file with your retrained files and rename their names to the available file names. In my case that is "graph.pb" and "label.txt". And one more thing, change the variable name OUTPUT_NAME and INPUT_NAME located in the "ClassifierActivity.java" file to "final_result" and "input". And build the application. Run it on an emulator or your device. Test the application using some images relevant to the flower types which you provide to retrain process.

1 comment:

  1. I am very happy when read this blog post because blog post written in good manner and write on good topic. Thanks for sharing valuable information.

    If you are running any Laundry business then launching an On-demand Laundry App Development is very beneficial for you. How much does it cost to develop an On-Demand Laundry App Development? Want to know then Contact Us.

    ReplyDelete