Skip to content

Instantly share code, notes, and snippets.

@meetmakwana7396
Created January 13, 2025 07:03
Show Gist options
  • Select an option

  • Save meetmakwana7396/d45252f45db1fe74bc4ae4966cec1f3d to your computer and use it in GitHub Desktop.

Select an option

Save meetmakwana7396/d45252f45db1fe74bc4ae4966cec1f3d to your computer and use it in GitHub Desktop.

How Convolutional Neural Network (CNN) Works?

Convolutional Neural Networks (CNNs) are a type of deep learning model widely used for image and video processing tasks, such as image classification, object detection, and facial recognition. CNNs are particularly well-suited for tasks involving spatial data because they can automatically capture spatial hierarchies of features (edges, textures, shapes, etc.) through layers of convolutions.

Here’s a breakdown of how CNNs work:


1. Input Layer

  • The input to a CNN is usually an image, represented as a tensor (a multi-dimensional array) with dimensions corresponding to height, width, and the number of color channels (e.g., 3 for RGB images).
  • For example, a 224x224 RGB image would be represented as a tensor of shape (224, 224, 3).

2. Convolutional Layers

  • Purpose: Extract spatial features like edges, corners, and textures by applying filters (or kernels).
  • How It Works:
    • A filter is a small matrix of weights (e.g., 3x3 or 5x5) that slides over the input image and performs element-wise multiplication with the image pixels it overlaps. The result is summed to produce a single value.
    • This process is repeated across the entire image, producing a feature map (or activation map).
  • Parameters:
    • Stride: Determines the step size of the filter movement. Larger strides result in smaller feature maps.
    • Padding: Adds extra pixels around the input image to control the output size. It can be:
      • Valid padding: No padding; the output size shrinks.
      • Same padding: Pads to maintain the same output size as input.

3. Activation Function (ReLU)

  • Purpose: Introduce non-linearity into the model, as most real-world data is non-linear.
  • How It Works: Applies the Rectified Linear Unit (ReLU) function, which replaces all negative values in the feature map with zero.

4. Pooling Layers

  • Purpose: Reduce the spatial dimensions of feature maps to decrease computation and prevent overfitting.
  • Types:
    • Max Pooling: Takes the maximum value from a patch of the feature map (e.g., a 2x2 region).
    • Average Pooling: Takes the average of the values in a patch.
  • Pooling layers retain the most important features while discarding less relevant details.

5. Fully Connected (Dense) Layers

  • Purpose: Map the extracted spatial features to output categories (e.g., dog, cat, etc.).
  • How It Works: Flatten the feature maps into a 1D vector and pass them through one or more fully connected layers. Each neuron in these layers is connected to every neuron in the previous layer.

6. Output Layer

  • Purpose: Generate predictions.
  • How It Works: Typically, a softmax activation function is applied in the output layer to convert raw scores into probabilities for each class.

Key Concepts

  1. Feature Hierarchy:

    • Early layers detect basic features like edges.
    • Middle layers detect complex patterns like textures and shapes.
    • Deeper layers detect high-level concepts like objects.
  2. Weight Sharing:

    • Filters are shared across the input image, reducing the number of parameters compared to fully connected networks.
  3. Training Process:

    • CNNs are trained using backpropagation and gradient descent. The weights of filters and neurons are updated to minimize a loss function (e.g., cross-entropy for classification tasks).

Example Workflow

  1. Input: A 32x32 RGB image.
  2. Convolution + ReLU: Produces multiple feature maps, each highlighting specific patterns.
  3. Pooling: Reduces feature map dimensions (e.g., 32x32 → 16x16).
  4. Repeat Convolution + Pooling: Extracts deeper features.
  5. Fully Connected Layer: Flattens and classifies the features into categories.

This structured approach allows CNNs to learn and recognize patterns in data hierarchically, making them powerful for visual and spatial tasks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment