A CNN helps a computer learn to recognize pictures by looking at small parts step-by-step.
CNN architecture for image classification in TensorFlow
Introduction
Syntax
TensorFlow
from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense model = Sequential([ Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(height, width, channels)), MaxPooling2D(pool_size=(2,2)), Conv2D(filters=64, kernel_size=(3,3), activation='relu'), MaxPooling2D(pool_size=(2,2)), Flatten(), Dense(units=128, activation='relu'), Dense(units=num_classes, activation='softmax') ])
Conv2D finds patterns like edges in small image parts.
MaxPooling2D shrinks the image to keep important info and reduce size.
Examples
TensorFlow
Conv2D(filters=16, kernel_size=(5,5), activation='relu', input_shape=(28,28,1))
TensorFlow
MaxPooling2D(pool_size=(3,3))
TensorFlow
Dense(units=10, activation='softmax')
Sample Model
This program trains a simple CNN on handwritten digit images and shows how well it learned.
TensorFlow
import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense # Load example data: MNIST handwritten digits (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # Normalize images to 0-1 and add channel dimension x_train = x_train.astype('float32') / 255.0 x_test = x_test.astype('float32') / 255.0 x_train = x_train[..., tf.newaxis] x_test = x_test[..., tf.newaxis] num_classes = 10 # Build CNN model model = Sequential([ Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)), MaxPooling2D((2,2)), Conv2D(64, (3,3), activation='relu'), MaxPooling2D((2,2)), Flatten(), Dense(128, activation='relu'), Dense(num_classes, activation='softmax') ]) # Compile model model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Train model for 1 epoch (for quick demo) history = model.fit(x_train, y_train, epochs=1, batch_size=64, validation_split=0.1) # Evaluate on test data loss, accuracy = model.evaluate(x_test, y_test) print(f'Test loss: {loss:.4f}') print(f'Test accuracy: {accuracy:.4f}')
Important Notes
Use small kernel sizes like (3,3) to capture fine details.
Pooling layers help reduce image size and computation.
Softmax activation in the last layer gives probabilities for each class.
Summary
CNNs look at images in small parts to find patterns.
Layers like Conv2D and MaxPooling2D help the model learn and shrink images.
Dense layers at the end decide the final class based on learned features.
Practice
1. What is the main role of the
Conv2D layer in a CNN for image classification?easy
Solution
Step 1: Understand the function of Conv2D
The Conv2D layer scans small parts of the image to find patterns such as edges and textures.Step 2: Compare with other layers
Pooling layers reduce size, flatten layers reshape data, and dense layers classify. Conv2D focuses on pattern detection.Final Answer:
To detect patterns like edges and shapes in small parts of the image -> Option DQuick Check:
Conv2D = pattern detection [OK]
Hint: Conv2D finds patterns; pooling shrinks images [OK]
Common Mistakes:
- Confusing Conv2D with pooling layers
- Thinking Conv2D flattens the image
- Believing Conv2D decides the final class
2. Which of the following is the correct way to add a MaxPooling2D layer in TensorFlow Keras?
easy
Solution
Step 1: Recall correct TensorFlow layer syntax
The correct class name is MaxPooling2D and it uses the argument pool_size to set the window size.Step 2: Check each option
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)) uses correct class and argument. Others have wrong class names or argument names.Final Answer:
tf.keras.layers.MaxPooling2D(pool_size=(2, 2)) -> Option AQuick Check:
MaxPooling2D with pool_size=(2,2) is correct [OK]
Hint: MaxPooling2D uses pool_size, not pool or size [OK]
Common Mistakes:
- Using wrong layer class names
- Using incorrect argument names like pool or size
- Omitting parentheses or tuple for pool_size
3. What will be the output shape after applying this CNN block on an input image of shape (64, 64, 3)?
model = tf.keras.Sequential([ tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(64,64,3)), tf.keras.layers.MaxPooling2D(pool_size=(2,2)) ]) output_shape = model.output_shape
medium
Solution
Step 1: Calculate Conv2D output shape
Conv2D with kernel size 3x3 and default padding='valid' reduces each spatial dimension by 2: 64 - 3 + 1 = 62, so output shape is (62, 62, 32).Step 2: Calculate MaxPooling2D output shape
MaxPooling2D with pool size 2x2 halves each spatial dimension: 62 / 2 = 31, so output shape is (31, 31, 32).Final Answer:
(None, 31, 31, 32) -> Option CQuick Check:
Conv2D valid padding reduces size; pooling halves it [OK]
Hint: Conv2D valid padding shrinks; pooling halves size [OK]
Common Mistakes:
- Assuming padding='same' by default
- Forgetting spatial dimension reduction by Conv2D
- Misapplying pooling size effect
4. Identify the error in this CNN model code snippet:
model = tf.keras.Sequential([ tf.keras.layers.Conv2D(64, (3,3), activation='relu', input_shape=(28,28,1)), tf.keras.layers.MaxPooling2D(pool_size=2), tf.keras.layers.Dense(10, activation='softmax') ])
medium
Solution
Step 1: Check layer order and compatibility
Conv2D and MaxPooling2D output 3D tensors, but Dense expects 1D input.Step 2: Identify missing layer
A Flatten layer is needed to convert 3D output to 1D before Dense.Final Answer:
Missing Flatten layer before Dense -> Option AQuick Check:
Dense needs 1D input; add Flatten [OK]
Hint: Add Flatten before Dense after Conv/Pooling [OK]
Common Mistakes:
- Skipping Flatten layer before Dense
- Assuming Dense accepts 3D input
- Confusing pool_size tuple requirement
5. You want to build a CNN to classify 100x100 RGB images into 5 classes. Which architecture below is best suited for this task?
hard
Solution
Step 1: Analyze each architecture's suitability
Conv2D(16, (3,3)) -> MaxPooling2D(2,2) -> Conv2D(32, (3,3)) -> MaxPooling2D(2,2) -> Flatten -> Dense(5, softmax) uses multiple Conv2D and MaxPooling layers to extract features and reduce size, then flattens before classification. This is a standard CNN design.Step 2: Identify issues in other options
Dense(128, relu) -> Dense(64, relu) -> Dense(5, softmax) uses only Dense layers, ignoring spatial info. Conv2D(64, (5,5), padding='same') -> Flatten -> Dense(5, softmax) lacks pooling, risking large feature maps. Conv2D(32, (3,3)) -> Conv2D(32, (3,3)) -> Dense(5, softmax) misses Flatten and pooling, causing shape errors.Final Answer:
Conv2D(16, (3,3)) -> MaxPooling2D(2,2) -> Conv2D(32, (3,3)) -> MaxPooling2D(2,2) -> Flatten -> Dense(5, softmax) -> Option BQuick Check:
Multiple Conv+Pooling layers + Flatten + Dense = best CNN [OK]
Hint: Use Conv + Pooling layers before Flatten and Dense [OK]
Common Mistakes:
- Using only Dense layers for images
- Skipping pooling layers
- Missing Flatten before Dense
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
