Core ML Insight: Crafting a Smart Body Fat Calculator

April 2, 2024

Exploring Apple's Core ML capabilities? Senior iOS Developer Alexander Karpenko published his recent project implementing Apple's Core ML to create a smart body fat calculator. Read the steps he took, and see his final result!

Intro

Leveraging Apple's Core ML, this app subtly touches on health and fitness by turning user inputs into detailed body fat metrics, illustrating AI's vast potential to refine app functionality. This project is aimed at those curious about leveraging AI to create more intelligent and responsive applications.

At the core of the app lies the Body Fat Extended Dataset from Kaggle, featuring detailed measurements from 252 males and 184 females. This dataset is unique not just in its size but in how the data was collected, using Siri's equation—a recognized method for estimating body fat percentage. This precision ensures the Core ML model I developed is based on reliable, comprehensive data, making it adept at predicting body fat percentage from diverse user inputs. It's a prime example of how machine learning can be applied to practical app development with accuracy and user inclusivity in mind.

In the development of the app, the primary goal is to predict body fat percentage based on specific input parameters. These parameters include sex, age, weight, height, and various body measurements such as neck, chest, abdomen, hip, thigh, knee, ankle, biceps, forearm, and wrist. By leveraging these inputs, the model aims to provide an estimation of an individual's body fat percentage, illustrating the practical application of machine learning in health and fitness technologies.

Dataset

Data preparation

I allocated 20% of the dataset for testing purposes, a key step to validate the model's effectiveness. This allocation allows for a robust evaluation of how accurately and reliably the model predicts body fat percentages under varied conditions, ensuring its practical applicability.

Core ML

When it came to implementing the Core ML model, selecting the right approach was crucial. With options like Tabular Classification, Tabular Regression, and Recommendation at my disposal, it was important to understand each one's use case.

Core ML templates

  • Tabular Classification is ideal for categorizing data into discrete classes. For example, if I were classifying body fat into 'low', 'medium', or 'high', this would be the method to use. However, body fat percentage is a precise, continuous value, not a category, making classification unsuitable for my app's needs.
  • Tabular Regression is tailored for predicting numerical values that vary continuously, which is exactly how body fat percentage is measured. It treats features as columns in a table, perfectly aligning with the structured nature of my dataset, hence it was the clear choice for predictions.
  • Recommendation systems are great for suggesting products or media, as seen with services like Netflix or Amazon. This model type, however, isn't designed for predicting exact numerical outcomes based on input features, making it irrelevant for an app calculating body fat percentage.

Tabular Regression, which excels in predicting continuous values, was the clear choice for its alignment with the dataset's structure.

With a solid dataset in place, the next step was to choose the best machine learning method for predicting body fat percentage.

Training methods

Create ML offers several approaches, each with its strengths:

  • Automatic: Ideal for its time-saving and built-in optimization, this option lets Create ML determine the best algorithm. While appealing for its simplicity, I wanted a method tailored to the specific nature of the data.
  • Random Forest: This method is a powerhouse, known for handling large datasets with a mix of features. It shines in accuracy and is robust against overfitting, thanks to its ensemble approach of constructing multiple decision trees. While it might introduce some complexity, its ability to discern intricate patterns in data is unparalleled—a key advantage for accurately predicting body fat percentages from a variety of measurements.
  • Boosted Tree: This high-accuracy technique builds upon the errors of previous trees, but it's computationally demanding and more susceptible to overfitting—traits that could hinder the app's efficiency.
  • Decision Tree: Its simplicity and interpretability are excellent; however, it may not capture the more nuanced patterns in the data and can overfit.
  • Linear Regression: A basic regression technique that models the relationship between a scalar response and one or more explanatory variables. It's simple and fast but might be too simplistic for complex relationships in data, leading to underfitting.

I opted for Random Forest. Despite being more complex, it's a robust method that maintains accuracy across various inputs, crucial for my app’s purpose. Random Forest's multiple decision trees offer a nuanced understanding of the data, which is essential for precise body fat predictions.

Training Parameters Configured

In the accompanying screenshot, you'll find the evaluation results that reflect the model's performance, providing a clear perspective on its predictive capabilities.

Insights from Model Performance

Seamless integration and application

Once the model was fine-tuned, the next step was to integrate it into the iOS app. The process was straightforward: I added the trained Core ML model directly into my Xcode project. From there, the app uses the model to calculate and display body fat percentage predictions instantly, based on the user's inputted measurements. Make sure that Xcode has generated the Swift model class; if so, it will be ready to use in the code.

Model card in project

Alright, now we're ready to use BodyFatModelExtended. To make a prediction, just call a specific method of the model. Here's my implementation that works with the generated model.

As you can see, everything is pretty simple: just pass the required parameters and get the result. Here's my example of using the BodyFatModel:

This simplicity masks the complex logic and calculations, highlighting Core ML's power and developer-friendliness. Let's run the app and see how it works.

Let's compare the results of the prediction with the test data to make sure that the predictions are good enough.

Preview of the values and prediction

As you can see, the result of prediction is pretty close to laboratory measurement, indicating a high level of accuracy in the app's performance.

Bringing it all together

Our implementation showcases the ease with which complex machine learning features can be incorporated into practical applications using Core ML. The accuracy of our app's predictions approaches that of laboratory measurements, highlighting the potential of AI in health and fitness apps.

I encourage everyone to check out the project on my GitHub. Your contributions, whether suggestions for enhancements, bug reports, or pull requests, are highly appreciated. Additionally, if there's interest in a breakdown of the UI or any other aspect of the code, feel free to drop a comment.

About the author

Alexander Karpenko is a Senior iOS Developer who has worked with First Line Software for more than 5 years. In his time on the team, he has worked on an enormous number of projects, primarily as an iOS/Android developer. Currently, his passion lies in AI, and he exploring ways to integrate AI into mobile apps.

Let’s talk!

Have any questions? Fill out the form and our team will be in touch!