I’ve recently been upgrading my tool set to the latest versions of Python, Keras, and Tensorflow, all running on a docker-based GPU-enabled deployment of Jupyter with CUDA 8 installed. In this notebook I walk through a quick test to verify that the GPU is recognized and is working, and define and train a basic CNN for the classic MNIST digit recognition task.
Overview
This entry is pretty self explanatory, pretty much going through the basics of loading modules, checking GPU detection, and model definition.
I found that the following references were helpful to get going:
-
https://elitedatascience.com/keras-tutorial-deep-learning-in-python
First network I saw and what was implemented
-
https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html
For adapting for TensorFlow (had been Theano) and 2.0 api
-
http://machinelearningmastery.com/handwritten-digit-recognition-using-convolutional-neural-networks-python-keras/
Very similar network, also comparison to other networks and some nice explanations
In addition to the above, this notebooks makes a few improvements for the latest library versions, and adaptations for Python 3.x.
General Notes
As long as the libraries are installed properly, this notebook should be able to be executed as-is.
While the epochs are running it can be instructive to check your GPU status, e.g. using nvidia-smi
.
I observed the following output before execution:
$ nvidia-smi
Sat Jun 17 19:05:40 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.66 Driver Version: 378.13 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 Tesla M40 24GB Off | 0000:00:06.0 Off | 0 |
| N/A 37C P0 58W / 250W | 21798MiB / 22939MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+
And this while running:
$ nvidia-smi
Sat Jun 17 19:09:25 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.66 Driver Version: 378.13 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 Tesla M40 24GB Off | 0000:00:06.0 Off | 0 |
| N/A 39C P0 74W / 250W | 21915MiB / 22939MiB | 28% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+
Note power increase of 16 W, slight temperature increase (2C), GPU utilization from 0 to 28%.
I was surprised that no processes appeared in the process list, will need to look into that.
Notebook
Full notebook available here
Introduction¶
Explorations into the newer versions of Keras, TF, and Python 3
Authors
- Eric Carlson
from datetime import datetime
import configparser
import hashlib
from importlib import reload
import logging
import numpy as np
import os
import pandas as pd
import pathlib as pl
import sys
import yaml
from IPython import display
import etc_utils as eu
importlib.reload(eu)
#%cat etc_utils.py
sys.version
Configure pandas and matplot lib for nice web printing¶
pd.options.display.max_rows = 1000
pd.options.display.max_columns = 50
pd.options.display.max_colwidth = 100
%matplotlib inline
Load config files, configure logging¶
work_desc = "deep_learning_intro"
time_str, path_config, creds = eu.load_config_v2()
print('Time string: {}'.format(time_str))
print('Paths:')
for k, item in path_config.items():
print('{}: {}'.format(k, item))
logger = logging.getLogger()
eu.configure_logging(logger, work_desc=work_desc, log_directory=path_config['log_dir'], time_str=time_str)
[k for k in creds.keys()]
[k for k in creds['mimic3_v1_4'].keys()]
Load Keras and Test GPU¶
import tensorflow as tf
from tensorflow.python.client import device_lib
def get_available_gpus():
local_device_protos = device_lib.list_local_devices()
return [x.name for x in local_device_protos if x.device_type == 'GPU']
get_available_gpus()
device_lib.list_local_devices()
import keras
keras.__version__
tf.__version__
import keras.applications as kapps
from keras.applications import imagenet_utils as iu
Start analysis¶
Following https://elitedatascience.com/keras-tutorial-deep-learning-in-python
Adapted for TensorFlow and 2.0 api, following https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html
Adapated for Python 3.x
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import MaxPooling2D, Conv2D
from keras.layers import InputLayer
# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
from keras.datasets import mnist
# Load pre-shuffled MNIST data into train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
print(X_train.shape)
from matplotlib import pyplot as plt
plt.imshow(X_train[0])
X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
print(X_train.shape)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print(y_train.shape)
print(y_train[:10])
from keras.utils import np_utils
# Convert 1-dimensional class arrays to 10-dimensional class matrices
Y_train = np_utils.to_categorical(y_train, 10)
Y_test = np_utils.to_categorical(y_test, 10)
print(Y_train.shape)
print(Y_train[:5,:10])
import tensorflow as tf
sess = tf.Session()
from keras import backend as K
K.set_session(sess)
K.image_data_format()
K.set_image_dim_ordering('th')
K.image_data_format()
model = Sequential()
model.add(InputLayer(input_shape=(1, 28, 28)))
model.add(Conv2D(32, (3, 3), activation='relu'))
print(model.output_shape)
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
with tf.device('/gpu:0'):
model.fit(X_train, Y_train,
batch_size=32, epochs=10, verbose=2)
model.summary()
score = model.evaluate(X_test, Y_test, verbose=0)
print("Baseline Error: %.2f%%" % (100-score[1]*100))
Comments
comments powered by Disqus