# bhooyas/nst architecture

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

- Interactive diagram: https://gitdiagram.com/bhooyas/nst
- Repository: https://github.com/bhooyas/nst

## Overview

##### Project Type and Purpose
- This is a hybrid machine-learning application that supports both model training/inference in Python (CLI) and real-time inference in the browser (UI).
- It implements Neural Style Transfer (NST) using PyTorch for training and ONNX for client-side inference via JavaScript.

##### Key Architectural Components (from file tree and README)
- Data & Configuration
- get_dataset.sh – shell script to fetch training data
- config.py – hyperparameters & paths
- Training Pipeline (Python)
- train.py – orchestrates data loading, model instantiation, optimization (uses model.py & optim_nst.py)
- model.py – defines the NST network
- optim_nst.py – custom loss functions and optimizer wrappers
- Output: .safetensors checkpoints in /models
- ONNX Conversion
- onnx-convertor.py – converts trained .safetensors → .onnx (drops into UI/models)
- Inference Pipelines
- inference.py – static-image inference (PyTorch)
- inference_webcam.py – real-time webcam inference (PyTorch + OpenCV)
- Web UI (Client-side)
- UI/index.html, rain_princess.html, starry_night.html – static pages
- UI/models/*.onnx – ONNX style models (rain_princess.onnx, starry_night.onnx)
- UI/script.js – loads ONNX runtime-web, handles image upload and inference in browser
- UI/style.css – styling

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Backend - Data & Training
    subgraph "Backend (Python CLI & Training)"
        direction TB
        GetDataset["get_dataset.sh"]:::python
        DataStore["Training Dataset"]:::data
        subgraph "Training Module"
            direction TB
            Config["config.py"]:::python
            Train["train.py"]:::python
            ModelDef["model.py"]:::python
            Optim["optim_nst.py"]:::python
            ViewImg["view_image.py"]:::python
        end
        Check1["rain_princess.safetensors"]:::storage
        Check2["starry_night.safetensors"]:::storage
        Convert["onnx-convertor.py"]:::onnx
        InferenceCLI["inference.py"]:::python
        Webcam["inference_webcam.py"]:::python
    end

    %% Frontend - Static UI & Browser
    subgraph "Frontend (Static UI & Browser)"
        direction TB
        HTTPServer["Static HTTP Server"]:::infra
        subgraph "UI Files"
            direction TB
            Index["index.html"]:::browser
            RainPage["rain_princess.html"]:::browser
            StarPage["starry_night.html"]:::browser
            Style["style.css"]:::browser
            Script["script.js"]:::browser
            subgraph "ONNX Models"
                direction TB
                ONNX1["rain_princess.onnx"]:::onnx
                ONNX2["starry_night.onnx"]:::onnx
            end
        end
        Browser["Browser UI"]:::browser
    end

    %% Data & Training Flow
    GetDataset -->|"fetches data"| DataStore
    DataStore -->|"loads data"| Train
    Config --> Train
    ModelDef --> Train
    Optim --> Train
    ViewImg --> Train
    Train -->|"saves checkpoints"| Check1
    Train -->|"saves checkpoints"| Check2

    %% Conversion & CLI Inference
    Check1 -->|"input checkpoint"| Convert
    Check2 -->|"input checkpoint"| Convert
    Convert -->|"outputs ONNX models"| ONNX1
    Convert -->|"outputs ONNX models"| ONNX2
    Check1 -->|"loads checkpoint"| InferenceCLI
    Check2 -->|"loads checkpoint"| InferenceCLI
    Check1 -->|"loads checkpoint"| Webcam
    Check2 -->|"loads checkpoint"| Webcam

    %% Frontend Serving & Browser Inference
    HTTPServer -->|"serves files"| Index
    HTTPServer -->|"serves files"| RainPage
    HTTPServer -->|"serves files"| StarPage
    HTTPServer -->|"serves files"| Style
    HTTPServer -->|"serves files"| Script
    HTTPServer -->|"serves files"| ONNX1
    HTTPServer -->|"serves files"| ONNX2
    Browser -->|"loads UI pages"| HTTPServer
    Browser -->|"executes script.js"| Script
    Script -->|"loads model"| ONNX1
    Script -->|"loads model"| ONNX2
    Browser -->|"displays styled images"| Script

    %% Click Events
    click GetDataset "https://github.com/bhooyas/nst/blob/main/get_dataset.sh"
    click Config "https://github.com/bhooyas/nst/blob/main/config.py"
    click Train "https://github.com/bhooyas/nst/blob/main/train.py"
    click ModelDef "https://github.com/bhooyas/nst/blob/main/model.py"
    click Optim "https://github.com/bhooyas/nst/blob/main/optim_nst.py"
    click ViewImg "https://github.com/bhooyas/nst/blob/main/view_image.py"
    click Check1 "https://github.com/bhooyas/nst/blob/main/models/rain_princess.safetensors"
    click Check2 "https://github.com/bhooyas/nst/blob/main/models/starry_night.safetensors"
    click Convert "https://github.com/bhooyas/nst/blob/main/onnx-convertor.py"
    click InferenceCLI "https://github.com/bhooyas/nst/blob/main/inference.py"
    click Webcam "https://github.com/bhooyas/nst/blob/main/inference_webcam.py"
    click Index "https://github.com/bhooyas/nst/blob/main/UI/index.html"
    click RainPage "https://github.com/bhooyas/nst/blob/main/UI/rain_princess.html"
    click StarPage "https://github.com/bhooyas/nst/blob/main/UI/starry_night.html"
    click Style "https://github.com/bhooyas/nst/blob/main/UI/style.css"
    click Script "https://github.com/bhooyas/nst/blob/main/UI/script.js"
    click ONNX1 "https://github.com/bhooyas/nst/blob/main/UI/models/rain_princess.onnx"
    click ONNX2 "https://github.com/bhooyas/nst/blob/main/UI/models/starry_night.onnx"

    %% Styles
    classDef data fill:#bbdefb,stroke:#0d47a1,color:#0d47a1
    classDef storage fill:#b3e5fc,stroke:#01579b,color:#01579b
    classDef python fill:#c8e6c9,stroke:#1b5e20,color:#1b5e20
    classDef onnx fill:#ffe0b2,stroke:#e65100,color:#e65100
    classDef browser fill:#fff9c4,stroke:#f57f17,color:#f57f17
    classDef infra fill:#d7ccc8,stroke:#4e342e,color:#4e342e
```
