# fingsinz/styletransfer-pytorch architecture

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

- Interactive diagram: https://gitdiagram.com/fingsinz/styletransfer-pytorch
- Repository: https://github.com/fingsinz/styletransfer-pytorch

## Overview

##### Project Type and Purpose
- This is a research-oriented PyTorch implementation of image style transfer (part of a graduation thesis).
- It provides two model variants (CNN-based and U-Net-based) for training, an evaluation module, and an inference module for runtime style transfer.

##### File Structure Analysis
- Top Level
- datasets/ – local storage for style/content images
- script/ – shell scripts to download/prepare datasets
- requirements.txt – Python dependencies
- src/ – all source code and configuration
- src/ Directory
- data/ – dataset loading classes (e.g. ImageDataset)
- models/ – network definitions (Attention, MetaNet, UNet)
- utils/ – configuration parser, logging/recorder, generic utilities
- train.py, train_cnn.py, train_unet.py – entry points to kick off training
- evaluation.py – batch evaluation logic
- inference.py – style transfer at runtime
- config*.yaml – training and evaluation parameter files

##### Key Modules and Their Roles
- Data Ingestion Layer
- Reads images, applies transforms, yields (content, style) pairs
- Model Layer
- MetaNetModel – the style-predictor
- TransformNet (in networks.py) – applies style to content
- UNetModel – alternative architecture
- attention.py – supports attention mechanisms
- Training Orchestrator
- train.py (generic)
- train_cnn.py / train_unet.py – select network and losses via config.yaml
- utils/recorder – logs loss curves and checkpoints
- Evaluation Layer
- evaluation.py – loads checkpoints, runs quantitative metrics
- Inference Layer
- inference.py – loads pre-trained MetaNet + TransformNet, processes single images

a. Main Components
1. Dataset Preparation
2. Training Pipeline
3. Evaluation Pipeline
4. Inference Pipeline
5. Configuration & Utilities
b. Relationships & Data Flows
- From Model → Loss Computation → Recorder → Checkpoints
- From Checkpoints → Evaluation → Metrics Report
- From Checkpoints → Inference → Stylized Output
- Utility modules (config parser, logger) hook into all pipelines
c. Architectural Patterns
- Modular Separation (data / models / utils)
- Pipeline Stages (ingestion → transformation → evaluation/inference)
- Config-driven behavior (YAML files control hyperparameters)
d. Technologies & Frameworks
- PyTorch (torch, nn.Module)
- YAML for configuration
- Shell scripts for data download
- Python standard libs for I/O and logging

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Data Preparation
    subgraph "Dataset Preparation"
        direction TB
        Scripts["Shell Scripts"]:::data
        Datasets["datasets/"]:::data
    end
    Scripts -->|"output: datasets/"| Datasets

    click Scripts "https://github.com/fingsinz/styletransfer-pytorch/blob/main/script/download_content_dataset.sh"
    click Scripts "https://github.com/fingsinz/styletransfer-pytorch/blob/main/script/download_style_dataset.sh"
    click Datasets "https://github.com/fingsinz/styletransfer-pytorch/tree/main/datasets/"

    %% Data Ingestion
    DataLoader["DataLoader\n(src/data/image_dataset.py)"]:::data
    Datasets -->|"images"| DataLoader
    click DataLoader "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/data/image_dataset.py"

    %% Utilities
    subgraph "Configuration & Utilities"
        direction TB
        ConfigFiles["config*.yaml"]:::util
        ConfigParser["Config Parser\n(src/utils/config.py)"]:::util
        Recorder["Recorder\n(src/utils/recorder.py)"]:::util
        Utils["General Utils\n(src/utils/utils.py)"]:::util
    end
    click ConfigFiles "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/config.example.yaml"
    click ConfigFiles "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/config_evaluation.yaml"
    click ConfigParser "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/utils/config.py"
    click Recorder "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/utils/recorder.py"
    click Utils "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/utils/utils.py"

    %% Training Pipeline
    subgraph "Training Pipeline"
        direction TB
        TrainerGeneric["train.py"]:::util
        TrainerCNN["train_cnn.py"]:::util
        TrainerUNet["train_unet.py"]:::util
        TrainerGeneric -->|config selects| TrainerCNN
        TrainerGeneric -->|config selects| TrainerUNet
    end
    click TrainerGeneric "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/train.py"
    click TrainerCNN "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/train_cnn.py"
    click TrainerUNet "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/train_unet.py"

    DataLoader -->|"batch/images"| TrainerGeneric
    ConfigFiles -->|parameters| TrainerGeneric
    ConfigParser -->|parses| TrainerGeneric
    Utils -->|utilities| TrainerGeneric

    %% Model Definitions
    subgraph "Model Layer"
        direction TB
        Attention["Attention\n(src/models/attention.py)"]:::model
        MetaNet["MetaNetModel\n(src/models/metanet_model.py)"]:::model
        Networks["TransformNet & Others\n(src/models/networks.py)"]:::model
        UNet["UNetModel\n(src/models/unet_model.py)"]:::model
    end
    click Attention "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/models/attention.py"
    click MetaNet "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/models/metanet_model.py"
    click Networks "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/models/networks.py"
    click UNet "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/models/unet_model.py"

    TrainerCNN -->|"uses"| MetaNet
    TrainerCNN -->|"uses"| Networks
    TrainerUNet -->|"uses"| UNet
    TrainerUNet -->|"uses"| Attention

    %% Checkpoint & Logs
    Checkpoint["Checkpoints & Logs\n(results/*.pth, recorder logs)"]:::output
    TrainerGeneric -->|"loss -> Recorder"| Recorder
    Recorder -->|"writes"| Checkpoint

    %% Evaluation Pipeline
    subgraph "Evaluation Pipeline"
        direction TB
        EvalBatch["batch_eval.py"]:::util
        Eval["evaluation.py"]:::util
    end
    click EvalBatch "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/batch_eval.py"
    click Eval "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/evaluation.py"

    Checkpoint -->|"load model"| Eval
    EvalBatch -->|"metrics"| Eval
    Eval -->|"report metrics"| Checkpoint

    %% Inference Pipeline
    Inference["inference.py"]:::util
    click Inference "https://github.com/fingsinz/styletransfer-pytorch/blob/main/src/inference.py"
    Checkpoint -->|"load model"| Inference
    DataLoader -->|"single image"| Inference
    Inference -->|"stylized_image"| StylizedOutput

    StylizedOutput["output/*.png"]:::output

    %% Legend
    subgraph Legend
        DataComp["Data Components"]:::data
        ModelComp["Model Components"]:::model
        UtilComp["Utility/Config"]:::util
        OutputComp["Output/Results"]:::output
    end

    classDef data fill:#D0E8FF,stroke:#0047B3;
    classDef model fill:#D6F5D6,stroke:#267326;
    classDef util fill:#FFE5B4,stroke:#FF8C00;
    classDef output fill:#E6CCFF,stroke:#800080;
```
