HomeMachine LearningMachine Learning DIYDeploying Machine Learning Models In Android Apps

Deploying Machine Learning Models In Android Apps

Audio version of the article

After working on the model building, the next step in the machine learning life cycle is usually the deployment in the real-world scenario to perform actionable tasks. In most cases, the model is deployed via the web interfaces, android apps, or IoT. Where the website deployment requires a lot of extra effort to set up the front-end, android apps seems a reasonable solution, and that too when the app is built in Python! In this article, I will walk you through building apps using Python, which will be a cross-platform application, meaning it can be converted into android apps and IOS too.

Overview of the Article

  • Introduction to the problem statement
  • Developing the API
  • Kivy and Kivymd Basics
  • Finishing the App
  • Conclusion

Problem Statement

The dataset for which we will build the model and deploying it is a music dataset. Primarily, the dataset comprises a JSON file comprising technical aspects of a song such as a tempo, valence, energy, and a CSV file with other metadata. The dataset is available on Kaggle: Classify Song Genres from Audio Data. This article is focused more on the deployment part, so the actual processing part is skipped here. For reference, here are the features used for building the model after combining the JSON and CSV data choosing appropriate features:

Deploying Machine Learning Models In Android Apps 8

After trying out different approaches, Decision Tree Classifier gave satisfactory results:

Deploying Machine Learning Models In Android Apps 9

Although the cross-validation score of logistic regression comes out to be more than the decision trees, we will still prefer Decision trees since it doesn’t require normalization/transformation of data adding up to the advantage of passing the values as received from the user with no explicit pre-processing. Then we pickle the model so it can be loaded into other environments and served in different scenarios.

Developing the API

Till now our model is ready and we can directly create an API for this model. We could directly load this model into the app, but to reduce dependencies of the final application, we will serve the model via API. Second, this can be further used in other applications like websites, telegram bots, slack bots, or anything you would think of.

API using python is pretty easy, and we will create it via the Flask framework. It is a lightweight micro-framework that serves any web service with few lines of code. For this application, we have created an endpoint named /api which takes all the parameters of the model as input and gives out the prediction. The overall structure of the flask application looks like this:

Deploying Machine Learning Models In Android Apps 10

After creating this file, we can easily deploy this API on the Heroku platform and this will serve as the results entry point for our app. Heroku is a great platform as a service that provides tools to make any service accessible from anywhere.

Kivy and Kivymd Basics

Now that we have the model ready, we can work on the app development. Python popularity is increasing at a higher rate, and it is covering the app development market too. Kivy is a great library to get started with building GUI based applications that can be exported for different platforms. This library assumes a strong grip over object-oriented programming, most of the programming done here is class-based.

The main thread of the app lies in the MDApp class. This class is the main entry point of all the things to be performed in the app. If you have a little experience with UI (User interface) designing, you must be familiar with terms like labels, buttons, screens, tabs, menus, etc. These are generic terms and their meaning is very straight forward. The Kivymd is material design components which is an improved version of Kivy and we will continue to use Kivymd. For installations run:

“`

pip install kivy 
pip install kivymd

“`

The striking feature of these elements is how they are defined. They are defined hierarchically in YAML format which has more readability, and all the attributes of the components are self-explanatory. This definition is done using Kivy lang which is a YAML format language and is easy to interpret. The indentation acts as the level of element, root, or children of the element.

For instance, a label can have properties of color, position, text to be displayed or a button can have events of press, release, etc. The label will have these properties defined as children of this element. Here is a basic app syntax/ boilerplate code:

Deploying Machine Learning Models In Android Apps 11

Kivymd documentation offers a lot of elements that can create interactive apps but being in the machine learning field, you don’t have to remember all the elements and that’s why I have listed the most commonly used elements:

  • MDApp: This is the main class that forms the entry point for all actions to be performed in the application. For instance, the build() function returns the screen which needs to be displayed first in the application, the on_start() function defines the activities, variables which are needed at the start of the application. It has a lot of other functions which can be changed according to the needs.
  • MDLabel: Label means the text that needs to be displayed is defined using this element.

“`

MDLabel:
text: ‘Sample Text Needed’
halign: ‘center’
id: output_tex
theme_text_color: “Custom”
text_color: 0, 1, 0, 1

“`

  • MDTextField: This element enables us to take input from the user. It has a wide range of attributes that can make it interactive. For example, this element has two types of modes: on_focus and persistent. In focus mode, the helper text below the input field is only visible when the field is selected, while in persistent mode, the helper text is always present.

“`

MDTextField:
        id: input_
        hint_text: ‘(0.014392 – 0.983649)’
        width: 100
        size_hint_x: None
        pos_hint: {‘center_y’:0.26, ‘center_x’:0.5}

“`

  • MDRaisedButton: As the name suggests, this element is a button and there are 10+ types of styles available via the KIvymd. Here the on_press event is defined to call a function in the app. A basic button looks like this:

“`

MDRaisedButton:
        pos_hint: {‘center_x’:0.5, ‘center_y’:0.1}
        text: ‘Predict’
        on_press: app.predict()

“`

Finishing The App

Now combining all the things we saw, the app is ready to be tested. Here are the new things to be implemented:

  • The main screen is inherited from the base screen.
  • The app layout to be designed such that it takes all the inputs from the user.
  • Whenever the user presses the predict button, the predict function in the app should be triggered and here it should take all the values passed by the user. This is done by referring to the id of the element.
  • After getting the values, we will make a GET request to get the final prediction and display it on the screen. The whole code implementation (App + API) can be found here.
  • To convert it to the android package, you need to have a Linux system.
  • In the terminal, run the following commands:

“`

git clone https://github.com/kivy/buildozer.git
cd buildozer
sudo python setup.py install
buildozer init
buildozer android debug deploy run

“`

  • Let’s see the end product:

Deploying Machine Learning Models In Android Apps 12

Conclusion

Kivy and Kivymd is a great tool to make GUI applications in Python and being a data scientist you can find its application in deploying or testing out your model in different scenarios. You can further explore this library by checking out the official documentation.

This article has been published from the source link without modifications to the text. Only the headline has been changed.

Source link

- Advertisment -Deploying Machine Learning Models In Android Apps 13Deploying Machine Learning Models In Android Apps 14

Most Popular

- Advertisment -Deploying Machine Learning Models In Android Apps 15Deploying Machine Learning Models In Android Apps 16