源代码链接:https://www.kaggle.com/yassineghouzam/introduction-to-cnn-keras-0-997-top-6
sns.countplot(label):作图,统计label中每个不同的label出现的次数
DataFrame.values.reshape(shape):以shape重新规划DataFrame中的值,eg:shape=(-1,28,28,1),-1表示此维度不限数目
注意:转换后DataFrame类型变为ndarray类型
keras.utils.np_utils.to_categorical(Y_train, num_classes = n):将标量表示的Y_train变为one hot向量,list变为矩阵
g = plt.imshow(X_train[0][:,:,0],cmap='gray'),若不加cmap='gray',灰度原图作出的图可能是彩色的,或在前加入plt.gray() 命令
keras定义神经网络:
# Set the CNN model
# my CNN architechture is In -> [[Conv2D->relu]*2 -> MaxPool2D -> Dropout]*2 -> Flatten -> Dense -> Dropout -> Out
from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
model = Sequential() model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', activation ='relu', input_shape = (28,28,1))) model.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', activation ='relu')) model.add(MaxPool2D(pool_size=(2,2))) model.add(Dropout(0.25))
数据增强:增加原始数据的variaions,例如数字不在图像中心,数字大小不同
常用方法:grayscales, horizontal flips, vertical flips, random crops, color jitters, translations, rotations, and much more.
# With data augmentation to prevent overfitting (accuracy 0.99286) 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 rotation_range=10, # randomly rotate images in the range (degrees, 0 to 180) zoom_range = 0.1, # Randomly zoom image width_shift_range=0.1, # randomly shift images horizontally (fraction of total width) height_shift_range=0.1, # randomly shift images vertically (fraction of total height) horizontal_flip=False, # randomly flip images vertical_flip=False) # randomly flip images datagen.fit(X_train)
应用数据增强和模拟退火算法后,keras的fit函数形式改变:
# Fit the model history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size), epochs = epochs, validation_data = (X_val,Y_val), verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size , callbacks=[learning_rate_reduction])
根据训练结果作图:
# Plot the loss and accuracy curves for training and validation fig, ax = plt.subplots(2,1) ax[0].plot(history.history['loss'], color='b', label="Training loss") ax[0].plot(history.history['val_loss'], color='r', label="validation loss",axes =ax[0]) legend = ax[0].legend(loc='best', shadow=True) ax[1].plot(history.history['acc'], color='b', label="Training accuracy") ax[1].plot(history.history['val_acc'], color='r',label="Validation accuracy") legend = ax[1].legend(loc='best', shadow=True)