Build convolutional networks as repeated blocks arranged in a feature hierarchy. Reuse modular layer patterns, increase filters as spatial maps shrink, and prefer deep, narrow stacks over broad, shallow ones. section 9.1
Use these practical rules when assembling a ConvNet:
- Group multiple convolution layers with a max-pooling layer into repeated blocks. section 9.5
- Increase the filter count as feature-map resolution decreases. section 9.1
- Add residual connections around blocks to make deeper networks easier to train. section 9.5
- Consider batch normalization after convolutions and SeparableConv2D for greater parameter efficiency. section 9.5
A compact Xception-like classifier
A dogs-versus-cats model repeats blocks with filter sizes 32, 64, 128, 256, and 512. Each block uses normalization, ReLU activation, two separable convolutions, max pooling, and a projected residual branch, followed by global average pooling and dropout. It uses 721,857 trainable parameters and reaches 90.8% test accuracy, compared with 83.9% for the earlier model. section 9.5
import keras inputs = keras.Input(shape=(180, 180, 3)) x = layers.Rescaling(1.0 / 255)(inputs) #1 x = layers.Conv2D(filters=32, kernel_size=5, use_bias=False)(x) #2 for size in [32, 64, 128, 256, 512]: #3 residual = x x = layers.BatchNormalization()(x) x = layers.Activation("relu")(x) x = layers.SeparableConv2D(size, 3, padding="same", use_bias=False)(x) x = layers.BatchNormalization()(x) x = layers.Activation("relu")(x) x = layers.SeparableConv2D(size, 3, padding="same", use_bias=False)(x) x = layers.MaxPooling2D(3, strides=2, padding="same")(x) residual = layers.Conv2D( size, 1, strides=2, padding="same", use_bias=False )(residual) x = layers.add([x, residual]) x = layers.GlobalAveragePooling2D()(x) #4 x = layers.Dropout(0.5)(x) #5 outputs = layers.Dense(1, activation="sigmoid")(x) model = keras.Model(inputs=inputs, outputs=outputs)
Inspect the complete Keras block pattern, including filter progression, separable convolutions, residual projection, global pooling, and dropout. section 9.5
