All Projects → shubham0204 → Age-Gender_Estimation_TF-Android

shubham0204 / Age-Gender_Estimation_TF-Android

Licence: MIT license
Age + Gender Estimation on Android with TensorFlow Lite

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Age-Gender Estimation TF-Android

The Third Eye
An AI based application to identify currency and gives audio feedback.
Stars: ✭ 63 (+85.29%)
Mutual labels:  android-development, tensorflow-experiments, tensorflow-models
Tensorflow-Wide-Deep-Local-Prediction
This project demonstrates how to run and save predictions locally using exported tensorflow estimator model
Stars: ✭ 28 (-17.65%)
Mutual labels:  tensorflow-experiments, tensorflow-models, tensorflow-examples
Open nsfw android
🔥🔥🔥色情图片离线识别,基于TensorFlow实现。识别只需20ms,可断网测试,成功率99%,调用只要一行代码,从雅虎的开源项目open_nsfw移植,该模型文件可用于iOS、java、C++等平台
Stars: ✭ 1,586 (+4564.71%)
Mutual labels:  tensorflow-models, tensorflow-examples, tensorflow-lite
Android-Machine-Learning-With-TensorFlow
Tensor Flow implementation for Android
Stars: ✭ 17 (-50%)
Mutual labels:  tensorflow-experiments, tensorflow-models, tensorflow-examples
Awesome-Tensorflow2
基于Tensorflow2开发的优秀扩展包及项目
Stars: ✭ 45 (+32.35%)
Mutual labels:  tensorflow-experiments, tensorflow-models, tensorflow-examples
Math object detection
An image recognition/object detection model that detects handwritten digits and simple math operators. The output of the predicted objects (numbers & math operators) is then evaluated and solved.
Stars: ✭ 52 (+52.94%)
Mutual labels:  tensorflow-experiments, tensorflow-models, tensorflow-examples
android-compose-mvvm-foodies
Android sample app following best practices: Kotlin, Compose, Coroutines and Flow, Hilt, JetPack Navigation, ViewModel, MVVM, Retrofit, Coil
Stars: ✭ 374 (+1000%)
Mutual labels:  android-development, kotlin-coroutines
permissions-flow
A simple library to make it easy requesting permissions in Android using Kotlin Coroutines.
Stars: ✭ 81 (+138.24%)
Mutual labels:  android-development, kotlin-coroutines
Expenso
📊 A Minimal Expense Tracker App built to demonstrate the use of modern android architecture component with MVVM Architecture
Stars: ✭ 325 (+855.88%)
Mutual labels:  android-development, kotlin-coroutines
TensorFlow-Multiclass-Image-Classification-using-CNN-s
Balanced Multiclass Image Classification with TensorFlow on Python.
Stars: ✭ 57 (+67.65%)
Mutual labels:  tensorflow-experiments, tensorflow-models
Self Driving Car
Automated Driving in NFS using CNN.
Stars: ✭ 105 (+208.82%)
Mutual labels:  tensorflow-experiments, tensorflow-examples
Permissionsflow
A simple library to make it easy requesting permissions in Android using Kotlin Coroutines.
Stars: ✭ 49 (+44.12%)
Mutual labels:  android-development, kotlin-coroutines
Cameraxdemo
A sample camera app with CameraX API from Android Jetpack
Stars: ✭ 112 (+229.41%)
Mutual labels:  android-development, kotlin-coroutines
Wgan
Tensorflow Implementation of Wasserstein GAN (and Improved version in wgan_v2)
Stars: ✭ 228 (+570.59%)
Mutual labels:  tensorflow-experiments, tensorflow-models
Ml Classifier Ui
A UI tool for quickly training image classifiers in the browser
Stars: ✭ 224 (+558.82%)
Mutual labels:  tensorflow-experiments, tensorflow-examples
Einsen
🎯 Einsen is a prioritization app that uses Eisenhower matrix technique as workflow to prioritize a list of tasks & built to Demonstrate use of Jetpack Compose with Modern Android Architecture Components & MVVM Architecture.
Stars: ✭ 821 (+2314.71%)
Mutual labels:  android-development, kotlin-coroutines
Cramer Gan
Tensorflow Implementation on "The Cramer Distance as a Solution to Biased Wasserstein Gradients" (https://arxiv.org/pdf/1705.10743.pdf)
Stars: ✭ 123 (+261.76%)
Mutual labels:  tensorflow-experiments, tensorflow-models
TensorFlow-Binary-Image-Classification-using-CNN-s
Binary Image Classification in TensorFlow
Stars: ✭ 26 (-23.53%)
Mutual labels:  tensorflow-models, tensorflow-examples
Droid Feed
Aggregated Android news, articles, podcasts and conferences about Android Development
Stars: ✭ 174 (+411.76%)
Mutual labels:  android-development, kotlin-coroutines
Neural-Turing-Machine
TensorFlow implementation of a Neural Turing Machine
Stars: ✭ 23 (-32.35%)
Mutual labels:  tensorflow-models, tensorflow-examples

Age + Gender Estimation in Android with TensorFlow

repo_banner


Contents

Python project

Android project

Issues and Suggestions

License

I'm open for freelancing in Android + ML projects and also in Technical Content Writing. You may send me an email/message on Google Chat at [email protected].


results


🖥️ Python Project

👉🏻 Colab Notebooks

As the application uses two different models for age and gender estimation, we provide two notebooks to train the age and gender detection models. These are Google Colab notebooks and are capable of downloading the dataset, training the model, and finally exporting the TFLite model ( used in the Android app ).

Open In Colab

Open In Colab

The notebooks describe each step in the implementation and are self explanatory. For any questions/issues/suggestions regarding the notebooks, see Issues and Suggestions

The Keras models ( .h5 ) are available here,

👉🏻 Dataset

The TensorFlow models are trained on the UTKFace dataset which contains 23K images with age, gender and ethnicity annotations. We use 20K samples from the dataset, in which 14K images are used for training and 6K images are used to evaluate the model.

For both the models, we use 3-channeled RGB images as inputs. The age estimation model takes in 200 * 200 images as inputs whereas the gender classification model takes in images of size 128 * 128.

  • For the age estimation model, we normalize the target variable i.e the age of the person. Hence the model's output is in the range ( 0 , 1 ]. The predicted age is obtained by multiplying the model's output with a suitable factor ( which in our case is 116 ).
  • For gender classification, the model outputs a probability distribution for the two labels male and female .

👉🏻 Model

We provide two different variants of our models. We refer these variants as vanilla and lite. The lite models are identical to the vanilla models except that they use separable convolutions instead of the standard convolutions. You'll notice the change in the Colab notebook,

# Define the conv block.
if lite_model:
        x = tf.keras.layers.SeparableConv2D( num_filters ,
                                            kernel_size=kernel_size ,
                                            strides=strides 
                                            , use_bias=False ,
                                            kernel_initializer=tf.keras.initializers.HeNormal() ,
                                            kernel_regularizer=tf.keras.regularizers.L2( 1e-5 )
                                             )( x )
 else:
        x = tf.keras.layers.Conv2D( num_filters ,
                                   kernel_size=kernel_size ,
                                   strides=strides ,
                                   use_bias=False ,
                                   kernel_initializer=tf.keras.initializers.HeNormal() ,
                                   kernel_regularizer=tf.keras.regularizers.L2( 1e-5 )
                                    )( x )
 
    x = tf.keras.layers.BatchNormalization()( x )
    x = tf.keras.layers.LeakyReLU( leaky_relu_alpha )( x )

Separable Convolutions have lesser parameters than standard convolutions, and hence reduce the size of our model. Also, the lite models run faster as compared to their vanilla counterparts. The increase in speed is companioned with a slight decrease in the performance of the model as shown below,

Model ( Age Estimation ) vanilla model lite model
No. of parameters 1,069,297 200,956
MAE 2.425 4.586
Model ( Gender Classification ) vanilla model lite model
No. of parameters 675,090 328,733
Accuracy ( % ) 99.8 96.4

📱 Android Project

👉🏻 Overview

The following GIF displays the basic functionality of the app. You may observe the four options provided for "Choose Model" and also the NNAPI and GPU Delegate options. The inference time may change depending on the device you're using.

working_of_app

👉🏻 Usage

  • Open the app. A dialog box saying "Initialize a Model" pops up. Select any one of the four models.
  • Select the additional options, like "Use NNAPI" and "Use GPU". If any of these options are not available on your device, a message will be shown for the same. See NNAPI and GpuDelegate compatibility
  • Once the models are initialized, tap "Take Photo" to open the default camera app.
  • If none of the faces are identified in the picture, a dialog will be displayed, prompting you to take another picture. If everything goes fine, the results appear on the screen in a couple of seconds.
  • Tap "Reinitialize" to try another model provided in the app.

Note: If the picture clicked by the user contains multiple faces, results will be shown for a single face only. This is a drawback and we'll try to improve it in the next releases.

👉🏻 Project Configuration

The project has been configured with the below settings, which can be easily found in the app's build.gradle file.

// SDK Info
compileSdkVersion 30  
buildToolsVersion "30.0.0"  

// App info
applicationId "com.ml.projects.age_genderdetection"  
minSdkVersion 23  
targetSdkVersion 30  

// Version info
versionCode 1  
versionName "1.0"

The versions of the Android Gradle plugin and the Gradle Version are mentioned below.

Android Gradle Plugin Version: 4.1.3
Gradle Version: 6.5

As mentioned, the project utilizes Firebase, and specifically MLKit FaceDetector. To connect the app with Firebase, follow these instructions. Download the google-services.json file and place it in the app folder. The Firebase library required for the face detection functionality is added in the app's build.gradle,

implementation 'com.google.android.gms:play-services-mlkit-face-detection:16.1.5'

Why isn't the google-services.json file shared alongside the code in this repo? That's because the file contains credentials unique for every user. ( You may need to create a Firebase project. Follow the instructions cited above )

👉🏻 TensorFlow Lite models

To enable TFLite capabilities in our Android project, we add the below libraries to our project,

implementation 'org.tensorflow:tensorflow-lite:2.4.0'  
implementation 'org.tensorflow:tensorflow-lite-gpu:2.4.0'  
implementation 'org.tensorflow:tensorflow-lite-support:0.1.0'

See the packages on JFrog Bintray.

All TFLite models are placed in the app's assets folder. In order to disable the compression performed on files present in the assets folder, we add the following flag in app's build.gradle ,

android {  
  ...
  aaptOptions {  
     noCompress "tflite"  
  }
  ...
}  

The names of these models are stored in the modelFilenames array in MainActivity.kt,

private val modelFilenames = arrayOf(  
    arrayOf("model_age_q.tflite", "model_gender_q.tflite"),  
    arrayOf("model_age_nonq.tflite", "model_gender_nonq.tflite"),  
    arrayOf("model_lite_age_q.tflite", "model_lite_gender_q.tflite"),  
    arrayOf("model_lite_age_nonq.tflite", "model_lite_gender_nonq.tflite"),  
)

Whenever the user selects a particular model, we use the FileUtil.loadMappedFile() method to get MappedByteBuffer of the model, which is then passed to the constructor of Interpreter,

ageModelInterpreter = Interpreter(FileUtil.loadMappedFile( applicationContext , "model_v6_age_q.tflite"), options )

Note: The method FileUtil.loadMappedBuffer() comes from the tf-lite-support library, which helps us parse TFLite models and also to preprocess model inputs.

👉🏻 NNAPI and GpuDelegate compatibility

The app offers acceleration through the means of NNAPI and GpuDelegate provided by TensorFlow Lite.

As mentioned in the docs, NNAPI is compatible for Android devices running Android Pie ( API level 27 ) and above. The app checks this compatibility in MainActivity.kt,

if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.P ) {  
    useNNApiCheckBox.isEnabled = false  
    useNNApiCheckBox.text = "Use NNAPI ( Not available as your Android version is less than 9 ( Android Pie )."  
    useNNApi = false  
}

The GpuDelegate also performs the following compatibility check,

if ( !compatList.isDelegateSupportedOnThisDevice ){  
    useGPUCheckBox.isEnabled = false  
    useGPUCheckBox.text = "Use GPU ( GPU acceleration is not available on this device )."  
    useGpu = false  
}

🔧 Issues and Suggestions

If any issue occurs, you may open an issue or start a new discussion. Please mention the following while addressing your issue,

  • The name of the device you're using ( along with its Android version ).
  • Crash logs if you think they might be helpful.
  • If you've found something related to the issue elsewhere, mention them as well so that the issue could be resolved as fast as possible.

👨🏻‍✈️ License

MIT License  
  
Copyright (c) 2021 Shubham Panchal  
  
Permission is hereby granted, free of charge, to any person obtaining a copy  
of this software and associated documentation files (the "Software"), to deal  
in the Software without restriction, including without limitation the rights  
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
copies of the Software, and to permit persons to whom the Software is  
furnished to do so, subject to the following conditions:  
  
The above copyright notice and this permission notice shall be included in all  
copies or substantial portions of the Software.  
  
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE  
SOFTWARE.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].