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!
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.
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.
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.
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.
Create ML offers several approaches, each with its strengths:
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.
In the accompanying screenshot, you'll find the evaluation results that reflect the model's performance, providing a clear perspective on its predictive capabilities.
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.
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.
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.
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.
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.