Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Neural Networks - Lesson 8
Convolutional layers, a first CNN, and data augmentation
32x32 color images across 10 classes, 6,000 images per class50,000 training images, 10,000 test imagesairplane, automobile, bird, cat, deer, dog, frog, horse, ship, truckfrom keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
# x_train shape: (50000, 32, 32, 3)categorical_crossentropy; scale pixels to [0, 1]num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# y_train[444] -> [0,0,0,0,0,0,0,0,0,1] (class 9)
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255Conv2DConv2Dkeras.layers.Conv2D(filters, kernel_size, strides=(1, 1), padding='valid', ...)filters: number of filters used per location, the depth of the outputkernel_size: (height, width) of the sliding kernelstrides: step size in each dimension, default (1,1)input_shape: required only on the first layerMaxPooling2D and FlattenMaxPooling2Dkeras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid')pool_size: size of the pooling window; strides defaults to pool_sizeFlatten turns a multi-dimensional feature map into a 1D vector, used when transitioning from conv layers to Dense layersConv
Conv
MaxPool
Dropout
Flatten
Dense
Dense
softmax
model_1 = Sequential()
model_1.add(Conv2D(32, kernel_size=(5,5), strides=(2,2), padding='same',
input_shape=x_train.shape[1:]))
model_1.add(Activation('relu'))
model_1.add(Conv2D(32, (5,5), strides=(2,2), activation="relu"))
model_1.add(MaxPooling2D(pool_size=(2, 2)))
model_1.add(Dropout(0.25))
model_1.add(Flatten())
model_1.add(Dense(512))
model_1.add(Activation('relu'))
model_1.add(Dropout(0.5))
model_1.add(Dense(num_classes))
model_1.add(Activation('softmax'))categorical_crossentropy (one-hot labels) + RMSprop + accuracymodel_1.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model_1.fit(x_train, y_train, batch_size=32, epochs=15,
validation_data=(x_test, y_test), shuffle=True)Epoch 1/15 accuracy: 0.3136 val_accuracy: 0.4506
Epoch 8/15 accuracy: 0.5748 val_accuracy: 0.6114
Epoch 15/15 accuracy: 0.5731 val_accuracy: 0.5863Exercise
Conv
Conv
MaxPool
Conv
Conv
MaxPool
Flatten
Dense
Classification
stride 1 for all convolutional layers this timeCNN5 epochs; compare training time, loss, and accuracy against the first CNN (train and validation)from tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=20,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
shear_range=0.1,
zoom_range=0.1,
fill_mode='nearest'
)datagen.flow() instead of raw arraysmodel_1.fit(
datagen.flow(x_train, y_train, batch_size=batch_size),
epochs=15,
validation_data=(x_test, y_test)
)Epoch 1/15 accuracy: 0.3195 val_accuracy: 0.5205
Epoch 9/15 accuracy: 0.6074 val_accuracy: 0.6604
Epoch 15/15 accuracy: 0.6221 val_accuracy: 0.6285~66% vs ~61%Conv2D extracts spatial features via learned filters; MaxPooling2D downsamples; Flatten bridges to Dense layersCNN can have 100K+ parameters, architecture choices (strides, filters) directly control model sizecategorical_crossentropy pairs with one-hot encoded labels, versus sparse_categorical_crossentropy for integer labelsCNN on CIFAR-10 plateaus around 55-61% validation accuracy, a starting point, not a ceilingConv/MaxPool stages) and augmentation are the two levers explored to push accuracy higherMahmoud Abas|Building a CNN to classify images in the CIFAR-10 Dataset