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
๐ 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../
โ โโโ ..
..
Each subfolder represents a class (e.g., cat, dog), and contains sample images.
To help you get started, weโve included a starter
datafolder with example class directories.
๐งช How It Works
- 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.
- Feature Vector:
- Flattened pooled feature maps are fed into fully connected layers.
- Feedforward + Softmax:
- Dense layers compute activations followed by a softmax for classification.
- 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
โ 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
๐ 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)
๐ก 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] ]
๐ 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)