Path: blob/master/BatchNormalization/cifar10_cnn_bn_100epochs.py
3119 views
'''1This code was originally written by the Keras team. It has been modified by2Sunita Nayak at BigVision LLC. to include Batch Normalization in the architecture.34Train a simple deep CNN on the CIFAR10 small images dataset using Batch Normalization.5It gets to a maximum of 87% validation accuracy. It gets to 79% in only 7 epochs. Note6that the keras team's maximum accuracy was 79% in 50 epochs. With Batch Normalization,7it exceeds 85% in just 21 epochs, and gets to 87% in 39 epochs.8'''910from __future__ import print_function11import keras12from keras.datasets import cifar1013from keras.preprocessing.image import ImageDataGenerator14from keras.models import Sequential15from keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization16from keras.layers import Conv2D, MaxPooling2D17import os18import pickle1920from numpy.random import seed21seed(7)2223batch_size = 3224num_classes = 1025epochs = 10026data_augmentation = True27num_predictions = 2028save_dir = os.path.join(os.getcwd(), 'saved_models_bn_100_s7')29model_name = 'keras_cifar10_trained_model.h5'3031# The data, split between train and test sets:32(x_train, y_train), (x_test, y_test) = cifar10.load_data()33print('x_train shape:', x_train.shape)34print(x_train.shape[0], 'train samples')35print(x_test.shape[0], 'test samples')3637# Convert class vectors to binary class matrices.38y_train = keras.utils.to_categorical(y_train, num_classes)39y_test = keras.utils.to_categorical(y_test, num_classes)4041model = Sequential()42model.add(Conv2D(32, (3, 3), padding='same',43input_shape=x_train.shape[1:]))44model.add(BatchNormalization())45model.add(Activation('relu'))46model.add(Conv2D(32, (3, 3)))47model.add(BatchNormalization())48model.add(Activation('relu'))49model.add(MaxPooling2D(pool_size=(2, 2)))50#model.add(Dropout(0.25))5152model.add(Conv2D(64, (3, 3), padding='same'))53model.add(BatchNormalization())54model.add(Activation('relu'))55model.add(Conv2D(64, (3, 3)))56model.add(BatchNormalization())57model.add(Activation('relu'))58model.add(MaxPooling2D(pool_size=(2, 2)))59#model.add(Dropout(0.25))6061model.add(Flatten())62model.add(Dense(512))63model.add(BatchNormalization())64model.add(Activation('relu'))65#model.add(Dropout(0.5))66model.add(Dense(num_classes))67model.add(BatchNormalization())68model.add(Activation('softmax'))6970# initiate RMSprop optimizer71opt = keras.optimizers.rmsprop(lr=0.001, decay=1e-6)7273# Let's train the model using RMSprop74model.compile(loss='categorical_crossentropy',75optimizer=opt,76metrics=['accuracy'])7778x_train = x_train.astype('float32')79x_test = x_test.astype('float32')80x_train /= 25581x_test /= 2558283if not data_augmentation:84print('Not using data augmentation.')85history = model.fit(x_train, y_train,86batch_size=batch_size,87epochs=epochs,88validation_data=(x_test, y_test),89shuffle=True)90else:91print('Using real-time data augmentation.')92# This will do preprocessing and realtime data augmentation:93datagen = ImageDataGenerator(94featurewise_center=False, # set input mean to 0 over the dataset95samplewise_center=False, # set each sample mean to 096featurewise_std_normalization=False, # divide inputs by std of the dataset97samplewise_std_normalization=False, # divide each input by its std98zca_whitening=False, # apply ZCA whitening99zca_epsilon=1e-06, # epsilon for ZCA whitening100rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)101width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)102height_shift_range=0.1, # randomly shift images vertically (fraction of total height)103shear_range=0., # set range for random shear104zoom_range=0., # set range for random zoom105channel_shift_range=0., # set range for random channel shifts106fill_mode='nearest', # set mode for filling points outside the input boundaries107cval=0., # value used for fill_mode = "constant"108horizontal_flip=True, # randomly flip images109vertical_flip=False, # randomly flip images110rescale=None, # set rescaling factor (applied before any other transformation)111preprocessing_function=None, # set function that will be applied on each input112data_format=None, # image data format, either "channels_first" or "channels_last"113validation_split=0.0) # fraction of images reserved for validation (strictly between 0 and 1)114115# Compute quantities required for feature-wise normalization116# (std, mean, and principal components if ZCA whitening is applied).117datagen.fit(x_train)118119# Fit the model on the batches generated by datagen.flow().120history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, validation_data=(x_test, y_test), workers=4)121122with open('./trainHistoryDictWithBn1', 'wb') as file_pi:123pickle.dump(history.history, file_pi)124125# Save model and weights126if not os.path.isdir(save_dir):127os.makedirs(save_dir)128model_path = os.path.join(save_dir, model_name)129model.save(model_path)130print('Saved trained model at %s ' % model_path)131132# Score trained model.133scores = model.evaluate(x_test, y_test, verbose=1)134print('Test loss:', scores[0])135print('Test accuracy:', scores[1])136137138139