Last active
July 23, 2016 07:45
-
-
Save fzenke/9ed965fbd42df4446c4d9f6bf07eab34 to your computer and use it in GitHub Desktop.
Simple mod of keras example cifar10_cnn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
'''Train a simple deep CNN on the CIFAR10 small images dataset and compare different optimizers | |
GPU run command: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python cifar10_opt_test.py | |
For the original script and Keras see https://github.com/fchollet/keras | |
''' | |
from __future__ import print_function | |
from keras.datasets import cifar10 | |
from keras.preprocessing.image import ImageDataGenerator | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Activation, Flatten | |
from keras.layers import Convolution2D, MaxPooling2D | |
from keras.optimizers import SGD, SMORMS3, Adam | |
from keras.utils import np_utils | |
import time | |
batch_size = 32 | |
nb_classes = 10 | |
nb_epoch = 10 | |
# input image dimensions | |
img_rows, img_cols = 32, 32 | |
# the CIFAR10 images are RGB | |
img_channels = 3 | |
# the data, shuffled and split between train and test sets | |
(X_train, y_train), (X_test, y_test) = cifar10.load_data() | |
print('X_train shape:', X_train.shape) | |
print(X_train.shape[0], 'train samples') | |
print(X_test.shape[0], 'test samples') | |
# convert class vectors to binary class matrices | |
Y_train = np_utils.to_categorical(y_train, nb_classes) | |
Y_test = np_utils.to_categorical(y_test, nb_classes) | |
model = Sequential() | |
model.add(Convolution2D(32, 3, 3, border_mode='same', | |
input_shape=(img_channels, img_rows, img_cols))) | |
model.add(Activation('relu')) | |
model.add(Convolution2D(32, 3, 3)) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
# model.add(Dropout(0.25)) | |
model.add(Convolution2D(64, 3, 3, border_mode='same')) | |
model.add(Activation('relu')) | |
model.add(Convolution2D(64, 3, 3)) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
# model.add(Dropout(0.25)) | |
model.add(Flatten()) | |
model.add(Dense(512)) | |
model.add(Activation('relu')) | |
# model.add(Dropout(0.5)) | |
model.add(Dense(nb_classes)) | |
model.add(Activation('softmax')) | |
X_train = X_train.astype('float32') | |
X_test = X_test.astype('float32') | |
X_train /= 255 | |
X_test /= 255 | |
saved_weights = model.get_weights() | |
optimizers = ['adam', 'smorms3', 'rmsprop'] | |
for opt in optimizers: | |
print("Training with %s"%opt) | |
model.set_weights(saved_weights) | |
model.compile(loss='categorical_crossentropy', | |
optimizer=opt, | |
metrics=['accuracy']) | |
t_start = time.time() | |
model.fit(X_train, Y_train, | |
batch_size=batch_size, | |
nb_epoch=nb_epoch, | |
validation_data=(X_test, Y_test), | |
shuffle=True) | |
t_stop = time.time() | |
score = model.evaluate(X_test, Y_test, verbose=0) | |
print('Test score:', score[0]) | |
print('Test accuracy:', score[1]) | |
print('Training time: %fs'%(t_stop-t_start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment