How aniketbadhan/convolutional-neural-network works
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