# vzhou842/cnn-from-scratch architecture

> Architecture diagram of the GitHub repository vzhou842/cnn-from-scratch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vzhou842/cnn-from-scratch
- Repository: https://github.com/vzhou842/cnn-from-scratch

## Overview

##### Project Type & Purpose
- This project is an educational implementation of a Convolutional Neural Network (CNN), built from scratch for demonstration purposes. It also includes an alternative implementation using Keras.
- The goal is to illustrate the core operations involved in CNNs (convolution, pooling, softmax, etc.) with both a manual implementation and a framework-backed (Keras) version.

##### Analyzing the File Structure
- Top-level files:
- "cnn.py" likely defines the main execution of the hand-crafted CNN.
- "cnn_keras.py" provides an alternative implementation using Keras.
- "conv.py", "maxpool.py", and "softmax.py" likely contain the core building blocks/modules performing convolution operations, max pooling operations, and output (softmax) computations respectively.
- "requirements.txt" lists the dependencies needed.
- The absence of nested directories means the components are modularized within individual files rather than separated into distinct directories. Therefore, each file (module) plays a key role in the overall design.

##### Reviewing the README Insights
- The README describes the project as a hands-on walkthrough of CNN operations (forward-propagation only).

a. Main Components:
- Data Input / Preprocessing (implied in the main script, before data goes through the CNN layers)
- Convolution Module (handled by conv.py)
- Pooling Module (handled by maxpool.py)
- Fully-Connected & Softmax Module (using softmax.py)
- Two Control Flows: One representing the custom CNN (cnn.py) and another utilizing Keras (cnn_keras.py)

b. Relationships and Interactions:
- From there, data flows into the Convolution Module (conv.py) which applies convolutional filters.
- The output of convolution is then passed to a Pooling Module (maxpool.py) to reduce the spatial dimensions.
- The resulting feature maps are then forwarded to a Softmax or fully-connected layer (softmax.py) that produces the final classification probabilities.
- For the branch using Keras (cnn_keras.py), show that it wraps these concepts into a higher-level API but essentially follows the same sequence.

c. Important Architectural Patterns:
- The project adheres to a modular design where each computational step in the CNN is isolated in its own file/module.
- The design emulates the standard pipeline of forward propagation in CNNs.
- For the Keras version, note the abstraction provided by the framework, which encapsulates the operations into high-level functions.
- Separation of concerns: Each module (convolution, pooling, softmax) is responsible for its specific transformation, aligning with design principles of modularity and single responsibility.

d. Notable Technologies and Libraries:
- Python is the primary language used.
- Keras is leveraged in the "cnn_keras.py" implementation.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Data Source"
        DI["Data Input/Preprocessing"]:::dataInput
    end

    subgraph "Custom CNN Pipeline"
        CC["Custom CNN Controller"]:::control
        CV["Convolution Layer"]:::core
        MP["Max Pooling"]:::core
        SM["Softmax/FC Layer"]:::core
    end

    subgraph "Keras CNN Pipeline"
        KC["Keras-based CNN Controller"]:::control
    end

    DI -->|"raw_input"| CC
    CC -->|"forward_to_conv"| CV
    CV -->|"feature_maps"| MP
    MP -->|"pooled_outputs"| SM
    DI -->|"raw_input"| KC

    click CC "https://github.com/vzhou842/cnn-from-scratch/blob/master/cnn.py"
    click CV "https://github.com/vzhou842/cnn-from-scratch/blob/master/conv.py"
    click MP "https://github.com/vzhou842/cnn-from-scratch/blob/master/maxpool.py"
    click SM "https://github.com/vzhou842/cnn-from-scratch/blob/master/softmax.py"
    click KC "https://github.com/vzhou842/cnn-from-scratch/blob/master/cnn_keras.py"

    classDef dataInput fill:#F9E79F,stroke:#F39C12,stroke-width:2px;
    classDef control fill:#FAD7A0,stroke:#E67E22,stroke-width:2px,stroke-dasharray: 5 5;
    classDef core fill:#AED6F1,stroke:#3498DB,stroke-width:2px;
    class DI dataInput
    class CC,KC control
    class CV,MP,SM core
```
