# jeffasante/nimble architecture

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

- Interactive diagram: https://gitdiagram.com/jeffasante/nimble
- Repository: https://github.com/jeffasante/nimble

## Overview

##### Project Type and Purpose
- This is a machine learning research project with a focus on image processing and generative modeling.
- Its purpose is to generate vector sketches from bitmap images using a diffusion process and a transformer decoder. The README explicitly highlights the model’s components, training/inference procedures, and usage examples.

##### And Map the File Structure
- Notice the top-level Python files: dataset.py, encoder.py, decoder.py, diffusion.py, losses.py, inference.py, train.py. These represent different modules of the model system.
- The file tree and README indicate the presence of training scripts, test images, and a Jupyter notebook (play.ipynb) for interactive exploration.
- Directories like checkpoints and test_images are used for storing model states and sample outputs.
- The deployment target is geared towards research and experimentation with ML pipelines, rather than a production web service.

##### Main Components to Capture
a. Input:
- Bitmap Image
- Target Coordinates (ground truth for training)
b. Diffusion Process:
- Sample Timestep (t)
- Noise Addition step which creates Noised Coordinates
c. Encoder Module (Bitmap Encoder):
- Convolutional and MobileNetV3-inspired layers (Initial Conv, Inverted Residual Blocks, SE Block, Global Average Pooling) to generate the image embedding.
d. Decoder Module (Transformer Decoder):
- Positional Encoding and Timestep Embedding; multiple stacked decoder layers that include Self-Attention, Cross-Attention, Feed Forward networks, and Layer Normalization; Output Projection to convert embeddings to sketch coordinates.
e. Loss Functions:
- Separate losses including Coordinate Loss (L2) and Rasterization Loss (Binary Cross-Entropy) that are combined into a Total Loss for training.
f. Training and Checkpoint Flow:
- Training loop (as implemented in train.py) that manages logging, checkpoint saving, and training metrics.
g. Inference:
- A dedicated inference module (inference.py) that enables sketch generation and visualization, making use of saved checkpoints and user-controlled parameters (e.g., temperature).

##### Architectural Patterns and Technologies
- The system adopts a clear separation of concerns between data processing (dataset.py), model components (encoder, diffusion, and decoder), and training/inference loops.
- The use of a transformer decoder hints at important design principles such as layer normalization and attention mechanisms.
- The diffusion process guides the denoising method, a generative technique that is foundational to the architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Subgraph
    subgraph "Input"
        BI["Bitmap Image"]:::input
        TC["Target Coordinates"]:::input
        ST["Sampled Timestep"]:::diffusion
    end

    %% Data Handling Module
    subgraph "Data Handling"
        DH["Dataset Handling"]:::data
    end

    %% Encoder Module Subgraph
    subgraph "Encoder Module"
        BE["Bitmap Encoder"]:::encoder
    end

    %% Diffusion Process Subgraph
    subgraph "Diffusion Process"
        DP["Diffusion Process"]:::diffusion
    end

    %% Decoder Module Subgraph
    subgraph "Decoder Module"
        TD["Transformer Decoder"]:::decoder
    end

    %% Loss Functions Module
    subgraph "Loss Functions"
        LF["Loss Functions"]:::loss
    end

    %% Training and Inference Subgraph
    subgraph "Training & Inference"
        TL["Training Loop & Checkpoints"]:::training
        IM["Inference Module"]:::inference
    end

    %% Technologies Legend
    subgraph "Technologies"
        jl["jax"]:::tech
        fl["flax"]:::tech
        op["optax"]:::tech
    end

    %% Data Flow
    BI --> DH
    TC --> DH
    DH --> BE
    BE -->|"Embeds"| TD
    TC --> DP
    ST --> DP
    DP -->|"NoisedCoordinates"| TD
    TD -->|"SketchCoordinates"| LF
    LF -->|"TotalLoss"| TL
    TL --> IM

    %% Click Events for Component Mapping
    click DH "https://github.com/jeffasante/nimble/blob/master/dataset.py"
    click BE "https://github.com/jeffasante/nimble/blob/master/encoder.py"
    click DP "https://github.com/jeffasante/nimble/blob/master/diffusion.py"
    click TD "https://github.com/jeffasante/nimble/blob/master/decoder.py"
    click LF "https://github.com/jeffasante/nimble/blob/master/losses.py"
    click TL "https://github.com/jeffasante/nimble/blob/master/train.py"
    click IM "https://github.com/jeffasante/nimble/blob/master/inference.py"

    %% Styles
    classDef input fill:#FDEDEC,stroke:#C0392B,stroke-width:2px,color:#000;
    classDef data fill:#EBF5FB,stroke:#2471A3,stroke-width:2px,color:#000;
    classDef encoder fill:#E8F8F5,stroke:#1ABC9C,stroke-width:2px,color:#000;
    classDef diffusion fill:#F9E79F,stroke:#F39C12,stroke-width:2px,color:#000;
    classDef decoder fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px,color:#000;
    classDef loss fill:#FDEBD0,stroke:#D68910,stroke-width:2px,color:#000;
    classDef training fill:#D5F5E3,stroke:#27AE60,stroke-width:2px,color:#000;
    classDef inference fill:#FADBD8,stroke:#E74C3C,stroke-width:2px,color:#000;
    classDef tech fill:#D6EAF8,stroke:#2E86C1,stroke-width:2px,color:#000;
```
