Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: pub@towardsai.net
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab VeloxTrend Ultrarix Capital Partners Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Our 15 AI experts built the most comprehensive, practical, 90+ lesson courses to master AI Engineering - we have pathways for any experience at Towards AI Academy. Cohorts still open - use COHORT10 for 10% off.

Publication

Deploy a Python Machine Learning Model on your iPhone
Machine Learning   Programming

Deploy a Python Machine Learning Model on your iPhone

Last Updated on February 10, 2021 by Editorial Team

Author(s): Patrick Long, Ph.D.

A minimalist guide

Deploy a Python Machine Learning Model on your iPhone
Photo by AltumCode on Unsplash

This article describes the shortest path from training a python machine learning model to a proof of concept iOS app you can deploy on an iPhone. The goal is to provide the basic scaffolding while leaving room for further customization suited to one’s specific use case. In the spirit of simplicity, we will overlook some tasks such as model validation and building a fully polished user interface (UI). By the end of this tutorial, you will have a trained model running on iOS that you can showcase as a prototype and load to your device.

Step 1. Set up your environment

First, let’s create a python virtual environment called .core_ml_demo and then install the necessary libraries i.e. pandas scikit-learn and coremltools. From your terminal run:

Next we will install Xcode. Xcode is a development toolkit for Apple products. Note that Xcode is quite large (> 10 Gb). I’d recommend grabbing a cup of coffee or running your install overnight. –Note, this guide uses Xcode Version 12.3 (12C33) on macOS Catalina 10.15.5.

‎Xcode

Step 2. Train a model

We’ll use scikit-learn’s Boston Housing Price toy dataset to train a linear regression model to predict home price based on property and socio-economic attributes. Since we’re aiming for simplicity, we’ll limit the feature space to 3 predictors and use house price as our target variable.

Step 3. Convert the model to Core ML

Apple provides two avenues to develop models for iOS. The first, Create ML, allows one to produce models entirely within the Apple ecosystem. The second, Core ML, allows one to integrate models from third parties into the Apple platform by converting them to the Core ML format. Since we’re interested in running a python trained model on iOS, we’ll use the latter.

We’ll convert our sklearn model to the Core ML format (.mlmodel) using python’s coremltools package before importing to Xcode. coremltools allows one to assign metadata to a model object such as authorship information and model feature and outcome descriptions.

Step 4. Start a new Xcode project

And that’s it for python. From hereon, we can complete a prototype app using only Xcode and Swift. This can be done with the setup below.

  • Open up Xcode and create a new Xcode project
  • Choose “iOS” as the Multiplatform type
  • Select “App” as the Application type
Creating a new Xcode project for iOS
  • Next, name your project and select the “SwiftUI” Interface.
Naming your Xcode project
  • Now simply drag and drop the .mlmodel file (saved above in step 3) into your Xcode directory. Xcode will automatically generate a Swift class for your model as shown in the editor below. If you inspect your model class, you’ll notice that it includes the details we entered when saving our python model using coremltools such as feature and target field descriptions. This is handy for model stewardship.
Importing your .coreml file into your Xcode project

Step 5. Build a model UI

Next we’ll build a basic UI by modifying the ContentView.swift file in your Xcode project. The Swift code below sets up a UI that allows users to adjust house attributes and then to predict house price. There are several elements we can review here.

The NavigationView contains our essential UI. It includes:

  • Stepper structs (lines 19–30) for each of our three features, which enable users to modify feature values. Steppers are basically widgets that modify the @State of our house attribute variables (lines 6–8).
  • A Button on the navigation bar (lines 31–40) to call our model from within the predictPrice function (line 46). This yields an Alert message on the screen with the predicted price.

Outside of the NavigationView we have our predictPrice function (lines 46–62). The predictPrice function instantiates our Swift Core ML model class and generates a prediction according the values stored in our feature states.

And at last the fun part. We can build and run a simulation of our app in Xcode to see our model in action. In the example below, I’ve created a simulation using the iPhone 12.

Simulation of your model running on iOS

Conclusion

And that’s it! Our initial prototype is complete. There’s plenty left to be done such as model validation, tests to confirm expected performance after import to iOS and a sleeker/more friendly UI. Nonetheless, I hope this serves as a useful reference for your mobile machine learning deployment endeavors.

New and improved tools continue to make mobile pursuits more widely accessible to the data science community and there are many creative opportunities waiting to be claimed in the mobile space. As mobile technology is inherently multi-media, it provides a richness of data types (e.g. audio, video, movement and location) along with unique point of use applications to expand one’s data science toolkit.

As always, I welcome any feedback or suggestions.

Thanks for reading!

Resources

CoreML

iOS Deployment

Apple Developer Documentation

SwiftUI

SwiftUI by Example


Deploy a Python Machine Learning Model on your iPhone was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI


Take our 90+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Towards AI has published Building LLMs for Production—our 470+ page guide to mastering LLMs with practical projects and expert insights!


Discover Your Dream AI Career at Towards AI Jobs

Towards AI has built a jobs board tailored specifically to Machine Learning and Data Science Jobs and Skills. Our software searches for live AI jobs each hour, labels and categorises them and makes them easily searchable. Explore over 40,000 live jobs today with Towards AI Jobs!

Note: Content contains the views of the contributing authors and not Towards AI.