3rd-Party On-Device Models

Sep 19 2024 · Swift 6.0, iOS 18.0, Xcode 16.0

Lesson 01: Illuminating Third-Party On-Device Models

Demo

Episode complete

Play next episode

Next
Transcript

Before installing Core ML Tools, you need Python and the pip package installer for Python. Pip works similarly to Swift Package Manager to make it easier to install and maintain Python platforms and libraries. There are several ways to set up an appropriate environment, and if you have an existing workflow, then you can continue to use it. If you’re starting without Python experience, the simplest way to install all required dependencies is to use Conda. Conda is an open-source, cross-platform tool that provides package, dependency, and environment management for any language.

Installing Python

Go to the Miniconda website at https://docs.anaconda.com/miniconda/. Miniconda is a free installer for conda that includes a minimal environment with conda, Python, and only the essential packages. You’ll need a bit less than 500 MB of disk space for the installation, so ensure you have that much free space available. The page lists several files for each supported platform and includes separate installers for Intel and ARM Macs. Download the package file for your computer. For an ARM Mac with an M1 (or greater) processor, download Miniconda3 macOS Apple M1 64-bit pkg. For a Mac with an Intel processor, download Miniconda3 macOS Intel x86 64-bit pkg.

Now, run the package and begin the installation wizard. Click Continue and review the installation screen. This screen lists the included Python packages. Click Continue and review and accept the License Agreement before clicking Continue. Select to Install for all users of this computer and click Continue. Finally, click Install to begin the installation. This will take a few minutes. When it is complete, click Close. Conda and Python are now installed on your Mac.

You can see how conda speeds the setup process. Its real power comes in providing isolated environments for different projects. This prevents package conflicts and allows others to create identical systems for reproducing projects. Conda also provides an easy way to switch between these different environments. It understands and supports Python packages natively.

Each environment provides a separate space for a set of packages. As each environment is isolated you can install different versions of packages in different environments. This separation avoids conflicts between packages that expect different versions of libraries and ensures you can install the exact library and tool versions for any project without breaking other projects.

Creating Conda Environments

To start, you’ll create a new environment to hold your Core ML Tools and related work. Open a macOS terminal window and create a conda environment for Core ML Tools by entering:

conda create --name coremltools python=3.11

Conda will display information about the environment it’ll create, including the path to the new environment. The last parameter tells conda to install version 3.11 of Python regardless of the latest version. You do this to match the current compatibility requirements of the model you’ll use in this module. Answer y to confirm the creation of the new environment. Now, activate the new environment by entering:

conda activate coremltools

Notice that (coremltools) appears at the start of the terminal prompt. When inside a conda environment, the name of the environment appears in parentheses at the start of the prompt. If you exit the terminal and open a new one later, you’ll find yourself back in the default base environment and need to activate the environment to return to it. To view the current environments, you can enter:

conda env list

In this case, you’ll see only two environments exist. If you have installed Conda through another system, such as Homebrew, then you’ll also see those environments in this list. The base environment is special, and the best practice is not to install packages into this environment. When you’re finished working with conda, you can deactivate the current environment by entering:

conda deactivate

Note that the name of the current environment no longer appears at the start of the prompt. By default, you’ll enter the base conda environment when you start a terminal window. Unless you’re only using the terminal for conda you’ll want to change this behavior. Enter:

conda config --set auto_activate_base false

This will stop an environment from automatically activating. To see this, close the terminal window and start a new one. You shouldn’t see a conda prompt. Go back into the environment by entering:

conda activate coremltools

Specifying the version of Python earlier also ensured the installation of pip into the environment. This package now only exists inside the coremltools environment. To confirm this, enter the following in the terminal:

pip list

This will display a list of packages installed with pip. Now enter:

conda activate base

This will change you to the base environment. Enter:

pip list

You’ll see a different list of packages showing how conda keeps each environment isolated. Now enter the following in the terminal:

conda deactivate

You’ll see from the prompt that you’re back in the coremltools environment. When you activate an environment from within another environment, you build a stack of environments. Enter:

conda deactivate

This will exit the coremltools environment and take you out of all environments. Now enter:

pip list

You’ll see an error that the command wasn’t found (unless you’ve installed Python outside of Conda). When using conda, the Python libraries and environments are isolated to each environment. To get back into your environment for Core ML Tools, enter:

conda activate coremltools

Installing Core ML Tools

Now that you understand the basics of managing conda environments, installing Core ML Tools into one needs only a single command:

pip install -U coremltools

This will begin the package installation. Let it complete before continuing. You can run this command again at any time to update Core ML Tools in the environment. This only installs Core ML Tools, not any other libraries you need. In the next lesson, you’ll work with a PyTorch model and need that library. To install this enter the following at the terminal:

conda install pytorch torchvision -c pytorch

You should now understand the basics of Python environments and have installed Core ML Tools and the other dependencies. In the next lesson, you’ll download a PyTorch model and convert it to Core ML.

See forum comments
Cinema mode Download course materials from Github
Previous: Instruction Next: Conclusion