Skip to content

Instantly share code, notes, and snippets.

@kmader
Last active September 9, 2020 04:06
Show Gist options
  • Save kmader/ebd1c2a07815e1f227c6e77ec996e928 to your computer and use it in GitHub Desktop.
Save kmader/ebd1c2a07815e1f227c6e77ec996e928 to your computer and use it in GitHub Desktop.
PlaidML Keras MNIST
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"if True:\n",
" import plaidml.keras\n",
" plaidml.keras.install_backend()\n",
" import plaidml.keras.backend"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Original source: https://github.com/fchollet/keras/raw/master/examples/mnist_mlp.py\n",
"#\n",
"# This example has been slightly modified:\n",
"# ) It uses PlaidML as a backend (by invoking plaidml.keras.install_backend()).\n",
"# ) It removes Dropout.\n",
"# ) It's been reworked slightly to act as a functional integration test.\n",
"\n",
"'''Trains a simple deep NN on the MNIST dataset.\n",
"\n",
"Gets to 98.40% test accuracy after 20 epochs\n",
"(there is *a lot* of margin for parameter tuning).\n",
"2 seconds per epoch on a K520 GPU.\n",
"'''\n",
"from __future__ import print_function\n",
"\n",
"import sys\n",
"\n",
"import numpy as np\n",
"np.random.seed(47) # for reproducibility\n",
"\n",
"from keras.datasets import mnist\n",
"from keras.models import Sequential\n",
"from keras.layers import Dense, Dropout, Activation, Reshape, MaxPooling2D, Conv2D, Flatten\n",
"from keras.layers.normalization import BatchNormalization\n",
"from keras.optimizers import SGD, Adam, RMSprop\n",
"from keras.utils import np_utils\n",
"\n",
"#import testing.plaidml_config\n",
"\n",
"\n",
"def load_data():\n",
" integration_test_limit = 20000\n",
" nb_classes = 10\n",
" # the data, shuffled and split between train and test sets\n",
" (X_train, y_train), (X_test, y_test) = mnist.load_data()\n",
"\n",
" X_train = X_train.reshape(60000, 784)\n",
" X_test = X_test.reshape(10000, 784)\n",
" X_train = X_train.astype('float32')\n",
" X_test = X_test.astype('float32')\n",
" X_train /= 255\n",
" X_test /= 255\n",
" X_train = X_train[:integration_test_limit]\n",
" y_train = y_train[:integration_test_limit]\n",
"\n",
" # convert class vectors to binary class matrices\n",
" Y_train = np_utils.to_categorical(y_train, nb_classes)\n",
" Y_test = np_utils.to_categorical(y_test, nb_classes)\n",
"\n",
" return X_train, Y_train, X_test, Y_test\n",
"\n",
"def build_cnn_model(use_batch_normalization=False, use_dropout=False):\n",
" model = Sequential()\n",
" model.add(Reshape((28, 28, 1), input_shape=(784,)))\n",
" model.add(Conv2D(16, kernel_size = (3,3), padding = 'same'))\n",
" model.add(Conv2D(16, kernel_size = (3,3), padding = 'valid'))\n",
" model.add(MaxPooling2D((2,2)))\n",
" model.add(Conv2D(16, kernel_size = (3,3), padding = 'same'))\n",
" model.add(Conv2D(16, kernel_size = (3,3), padding = 'valid'))\n",
" model.add(MaxPooling2D((2,2)))\n",
" model.add(Flatten())\n",
" if use_dropout:\n",
" model.add(Dropout(0.2))\n",
" model.add(Dense(10, activation = 'softmax'))\n",
" \n",
" model.compile(loss='categorical_crossentropy',\n",
" optimizer=RMSprop(),\n",
" metrics=['accuracy'])\n",
" \n",
" return model\n",
" \n",
" \n",
"def build_model(use_batch_normalization=False, use_dropout=False):\n",
" model = Sequential() \n",
" model.add(Dense(128, input_shape=(784,)))\n",
" model.add(Activation('relu'))\n",
" if use_batch_normalization:\n",
" model.add(BatchNormalization())\n",
" if use_dropout:\n",
" model.add(Dropout(0.2))\n",
" \n",
" model.add(Dense(256))\n",
" model.add(Activation('relu'))\n",
" if use_batch_normalization:\n",
" model.add(BatchNormalization())\n",
" model.add(Dense(64))\n",
" model.add(Activation('relu'))\n",
" model.add(Dense(32))\n",
" model.add(Activation('relu'))\n",
" if use_dropout:\n",
" model.add(Dropout(0.2))\n",
" model.add(Dense(10))\n",
" model.add(Activation('softmax'))\n",
"\n",
" return model\n",
"\n",
"\n",
"def build(use_batch_normalization=False, use_dropout=False):\n",
" model = build_cnn_model(use_batch_normalization=use_batch_normalization, use_dropout=use_dropout)\n",
"\n",
" model.summary()\n",
"\n",
" model.compile(loss='categorical_crossentropy',\n",
" optimizer=RMSprop(),\n",
" metrics=['accuracy'])\n",
" return model\n",
"\n",
"def run(model,X_train, Y_train, X_test, Y_test):\n",
" batch_size = 128\n",
" epochs = 2\n",
" \n",
" history = model.fit(X_train, Y_train,\n",
" batch_size=batch_size, epochs=epochs,\n",
" verbose=1)\n",
"\n",
" score = model.evaluate(X_test, Y_test, verbose=1)\n",
"\n",
" return score"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:plaidml:b'Opening device \"intel(r)_iris(tm)_graphics_550.0'\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"20000 train samples\n",
"10000 test samples\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"reshape_1 (Reshape) (None, 28, 28, 1) 0 \n",
"_________________________________________________________________\n",
"conv2d_1 (Conv2D) (None, 28, 28, 16) 160 \n",
"_________________________________________________________________\n",
"conv2d_2 (Conv2D) (None, 26, 26, 16) 2320 \n",
"_________________________________________________________________\n",
"max_pooling2d_1 (MaxPooling2 (None, 13, 13, 16) 0 \n",
"_________________________________________________________________\n",
"conv2d_3 (Conv2D) (None, 13, 13, 16) 2320 \n",
"_________________________________________________________________\n",
"conv2d_4 (Conv2D) (None, 11, 11, 16) 2320 \n",
"_________________________________________________________________\n",
"max_pooling2d_2 (MaxPooling2 (None, 5, 5, 16) 0 \n",
"_________________________________________________________________\n",
"flatten_1 (Flatten) (None, 400) 0 \n",
"_________________________________________________________________\n",
"dense_1 (Dense) (None, 10) 4010 \n",
"=================================================================\n",
"Total params: 11,130\n",
"Trainable params: 11,130\n",
"Non-trainable params: 0\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"# the data, shuffled and split between train and test sets\n",
"X_train, Y_train, X_test, Y_test = load_data()\n",
"print(X_train.shape[0], 'train samples')\n",
"print(X_test.shape[0], 'test samples')\n",
"model = build()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 1/2\n",
"19968/20000 [============================>.] - ETA: 0s - loss: 0.4714 - acc: 0.8575"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:plaidml:b'Analyzing Ops: 76 of 237 operations complete'\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"20000/20000 [==============================] - 23s - loss: 0.4711 - acc: 0.8575 \n",
"Epoch 2/2\n",
"20000/20000 [==============================] - 20s - loss: 0.1809 - acc: 0.9470 \n",
"10000/10000 [==============================] - 4s \n",
"Test score: 0.16621265831\n",
"Test accuracy: 0.9482\n",
"CPU times: user 43.8 s, sys: 6.03 s, total: 49.8 s\n",
"Wall time: 49 s\n"
]
}
],
"source": [
"%%time\n",
"#plaidml.keras.backend.set_config(testing.plaidml_config.config())\n",
"score = run(model, X_train, Y_train, X_test, Y_test)\n",
"print('Test score:', score[0])\n",
"print('Test accuracy:', score[1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Results\n",
"## Tensorflow 1.3 CPU\n",
"```\n",
"CPU times: user 1min 41s, sys: 17.1 s, total: 1min 58s\n",
"Wall time: 32.5 s\n",
"```\n",
"## plaidml backend\n",
"- Slowest\n",
"```\n",
"CPU times: user 44.2 s, sys: 5.76 s, total: 49.9 s\n",
"Wall time: 1min 9s\n",
"```\n",
"- Fastest\n",
"```\n",
"CPU times: user 43.8 s, sys: 6.03 s, total: 49.8 s\n",
"Wall time: 49 s\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment