Loading the lesson...
In the print dialog, choose "Save as PDF" as the destination.
Convolutional layers, a first CNN, and boosting it with data augmentation
60,000 32x32 color images across 10 classes, 6,000 images per class50,000 training images, 10,000 test imagesfrom keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print('x_train shape:', x_train.shape) # (50000, 32, 32, 3)
# Each image is a 32x32x3 array (height, width, RGB channels)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255 # scale pixel values to [0, 1]
x_test /= 255num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
# Instead of an integer 0-9, each label becomes a one-hot vector
y_train[444]
# array([0., 0., 0., 0., 0., 0., 0., 0., 0., 1.])categorical_crossentropy losskeras.layers.Conv2D(filters, kernel_size, strides=(1, 1),
padding='valid', activation=None, ...)| Parameter | Meaning |
|---|---|
filters | Number of filters applied per location, the depth of the output |
kernel_size | (x, y) tuple giving the height/width of the convolution kernel |
strides | (x, y) tuple giving the step in each dimension, default (1, 1) |
input_shape | Required only for the first layer in the model |
kernel_size and strideskeras.layers.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid')| Parameter | Meaning |
|---|---|
pool_size | The (x, y) grid size to pool over |
strides | Defaults to pool_size if not specified |
model_1 = Sequential()
# 5x5 convolution, stride 2, 32 filters
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))) # reduces to 3x3x32
model_1.add(Dropout(0.25))
model_1.add(Flatten()) # 3x3x32 -> 288x1
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'))Conv2D
5x5, stride 2
Conv2D
5x5, stride 2
MaxPool
2x2
Flatten
Dense
512
Softmax
10 classes
model_1.summary()| Layer | Output Shape | Param # |
|---|---|---|
| conv2d | (None, 16, 16, 32) | 2,432 |
| conv2d_1 | (None, 6, 6, 32) | 25,632 |
| max_pooling2d | (None, 3, 3, 32) | 0 |
| flatten | (None, 288) | 0 |
| dense | (None, 512) | 147,968 |
| dense_1 | (None, 10) | 5,130 |
model_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)57-58%, validation accuracy fluctuates around ~55-60% over 15 epochs, a shallow CNN has limited capacityExercise
Conv2D
Conv2D
MaxPool
Conv2D
Conv2D
MaxPool
Flatten
Dense
Softmax
1 for all convolutional layers5 epochs and compare training time, loss, and accuracy on both training and validation setsfrom tensorflow.keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
rotation_range=20, # random rotation, degrees
width_shift_range=0.1, # random horizontal shift (fraction of width)
height_shift_range=0.1, # random vertical shift (fraction of height)
shear_range=0.1, # shear intensity
zoom_range=0.1, # random zoom
fill_mode='nearest' # how to fill new pixels
)model_1.fit(
datagen.flow(x_train, y_train, batch_size=batch_size),
epochs=15,
validation_data=(x_test, y_test)
)62% training accuracy and ~63% validation accuracy, a meaningful improvement over the non-augmented run, though each epoch takes longer10-class, 32x32 color image classification benchmark (50,000 train / 10,000 test images)Conv2D extracts spatial features via filters and strides; MaxPooling2D downsamples; Flatten bridges convolutional output into Dense layers55-60% validation accuracy on CIFAR-10Mahmoud Abas|Building a CNN to classify images in the CIFAR-10 Dataset