A first-principles implementation of a Convolutional Neural Network in pure C. No frameworks, no GPU, just math.
demo.mp4
- Manual Tensor Engine: Custom
malloc/freememory management. - Explicit Backpropagation: Gradients computed manually for Conv, ReLU, Softmax, and Cross-Entropy.
- Naive Convolution: Implemented with 6 nested loops for educational clarity.
- Hybrid Workflow: Python for data processing, C for training/inference.
interface/ # Header files (.h)
src/ # Implementation files (.c)
dataset/ # Data folders (cat/ and dog/)
tools/ # Helper scripts
main.c # Entry point
- GCC (MinGW on Windows or standard Linux/Mac GCC)
- Python 3 (with
numpyandpillow)
Pack your images from dataset/cat and dataset/dog into a single file.
pip install numpy pillow
python make_dataset.py dataset dataset.txtBuild the C executable.
gcc main.c src/*.c -o cnn.exe -lmTrain the model and save weights to model.weights.
./cnn.exe train dataset.txt model.weightsConvert a test image and run inference.
# Convert image to text format
python img2txt.py "path/to/image.jpg" image.txt
# Run prediction
./cnn.exe predict model.weights image.txt- Forward Pass: See
conv2d_forwardinsrc/conv.c. - Backward Pass: See
conv2d_backwardinsrc/conv.cfor explicit gradient calculation. - Optimizer: Simple SGD in
src/optim.c.