1 from __future__ import print_function
3 from keras.datasets import mnist
4 from keras.models import Sequential
5 from keras.layers import Dense, Dropout, Flatten
6 from keras.layers import Conv2D, MaxPooling2D
7 from keras.preprocessing.image import ImageDataGenerator
8 from keras import backend as K
9 import tensorflow as tf
10 import horovod.keras as hvd
12 # Horovod: initialize Horovod.
15 # Horovod: pin GPU to be used to process local rank (one GPU per process)
16 config = tf.ConfigProto()
17 #config.gpu_options.allow_growth = True
18 #config.gpu_options.visible_device_list = str(hvd.local_rank())
19 K.set_session(tf.Session(config=config))
24 # Enough epochs to demonstrate learning rate warmup and the reduction of
25 # learning rate when training plateaues.
28 # Input image dimensions
29 img_rows, img_cols = 28, 28
31 # The data, shuffled and split between train and test sets
32 (x_train, y_train), (x_test, y_test) = mnist.load_data()
34 # Determine how many batches are there in train and test sets
35 train_batches = len(x_train) // batch_size
36 test_batches = len(x_test) // batch_size
38 if K.image_data_format() == 'channels_first':
39 x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
40 x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
41 input_shape = (1, img_rows, img_cols)
43 x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
44 x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
45 input_shape = (img_rows, img_cols, 1)
47 x_train = x_train.astype('float32')
48 x_test = x_test.astype('float32')
51 print('x_train shape:', x_train.shape)
52 print(x_train.shape[0], 'train samples')
53 print(x_test.shape[0], 'test samples')
55 # Convert class vectors to binary class matrices
56 y_train = keras.utils.to_categorical(y_train, num_classes)
57 y_test = keras.utils.to_categorical(y_test, num_classes)
60 model.add(Conv2D(32, kernel_size=(3, 3),
62 input_shape=input_shape))
63 model.add(Conv2D(64, (3, 3), activation='relu'))
64 model.add(MaxPooling2D(pool_size=(2, 2)))
65 model.add(Dropout(0.25))
67 model.add(Dense(128, activation='relu'))
68 model.add(Dropout(0.5))
69 model.add(Dense(num_classes, activation='softmax'))
71 # Horovod: adjust learning rate based on number of GPUs.
72 opt = keras.optimizers.Adadelta(lr=1.0 * hvd.size())
74 # Horovod: add Horovod Distributed Optimizer.
75 opt = hvd.DistributedOptimizer(opt)
77 model.compile(loss=keras.losses.categorical_crossentropy,
82 # Horovod: broadcast initial variable states from rank 0 to all other processes.
83 # This is necessary to ensure consistent initialization of all workers when
84 # training is started with random weights or restored from a checkpoint.
85 hvd.callbacks.BroadcastGlobalVariablesCallback(0),
87 # Horovod: average metrics among workers at the end of every epoch.
89 # Note: This callback must be in the list before the ReduceLROnPlateau,
90 # TensorBoard or other metrics-based callbacks.
91 hvd.callbacks.MetricAverageCallback(),
93 # Horovod: using `lr = 1.0 * hvd.size()` from the very beginning leads to worse final
94 # accuracy. Scale the learning rate `lr = 1.0` ---> `lr = 1.0 * hvd.size()` during
95 # the first five epochs. See https://arxiv.org/abs/1706.02677 for details.
96 hvd.callbacks.LearningRateWarmupCallback(warmup_epochs=5, verbose=1),
98 # Reduce the learning rate if training plateaues.
99 keras.callbacks.ReduceLROnPlateau(patience=10, verbose=1),
102 # Horovod: save checkpoints only on worker 0 to prevent other workers from corrupting them.
104 callbacks.append(keras.callbacks.ModelCheckpoint('./checkpoint-{epoch}.h5'))
106 # Set up ImageDataGenerators to do data augmentation for the training images.
107 train_gen = ImageDataGenerator(rotation_range=8, width_shift_range=0.08, shear_range=0.3,
108 height_shift_range=0.08, zoom_range=0.08)
109 test_gen = ImageDataGenerator()
112 # Horovod: the training will randomly sample 1 / N batches of training data and
113 # 3 / N batches of validation data on every worker, where N is the number of workers.
114 # Over-sampling of validation data helps to increase probability that every validation
115 # example will be evaluated.
116 model.fit_generator(train_gen.flow(x_train, y_train, batch_size=batch_size),
117 steps_per_epoch=train_batches // hvd.size(),
121 validation_data=test_gen.flow(x_test, y_test, batch_size=batch_size),
122 validation_steps=3 * test_batches // hvd.size())
124 # Evaluate the model on the full data set.
125 score = model.evaluate(x_test, y_test, verbose=0)
126 print('Test loss:', score[0])
127 print('Test accuracy:', score[1])