# naohzjh/cnn-mnist architecture

> Architecture diagram of the GitHub repository naohzjh/cnn-mnist, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/naohzjh/cnn-mnist
- Repository: https://github.com/naohzjh/cnn-mnist

## Overview

This repository is a from-scratch C++17 MNIST CNN with console training and a Qt application for background training, metric visualization, and browsing predictions. The main workflow loads labeled CSV samples, runs batches through a sequential CNN, computes cross-entropy gradients, updates parameters with SGD, and optionally saves weights; prediction rebuilds the same network, loads weights and unlabeled test data, then displays predictions. The graph follows README-documented capabilities and the sampled GUI prediction code; unsampled console and GUI internals are represented at their documented boundaries.

Main entry points: [`src/core/network.cpp`](https://github.com/naohzjh/cnn-mnist/blob/main/src/core/network.cpp) (Sequential network), [`gui/src/training/trainer.cpp`](https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/training/trainer.cpp) (GUI trainer), [`gui/src/windows/predictwindow.cpp`](https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/windows/predictwindow.cpp) (Prediction window), [`src/mnist_test.cpp`](https://github.com/naohzjh/cnn-mnist/blob/main/src/mnist_test.cpp) (Console training)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["User interfaces"]
  node_gui_main["Main window<br/>[mainwindow.cpp]"]
  node_trainer["GUI trainer<br/>[trainer.cpp]"]
  node_plot["Metric plots<br/>[plotwidget.cpp]"]
  node_digit["Digit display<br/>[digitwidget.cpp]"]
  node_predict["Prediction window<br/>[predictwindow.cpp]"]
  node_console_train["Console training<br/>[mnist_test.cpp]"]
end

subgraph group_data_model["Data and models"]
  node_dataset["MNIST CSV loader<br/>[dataset.cpp]"]
  node_model_io["Model serialization<br/>[model_io.cpp]"]
end

subgraph group_runtime["CNN runtime"]
  node_tensor["Tensor operations<br/>[tensor.cpp]"]
  node_network["Sequential network<br/>[network.cpp]"]
  node_conv["Convolution<br/>[conv2d.cpp]"]
  node_relu["ReLU<br/>[relu.cpp]"]
  node_pool["Max pooling<br/>[pooling.cpp]"]
  node_flatten["Flatten<br/>[flatten.cpp]"]
  node_dense["Dense layer<br/>[dense.cpp]"]
  node_softmax["Softmax<br/>[softmax.cpp]"]
end

subgraph group_learning["Training"]
  node_loss["Cross entropy<br/>[loss.cpp]"]
  node_optimizer["SGD optimizer<br/>[optimizer.cpp]"]
end

node_user(("User"))

node_user -->|"uses"| node_gui_main
node_gui_main -->|"starts training"| node_trainer
node_trainer -->|"reports metrics"| node_plot
node_gui_main -->|"shows samples"| node_digit
node_gui_main -->|"opens"| node_predict
node_user -->|"runs"| node_console_train
node_console_train -->|"loads samples"| node_dataset
node_trainer -->|"loads samples"| node_dataset
node_predict -->|"loads test CSV"| node_dataset
node_trainer -->|"trains"| node_network
node_console_train -->|"trains"| node_network
node_network -->|"dispatches layers"| node_conv
node_network -->|"dispatches layers"| node_relu
node_network -->|"dispatches layers"| node_pool
node_network -->|"dispatches layers"| node_flatten
node_network -->|"dispatches layers"| node_dense
node_trainer -->|"computes loss"| node_loss
node_console_train -->|"computes loss"| node_loss
node_trainer -->|"updates weights"| node_optimizer
node_console_train -->|"updates weights"| node_optimizer
node_trainer -->|"saves model"| node_model_io
node_predict -->|"loads weights"| node_model_io
node_predict -->|"predicts samples"| node_network
node_predict -->|"renders predictions"| node_digit

click node_gui_main "https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/windows/mainwindow.cpp"
click node_trainer "https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/training/trainer.cpp"
click node_plot "https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/widgets/plotwidget.cpp"
click node_digit "https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/widgets/digitwidget.cpp"
click node_predict "https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/windows/predictwindow.cpp"
click node_console_train "https://github.com/naohzjh/cnn-mnist/blob/main/src/mnist_test.cpp"
click node_dataset "https://github.com/naohzjh/cnn-mnist/blob/main/src/data/dataset.cpp"
click node_model_io "https://github.com/naohzjh/cnn-mnist/blob/main/src/io/model_io.cpp"
click node_tensor "https://github.com/naohzjh/cnn-mnist/blob/main/src/core/tensor.cpp"
click node_network "https://github.com/naohzjh/cnn-mnist/blob/main/src/core/network.cpp"
click node_conv "https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/conv2d.cpp"
click node_relu "https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/relu.cpp"
click node_pool "https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/pooling.cpp"
click node_flatten "https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/flatten.cpp"
click node_dense "https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/dense.cpp"
click node_softmax "https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/softmax.cpp"
click node_loss "https://github.com/naohzjh/cnn-mnist/blob/main/src/training/loss.cpp"
click node_optimizer "https://github.com/naohzjh/cnn-mnist/blob/main/src/training/optimizer.cpp"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_gui_main,node_trainer,node_plot,node_digit,node_predict,node_console_train,node_user toneBlue
class node_dataset,node_model_io toneAmber
class node_tensor,node_network,node_conv,node_relu,node_pool,node_flatten,node_dense,node_softmax toneMint
class node_loss,node_optimizer toneRose
```

## Components

### User interfaces

- [Main window](https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/windows/mainwindow.cpp): `gui/src/windows/mainwindow.cpp`
- [GUI trainer](https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/training/trainer.cpp): `gui/src/training/trainer.cpp`
- [Metric plots](https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/widgets/plotwidget.cpp): `gui/src/widgets/plotwidget.cpp`
- [Digit display](https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/widgets/digitwidget.cpp): `gui/src/widgets/digitwidget.cpp`
- [Prediction window](https://github.com/naohzjh/cnn-mnist/blob/main/gui/src/windows/predictwindow.cpp): `gui/src/windows/predictwindow.cpp`
- [Console training](https://github.com/naohzjh/cnn-mnist/blob/main/src/mnist_test.cpp): `src/mnist_test.cpp`

### Data and models

- [MNIST CSV loader](https://github.com/naohzjh/cnn-mnist/blob/main/src/data/dataset.cpp): `src/data/dataset.cpp`
- [Model serialization](https://github.com/naohzjh/cnn-mnist/blob/main/src/io/model_io.cpp): `src/io/model_io.cpp`

### CNN runtime

- [Tensor operations](https://github.com/naohzjh/cnn-mnist/blob/main/src/core/tensor.cpp): `src/core/tensor.cpp`
- [Sequential network](https://github.com/naohzjh/cnn-mnist/blob/main/src/core/network.cpp): `src/core/network.cpp`
- [Convolution](https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/conv2d.cpp): `src/layers/conv2d.cpp`
- [ReLU](https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/relu.cpp): `src/layers/relu.cpp`
- [Max pooling](https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/pooling.cpp): `src/layers/pooling.cpp`
- [Flatten](https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/flatten.cpp): `src/layers/flatten.cpp`
- [Dense layer](https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/dense.cpp): `src/layers/dense.cpp`
- [Softmax](https://github.com/naohzjh/cnn-mnist/blob/main/src/layers/softmax.cpp): `src/layers/softmax.cpp`

### Training

- [Cross entropy](https://github.com/naohzjh/cnn-mnist/blob/main/src/training/loss.cpp): `src/training/loss.cpp`
- [SGD optimizer](https://github.com/naohzjh/cnn-mnist/blob/main/src/training/optimizer.cpp): `src/training/optimizer.cpp`

### Other components

- **User**

## Connections

- User → Main window: uses (evidence: `README.md`)
- Main window → GUI trainer: starts training (evidence: `gui/src/windows/mainwindow.cpp`)
- GUI trainer → Metric plots: reports metrics (evidence: `README.md`)
- Main window → Digit display: shows samples (evidence: `gui/src/windows/mainwindow.cpp`)
- Main window → Prediction window: opens (evidence: `gui/src/windows/mainwindow.cpp`)
- User → Console training: runs (evidence: `README.md`)
- Console training → MNIST CSV loader: loads samples (evidence: `README.md`)
- GUI trainer → MNIST CSV loader: loads samples (evidence: `gui/src/training/trainer.cpp`)
- Prediction window → MNIST CSV loader: loads test CSV (evidence: `gui/src/windows/predictwindow.cpp`)
- GUI trainer → Sequential network: trains (evidence: `gui/src/training/trainer.cpp`)
- Console training → Sequential network: trains (evidence: `README.md`)
- Sequential network → Convolution: dispatches layers (evidence: `README.md`)
- Sequential network → ReLU: dispatches layers (evidence: `README.md`)
- Sequential network → Max pooling: dispatches layers (evidence: `README.md`)
- Sequential network → Flatten: dispatches layers (evidence: `README.md`)
- Sequential network → Dense layer: dispatches layers (evidence: `README.md`)
- GUI trainer → Cross entropy: computes loss (evidence: `gui/src/training/trainer.cpp`)
- Console training → Cross entropy: computes loss (evidence: `README.md`)
- GUI trainer → SGD optimizer: updates weights (evidence: `gui/src/training/trainer.cpp`)
- Console training → SGD optimizer: updates weights (evidence: `README.md`)
- GUI trainer → Model serialization: saves model (evidence: `gui/src/training/trainer.cpp`)
- Prediction window → Model serialization: loads weights (evidence: `gui/src/windows/predictwindow.cpp`)
- Prediction window → Sequential network: predicts samples (evidence: `gui/src/windows/predictwindow.cpp`)
- Prediction window → Digit display: renders predictions (evidence: `gui/src/windows/predictwindow.cpp`)
