Study interactive :: Progress tools open in the Study Hub reader.

Computer Vision Quick Reference Guide

Quick reference for CNN architectures, code snippets, and best practices.

Table of Contents


Image Fundamentals

Image Structure

Common Image Sizes

Convolution Basics

Convolution Operation

Output Size Formula

Output = (Input - Filter + 2×Padding) / Stride + 1

Edge Detection Filters

# Sobel (vertical edges)
sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])

# Canny (OpenCV)
edges = cv2.Canny(image, threshold1=50, threshold2=150)

Pooling

CNN Architecture

Architecture Pattern

Input → Conv → BN → ReLU → Pool → Conv → BN → ReLU → Pool → FC → Output

Key Components


Code Snippets

Basic CNN (Keras)

model = keras.Sequential([
    layers.Conv2D(32, 3, activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D(2),
    layers.Conv2D(64, 3, activation='relu'),
    layers.MaxPooling2D(2),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

Basic CNN (PyTorch)

class CNN(nn.Module):
    def __init__(self):
        super().__init__()
        # padding=1 keeps spatial size before pooling (28→14→7 on MNIST)
        self.c>1, 32, 3, padding=1)
        self.pool = nn.MaxPool2d(2)
        self.c>32, 64, 3, padding=1)
        self.fc = nn.Linear(64 * 7 * 7, 10)
    
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 64 * 7 * 7)
        x = self.fc(x)
        return x

Transfer Learning

# Keras
base_model = VGG16(weights='imagenet', include_top=False)
base_model.trainable = False
model = keras.Sequential([base_model, layers.GlobalAveragePooling2D(), layers.Dense(10, activation='softmax')])

Architecture Comparison

Architecture Year Parameters Top-1 Acc Key Innovation Use Case
LeNet-5 1998 60K - First practical CNN Digit recognition
AlexNet 2012 60M 63% ReLU, Dropout, GPU training ImageNet breakthrough
VGG16 2014 138M 71% Small 3×3 filters, deep Good baseline
ResNet50 2015 25M 76% Residual connections General purpose
MobileNetV2 2018 3.4M 71% Depthwise separable conv Mobile devices
EfficientNet 2019 5.3M 77% Compound scaling Best efficiency

Key Architecture Details

LeNet-5: 2 conv + 2 pooling + 3 FC layers, tanh activation

AlexNet: 5 conv + 3 FC, ReLU, dropout, data augmentation

VGG16: 13 conv + 3 FC, all 3×3 filters, very deep

ResNet: Residual/skip connections ease deep training (degradation); enables 100+ layers in practice


Common Issues & Solutions

Issue 1: Overfitting

Solution: Data augmentation, dropout, early stopping

Issue 2: Slow Training

Solution: Use GPU, reduce image size, efficient architectures

Issue 3: Low Accuracy

Solution: Transfer learning, more data, better architecture


Object Detection

YOLO (Quick Start)

from ultralytics import YOLO

# Load model
model = YOLO('yolov8n.pt')

# Detect
results = model('image.jpg')

# Train
model.train(data='dataset.yaml', epochs=100)

Detection vs Classification

Segmentation

Types

U-Net Architecture

# Encoder-Decoder with skip connections
# Encoder: Downsample (VGG16, ResNet)
# Decoder: Upsample + concatenate skip connections
# Output: Pixel-wise classification

Training Optimization

Callbacks

callbacks = [
    EarlyStopping(patience=10, restore_best_weights=True),
    ReduceLROnPlateau(patience=5, factor=0.5),
    ModelCheckpoint('best_model.h5', save_best_only=True)
]

Batch Normalization

# Add after Conv2D, before activation
layers.Conv2D(32, 3),
layers.BatchNormalization(),
layers.ReLU()

Data Augmentation

datagen = ImageDataGenerator(
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True,
    zoom_range=0.2
)

Best Practices Checklist


Quick Tips

  1. Start with transfer learning - Pre-trained models work great
  2. Use data augmentation - Essential for preventing overfitting
  3. Batch normalization - Stabilizes training in deep networks
  4. Monitor learning curves - Diagnose overfitting/underfitting
  5. Use appropriate architecture - MobileNet for mobile, ResNet for accuracy
  6. Normalize images - Scale to 0-1 or use ImageNet stats
  7. Use callbacks - Early stopping saves time
  8. Try different optimizers - Adam is good default
  9. Visualize feature maps - Understand what model learns
  10. Test on diverse data - Ensure generalization

Try next: Freeze a pretrained backbone, train a linear head, then unfreeze the last block.