0% found this document useful (0 votes)
15 views11 pages

CNN

The document outlines the implementation of a Convolutional Neural Network (CNN) from scratch using Python, including functions for 2D convolution, ReLU activation, and max pooling. It demonstrates the processing of an 8x8 grayscale image through various convolutional filters, followed by activation and pooling layers, and culminates in a fully connected layer that outputs class probabilities. Visualizations are provided at each step to illustrate the transformations of the image and feature maps.

Uploaded by

Vansh negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views11 pages

CNN

The document outlines the implementation of a Convolutional Neural Network (CNN) from scratch using Python, including functions for 2D convolution, ReLU activation, and max pooling. It demonstrates the processing of an 8x8 grayscale image through various convolutional filters, followed by activation and pooling layers, and culminates in a fully connected layer that outputs class probabilities. Visualizations are provided at each step to illustrate the transformations of the image and feature maps.

Uploaded by

Vansh negi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

import numpy as np

import matplotlib.pyplot as plt

# Part 1: Basic Convolution Operation from Scratch

def convolution2d(image, kernel, padding=0, stride=1):

"""

Apply a 2D convolution operation using a kernel on an image

Parameters:

image (numpy.ndarray): Input image (2D array)

kernel (numpy.ndarray): Convolution kernel/filter (2D array)

padding (int): Number of zero padding layers

stride (int): Step size when sliding the kernel

Returns:

numpy.ndarray: Resulting feature map after convolution

"""

# Get dimensions

image_height, image_width = image.shape

kernel_height, kernel_width = kernel.shape

# Apply padding if needed

if padding > 0:

padded_image = np.zeros((image_height + 2 * padding, image_width + 2 * padding))

padded_image[padding:-padding, padding:-padding] = image

image = padded_image

image_height, image_width = image.shape

# Calculate output dimensions

output_height = (image_height - kernel_height) // stride + 1

output_width = (image_width - kernel_width) // stride + 1


# Initialize output feature map

output = np.zeros((output_height, output_width))

# Perform convolution

for y in range(0, output_height):

for x in range(0, output_width):

# Calculate window position with stride

y_pos = y * stride

x_pos = x * stride

# Extract the current window from the image

window = image[y_pos:y_pos+kernel_height, x_pos:x_pos+kernel_width]

# Perform element-wise multiplication and sum

output[y, x] = np.sum(window * kernel)

return output

# ReLU activation function

def relu(x):

"""Apply ReLU activation function"""

return np.maximum(0, x)

# Max pooling operation

def max_pooling(feature_map, pool_size=2, stride=2):

"""

Apply max pooling to the feature map

Parameters:

feature_map (numpy.ndarray): Input feature map


pool_size (int): Size of the pooling window

stride (int): Step size when sliding the window

Returns:

numpy.ndarray: Pooled feature map

"""

# Get dimensions

height, width = feature_map.shape

# Calculate output dimensions

output_height = (height - pool_size) // stride + 1

output_width = (width - pool_size) // stride + 1

# Initialize output

output = np.zeros((output_height, output_width))

# Perform max pooling

for y in range(output_height):

for x in range(output_width):

# Calculate window position with stride

y_pos = y * stride

x_pos = x * stride

# Extract window

window = feature_map[y_pos:y_pos+pool_size, x_pos:x_pos+pool_size]

# Take the maximum value

output[y, x] = np.max(window)

return output
# Create a simple 8x8 image (grayscale)

image = np.zeros((8, 8))

image[2:6, 2:6] = 1 # Create a white square in the middle

# Create some common filters/kernels

# Horizontal edge detection

horizontal_edge_kernel = np.array([

[-1, -1, -1],

[0, 0, 0],

[1, 1, 1]

])

# Vertical edge detection

vertical_edge_kernel = np.array([

[-1, 0, 1],

[-1, 0, 1],

[-1, 0, 1]

])

# Sobel edge detection (diagonal)

diagonal_edge_kernel = np.array([

[-1, -1, 0],

[-1, 0, 1],

[0, 1, 1]

])

# Apply convolutions

horizontal_feature_map = convolution2d(image, horizontal_edge_kernel)

vertical_feature_map = convolution2d(image, vertical_edge_kernel)

diagonal_feature_map = convolution2d(image, diagonal_edge_kernel)


# Apply ReLU activation (this simulates non-linearity in CNN)

horizontal_feature_map_relu = relu(horizontal_feature_map)

vertical_feature_map_relu = relu(vertical_feature_map)

diagonal_feature_map_relu = relu(diagonal_feature_map)

# Apply max pooling (this simulates pooling layer in CNN)

horizontal_feature_map_pooled = max_pooling(horizontal_feature_map_relu)

vertical_feature_map_pooled = max_pooling(vertical_feature_map_relu)

diagonal_feature_map_pooled = max_pooling(diagonal_feature_map_relu)

# Visualize the original image and kernels

plt.figure(figsize=(12, 4))

plt.subplot(1, 4, 1)

plt.imshow(image, cmap='gray')

plt.title('Original Image')

plt.axis('off')

plt.subplot(1, 4, 2)

plt.imshow(horizontal_edge_kernel, cmap='viridis')

plt.title('Horizontal Edge Kernel')

plt.axis('off')

plt.subplot(1, 4, 3)

plt.imshow(vertical_edge_kernel, cmap='viridis')

plt.title('Vertical Edge Kernel')

plt.axis('off')

plt.subplot(1, 4, 4)

plt.imshow(diagonal_edge_kernel, cmap='viridis')

plt.title('Diagonal Edge Kernel')


plt.axis('off')

plt.tight_layout()

