Path: blob/master/BatchNormalization/cifar10_cnn_100epochs.py
3119 views
'''Train a simple deep CNN on the CIFAR10 small images dataset.1It gets to 75% validation accuracy in 25 epochs, and 79% after 50 epochs.2(it's still underfitting at that point, though).3https://github.com/keras-team/keras/blob/master/examples/cifar10_cnn.py4'''56from __future__ import print_function7import keras8from keras.datasets import cifar109from keras.preprocessing.image import ImageDataGenerator10from keras.models import Sequential11from keras.layers import Dense, Dropout, Activation, Flatten12from keras.layers import Conv2D, MaxPooling2D13import os14import pickle1516from numpy.random import seed17seed(7)1819batch_size = 3220num_classes = 1021epochs = 10022data_augmentation = True23num_predictions = 2024save_dir = os.path.join(os.getcwd(), 'saved_models_noBn_100_s7')25model_name = 'keras_cifar10_trained_model.h5'2627# The data, split between train and test sets:28(x_train, y_train), (x_test, y_test) = cifar10.load_data()29print('x_train shape:', x_train.shape)30print(x_train.shape[0], 'train samples')31print(x_test.shape[0], 'test samples')3233# Convert class vectors to binary class matrices.34y_train = keras.utils.to_categorical(y_train, num_classes)35y_test = keras.utils.to_categorical(y_test, num_classes)3637model = Sequential()38model.add(Conv2D(32, (3, 3), padding='same',39input_shape=x_train.shape[1:]))40model.add(Activation('relu'))41model.add(Conv2D(32, (3, 3)))42model.add(Activation('relu'))43model.add(MaxPooling2D(pool_size=(2, 2)))44model.add(Dropout(0.25))4546model.add(Conv2D(64, (3, 3), padding='same'))47model.add(Activation('relu'))48model.add(Conv2D(64, (3, 3)))49model.add(Activation('relu'))50model.add(MaxPooling2D(pool_size=(2, 2)))51model.add(Dropout(0.25))5253model.add(Flatten())54model.add(Dense(512))55model.add(Activation('relu'))56model.add(Dropout(0.5))57model.add(Dense(num_classes))58model.add(Activation('softmax'))5960# initiate RMSprop optimizer61opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)6263# Let's train the model using RMSprop64model.compile(loss='categorical_crossentropy',65optimizer=opt,66metrics=['accuracy'])6768x_train = x_train.astype('float32')69x_test = x_test.astype('float32')70x_train /= 25571x_test /= 2557273if not data_augmentation:74print('Not using data augmentation.')75history = model.fit(x_train, y_train,76batch_size=batch_size,77epochs=epochs,78validation_data=(x_test, y_test),79shuffle=True)80else:81print('Using real-time data augmentation.')82# This will do preprocessing and realtime data augmentation:83datagen = ImageDataGenerator(84featurewise_center=False, # set input mean to 0 over the dataset85samplewise_center=False, # set each sample mean to 086featurewise_std_normalization=False, # divide inputs by std of the dataset87samplewise_std_normalization=False, # divide each input by its std88zca_whitening=False, # apply ZCA whitening89zca_epsilon=1e-06, # epsilon for ZCA whitening90rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)91width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)92height_shift_range=0.1, # randomly shift images vertically (fraction of total height)93shear_range=0., # set range for random shear94zoom_range=0., # set range for random zoom95channel_shift_range=0., # set range for random channel shifts96fill_mode='nearest', # set mode for filling points outside the input boundaries97cval=0., # value used for fill_mode = "constant"98horizontal_flip=True, # randomly flip images99vertical_flip=False, # randomly flip images100rescale=None, # set rescaling factor (applied before any other transformation)101preprocessing_function=None, # set function that will be applied on each input102data_format=None, # image data format, either "channels_first" or "channels_last"103validation_split=0.0) # fraction of images reserved for validation (strictly between 0 and 1)104105# Compute quantities required for feature-wise normalization106# (std, mean, and principal components if ZCA whitening is applied).107datagen.fit(x_train)108109# Fit the model on the batches generated by datagen.flow().110history = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), epochs=epochs, validation_data=(x_test, y_test), workers=4)111112with open('./trainHistoryDictNoBn50', 'wb') as file_pi:113pickle.dump(history.history, file_pi)114115# Save model and weights116if not os.path.isdir(save_dir):117os.makedirs(save_dir)118model_path = os.path.join(save_dir, model_name)119model.save(model_path)120print('Saved trained model at %s ' % model_path)121122# Score trained model.123scores = model.evaluate(x_test, y_test, verbose=1)124print('Test loss:', scores[0])125print('Test accuracy:', scores[1])126127128129