# therealyubraj/cnn_js architecture

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

- Interactive diagram: https://gitdiagram.com/therealyubraj/cnn_js
- Repository: https://github.com/therealyubraj/cnn_js

## Overview

##### Main Components
- Frontend/UI Layer: This is represented by index.html, sketch.js, and sketch.properties along with the p5 libraries (p5.js and p5.dom.js). This layer handles rendering and user interaction in the browser.
- Neural Network Core: This includes files like NeuralNetwork.js and Matrix.js which provide the general neural network framework and mathematical operations.
- CNN Specific Modules: These files – CNN.js, CNNChannel.js, CNNImage.js, and CNNKernel.js – encapsulate the CNN-specific logic, such as convolution operations and feature extraction.
- Training Module: The trainer.js script handles the training procedures, likely interfacing with the MNIST dataset files (train-images.idx3-ubyte, train-labels.idx1-ubyte, t10k-images.idx3-ubyte, t10k-labels.idx1-ubyte).
- Pre-Trained Model: The brain.json file holds a pre-trained CNN model that is loaded on startup, which is important for recognizing the MNIST images.
- Static Assets: The images directory (e.g., the pages_screen.png in the README) for showcasing UI screenshots or other related media.

##### Representing Relationships and Interactions
- Next, illustrate the “CNN Core” as a subcomponent of the neural network where classes/files like CNN.js, CNNChannel.js, CNNImage.js, and CNNKernel.js are grouped together. Connect the neural network core to the CNN-specific modules to denote that the CNN is implemented on top of the general neural network functionalities.
- A link from the UI layer to the pre-trained brain.json loader (either as part of the initialization in the neural network core or as a separate loading routine), indicating the use of a pre-configured network model.

- Use clear labels for each component.
- Rectangles for software modules (UI, network components),
- That any interaction points (like functions in p5.js initiating training or predictions) are easily spotted and noted.

- External inputs (MNIST files and brain.json) as sources feeding into the training and initialization processes.
- Use annotations if necessary to clarify the role of each component.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "UI & Visualization Layer"
        UI1["index.html"]:::ui
        UI2["sketch.js"]:::ui
        UI3["sketch.properties"]:::ui
        UI4["p5.js"]:::lib
        UI5["p5.dom.js"]:::lib
    end

    subgraph "Core Neural Network & CNN Modules"
        NN1["NeuralNetwork.js"]:::core
        NN2["Matrix.js"]:::core
        CNN1["CNN.js"]:::cnn
        CNN2["CNNChannel.js"]:::cnn
        CNN3["CNNImage.js"]:::cnn
        CNN4["CNNKernel.js"]:::cnn
    end

    subgraph "Training Module"
        T1["trainer.js"]:::training
        D1["train-images.idx3-ubyte"]:::data
        D2["train-labels.idx1-ubyte"]:::data
        D3["t10k-images.idx3-ubyte"]:::data
        D4["t10k-labels.idx1-ubyte"]:::data
    end

    P1["brain.json"]:::pretrained
    S1["images"]:::assets

    %% Data Flow connections
    UI1 -->|"start"| NN1
    UI2 -->|"initiate"| NN1
    UI1 -->|"loadModel"| P1
    P1 -->|"initialize"| NN1
    T1 -->|"trains"| NN1
    D1 --> T1
    D2 --> T1
    D3 --> T1
    D4 --> T1

    %% Neural Network core uses CNN modules
    NN1 -->|"uses"| CNN1
    NN1 -->|"uses"| CNN2
    NN1 -->|"uses"| CNN3
    NN1 -->|"uses"| CNN4

    %% UI receives predictions from NN Core/CNN Modules
    NN1 -->|"prediction"| UI1

    %% Static assets usage by UI
    S1 -->|"display"| UI1

    %% Class definitions for coloring
    classDef ui fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef lib fill:#BFEFFF,stroke:#000,stroke-width:2px;
    classDef core fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef cnn fill:#FFFF99,stroke:#000,stroke-width:2px;
    classDef training fill:#FFA500,stroke:#000,stroke-width:2px;
    classDef data fill:#FFB6C1,stroke:#000,stroke-width:2px;
    classDef pretrained fill:#EE82EE,stroke:#000,stroke-width:2px;
    classDef assets fill:#D3D3D3,stroke:#000,stroke-width:2px;

    %% Click Events for UI & Libraries
    click UI1 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/index.html"
    click UI2 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/sketch.js"
    click UI3 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/sketch.properties"
    click UI4 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/libraries/p5.js"
    click UI5 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/libraries/p5.dom.js"

    %% Click Events for Neural Network Core & CNN Modules
    click NN1 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/NeuralNetwork.js"
    click NN2 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/Matrix.js"
    click CNN1 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/CNN.js"
    click CNN2 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/CNNChannel.js"
    click CNN3 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/CNNImage.js"
    click CNN4 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/CNNKernel.js"

    %% Click Events for Training Module and Data Files
    click T1 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/trainer.js"
    click D1 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/train-images.idx3-ubyte"
    click D2 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/train-labels.idx1-ubyte"
    click D3 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/t10k-images.idx3-ubyte"
    click D4 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/t10k-labels.idx1-ubyte"

    %% Click Events for Pre-trained Model Loader & Static Assets
    click P1 "https://github.com/therealyubraj/cnn_js/blob/gh-pages/brain.json"
    click S1 "https://github.com/therealyubraj/cnn_js/tree/gh-pages/images"
```
