Skip to content

sh13y/Android-iris-tflite-classifier

Repository files navigation

Iris Flower Classification - ML Model Integration in Android

This project demonstrates how to integrate a TensorFlow Lite machine learning model into an Android application.

📋 Project Overview

Model: Iris Flower Classification

  • Input: 4 features (Sepal Length, Sepal Width, Petal Length, Petal Width)
  • Output: 3 flower species (Setosa, Versicolor, Virginica)
  • Task: Multi-class classification using a neural network

✅ What Has Been Completed

  1. ✅ Created Jupyter notebook (ML_Model_Training.ipynb) for training the model
  2. ✅ Added TensorFlow Lite dependencies to Android project
  3. ✅ Created IrisModelHelper class for model inference
  4. ✅ Designed complete UI with input fields and results display
  5. ✅ Implemented MainActivity with prediction logic

🔧 Manual Steps Required

Step 1: Train the ML Model

  1. Install Python dependencies (if not already installed):

    pip install tensorflow numpy scikit-learn matplotlib
  2. Open the Jupyter notebook:

    • Open ML_Model_Training.ipynb in Jupyter Notebook or VS Code
    • You can also use Google Colab
  3. Run all cells in order:

    • Click "Run All" or execute each cell sequentially
    • The notebook will:
      • Load the Iris dataset
      • Train a neural network model
      • Convert it to TensorFlow Lite format
      • Create iris_model.tflite file
  4. Expected output files:

    • iris_model.tflite (~3-5 KB)
    • preprocessing_params.json (for reference)

Step 2: Copy the Model File

  1. Locate the generated model:

    • After running the notebook, find iris_model.tflite in the project root
  2. Copy to Android assets folder:

    Copy: iris_model.tflite
    To: app/src/main/assets/iris_model.tflite
    
  3. Delete the placeholder file (optional):

    • Remove app/src/main/assets/README_MODEL.txt

Step 3: Sync and Build the Android Project

  1. In Android Studio:

    • Click "Sync Project with Gradle Files" (toolbar icon)
    • Wait for Gradle sync to complete
  2. Resolve any errors:

    • Check that TensorFlow Lite dependencies downloaded correctly
    • Verify iris_model.tflite is in the assets folder

Step 4: Run the Application

  1. Connect an Android device or start an emulator:

    • Minimum SDK: API 24 (Android 7.0)
    • Target SDK: API 36
  2. Build and run:

    • Click the "Run" button (green play icon)
    • Select your device/emulator
  3. Test the app:

    • Use the sample values provided in the UI:
      • Setosa: 5.1, 3.5, 1.4, 0.2
      • Versicolor: 6.4, 3.2, 4.5, 1.5
      • Virginica: 6.3, 3.3, 6.0, 2.5

📱 Using the App

  1. Enter flower measurements (in centimeters):

    • Sepal Length: 4.0 - 8.0 cm
    • Sepal Width: 2.0 - 4.5 cm
    • Petal Length: 1.0 - 7.0 cm
    • Petal Width: 0.1 - 3.0 cm
  2. Click "Predict Flower Species"

  3. View results:

    • Predicted species name
    • Confidence percentage
    • Detailed probabilities for all three species

🎯 Expected Results

When you input the default values (5.1, 3.5, 1.4, 0.2):

  • Prediction: Setosa
  • Confidence: ~99%
  • Probabilities:
    • Setosa: ~99%
    • Versicolor: ~1%
    • Virginica: ~0%

📁 Project Structure

Pythonmodel/
├── ML_Model_Training.ipynb          # Jupyter notebook for training
├── iris_model.tflite                # Generated model (after running notebook)
├── preprocessing_params.json         # Normalization parameters
└── app/
    ├── build.gradle.kts              # TensorFlow Lite dependencies
    └── src/main/
        ├── assets/
        │   └── iris_model.tflite     # Copy the model here
        ├── java/com/example/pythonmodel/
        │   ├── MainActivity.java     # Main app logic
        │   └── IrisModelHelper.java  # ML model wrapper
        └── res/layout/
            └── activity_main.xml     # UI layout

🛠️ Technical Details

Model Architecture

  • Input layer: 4 neurons (4 features)
  • Hidden layer 1: 16 neurons, ReLU activation
  • Hidden layer 2: 8 neurons, ReLU activation
  • Output layer: 3 neurons, Softmax activation

Data Preprocessing

The model expects normalized inputs using StandardScaler:

  • Formula: (value - mean) / std
  • The normalization is handled automatically in IrisModelHelper

Dependencies

implementation("org.tensorflow:tensorflow-lite:2.14.0")
implementation("org.tensorflow:tensorflow-lite-support:0.4.4")

🐛 Troubleshooting

Model Not Found Error

  • Issue: "Error loading model: iris_model.tflite"
  • Solution: Ensure you copied iris_model.tflite to app/src/main/assets/

Gradle Sync Failed

  • Issue: Dependencies not downloading
  • Solution:
    • Check internet connection
    • File → Invalidate Caches → Restart
    • Update Android Studio to latest version

Compilation Errors

  • Issue: Cannot resolve symbol errors
  • Solution:
    • Sync Gradle files
    • Clean project: Build → Clean Project
    • Rebuild: Build → Rebuild Project

Low Prediction Accuracy

  • Issue: Unexpected predictions
  • Solution:
    • Verify you're using values in the valid range
    • Try the sample values provided in the app
    • Retrain the model (run notebook again)

📚 Learning Resources

✨ Features Implemented

  • ✅ Real-time prediction with TensorFlow Lite
  • ✅ Input validation and error handling
  • ✅ Confidence-based color coding
  • ✅ Detailed probability display
  • ✅ Sample values for quick testing
  • ✅ Material Design 3 UI components

🎓 What You've Learned

  1. Training a neural network with TensorFlow/Keras
  2. Converting models to TensorFlow Lite format
  3. Integrating ML models into Android apps
  4. Data preprocessing and normalization
  5. Building a complete ML-powered Android application

Good luck with your project! 🚀

If you encounter any issues, refer to the troubleshooting section or check the inline comments in the code.

About

Android app demonstrating TensorFlow Lite integration for Iris flower species classification using a neural network model.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors