A clean, educational Convolutional Neural Network framework built from scratch using pure Python and NumPy
This project was created as a school assignment with the goal of understanding deep learning from the ground up. It's designed to be easy to understand and learn from, implementing a complete CNN framework using only NumPy for core computations. Additional modules are used only for visualization (tqdm), optimization (Numba JIT), and optional GPU acceleration (CuPy).
- โ Pure NumPy Core - All neural network math implemented from scratch
- ๐ฅ Complete CNN Support - Conv2D, MaxPool2D, Flatten, Dense layers
- ๐ Modern Training - Batch normalization, dropout, early stopping
- ๐ฏ Smart Optimizers - SGD with momentum and Adam optimizer
- ๐ Learning Rate Scheduling - Plateau-based LR reduction
- ๐พ Model Persistence - Save/load models in HDF5 or NPZ format
- ๐ Data Augmentation Ready - Thread-pooled data loading
- โก Numba JIT Compilation - Automatic acceleration of critical operations
- ๐ Optional GPU Support - CUDA acceleration via CuPy
- ๐งต Multi-threading - Auto-configured BLAS threads for CPU optimization
- ๐ฆ Batch Processing - Efficient mini-batch training
- ๐ Clean Code - Well-documented and easy to follow
- ๐ Educational - Built for learning deep learning fundamentals
- ๐ง Modular Design - Easy to extend and customize
- ๐ป Examples Included - MNIST training example and GUI demo
Install from PyPI (Recommended):
# Install the latest version from PyPI
pip install convnet
# Or install with GPU support
pip install convnet[cuda11] # For CUDA 11.x
pip install convnet[cuda12] # For CUDA 12.x
pip install convnet[cuda13] # For CUDA 13.xInstall from Source:
# Clone the repository
git clone https://github.com/codinggamer-dev/ConvNet-NumPy.git
cd ConvNet-NumPy
# Install in development mode
pip install -e .from convnet import Model
from convnet.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense
# Build a simple CNN
model = Model([
Conv2D(8, (3, 3)), Activation('relu'),
MaxPool2D((2, 2)),
Flatten(),
Dense(10)
])
# Configure training
model.compile(loss='categorical_crossentropy', optimizer='adam', lr=0.001)
# Train on your data
history = model.fit(train_dataset, epochs=10, batch_size=32)Here's a full example training a CNN on MNIST:
import numpy as np
from convnet import Model, data
from convnet.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense, Dropout
# Load MNIST data
train_data, test_data = data.load_mnist_gz('mnist_dataset')
# Build the model
model = Model([
Conv2D(8, (3, 3)), Activation('relu'),
MaxPool2D((2, 2)),
Conv2D(16, (3, 3)), Activation('relu'),
MaxPool2D((2, 2)),
Flatten(),
Dense(64), Activation('relu'), Dropout(0.2),
Dense(10) # 10 classes for MNIST
])
# Compile with Adam optimizer
model.compile(
loss='categorical_crossentropy',
optimizer='adam',
lr=0.001,
weight_decay=1e-4,
clip_norm=5.0
)
# Create validation split
split_idx = int(0.9 * len(train_data))
X_val = train_data.images[split_idx:].astype(np.float32) / 255.0
y_val = train_data.labels[split_idx:]
train_subset = data.Dataset(train_data.images[:split_idx], train_data.labels[:split_idx])
# Train with early stopping and LR scheduling
history = model.fit(
train_subset,
epochs=100,
batch_size=256,
num_classes=10,
val_data=(X_val, y_val),
early_stopping=True,
patience=15,
lr_schedule='plateau',
lr_factor=0.5,
lr_patience=4
)
# Save the model
model.save('my_mnist_model.hdf5')
# Later... load and use
loaded_model = Model.load('my_mnist_model.hdf5')
predictions = loaded_model.predict(test_images)| Layer | Description | Parameters |
|---|---|---|
Conv2D(filters, kernel_size) |
2D Convolutional layer | filters, kernel_size, stride, padding |
Dense(units) |
Fully connected layer | units, use_bias |
MaxPool2D(pool_size) |
Max pooling layer | pool_size, stride |
Activation(type) |
Activation function | 'relu', 'tanh', 'sigmoid', 'softmax' |
Flatten() |
Reshape to 1D | None |
Dropout(rate) |
Dropout regularization | rate (0.0 to 1.0) |
BatchNorm2D() |
Batch normalization | momentum, epsilon |
-
SGD - Stochastic Gradient Descent with momentum
model.compile(optimizer='sgd', lr=0.01, momentum=0.9)
-
Adam - Adaptive Moment Estimation (recommended)
model.compile(optimizer='adam', lr=0.001, beta1=0.9, beta2=0.999)
'categorical_crossentropy'- For multi-class classification'mse'- Mean Squared Error for regression
The examples/ directory contains several demonstrations:
Complete training pipeline with early stopping, LR scheduling, and model persistence.
python examples/mnist_train-example.pyDraw digits and see real-time predictions! Requires tkinter.
python examples/mnist_gui.pyBenchmark GPU vs CPU performance.
python examples/test_gpu_training.pyCompare Numba JIT vs pure NumPy performance.
python examples/benchmark_numba.pyConvNet-NumPy automatically detects and uses CUDA GPUs when CuPy is installed:
# Install with GPU support using extras
pip install convnet[cuda11] # For CUDA 11.x
pip install convnet[cuda12] # For CUDA 12.x
pip install convnet[cuda13] # For CUDA 13.x
# Or install CuPy separately
pip install cupy-cuda11x # For CUDA 11.x
pip install cupy-cuda12x # For CUDA 12.x
pip install cupy-cuda13x # For CUDA 13.xThe framework will automatically:
- Move tensors to GPU
- Use GPU-accelerated operations
- Handle CPU โ GPU transfers transparently
model.compile(
optimizer='adam',
lr=0.001,
weight_decay=1e-4, # L2 regularization
clip_norm=5.0 # Gradient clipping
)history = model.fit(
dataset,
lr_schedule='plateau', # Reduce LR when validation plateaus
lr_factor=0.5, # Multiply LR by 0.5
lr_patience=5, # Wait 5 epochs before reducing
lr_min=1e-6 # Minimum learning rate
)history = model.fit(
dataset,
val_data=(X_val, y_val),
early_stopping=True,
patience=10, # Stop after 10 epochs without improvement
min_delta=0.001 # Minimum change to qualify as improvement
)# Print model architecture and parameter counts
model.summary()
# Output:
# Model summary:
# Conv2D: params=80
# Activation: params=0
# MaxPool2D: params=0
# Conv2D: params=1168
# Activation: params=0
# MaxPool2D: params=0
# Flatten: params=0
# Dense: params=40064
# Activation: params=0
# Dropout: params=0
# Dense: params=650
# Total params: 41962The framework automatically configures BLAS threads for optimal CPU performance:
import os
os.environ['NN_DISABLE_AUTO_THREADS'] = '1' # Disable auto-configuration
import convnetFor reproducibility:
import numpy as np
rng = np.random.default_rng(seed=42)
model = Model([
Conv2D(8, (3, 3), rng=rng),
Dense(10, rng=rng)
])This project is designed for learning. Here's how to explore:
convnet/layers.py- See how Conv2D, Dense, and other layers workconvnet/model.py- Understand forward/backward propagationconvnet/optim.py- Learn how optimizers update weightsexamples/mnist_train-example.py- Complete training example
- ๐ Backpropagation - Full gradient computation chain
- ๐ Gradient Descent - SGD and Adam optimization
- ๐ฒ Weight Initialization - Glorot/Xavier uniform
- ๐งฎ Convolution Math - Pure NumPy implementation
- ๐ Batch Normalization - Running mean/variance tracking
- ๐ฏ Softmax & Cross-Entropy - Numerically stable implementation
This framework was built to:
- Understand deep learning by implementing it from scratch
- Learn how CNNs actually work under the hood
- Teach others the fundamentals of neural networks
- Provide a clean, readable codebase for education
Not for production use - Use PyTorch, TensorFlow, or JAX for real applications!
ConvNet-NumPy/
โโโ convnet/ # Core framework
โ โโโ __init__.py # Package initialization & auto-config
โ โโโ layers.py # Layer implementations
โ โโโ model.py # Model class with training loop
โ โโโ optim.py # Optimizers (SGD, Adam)
โ โโโ losses.py # Loss functions
โ โโโ data.py # Data loading utilities
โ โโโ utils.py # Helper functions
โ โโโ cuda.py # GPU acceleration wrapper
โ โโโ numba_ops.py # JIT-compiled operations
โ โโโ io.py # Model save/load
โโโ examples/ # Example scripts
โ โโโ mnist_train-example.py
โ โโโ mnist_gui.py
โ โโโ test_gpu_training.py
โ โโโ benchmark_numba.py
โโโ requirements.txt # Dependencies
โโโ setup.py # Package setup
โโโ LICENSE.md # MIT License
โโโ README.md # This file
This is an educational project, but contributions are welcome! Feel free to:
- ๐ Report bugs
- ๐ก Suggest improvements
- ๐ Improve documentation
- โจ Add new features
- Python 3.8 or higher
- NumPy โฅ 1.20.0 (the star of the show! ๐)
- tqdm โฅ 4.60.0 (progress bars)
- h5py โฅ 3.0.0 (model serialization)
- Numba โฅ 0.56.0 (JIT compilation)
- CuPy โฅ 10.0.0 (GPU acceleration)
- tkinter (for GUI demo, usually included with Python)
This project is licensed under the MIT License - see the LICENSE.md file for details.
Copyright (c) 2025 Tim Bauer
- Built as a school project to learn deep learning fundamentals
- Inspired by PyTorch and TensorFlow's clean APIs
- Thanks to the NumPy, Numba, and CuPy teams for amazing tools
- MNIST dataset by Yann LeCun and Corinna Cortes - the perfect dataset for learning CNNs
Feel free to open an issue on GitHub if you have questions or run into problems!
Made with โค๏ธ for learning and education
โญ If this helped you understand CNNs better, consider giving it a star! โญ