BMI Estimation from an Image
This text details the technical workings of a BMI prediction application built using Streamlit.
It uses a dataset of 526 individuals, and a Random Forest Regression model. The application leverages several machine learning techniques alongside the dlib library for facial landmark detection, aiming to deliver BMI estimates based solely on facial features.
Data Preparation
The core of this application is a dataset obtained from a publicly available source. This dataset contains facial measurements and corresponding BMI values, which serve as the foundation for training a predictive model.
The dataset was preprocessed to exclude missing data, and relevant features were selected for the model. The features considered in the final model exclude demographic factors such as age and height, focusing on physical facial measurements derived from the images. These features were then scaled using StandardScaler to normalize the values, ensuring the model can learn effectively from the data without being skewed by large variations.
The application utilized a Random Forest Regressor, a versatile ensemble method suitable for regression tasks. This model was selected for its robustness and ability to capture complex non-linear relationships between the features and the target variable (BMI). A grid search was performed to fine-tune the hyperparameters of the model, enhancing its predictive capabilities.
Workflow and Architecture
The application operates as follows:
- Loading and Preprocessing Data: The BMI dataset is loaded, cleaned, and preprocessed, with features scaled using a StandardScaler object. The BMI values themselves are scaled separately to ensure consistency during training and predictions.
- Facial Landmark Detection: The dlib library is employed to detect 68 facial landmarks. This step is crucial as it converts raw image data into quantifiable metrics like cheek width, jaw width, and the ratio of face width to height. These metrics serve as proxy variables for predicting BMI.
- Feature Engineering: From the facial landmarks, additional features such as cheek-to-jaw width ratio, face width-to-height ratio, and a derived facial area metric are computed. These features, combined with the scaled dataset, form the feature vector that the Random Forest model uses to predict BMI.
- Training and Saving the Model: If no trained model is available, the application trains a new Random Forest model, which is then saved alongside scalers and feature columns for future use. This prevents the need for repeated model training, improving efficiency.
- Prediction: Upon uploading an image, the application processes the facial landmarks, prepares the feature vector, scales it, and passes it to the trained model for BMI prediction.
Challenge: Integrating dlib with Streamlit
One of the major technical challenges in building this application was integrating dlib with Streamlit. dlib is not natively supported by Streamlit due to its reliance on low-level C++ libraries, which conflicts with the simplicity and modularity of the Streamlit framework. To resolve this, I attempted to use a conda environment, but Streamlit did not properly support numpy installed through conda, resulting in compatibility issues.
To circumvent this limitation, I forked an existing GitHub repository dedicated to face recognition, extracting only the parts necessary for facial landmark detection. This allowed the application to function, but at a significant cost to efficiency. While functional, this workaround introduced latency during image processing, making the user experience slower than desired.
Feature Engineering and Correlation Analysis
Once the facial landmarks are detected, the application calculates key metrics such as cheek-to-jaw width ratio and face width-to-height ratio. These derived features are particularly relevant, as previous research has shown that facial proportions can be indicative of BMI.
A correlation matrix was computed to identify the features most strongly associated with BMI. Features with a correlation above a threshold of 0.1 were selected for training the model, ensuring that only the most relevant data contributed to the prediction process.
Model Selection, Training, and Optimization
A Random Forest Regressor was employed to handle the regression task. Random Forest is well-suited for this application as it reduces overfitting through the use of multiple decision trees and provides an ensemble of models that collectively predict BMI. The model was optimized using GridSearchCV, which tested various combinations of hyperparameters to minimize mean squared error (MSE).
When comparing the performance of the Random Forest model with a baseline linear regression model, the Random Forest produced slightly better results in terms of both Mean Squared Error (MSE) and R² values. Random Forest's ability to handle non-linearity allowed it to capture complex interactions between facial features and BMI, leading to a marginal improvement in performance.
- MSE: Random Forest had a lower MSE compared to linear regression, indicating that its predictions were closer to the true BMI values on average.
- R²: The R² value, which measures the proportion of variance in the target variable explained by the model, was higher for Random Forest, further confirming its improved performance over linear regression.
While the improvements were not drastic, they underscore the benefits of using an ensemble learning method like Random Forest in capturing complex patterns that a linear model might overlook.
Once trained, the model, along with scalers and important feature columns, was saved using the joblib library for efficient reloading in future sessions. This setup minimizes the need for retraining the model, allowing the application to load and make predictions quickly on new images.
To access the application, you can visit this website: bmiestimate.oorjitsethi.com
Comments
Post a Comment