DEV Community

Cover image for 🧠 CNN from Scratch
Axel
Axel

Posted on

🧠 CNN from Scratch

This project is a simple Convolutional Neural Network (CNN) implemented entirely from scratch using only low-level libraries like NumPy, PIL, and SciPyβ€”no deep learning frameworks (e.g., TensorFlow or PyTorch) are used. It includes image preprocessing, convolution and pooling operations, ReLU and softmax activations, forward/backward propagation, and a fully connected classifier.

πŸ“¦ Releases

Version Latest Stable Test a trained model
0.1.1 βœ… βœ… Test
0.1.0 ❌ βœ… Test

πŸš€ Features

  • Manual image preprocessing (RGB separation, resizing, normalization)
  • Handcrafted convolution and max-pooling operations
  • Fully connected layers (L1, L2, and output)
  • Softmax + Cross-Entropy Loss
  • Mini-batch gradient descent with backpropagation
  • Model saving/loading using pickle
  • Class prediction on new images
  • Realtime training visualization using matplotlib

πŸ–Ό Dataset Structure

Make sure your dataset folder is structured like this:

data/
β”œβ”€β”€ class1/
β”‚   β”œβ”€β”€ image1.png
β”‚   β”œβ”€β”€ image2.png
β”œβ”€β”€ class2/
β”‚   β”œβ”€β”€ image1.png
β”‚   β”œβ”€β”€ image2.png
β”œβ”€β”€ class../
β”‚   β”œβ”€β”€ ..
..
Enter fullscreen mode Exit fullscreen mode

Each subfolder represents a class (e.g., cat, dog), and contains sample images.

To help you get started, we’ve included a starter data folder with example class directories.

πŸ§ͺ How It Works

  1. Image Preprocessing:
  • Each image is resized to a fixed size and normalized.
  • Filters (e.g., sharpening, edge detection) are applied using 2D convolution.
  • ReLU activation and 2Γ—2 max-pooling reduce spatial dimensions.
  1. Feature Vector:
  • Flattened pooled feature maps are fed into fully connected layers.
  1. Feedforward + Softmax:
  • Dense layers compute activations followed by a softmax for classification.
  1. Backpropagation:
  • Gradients are computed layer-by-layer.
  • Weights and biases are updated using basic gradient descent.

πŸ›  Setup

pip install git+https://github.com/77AXEL/CNN-FS.git
Enter fullscreen mode Exit fullscreen mode

βœ… Training

Update and run the training block:

from cnnfs.model import CNN

model = CNN()
model.init(
    image_size=64,
    batch_size=32,
    h1=128,
    h2=64,
    learning_rate=0.001,
    epochs=400,
    dataset_path="data", # Your dataset folder path
    max_image=200, # If not specified, the model will load all images for each class
    filters=[
        [[0, -1, 0], [-1, 5, -1], [0, -1, 0]],
        [[1, 0, -1], [1, 0, -1], [1, 0, -1]],
        [[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]]
    ] # If not specified, the model will use its own default filters
)
model.load_dataset() # Processes all images for each class to prepare them for later use in training
model.train_model(visualize=True) # Starts model training based on the classes in your dataset with optional visualization support
model.save_model() # Stores the trained model's weights and biases in a model.bin file
Enter fullscreen mode Exit fullscreen mode

πŸ” Predicting New Images

model.load_model("model.bin") # Load the trained model
prediction = model.predict("test_images/mycat.png") # Applies the trained model to classify the input image
print("Predicted class:", prediction)
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Example Filters Used

[ [0, -1,  0],   Sharpen
  [-1, 5, -1],
  [0, -1,  0] ]

[ [1,  0, -1],   Edge detection
  [1,  0, -1],
  [1,  0, -1] ]

[[-1, -1, -1],   Laplacian
 [-1,  8, -1],
 [-1, -1, -1] ]
Enter fullscreen mode Exit fullscreen mode

πŸ“Š Performance

Metric Value (example)
Accuracy ~90% (binary class)
Epochs 10–50
Dataset Custom / ~8000 imgs

* Note that a larger dataset and more training epochs typically lead to higher accuracy.

🧠 Concepts Demonstrated

  • CNNs without frameworks
  • Data vectorization
  • Forward and backward propagation
  • Optimization from scratch
  • One-hot encoding for multi-class classification

πŸ“¦ Dependencies


πŸ“œ License

MIT License β€” feel free to use, modify, and share.


🀝 Contributing

PRs are welcome! You can help:

  • Add evaluation functions
  • Improve filter design
  • Extend to grayscale or multi-channel separately
  • Parallelize dataset loading

. Github repo: https://github.com/77AXEL/CNN-FS
. Github page: https://77axel.github.io/CNN-FS

Top comments (0)