Audio Classification in an Android App with TensorFlow Lite

Audio Classification in an Android App with TensorFlow Lite

Deploying machine learning-based Android apps is gaining prominence and momentum with frameworks like TensorFlow Lite, and there are fairly a couple of articles that describe the best way to develop cellular apps for laptop imaginative and prescient duties like textual content classification and picture classification.

But there’s very a lot much less that exists about working with audio-based ML duties in cellular apps, and this weblog is supposed to handle that hole — particularly, I’ll describe the steps and code required to carry out audio classification in Android apps.


Intended Audience and Pre-requisites:

This article covers completely different applied sciences required to develop ML apps in cellular and offers with audio processing methods. As such, the next are the pre-requisite to get the whole understanding of the article:

→ Familiarity with deep studying, Keras, and convolutional neural networks

→ Experience with Python and Jupyter Notebooks

→ Basic understanding of audio processing and vocal classification ideas

→ Basics of Android app growth with Kotlin

A Major Challenge

One main problem with regard to growth of audio-based ML apps in Android is the shortage of libraries in Java that carry out audio processing.

I used to be stunned to search out that there are not any libraries obtainable in Java for Android that assist with the calculation of MFCC and different options required for audio classification. Most of my time with regard to this text has been spent in direction of growing a Java parts that generates MFCC values similar to Librosa does — which could be very vital to a mannequin’s potential to make predictions.

What We’ll Build

At the top of the tutorial, you’ll have developed an Android app that helps you classify audio recordsdata current in your cellular sdcard listing into any one of many noise sort of the Urbancode Challenge dataset. Your app ought to kind of appear to be beneath:

Full supply code for the answer is out there on this repository, and you’ll find the Python code associated to mannequin creation within the Python_Notebook_Files listing.

TensorFlow Lite Model Creation for Audio Classification in Python

Let’s get began with the Python code required to create a TensorFlow Lite mannequin for audio classification.

Model creation is a fairly straight-forward course of, by which we’ll be calculating MFCC values for audio indicators and utilizing them to create a Keras-CNN mannequin that enables us to categorise these indicators. We’ll use the CitySound audio dataset for our classification functions. You can obtain the dataset right here:

Once downloaded, place the extracted audio recordsdata within the CitySound8K listing and ensure to supply the right path within the Urban_data_preprocess.ipynb file and launch it in Jupyter Notebook.

For the aim of this demo, we are going to use solely 200 knowledge records for coaching — as our intent is to easily showcase how we are able to deploy our TFLite mannequin in an Android app—as such, accuracy doesn’t matter right here, for our demo functions.

Refer to the CitySound_200.csv file for the 200 records and ensure to put this file beneath the metadata listing of the CitySound dataset. You can observe the identical steps by coaching with your complete 8K records of the supply dataset for those who’d like.

As talked about above, we’ll be calculating the MFCC values for every audio file from coaching set and take the imply worth throughout the frames over an ‘FFT’ window.

**Important level to watch right here — After producing MFCC values for every of the audio recordsdata, we’re taking the imply of the values throughout its period, utilizing the np.imply perform. We’ll be feeding the neural community with these imply values, and once we use the mannequin for prediction within the Android app — we’ll have to take the imply of the MFCC values in Java earlier than feeding it. We will see this once we get to that part — however only a be aware of commentary.**

Once pre-processing is completed, discuss with the Urban_CNN_Model.ipynb file for the mannequin constructing code. Here, we’ll take the pre-processed knowledge and feed it to a easy Keras convolutional neural community to create the mannequin. Upon coaching, the mannequin would supply accuracy of round 74% on take a look at knowledge — which is pretty good contemplating we’ve processed solely 200 records.

Good, we’ve educated the mannequin. But the best way to export this mannequin and use it in Android app? The mannequin has been educated in Python, so how can we port it to our Android atmosphere? This is the place the TFLite framework comes into the image. This is an official framework from TensorFlow, which helps in exporting the fashions educated in Python to edge gadgets resembling cell phones.

We’ll be changing this mannequin to a TFLite model with the intention to use it within the app we’re going to construct. We do that utilizing the TFLite converter:

The above code block generates the TFLite model from the unique mannequin— and now we’re good to make use of this mannequin within the Android app.

To summarize up till this level, we’ve pre-processed audio knowledge from the CitySound dataset, educated a neural community, measured its accuracy, and transformed the mannequin into tflite format.

Next, we have to do the port the mannequin to our Android app, course of the audio recordsdata, and feed them to the mannequin for prediction.

