0
0
TensorFlowml~5 mins

CNN architecture for image classification in TensorFlow

Choose your learning style9 modes available
Introduction

A CNN helps a computer learn to recognize pictures by looking at small parts step-by-step.

When you want a computer to tell if a photo has a cat or a dog.
When sorting pictures into groups like cars, trees, or people.
When building apps that read handwritten numbers or letters.
When you want to find objects in photos automatically.
When improving photo search by understanding image content.
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
This layer looks for 16 patterns using 5x5 squares on 28x28 grayscale images.
TensorFlow
Conv2D(filters=16, kernel_size=(5,5), activation='relu', input_shape=(28,28,1))
This layer reduces the image size by taking the biggest value in each 3x3 block.
TensorFlow
MaxPooling2D(pool_size=(3,3))
This layer outputs probabilities for 10 classes, like digits 0-9.
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}')
OutputSuccess
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.