Implementing A Convolutional Neural Network CNN 1718899610
Implementing A Convolutional Neural Network CNN 1718899610
1 ANSHUMAN JHA
Implementing a Custom CNN (Convolutional Neural Network)
for Image Classification from Scratch in Python
Table of Contents
1. Introduction to CNN (Convolutional Neural Network)
2. The Structure of a Convolutional Neural Network
3. Implementation in Python
a. Image Preprocessing
b. Building the Convolutional Layer
c. Implementing the Pooling Layer
d. Creating the Fully Connected Layer
e. Putting It All Together
f. Training and Evaluation
4. Conclusion
2 ANSHUMAN JHA
Implementing a Custom CNN (Convolutional Neural Network)
for Image Classification from Scratch in Python
1. Input Layer:
- Input image of size 64x64x3.
6. Flatten Layer:
- Flatten the output of the second pooling layer into a 1D array.
8. Output Layer:
- Softmax activation function to produce a probability distribution over the 10 classes.
3 ANSHUMAN JHA
Implementing a Custom CNN (Convolutional Neural Network)
for Image Classification from Scratch in Python
4 ANSHUMAN JHA
Implementing a Custom CNN (Convolutional Neural Network)
for Image Classification from Scratch in Python
3. Implementation in Python
Let's implement a simple CNN (Convolutional Neural Network) in Python to classify image.
import numpy as np
import cv2
import os
# Example usage
images, labels = load_images_from_folder('path_to_images', 64)
In this example, cv2 is used to read and resize images, while the pixel values are normalized to the range [0, 1].
5 ANSHUMAN JHA
Implementing a Custom CNN (Convolutional Neural Network)
for Image Classification from Scratch in Python
for h in range(n_H):
for w in range(n_W):
for c in range(n_C):
vert_start = h * stride
vert_end = vert_start + f
horiz_start = w * stride
horiz_end = horiz_start + f
return Z
# Example usage
X = np.random.randn(64, 64, 3) # Example input
W = np.random.randn(3, 3, 3, 8) # 8 filters of size 3x3x3
b = np.random.randn(8)
Z = conv2d(X, W, b)
This function performs a 2D convolution operation with the specified filters, stride, and padding. The input X is
padded to ensure the output dimensions match the desired size.
6 ANSHUMAN JHA
Implementing a Custom CNN (Convolutional Neural Network)
for Image Classification from Scratch in Python
for h in range(n_H):
for w in range(n_W):
for c in range(n_C):
vert_start = h * stride
vert_end = vert_start + f
horiz_start = w * stride
horiz_end = horiz_start + f
return Z
# Example usage
X = np.random.randn(64, 64, 8) # Example input
Z = max_pooling(X)
This function performs max-pooling, which extracts the maximum value from each patch of the input.
The fully connected (FC) layer is a standard neural network layer where each neuron is connected to every
neuron in the previous layer. This layer combines features learned by convolutional and pooling layers to make
predictions.
This function performs a linear transformation of the input X using weights W and biases b.
def fully_connected(X, W, b):
Z = np.dot(W, X) + b
return Z
# Example usage
X = np.random.randn(64 * 64 * 8) # Flattened input
W = np.random.randn(10, 64 * 64 * 8) # 10 output classes
b = np.random.randn(10)
Z = fully_connected(X, W, b)
7 ANSHUMAN JHA
Implementing a Custom CNN (Convolutional Neural Network)
for Image Classification from Scratch in Python
F = P2.flatten()
Z3 = fully_connected(F, W3, b3)
return Z3
# Initialize parameters
W1 = np.random.randn(3, 3, 3, 8)
b1 = np.random.randn(8)
W2 = np.random.randn(3, 3, 8, 16)
b2 = np.random.randn(16)
W3 = np.random.randn(10, 16 * 16 * 16)
b3 = np.random.randn(10)
# Example usage
X = np.random.randn(64, 64, 3) # Example input
Z3 = cnn_forward(X, parameters)
In this example, we define a simple CNN model with two convolutional layers followed by ReLU activation
and max-pooling, and a final fully connected layer for classification.
8 ANSHUMAN JHA
Implementing a Custom CNN (Convolutional Neural Network)
for Image Classification from Scratch in Python
def softmax(Z):
expZ = np.exp(Z - np.max(Z))
return expZ / expZ.sum(axis=0, keepdims=True)
# Example usage
# Example target (one-hot encoded)
Y = np.eye(10)[np.random.choice(10, 1)].T
Y_hat = softmax(Z3) # Applying softmax to the output of the CNN
loss = compute_loss(Y_hat, Y)
This code computes the cross-entropy loss, which is commonly used for classification tasks.
7. Conclusion
In this post, we built a custom CNN from scratch in Python, implementing each layer manually. This process
helps in understanding the inner workings of CNNs and provides a foundation for using higher-level libraries
for more complex tasks
By following this guide, you should have a clear understanding of how to build and understand a CNN from
scratch, providing a solid foundation for further learning and development in the field of deep learning and
computer vision.
9 ANSHUMAN JHA
Implementing a Custom CNN (Convolutional Neural Network)
for Image Classification from Scratch in Python
10 ANSHUMAN JHA