Delivering ML to Mobile Apps with Skafos and AWS SageMaker

Miriam Friedel
7 min readJun 13, 2019

--

AWS has long been a go-to platform for data storage and compute for citizen data scientists and professionals alike. With the introduction of AWS SageMaker, Amazon has provided its customers with an end-to-end solution for building, training, and deploying machine learning models to production.

At Skafos, mobile is in our DNA. We understand that mobile developers want to leverage the power of machine learning in their apps, but don’t always know where to start. We also know that delivering machine learning to mobile apps presents some unique challenges, and we are very familiar with Apple’s App Store Review Guidelines.

This is why we are so excited to walk you through how Skafos can seamlessly integrate with AWS SageMaker. Use SageMaker to train your model, use Skafos to deliver it. For a higher level overview, check out this post from our intern Ashley Dahl. Ashley had no Python experience before her internship, but was able to build and deliver a Core ML model with SageMaker and Skafos in just a few hours! I can’t think of a better advertisement for both platforms.

To make things really easy, we’ve created an open source Github repository of models that can easily be ported to AWS SageMaker, along with instructions on how to do it, so we’ll start there.

Getting started with AWS SageMaker

Note: This section is also described in a step-by-step tutorial in our github repository, so if you’ve already done that, feel free to jump to the next section!

First, log in to AWS SageMaker at console.aws.amazon.com/sagemaker. When you get there, you will see the option to select a Notebook instance on the left. Click it!

Create a notebook instance in SageMaker, Step 1

Next, you’ll want to select Create notebook instance.

Create a notebook instance in SageMaker, Step 2

In the notebook instance, you will want to create a notebook name, and (if using GPU support) select one of the ml.p2.xxxx instance types. In the example below, I chose ml.p2.xlarge and opted not to do any additional configuration. Note: your notebook name needs to follow the conventions that AWS helpfully lists below the field where you type it in. It’s possible that I ignored those the first few times and ran into trouble. :)

Scroll down to the Git repositories section, chose Clone a public Git repository to this notebook instance only, and enter the repository that contains our Skafos SageMaker example models. Click the Create SageMaker instance link.

Connecting your SakeMaker Jupyter Notebook instance to the Skafos example models repository.

It takes a few minutes for the notebook to spin up, particularly if it is provisioning a GPU on the backend. The wait is worth it, in my opinion, since I don’t have to worry about setting up my own GPU. Who doesn’t have 2–3 minutes for a GPU?

Once your notebook status shows up as In Service, Click Open Jupyter and proceed to the next section.

Note: If you have your own predictive model or Jupyter Notebook, feel free to link to that repository as well. The goal at the end is to have a model object or artifact that can be exported to Core ML and incorporated into an app.

Training a predictive model

As soon as you clicked the Open Jupyter, you will immediately be taken to the git repo you specified. That landing page looks like this:

For this exercise, I wanted to train a new “Dogs and Cats” model, so I clicked on the dogs_and_cats.ipynb link in the ImageClassification subfolder, and SageMaker opened the notebook for me. You can choose a different notebook if you want. Before proceeding, you will have to select a kernel. We chose conda_amazonei_mxnet_p36.

Once in the notebook, run through all of the cells prior toModel Export and Skafos Upload, which we’ll cover in the next section. Prior to that section, this notebook is doing the following:

  • Installing the necessary Python libraries, including the correct version of mxnet, turicreate, and coremltools.
  • Importing photos of dogs and cats from a public S3 bucket and reading them into a data frame
  • Building an image classifier model using the Turi Create library. If you are new to machine learning, it is a fantastic place to start!

At the end of all of this, you will have a model artifact that you can convert to Core ML, export to Skafos, and integrate into an iOS app.

Delivering your model using Skafos

In my opinion (and of course I’m biased), this is the fun part. We can convert our model to Core ML, deliver it into an app, and immediately see the results. With Skafos, you can do this over the air, without having to resubmit your app to the app store.

Before I walk you through the rest of this notebook, which will show how easy it is to deliver models with Skafos, I’m assuming you’ve taken the following steps:

  • Signed up for a Skafos account
  • In your Skafos dashboard, created an Image Classifier app by selecting Try an Example App and selecting the Image Classifier. Once created, the integration guide will take you through setting up your app with Skafos.
  • Once you’ve done this, use XCode to install the app on your phone!

We now have the ability to tie this all together; a newly trained image classification model, trained on AWS SageMaker, and a place to deliver it. To do this, let’s go back to our Jupyter Notebook.

First, you’ll need to specify a Core ML model name, and export the model you just created to Core ML. While other types of models can be used, our example app uses Core ML, and if you are just getting started, it is the easy to integrate into iOS apps using Swift.

Next, you will need to import the Skafos Python SDK into your notebook, and using your Skafos API Token, get a summary of all of the apps and models you have access to. This will help you to know which app to deliver this particular model to.

You can find your existing API tokens, generate new ones, and revoke old ones on your account settings page. I’ve redacted mine in the screenshot above, but it will be something like d5c008256d3cc15a3735929897aaa411.

Now that you have a token and summary of models, you only need one function call (yes, really!), to deliver this model to Skafos. In the example below, you’d replace my org and app information with your own. Note that by setting the API token as an environment variable above, you no longer have to specify it in any of the Skafos function calls.

Finally, navigate to your Skafos dashboard and verify that your new model is there!

Hooray! Our new model is listed as version 4.

If you want to deliver this model to your app, click the Deploy button.

That’s it. You can do this as many times as you need to iterate on your model. You can deliver to Skafos, deploy to Dev and/or Prod as needed, or roll back to older model versions.

Interested? Want to try this out? Have questions? Here are some resources:

We can’t wait to see what you build!

--

--

Miriam Friedel

Mom, Data Scientist, Physics PhD. Currently: Senior Director, Machine Learning Engineering at Capital One. Previously: Director of Data Science at Skafos.