0
0
Computer Visionml~5 mins

CNN architecture review in Computer Vision

Choose your learning style9 modes available
Introduction

A CNN (Convolutional Neural Network) helps computers see and understand images by looking at small parts step-by-step.

When you want to recognize objects in photos, like cats or cars.
When you need to find patterns in medical images, like X-rays.
When you want to sort pictures by their content automatically.
When you want to detect faces or handwriting in images.
When you want to improve image quality or remove noise.
Syntax
Computer Vision
model = Sequential([
    Conv2D(filters, kernel_size, activation='relu', input_shape=(height, width, channels)),
    MaxPooling2D(pool_size=pool_size),
    Flatten(),
    Dense(units, activation='relu'),
    Dense(num_classes, activation='softmax')
])

Conv2D looks at small image parts to find features.

MaxPooling2D shrinks the image to keep important info and reduce size.

Examples
First layer looks at 3x3 parts of a 28x28 grayscale image with 32 filters.
Computer Vision
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))
Reduces image size by half in height and width by taking the max value in 2x2 blocks.
Computer Vision
MaxPooling2D((2, 2))
Fully connected layer with 128 neurons to learn complex patterns.
Computer Vision
Dense(128, activation='relu')
Output layer with 10 neurons for 10 classes, giving probabilities for each class.
Computer Vision
Dense(10, activation='softmax')
Sample Model

This code builds a small CNN to classify 28x28 grayscale images into 10 classes. It trains on random data for 1 round and shows predicted classes for 5 images.

Computer Vision
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

# Build a simple CNN model
model = Sequential([
    Conv2D(16, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    MaxPooling2D((2, 2)),
    Flatten(),
    Dense(32, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Create dummy data: 100 grayscale images 28x28 and labels
import numpy as np
x_train = np.random.random((100, 28, 28, 1))
y_train = np.random.randint(0, 10, 100)

# Train the model for 1 epoch
history = model.fit(x_train, y_train, epochs=1, batch_size=10, verbose=2)

# Make predictions on first 5 images
predictions = model.predict(x_train[:5])
predicted_classes = predictions.argmax(axis=1)

print('Predicted classes for first 5 images:', predicted_classes)
OutputSuccess
Important Notes

Start with small filters like 3x3 to capture details.

Pooling layers help reduce image size and computation.

Use activation functions like ReLU to add non-linearity.

Summary

CNNs look at images piece by piece to find patterns.

They use layers like Conv2D, Pooling, Flatten, and Dense.

They are great for tasks like image recognition and classification.