下面是keras的官方源码,但是我对其进行了更改,主要就是将 源码的 dropout 改为了 BatchNormalization。更改之前,epochs=4,花费了50分钟左右,才达到了71%的准确率。更改之后, epochs=1,花费了15分钟左右,就已经达到了82.63%的准确率。并且源码有一点错误,导致代码不能运行,我也在代码中进行了注释。下载的数据集会保存在 c:\user\.keras 目录下面。源码链接https://github.com/keras-team/keras/blob/master/examples/cifar10_cnn.py
import keras
import os
from keras.layers import Dense, Flatten, Dropout, Activation
from keras.layers import Conv2D, MaxPool2D, BatchNormalization
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
batch_size = 32
num_classes = 10
epochs = 1
data_augmentation = True
num_predictions = 20
save_dir = os.path.join(os.getcwd(), 'save_models')
model_name = 'keras_cifar10_trained_model.h5'
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape{0}'.format(x_train.shape))
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
y_train = keras.utils.to_categorical(y=y_train, num_classes=num_classes)
y_test = keras.utils.to_categorical(y=y_test, num_classes=num_classes)
model = Sequential()
model.add(layer=Conv2D(filters=32,
kernel_size=(3, 3),
padding='same',
activation='relu',
input_shape=x_train.shape[1:]))
model.add(layer=Conv2D(filters=32,
kernel_size=(3, 3),
activation='relu'))
model.add(layer=MaxPool2D(pool_size=(2, 2)))
model.add(layer=BatchNormalization()) #这里将原来的Dropout进行更改
model.add(layer=Conv2D(filters=64,
kernel_size=(3, 3),
padding='same',
activation='relu'))
model.add(layer=Conv2D(filters=64,
kernel_size=(3, 3),
activation='relu'))
model.add(layer=MaxPool2D(pool_size=(2, 2)))
model.add(layer=BatchNormalization()) #这里将原来的Dropout进行更改
model.add(layer=Flatten())
model.add(layer=Dense(units=512, activation='relu'))
model.add(layer=BatchNormalization()) #这里将原来的Dropout进行更改
model.add(layer=Dense(units=num_classes, activation='softmax'))
opt = keras.optimizers.rmsprop(lr=0.001, decay=1e-6)
model.compile(optimizer=opt,
loss=keras.losses.categorical_crossentropy,
metrics=['accuracy'])
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
if not data_augmentation:
print("Not using data augmentation")
model.fit(x=x_train, y=y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=(x_test, y_test))
else:
print('Using real-time data augmentation.')
datagen = ImageDataGenerator(featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
zca_epsilon=1e-06, # epsilon for ZCA whitening
rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
# randomly shift images horizontally (fraction of total width)
width_shift_range=0.1,
# randomly shift images vertically (fraction of total height)
height_shift_range=0.1,
shear_range=0., # set range for random shear
zoom_range=0., # set range for random zoom
channel_shift_range=0., # set range for random channel shifts
# set mode for filling points outside the input boundaries
fill_mode='nearest',
cval=0., # value used for fill_mode = "constant"
horizontal_flip=True, # randomly flip images
vertical_flip=False, # randomly flip images
# set rescaling factor (applied before any other transformation)
rescale=None,
# set function that will be applied on each input
preprocessing_function=None,
# image data format, either "channels_first" or "channels_last"
data_format=None,
# fraction of images reserved for validation (strictly between 0 and 1)
validation_split=0.0)
datagen.fit(x=x_train)
model.fit_generator(generator=datagen.flow(x_train, y_train,
batch_size=batch_size),
epochs=epochs,
validation_data=(x_test, y_test),
steps_per_epoch=x_train.shape[0], #这一句是添加的,少了这一句,运行就会出错
workers=4)
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)
scores = model.evaluate(x=x_test, y=y_test, verbose=1)
print('Test loss:', scores[0])
print('Test accuracy:', scores[1])