When I changed my laptop last time back in 2018, I wanted a machine to explore the intricacies of Neural Networks and AI in general. Then, I decided to go for a MacBook Pro (15-inch, 2018). It was an expensive choice, but its robust performance has never fallen short with handling Data Science and middle-complex AI projects. However, I never leveraged its GPU for training purposes until now! In the following sections, I will show you how to get the advantage of this graphic processor already included on your MacBook Pro. For this matter, I will start presenting you the library used to bridge our beloved DL-libraries with our hardware.
You can follow this tutorial for the installation of PlaidML. Here are the steps, though, for setting up everything on your macOS:
- Make sure your computer is listed on Apple’s compatibility list with support for OpenCL.
- Create an environment for using PlaidML.
python3 -m venv plaidml-venv source plaidml-venv/bin/activate
- Install PlaidML with Keras and set up PlaidML to use your preferred computing device.
- You will be asked whether to use experimental device support. In any case, you can type in “n” and then return to choose using default config devices.
- I choose AMD Radeon pro 560 as it is the most powerful one available.
pip install -U plaidml-keras plaidml-setup
- Test the installation by running MobileNet in plaidbench.
pip install plaidml-keras plaidbench plaidbench keras mobilenet
Building a simple CNN classifier
You can either code the following using Jypyter Notebook or in a script.
The first part is when you specify the usage of PlaidML as Keras Backend. Otherwise, it would be using TensorFlow by default.
# Importing PlaidML. Make sure you follow this order import plaidml.keras plaidml.keras.install_backend() import os os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
Here, you import the libraries needed, download the dataset, and split it into a training set and a testing set.
import keras from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras import backend as K # Download fashion dataset from Keras fashion_mnist = keras.datasets.fashion_mnist (x_train, y_train), (x_test, y_test) = keras.datasets.fashion_mnist.load_data() # Reshape and normalize the data x_train = x_train.astype('float32').reshape(60000,28,28,1) / 255 x_test = x_test.astype('float32').reshape(10000,28,28,1) / 255
Next, you build a simple CNN (Convolutional Neural Network) using Keras Sequential model.
# Build a CNN model. You should see "INFO:plaidml:Opening device xxx" after you run this chunk model = keras.Sequential() model.add(keras.layers.Conv2D(filters=64, kernel_size=2, padding='same', activation='relu', input_shape=(28,28,1))) model.add(keras.layers.MaxPooling2D(pool_size=2)) model.add(keras.layers.Dropout(0.3)) model.add(keras.layers.Conv2D(filters=32, kernel_size=2, padding='same', activation='relu')) model.add(keras.layers.MaxPooling2D(pool_size=2)) model.add(keras.layers.Dropout(0.3)) model.add(keras.layers.Flatten()) model.add(keras.layers.Dense(256, activation='relu')) model.add(keras.layers.Dropout(0.5)) model.add(keras.layers.Dense(10, activation='softmax'))
# Compile the model model.compile(optimizer='adam', loss=keras.losses.sparse_categorical_crossentropy, metrics=['accuracy'])
Finally, you fit the model and test the accuracy:
import time start = time.process_time() # Fit the model on training set model.fit(x_train, y_train, batch_size=64, epochs=10) # Evaluate the model on test set score = model.evaluate(x_test, y_test, verbose=0) # Print test accuracy print('\n', 'Test accuracy:', score) # your code here print(time.process_time() - start)
At this point, you have trained a CNN with around 90% accuracy in about 4 minutes. You can compare the performance in following this novel library and using the traditional way (TensorFlow as backend). In my case, the performance enhancement was about 15 times faster than using a CPU.
Making use of your GPU power as a MacBook Pro user will definitively enable you to handle more complicated problems. Enjoy!
If you want to know more about PlaidML, visit its Github repository.
This post was inspired by Frank Xu in Towards Data Science.