plt.show()

# Visualize the feature maps after convolution

plt.figure(figsize=(12, 4))

plt.subplot(1, 3, 1)

plt.imshow(horizontal_feature_map, cmap='gray')

plt.title('Horizontal Edge Detection')

plt.axis('off')

plt.subplot(1, 3, 2)

plt.imshow(vertical_feature_map, cmap='gray')

plt.title('Vertical Edge Detection')

plt.axis('off')

plt.subplot(1, 3, 3)

plt.imshow(diagonal_feature_map, cmap='gray')

plt.title('Diagonal Edge Detection')

plt.axis('off')

plt.tight_layout()

plt.show()

# Visualize the feature maps after ReLU

plt.figure(figsize=(12, 4))

plt.subplot(1, 3, 1)

plt.imshow(horizontal_feature_map_relu, cmap='gray')
plt.title('Horizontal Feature Map (ReLU)')

plt.axis('off')

plt.subplot(1, 3, 2)

plt.imshow(vertical_feature_map_relu, cmap='gray')

plt.title('Vertical Feature Map (ReLU)')

plt.axis('off')

plt.subplot(1, 3, 3)

plt.imshow(diagonal_feature_map_relu, cmap='gray')

plt.title('Diagonal Feature Map (ReLU)')

plt.axis('off')

plt.tight_layout()

plt.show()

# Visualize the feature maps after Pooling

plt.figure(figsize=(12, 4))

plt.subplot(1, 3, 1)

plt.imshow(horizontal_feature_map_pooled, cmap='gray')

plt.title('Horizontal Feature Map (Pooled)')

plt.axis('off')

plt.subplot(1, 3, 2)

plt.imshow(vertical_feature_map_pooled, cmap='gray')

plt.title('Vertical Feature Map (Pooled)')

plt.axis('off')

plt.subplot(1, 3, 3)

plt.imshow(diagonal_feature_map_pooled, cmap='gray')
plt.title('Diagonal Feature Map (Pooled)')

plt.axis('off')

plt.tight_layout()

plt.show()

# Simulate a full CNN pipeline with multiple layers

# Let's create a second layer of filters that operate on the pooled feature maps

# Create a second layer filter

second_layer_filter = np.array([

[1, 0],

[0, 1]

])

# Apply second layer of convolution to the pooled feature maps

second_layer_h = convolution2d(horizontal_feature_map_pooled, second_layer_filter)

second_layer_v = convolution2d(vertical_feature_map_pooled, second_layer_filter)

second_layer_d = convolution2d(diagonal_feature_map_pooled, second_layer_filter)

# Apply ReLU to the second layer

second_layer_h_relu = relu(second_layer_h)

second_layer_v_relu = relu(second_layer_v)

second_layer_d_relu = relu(second_layer_d)

# Visualize the second layer feature maps

plt.figure(figsize=(12, 4))

plt.subplot(1, 3, 1)

plt.imshow(second_layer_h_relu, cmap='gray')

plt.title('Second Layer (from Horizontal)')


plt.axis('off')

plt.subplot(1, 3, 2)

plt.imshow(second_layer_v_relu, cmap='gray')

plt.title('Second Layer (from Vertical)')

plt.axis('off')

plt.subplot(1, 3, 3)

plt.imshow(second_layer_d_relu, cmap='gray')

plt.title('Second Layer (from Diagonal)')

plt.axis('off')

plt.tight_layout()

plt.show()

# Simulate feature combination (what happens in fully connected layers)

# We'll just flatten and combine our feature maps

flattened_features = np.concatenate([

second_layer_h_relu.flatten(),

second_layer_v_relu.flatten(),

second_layer_d_relu.flatten()

])

# Visualize the flattened features (what would be fed to fully connected layers)

plt.figure(figsize=(10, 4))

plt.bar(range(len(flattened_features)), flattened_features)

plt.title('Flattened Features (Input to Fully Connected Layer)')

plt.xlabel('Feature Index')

plt.ylabel('Feature Value')

plt.tight_layout()

plt.show()
# Simple demonstration of how a fully connected layer would process these features

# Define some random weights (in a real CNN these would be learned)

np.random.seed(42) # For reproducibility

num_classes = 10 # Simulating a 10-class classification problem

fc_weights = np.random.randn(len(flattened_features), num_classes) * 0.1

# Forward pass through the fully connected layer

fc_output = np.dot(flattened_features, fc_weights)

# Apply softmax to get class probabilities

def softmax(x):

"""Apply softmax function to get probabilities"""

exp_x = np.exp(x - np.max(x)) # Subtract max for numerical stability

return exp_x / exp_x.sum()

class_probabilities = softmax(fc_output)

# Visualize the output probabilities

plt.figure(figsize=(8, 4))

plt.bar(range(num_classes), class_probabilities)

plt.title('CNN Output: Class Probabilities')

plt.xlabel('Class')

plt.ylabel('Probability')

plt.xticks(range(num_classes))

plt.tight_layout()

plt.show()

# Print explanation of the CNN flow

print("CNN Flow Demonstration:")

print("1. Input image (8x8) with a central square")


print("2. Three convolution filters detect edges in different directions")

print("3. ReLU activation introduces non-linearity (keeping only positive values)")

print("4. Max pooling reduces spatial dimensions (downsampling)")

print("5. Second convolutional layer extracts higher-level features")

print("6. Features are flattened and fed to a fully connected layer")

print("7. Output layer gives class probabilities")

print("\nThis demonstrates the key operations in a CNN: convolution, activation, pooling,")

print("and fully connected layers, showing how a CNN processes an image step by step.")

You might also like