# aniketbadhan/convolutional-neural-network architecture

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

- Interactive diagram: https://gitdiagram.com/aniketbadhan/convolutional-neural-network
- Repository: https://github.com/aniketbadhan/convolutional-neural-network

## Overview

##### Project type and purpose
- This is an FPGA/ASIC‐style hardware implementation of a small CNN (convolutional neural network) written entirely in Verilog.
- Purpose: perform 2 stages of convolution (with Laplacian filters), followed by max‑pooling and thresholding to count pattern occurrences in an image.

##### File structure
- Top‑level files:
- CNN.v – the top‑level module that wires together all sub‑modules
- CNN_tb.v – the testbench that instantiates CNN.v, drives inputs (image1.txt, pattern.txt) and checks outputs

- Subdirectory breakdown:
a) “Convolution Stage 1”
– ConvolutionStage1.v: top module for first convolution layer
– Conv1.v + adderStage2.v, adderStage3.v, adderStage4.v: building blocks of the adder tree that implement the Laplacian filter’s multiply–accumulate network
– Conv1_tb.v: testbench for Stage‑1
– Readme.md: describes Stage‑1 specifics
b) “Convolution Stage 2”
– ConvolutionStage2.v: top module for second convolution layer
– Conv2_Full.v + adderStage2_2.v, adderStage3_2.v, adderStage4_2.v, adderStage5_2.v: adder tree for the second convolution (combining outputs from Stage‑1)
– Conv2_Full_tb.v: testbench for Stage‑2
– Readme.md: Stage‑2 notes
c) “Max Pooling Layer”
– maxPooling.v: module that takes Stage‑2 outputs and performs a 2×2 max‑pool operation
– maxPooling_tb.v: testbench for the pooling stage
– Readme.md: pooling specifics
- Global utility files: adderStage*.v, adderStage*_2.v (shared adder trees), image1.txt, pattern.txt, pattern data, and CNN Project Report.pdf

##### Extract insights from README
- Dataflow pipeline: Image + Pattern → Conv Stage‑1 (Laplacian) → Conv Stage‑2 → Max‑Pooling → Threshold & Counting
- Testbenches at each stage validate correctness
- External inputs are file‑based; outputs are counts of pattern occurrences

a. Main components (high level)
1) Input Interface
- File Reader (reads image1.txt & pattern.txt via testbench)
2) Convolution Stage 1
- Conv1.v core + hierarchical adder modules
3) Convolution Stage 2
- Conv2_Full.v core + its adder tree
4) Max‑Pooling Layer
- maxPooling.v
5) Threshold & Counter
- simple comparator + counter logic inside CNN.v
6) Output Interface
- Testbench monitor or host interface that records the count
b. Relationships & data flow
File Reader → Conv1 → Conv2 → MaxPool → Threshold/Counter → Output
c. Architectural patterns & principles
- Pipelined dataflow: each stage is combinational logic with registers on inputs/outputs
- Hierarchical decomposition: each convolution stage is built from smaller adder tree modules
- Modular testbenches isolate and verify each stage
d. Key technologies & libraries
- Verilog HDL modules (no external IP cores)
- Standard multi‑stage adder trees for MAC operations
- File I/O in testbenches for stimulus and verification

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Verification Swimlane
    subgraph "Verification"
        direction TB
        CNN_tb["CNN_tb.v"]:::verification
        Conv1_tb["Conv1_tb.v"]:::verification
        Conv2_tb["Conv2_Full_tb.v"]:::verification
        Pool_tb["maxPooling_tb.v"]:::verification
    end

    %% Pipeline
    FileReader["File Reader (image1.txt, pattern.txt)"]:::interface
    CNN["CNN.v (Top-Level)"]:::compute

    subgraph "Stage 1: Convolution"
        direction TB
        Conv1Stage["ConvolutionStage1.v"]:::compute
        Conv1Core["Conv1.v"]:::compute
        AST1_2["adderStage2.v"]:::compute
        AST1_3["adderStage3.v"]:::compute
        AST1_4["adderStage4.v"]:::compute
    end

    subgraph "Stage 2: Convolution"
        direction TB
        Conv2Stage["ConvolutionStage2.v"]:::compute
        Conv2Core["Conv2_Full.v"]:::compute
        AST2_2["adderStage2_2.v"]:::compute
        AST2_3["adderStage3_2.v"]:::compute
        AST2_4["adderStage4_2.v"]:::compute
        AST2_5["adderStage5_2.v"]:::compute
    end

    MaxPool["maxPooling.v"]:::compute
    Threshold["Threshold & Counter"]:::compute
    Output["Output Monitor"]:::interface
    Clock["clk, rst"]:::control

    %% Data Flow
    FileReader -->|data| Conv1Stage
    Conv1Stage --> Conv2Stage
    Conv2Stage --> MaxPool
    MaxPool --> Threshold
    Threshold --> Output

    %% Control Signals
    Clock -.-> FileReader
    Clock -.-> Conv1Stage
    Clock -.-> Conv2Stage
    Clock -.-> MaxPool
    Clock -.-> Threshold

    %% Testbench Invocations
    CNN_tb --> CNN
    CNN_tb --> FileReader
    Conv1_tb --> Conv1Stage
    Conv2_tb --> Conv2Stage
    Pool_tb --> MaxPool

    %% Click Events
    click CNN_tb "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/CNN_tb.v"
    click CNN "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/CNN.v"
    click Conv1Stage "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 1/ConvolutionStage1.v"
    click Conv1Core "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 1/Conv1.v"
    click AST1_2 "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 1/adderStage2.v"
    click AST1_3 "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 1/adderStage3.v"
    click AST1_4 "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 1/adderStage4.v"
    click Conv1_tb "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 1/Conv1_tb.v"
    click Conv2Stage "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 2/ConvolutionStage2.v"
    click Conv2Core "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 2/Conv2_Full.v"
    click AST2_2 "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 2/adderStage2_2.v"
    click AST2_3 "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 2/adderStage3_2.v"
    click AST2_4 "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 2/adderStage4_2.v"
    click AST2_5 "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 2/adderStage5_2.v"
    click Conv2_tb "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Convolution Stage 2/Conv2_Full_tb.v"
    click MaxPool "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Max Pooling Layer/maxPooling.v"
    click Pool_tb "https://github.com/aniketbadhan/convolutional-neural-network/blob/master/Max Pooling Layer/maxPooling_tb.v"

    %% Styles
    classDef compute fill:#E0F7FA,stroke:#006064,color:#004D40
    classDef interface fill:#E8F5E9,stroke:#1B5E20,color:#2E7D32
    classDef control stroke-dasharray: 5 5,stroke:#D32F2F,color:#B71C1C
    classDef verification fill:#FFF3E0,stroke:#F57C00,color:#E65100
```
