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: [email protected]
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 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

Take our 85+ 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!

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

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

Feedback ↓