CNN
First-principles implementation of a Convolutional Neural Network (CNN) in C.
Install / Use
/learn @rajarshidattapy/CNNREADME
CNN from Scratch in C
A first-principles implementation of a Convolutional Neural Network in pure C. No frameworks, no GPU, just math.
Demo:
https://github.com/user-attachments/assets/79cb891c-effa-4d2d-9e06-b5f5be1c9cc7
Features
- 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.
Structure
interface/ # Header files (.h)
src/ # Implementation files (.c)
dataset/ # Data folders (cat/ and dog/)
tools/ # Helper scripts
main.c # Entry point
Prerequisites
- GCC (MinGW on Windows or standard Linux/Mac GCC)
- Python 3 (with
numpyandpillow)
Quick Start
1. Prepare Dataset
Pack your images from dataset/cat and dataset/dog into a single file.
pip install numpy pillow
python make_dataset.py dataset dataset.txt
2. Compile (MinGW/GCC)
Build the C executable.
gcc main.c src/*.c -o cnn.exe -lm
3. Train
Train the model and save weights to model.weights.
./cnn.exe train dataset.txt model.weights
4. Predict
Convert 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
Implementation Details
- Forward Pass: See
conv2d_forwardinsrc/conv.c. - Backward Pass: See
conv2d_backwardinsrc/conv.cfor explicit gradient calculation. - Optimizer: Simple SGD in
src/optim.c.
Related Skills
node-connect
337.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.3kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
337.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.3kCommit, push, and open a PR
