Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / artificial-intelligence / machine-learning

Setting up macOS for ML and AI Development

5.00/5 (5 votes)
26 Aug 2020CPOL4 min read 9.1K   44  
In this article we prepare our development environment.
Here we'll set up a Python environment, activate your Conda environment, and set up a Jupyter Notebook to interactively write code in the browser.

Introduction

Deep neural networks are awesome at tasks like image classification. Results that would have taken millions of dollars and an entire research team a decade ago are now easily available to anyone with a half-decent GPU. However, deep neural networks have a downside. They can be very heavy and slow, so they don’t always run well on mobile devices. Fortunately, Core ML offers a solution: it enables you to create models that run well on iOS devices.

In this article series, we’ll show you how to use Core ML in two ways. First, you’ll learn how to convert a pre-trained image classifier model to a Core ML and use it in an iOS app. Then, you’ll train your own Machine Learning (ML) model and use it to make a Not Hotdog app — just like the one you might have seen in HBO’s Silicon Valley.

Prerequisites

We’ll use macOS 10.15.6, Xcode 11.6 and iOS 13.5. Most code examples should be compatible with earlier versions, except iOS, which must be at least 13.0.

While macOS includes Python, it is an older version – 2.7. We won't use this version, which is outdated and no longer officially supported.

Using Python means working with package managers and virtual environments. Our preference is to use Conda for package and virtual environments management. Here is why:

  • Conda explicitly states what it will do with an environment and waits for confirmation before it continues (in contrast to a standard pip)
  • It handles both Python and non-Python dependencies, which simplifies installation in many complex cases
  • Conda is becoming a de-facto standard for managing Python packages in many environments, including Azure; it is also recommended by Apple to work with its coremltools Python library (even though you still need to install it using pip).
  • Having a single tool to perform two tasks reduces our toolkit by one.

Not all packages are available in Conda, so occasionally we’ll need to use the default Python’s package manager – pip.

Conda comes in two flavours:

  • Anaconda – a full-blown Python distribution that contains a set of curated Python libraries used in many scenarios, including Data Science and Machine Learning projects
  • Miniconda – a minimalistic package and environment manager

We use Miniconda because it enables us to select only those components we need. However, if you prefer a "swiss knife" option, feel free to grab the full Anaconda distribution.

Create Environment

To set up a Python environment that we’ll use in this series, first download and install the macOS package. You may use one of the alternatives described in the Conda user guide.

Create the env-coreml.yml file with the following content (make sure you use exactly two spaces per indentation level):

Python
name: coreml
channels:
  - defaults
  - conda-forge
dependencies:
  - python=3.8.3
  - jupyter=1.0.0
  - onnx=1.7.0
  - numpy=1.19.1
  - pandas=1.0.5
  - tqdm=4.48.2
  - pip
  - pip:
    - coremltools==4.0b1
    - pillow==7.2.0

Execute the following command in the terminal to create the coreml environment:

Python
$ conda env create -f env-coreml.yml

After a couple of minutes, you should see a message similar to this:

Image 1

Test Environment

In the terminal, create a new empty folder and move to that folder:

Python
$ mkdir fun-with-coreml
$ cd fun-with-coreml

Activate the created environment:

Python
$ conda activate coreml

Confirm Python version (expected value: 3.8.3):

Python
$ python --version

Start a Jupyter notebook:

Python
$ jupyter notebook

If everything is completed successfully, your terminal window should look similar to this:

Image 2

In addition, a new browser tab opens with the Jupyter Notebook instance:

Image 3

If the browser hasn’t started automatically, use the URL with the token logged in the terminal.

If you cannot activate your Conda environment, follow the instructions displayed by Conda. This often this amounts to executing the following command:

Python
$ conda init {shell name - e.g. "bash"}

The Jupyter Notebook allows you to interactively write code in the browser, and to save it with results and comments in a single document – a notebook. Data scientists and ML experts use it extensively, so let’s follow the suit.

To continue, click the New button, and then select the first item from the drop-down list (Python 3). Now click within the highlighted row and enter this code snippet:

Python
print("Hello, World!")

Execute the code with Shift+Return.

The code executes, and the focus moves to the new cell:

Image 4

Summary

Congratulations: you now have all the tools you’ll need to work with ML models for your iOS application.

In the next article, we’ll use these tools to convert an ONNX model trained for image classification to the Core ML format.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)