GPU-accelerated Machine Learning on MacOS with Keras and PlaidML

GPU-accelerated Machine Learning on MacOS with Keras and PlaidML

There aren't a lot of GPU-accelerated Machine Learning Framework in MacOS besides CreateML or TuriCreate. Luckily, we could use PlaidML as a backend for Keras as it implements Metal Performance Shaader.

Install Keras

Easiest way is installing Anaconda and install Keras in an environment

$ brew install anaconda

Creating an isolated environment for each of our project is also a good idea

$ conda create -n keras
$ conda activate keras

Install Plaid ML

Installing and Configuring PlaidML is surprisingly easy.

$ pip install plaidml-keras

$ plaidml-setup

plaidml-setup automatically detects viable options in the system. Simply select the strongest GPU we have with metal in the name. The settings will be persisted as .plaidml in the home directory.

Integrating with Keras

Simply set KERAS_BACKEND environment variable. Easiest way is to include this line in the same Python script

import os

os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"

Let's try with MNIST example from Keras. Look for two messages confirming the backend used.

Confirmed that Keras is using the specifed backend
And again when creating model

Let the training begin!