Skip to content

rajarshidattapy/CNN

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CNN from Scratch in C

A first-principles implementation of a Convolutional Neural Network in pure C. No frameworks, no GPU, just math.

Demo:

demo.mp4

Features

  • Manual Tensor Engine: Custom malloc/free memory 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 numpy and pillow)

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_forward in src/conv.c.
  • Backward Pass: See conv2d_backward in src/conv.c for explicit gradient calculation.
  • Optimizer: Simple SGD in src/optim.c.

About

First-principles implementation of a Convolutional Neural Network (CNN) in C.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published