This project demonstrates how to integrate a TensorFlow Lite machine learning model into an Android application.
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
- ✅ Created Jupyter notebook (
ML_Model_Training.ipynb) for training the model - ✅ Added TensorFlow Lite dependencies to Android project
- ✅ Created
IrisModelHelperclass for model inference - ✅ Designed complete UI with input fields and results display
- ✅ Implemented
MainActivitywith prediction logic
-
Install Python dependencies (if not already installed):
pip install tensorflow numpy scikit-learn matplotlib
-
Open the Jupyter notebook:
- Open
ML_Model_Training.ipynbin Jupyter Notebook or VS Code - You can also use Google Colab
- Open
-
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.tflitefile
-
Expected output files:
iris_model.tflite(~3-5 KB)preprocessing_params.json(for reference)
-
Locate the generated model:
- After running the notebook, find
iris_model.tflitein the project root
- After running the notebook, find
-
Copy to Android assets folder:
Copy: iris_model.tflite To: app/src/main/assets/iris_model.tflite -
Delete the placeholder file (optional):
- Remove
app/src/main/assets/README_MODEL.txt
- Remove
-
In Android Studio:
- Click "Sync Project with Gradle Files" (toolbar icon)
- Wait for Gradle sync to complete
-
Resolve any errors:
- Check that TensorFlow Lite dependencies downloaded correctly
- Verify
iris_model.tfliteis in the assets folder
-
Connect an Android device or start an emulator:
- Minimum SDK: API 24 (Android 7.0)
- Target SDK: API 36
-
Build and run:
- Click the "Run" button (green play icon)
- Select your device/emulator
-
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
- Use the sample values provided in the UI:
-
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
-
Click "Predict Flower Species"
-
View results:
- Predicted species name
- Confidence percentage
- Detailed probabilities for all three species
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%
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
- 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
The model expects normalized inputs using StandardScaler:
- Formula:
(value - mean) / std - The normalization is handled automatically in
IrisModelHelper
implementation("org.tensorflow:tensorflow-lite:2.14.0")
implementation("org.tensorflow:tensorflow-lite-support:0.4.4")- Issue: "Error loading model: iris_model.tflite"
- Solution: Ensure you copied
iris_model.tflitetoapp/src/main/assets/
- Issue: Dependencies not downloading
- Solution:
- Check internet connection
- File → Invalidate Caches → Restart
- Update Android Studio to latest version
- Issue: Cannot resolve symbol errors
- Solution:
- Sync Gradle files
- Clean project: Build → Clean Project
- Rebuild: Build → Rebuild Project
- 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)
- ✅ 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
- Training a neural network with TensorFlow/Keras
- Converting models to TensorFlow Lite format
- Integrating ML models into Android apps
- Data preprocessing and normalization
- 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.