Android App Development in Kotlin for Audio Classification

Let’s shift gears and bounce into our Android growth atmosphere with Kotlin in Android Studio. This article assumes that you just’re aware of the fundamentals of Android app growth and the best way to entry a tool emulator.

Let’s create an empty venture in Android Studio and make the next updates with regard to how TensorFlow is supported in Android.

Build.gradle

Once the app is created, open the construct.gradle file and point out the TensorFlow dependencies which are required to be bundled as a part of the app. There are two particular TensorFlow libraries: tensorflow-lite and tensorflow-lite-support.

Also, we have to allow the setting that doesn’t compress the tflite mannequin throughout app creation via aaptOptions— as proven beneath:

AndroidManifest.xml

Make positive to say the write permission on the app with uses-permission within the AndroidManifest.xml file:

Assets folder:

Create an belongings folder in your app by proper clicking in your venture in Android Studio and deciding on ‘New → Folder → Assets Folder’. Once this belongings folder is created, copy the tflite mannequin created within the Python part together with a textual content file itemizing the labels within the CitySound classification dataset in .txt file format.

Sample Audio recordsdata:

Since this demo app is about audio classification utilizing the CitySound dataset, we have to copy a number of the pattern audio recordsdata current beneath the Sample Audio listing into the exterior storage listing of our emulator with the beneath steps:

→ Launch the emulator.

→ Open ‘Device File Explorer’, current on the backside proper nook of Android Studio.

→ Navigate to the mnt/sdcard listing and create a listing named audioData.

→ Right click on and add the pattern audio recordsdata beneath the audioData listing.

Activity_main.xml

In the activity_main.xml file, beneath the res/structure listing, point out the UI controls required for the app. Here, we’ll create some quite simple UI controls, with one spinner itemizing ‘wav’ recordsdata within the audioData listing, together with a button to categorise the sounds in it.

MFCC era from audio recordsdata

As talked about originally of this put up, one of many main challenges with audio classification apps in Android is with regard to characteristic era from audio recordsdata. We don’t have any readily-available library to carry out this process.

Also, one other vital requirement— MFCC values generated by the Java library ought to precisely match the corresponding python values, as solely then will our mannequin carry out correctly.

MainActivity.kt

Let’s now bounce into the Android app code. As you may see, I merely listing the ‘wav’ recordsdata current in exterior listing as Spinner choices and set off the ClassifyNoise perform on the click on of the button.

In the ClassifyNoise perform, I first loaded the wav file and seize the magnitude values of the audio file. Then, the captured magnitude values are handed on to the MFCC class to generate these values.

Please be aware that I’m utilizing an nMFCC worth of 40 — as used within the Python part — in order that the generated characteristic is of similar dimension. We additionally have to take the imply of MFCC values throughout the period of the file. So I’ve carried out the identical right here.

TFLite Interpreter

Ok…we’ve created an Android app, made essential imports and configurations, processed the audio recordsdata, and generated the imply MFCC values. just like the Python library — subsequent, let’s feed the values to our TensorFlow Lite mannequin and generate the predictions.

This is the place it will get attention-grabbing. Our TFLite mannequin expects the enter within the type of ‘1x40x1x1’ tensor — whereas no matter MFCC values we’ve generated are in 1D float array format. So we have to create a tensor in Java and have it fed to the mannequin.

TensorBuffer is the characteristic to make use of right here, and you may discuss with the code, the place I’ve supplied the detailed feedback on every of the processing steps.

Once the predictions are made, we have to course of the chance values to retrieve the highest predictions and to show the worth.

File and App Permissions:

Lastly, we’d like to verify our app has the precise permissions to do that work. In the emulator, go to ‘Settings → App & Notifications → NoiseClassifier_Android_Tensorflow’ [or the respective app name of your project] and select ‘Permissions’. Ensure to supply entry to ‘Files and Media’ for the app.

Upon completion, that is how the demo app ought to look—the place customers can choose a wav file and the mannequin will predict the category of the noise sort.

Conclusion

Thus, we come to the top of our demo and article. Mobile + ML + audio is an thrilling subject, and we’ve got extraordinary use circumstances on this space.

As many of the focus is on textual content and picture classification on cellular apps, audio-related content material is few and much between. I wish to deal with this hole with a collection of articles on this space. So keep tuned!

Thanks, and Happy Learning.

The put up Audio Classification in an Android App with TensorFlow Lite appeared first on Fritz ai.

HI-FI News

through Fritz ai https://fritz.ai/

December 14, 2023 at 11:30PM