CNN
Keras
Python3
Neural Networks
Machine Learning

Optimizing the Architecture of a CNN Using Keras in Python3

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Optimizing a CNN architecture in Keras is mostly about making disciplined tradeoffs, not randomly adding layers. Start with a small baseline that trains correctly, then adjust depth, filter count, regularization, and training settings one change at a time while watching validation behavior.

Build a Clean Baseline First

Before tuning, make sure the input shape, label format, and normalization are correct. A simple baseline gives you something trustworthy to improve:

python
1from tensorflow import keras
2from tensorflow.keras import layers
3
4def build_model():
5    model = keras.Sequential([
6        layers.Input(shape=(64, 64, 3)),
7        layers.Rescaling(1.0 / 255),
8        layers.Conv2D(32, 3, activation="relu"),
9        layers.MaxPooling2D(),
10        layers.Conv2D(64, 3, activation="relu"),
11        layers.MaxPooling2D(),
12        layers.Flatten(),
13        layers.Dense(128, activation="relu"),
14        layers.Dropout(0.3),
15        layers.Dense(10, activation="softmax"),
16    ])
17
18    model.compile(
19        optimizer="adam",
20        loss="sparse_categorical_crossentropy",
21        metrics=["accuracy"],
22    )
23    return model

If this model does not train sensibly, tuning the architecture will just hide the real problem.

Tune Architecture in Meaningful Directions

Once the baseline is stable, adjust parameters that actually change model capacity:

  • number of convolution blocks
  • filters per block
  • kernel sizes
  • dense layer width
  • dropout or batch normalization

A common improvement is to deepen gradually instead of making one giant dense layer:

python
1def build_larger_model():
2    model = keras.Sequential([
3        layers.Input(shape=(64, 64, 3)),
4        layers.Rescaling(1.0 / 255),
5        layers.Conv2D(32, 3, padding="same", activation="relu"),
6        layers.Conv2D(32, 3, activation="relu"),
7        layers.MaxPooling2D(),
8        layers.Conv2D(64, 3, padding="same", activation="relu"),
9        layers.Conv2D(64, 3, activation="relu"),
10        layers.MaxPooling2D(),
11        layers.Conv2D(128, 3, activation="relu"),
12        layers.GlobalAveragePooling2D(),
13        layers.Dense(128, activation="relu"),
14        layers.Dropout(0.4),
15        layers.Dense(10, activation="softmax"),
16    ])
17    return model

The goal is not "more layers at any cost." The goal is a model whose capacity fits the dataset.

Use Validation to Detect the Real Problem

Watch training and validation curves together. If training accuracy rises while validation stalls or drops, the model is probably overfitting. If both stay low, the model may be too small, the learning rate may be wrong, or the data pipeline may be noisy.

Callbacks make this easier:

python
1callbacks = [
2    keras.callbacks.EarlyStopping(
3        monitor="val_loss",
4        patience=5,
5        restore_best_weights=True
6    )
7]
8
9model = build_model()
10history = model.fit(
11    train_ds,
12    validation_data=val_ds,
13    epochs=30,
14    callbacks=callbacks
15)

Early stopping does not optimize the architecture by itself, but it prevents you from judging models based on overtrained checkpoints.

Tune Training Alongside Architecture

Architecture and training are tightly coupled. A model that looks weak may simply need a different learning rate, augmentation policy, or batch size. That is why serious optimization usually includes both model and training configuration.

A practical workflow is:

  1. fix preprocessing and data splits
  2. train a small baseline
  3. adjust one architectural dimension at a time
  4. compare on the same validation split
  5. only then consider more automated search

If you want more systematic search, tools such as KerasTuner can explore combinations, but they still depend on a well-formed search space and a reliable validation setup.

Common Pitfalls

  • Tuning architecture before confirming that preprocessing and labels are correct. Data problems can make every architecture look bad.
  • Increasing model size whenever results disappoint. Bigger CNNs often just overfit faster.
  • Judging models only by training accuracy instead of validation behavior.
  • Changing many variables at once, which makes it impossible to know what actually helped.
  • Ignoring runtime cost. A slightly more accurate model can still be the wrong choice if it is far slower or far harder to deploy.

Summary

  • Start with a small CNN that trains correctly before optimizing anything.
  • Adjust depth, filters, pooling, and regularization in controlled steps.
  • Use validation metrics and callbacks to distinguish underfitting from overfitting.
  • Tune training settings alongside the architecture instead of treating them as separate worlds.
  • Optimize for the best tradeoff between accuracy, stability, and deployment cost.

Course illustration
Course illustration

All Rights Reserved.