SkillAgentSearch skills...

CNN

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

Install / Use

/learn @rajarshidattapy/CNN
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

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/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.

Related Skills

View on GitHub
GitHub Stars30
CategoryDevelopment
Updated18d ago
Forks1

Languages

C

Security Score

75/100

Audited on Mar 8, 2026

No findings