Adding an ML model into Android Studio - android

I have written an ML model using Keras in Google Colab which detects whether an animal is a cat or a dog based on an input image.
Now, I want to create an app in Android Studio that takes an image as input, uses the algorithm I have designed to detect whether the image is a cat or a dog and outputs the result.
My question is how can I incorporate this algorithm into Android Studio? My first thought was to rewrite the code in Java and copy it into Android Studio but writing Java in Google Colab would lead to multiple complications. Hence, is there a way to download the algorithm I have created and upload it into Android Studio such that it works? If not, what other approach can I implement?
My desired outcome is something where I can add the algorithm into Android Studio and write a code like:
if (algorithm == true)
//output dog detected
else
//output cat detected

Android Studio is just an IDE. It doesn't run the actual code. And no, it doesn't run Python.
You should be able to export the Keras model into a offline format that Android can use via Tensorflow; Keras deep learning model to android
Alternatively, to deploy an "online model", you'd run a hosted web-server that exposed the model over HTTP, which your Android code would send in requests to and parse the response.

Related

ML project in android app using Tensorflowlite

I have a model file in tensorflow. I have to integrate in Android application and build a sample app. The model takes in tokenised input. (number array instead of sentences). The application takes sentence input from user. What should be done to implement the tokeniser too in Java android application if the tensorflow model is in Python?
I think you can find some reusable code here: https://github.com/tensorflow/examples/tree/master/lite/examples
May be in the bert_qa example? https://github.com/tensorflow/examples/tree/master/lite/examples/bert_qa/android/app/src/main/java/org/tensorflow/lite/examples/bertqa/tokenization

MLKit bad recognition

Hi I trained the model through Firebase MLKit. I have selected the option to train him for 13 hours. And despite this, when I get into the details, Firebase only shows me 1 hour during training hours. And after uploading to Android device, CameraX is detecting images incorrectly. The same when I test the model in Firebase also incorrectly detects. I use photos from here:
https://www.kaggle.com/grassknoted/asl-alphabet/data
How can I improve the detection and classification of photos?
I will try to help with my version, what you need to know is that MLkit runs on TensorFlow.
MLKit can use the cloud model or ondevice, when you use your own model you must first upload it to the cloud (if you use MLKit), but if you use TensorFlowLite then you can put the model and label directly on the device.
Errors may occur when you make a model, you can use this tool to make your own model easily https://teachablemachine.withgoogle.com/train .
After that, upload your model on the MLKit Firebase, and apply it to the Android / iOS application using this sample method https://firebase.google.com/docs/ml-kit/use-custom-models
i have sample project using MLKit & its work for more detail check my article here https://medium.com/#rakaadinugroho/face-image-cropper-with-machine-learning-c448f9d19858

Creating a simple neural network on Tensorflow by means of Android

I want to create a simple neural network based on the example https://github.com/googlesamples/android-ndk/tree/master/nn_sample. Is it possible to create this with the help on Tensorflow only with Android tools on Java
Take a look at this folder https://github.com/googlesamples/android-ndk/tree/master/nn_sample/app/src/main/cpp
simple_model.h is the model trained in Tensorflow before creating the Android project. Now the model likes black-box, get input and predict output only, if you want to build your own model, try this tutorial (All steps from training, evaluating, prediction to deploy onto Android):
https://medium.com/#elye.project/applying-tensorflow-in-android-in-4-steps-to-recognize-superhero-f224597eb055
Affirmative. You can use TensorFlow Lite on Android, it's an open source deep learning framework which helps to compress and deploy models to a mobile or embedded application. It basically can take models as input and then deploys and interpret and perform resource-conserving optimizations for mobile applications. The NNAPI of Android NDK can interface with TFLite easily too. This link contains gesture, image, object & speech detection and classification example implementations on Android with Java using TFLite.

Tensorflow on android: directly build app in python?

The sample app given by google for tensorflow on android is written in C++.
I have a tensorflow application written in python. This application currently runs on desktop. I want to move the application to android platform. Can I use bazel to build the application that is written in python directly for android? Thanks.
Also sample tensorflow app in python on android will be much appreciated.
Currently, there is no simple way to run tensorflow on android. Typically, you would only have to use inference (runtime), not training.
Another way is to use TensorFlow serving to host models in the cloud and issue RPC calls from an Android client.
I tried to use python in my android application with some 3rd party terminals like SL4A and Qpython. Those will support to run the python files directly in our android application so we have to install SL4A apk's and we need to call that intent.But these will support for some level I guess.
I tried to import tensorflow in that terminal it shows module not found. So I thought this tensorflow will not work in these terminals.
So I am trying to create one .pb file from the python files which are working in unix platform.So We need to include that output .pb file in our android application and we need to change the c++ code regarding that .pb file.I am thinking in this way.let see it will work or not.I will update soon if it working.
You can create your tensorflow model on your desktop and save it as a .pb file. Then you can add this model to your android project and make use of it to make predictions on the android device.
Its like training(which involves heavy computations) on a desktop machine(which is more powerful) and using the model to make predictions(which involves less computations) on a mobile device(comparatively less powerful).
This is a link to a great video by Siraj Raval
https://www.youtube.com/watch?v=kFWKdLOxykE

Creating a "generic" Tensorflow Android app with Python code

I created a Tensorflow image classification app in python 2.7 using Kivy and Pycharm. I used my own data to create a custom graph and labels file. The app works great and does what I want it to do. It took me months of learning and coding to get to this point. My last part of this "journey" has been trying to port the app to the android platform (I'd like to do Windows or a web app too -- but that does not seem to be a real option today . . .) I've created the Tensorflow Android Camera Demo app using Bazel and it worked fine on my Galaxy S5. However, after spending several long days searching all the references I could fine in Google searches, Packt (and other) books, and so on I am at an impasse. My question is does anyone in this forum have any advice on a method to create an Android app from a working Python app as I described? I would be really grateful for any help from someone who has done this.
I used my own data to create a custom graph and labels file
Since you have already trained your TensorFlow model, you can import it into an Android app relatively easily.
The TensorFlow Android demo app can now be built in Android Studio without using Bazel. You should be able to replace the Inception v3 image classifier model with your own model.
Check out my blog post here for more information about how to use the Java TensorFlowInferenceInterface class to interact with your pre-trained model:
https://medium.com/#daj/using-a-pre-trained-tensorflow-model-on-android-e747831a3d6
As for how to port a Python app to Android, I'm not aware of an easy way to do that.

Categories

Resources