# junyanz/pytorch-cyclegan-and-pix2pix architecture

> Architecture diagram of the GitHub repository junyanz/pytorch-cyclegan-and-pix2pix, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/junyanz/pytorch-cyclegan-and-pix2pix
- Repository: https://github.com/junyanz/pytorch-cyclegan-and-pix2pix

## Overview

This repository is a PyTorch framework for image-to-image translation, centered on two core problem settings: paired translation with pix2pix and unpaired translation with CycleGAN. The architecture is organized around a small number of reusable abstractions: datasets, models, options, training/testing entrypoints, and utility code.

1. **Primary runtime flow**
   - `train.py` and `test.py` are the top-level entrypoints.
   - They parse CLI options, build the selected dataset and model, then run either the training loop or inference loop.
   - The same option system drives both workflows, so architecture is highly configuration-driven rather than hardcoded per experiment.

2. **Model boundary**
   - The `models/` package contains the GAN implementations and a shared base class.
   - `cycle_gan_model.py`, `pix2pix_model.py`, and `colorization_model.py` are the main task-specific model definitions.
   - `test_model.py` supports inference-only use cases, especially single-sided generation.
   - `networks.py` centralizes generator/discriminator architectures, normalization choices, losses, and related building blocks.

3. **Dataset boundary**
   - The `data/` package abstracts how training samples are loaded and paired.
   - `aligned_dataset.py` supports paired A/B examples for pix2pix-style training.
   - `unaligned_dataset.py` supports unpaired A/B domains for CycleGAN.
   - `single_dataset.py` is for single-domain inference or one-sided generation.
   - `colorization_dataset.py` and `template_dataset.py` show how the framework extends to custom tasks.

4. **Data preparation layer**
   - The `datasets/` directory contains scripts for downloading and preprocessing standard benchmark datasets.
   - `combine_A_and_B.py`, `make_dataset_aligned.py`, and `prepare_cityscapes_dataset.py` bridge raw datasets into the folder layouts expected by the loaders.
   - This repository assumes data is organized by domain and split, with the loader deciding whether images are paired or unpaired.

5. **Training loop and checkpointing**
   - Training is encapsulated in the model classes, which expose the standard phases: set input, forward pass, backward pass, optimize, save/load.
   - Checkpoints are written under `checkpoints/` and are later reused by `test.py`.
   - The shared `base_model.py` handles persistence, device placement, and common lifecycle logic.

6. **Visualization and result export**
   - `util/visualizer.py` and `util/html.py` generate browser-viewable training and test outputs.
   - Training progress can be logged locally as HTML pages and optionally to Weights & Biases via `--use_wandb`.
   - This makes qualitative image inspection a first-class part of the architecture, not just metric reporting.

7. **Shared utility layer**
   - `util/image_pool.py` supports the historical CycleGAN training stabilization pattern of replay buffers for discriminator updates.
   - `util/util.py` and `util/get_data.py` contain image/file helpers and dataset retrieval utilities.
   - These helpers are used across the loaders, visualizer, and model code to avoid duplication.

8. **Experiment configuration**
   - `options/base_options.py`, `train_options.py`, and `test_options.py` define the command-line contract for the whole repo.
   - Options control dataset mode, model type, direction, normalization, GPU/DDP behavior, output directories, and logging.
   - This makes the system modular: the same codebase supports different tasks by swapping options rather than changing code paths.

9. **Supported problem modes**
   - The repo is not just “CycleGAN and pix2pix”; it also includes a colorization mode and a generic test mode.
   - The `--model` and `--dataset_mode` flags determine whether the pipeline is paired, unpaired, single-image, or colorization-oriented.
   - These modes share the same runner but diverge in input structure and loss behavior.

10. **Evaluation and benchmarks**
   - `scripts/eval_cityscapes/` contains a separate evaluation pipeline for Cityscapes-style semantic translation benchmarks.
   - It includes model assets and evaluation scripts, indicating that some use cases depend on external metric tooling beyond the core GAN code.
   - This is a distinct postprocessing/benchmarking boundary, not part of the training loop itself.

11. **Automation scripts**
   - `scripts/train_*.sh`, `scripts/test_*.sh`, and download/install scripts provide reproducible command templates.
   - These scripts are convenience wrappers around the Python entrypoints and dataset/model fetchers.
   - They reflect the repo’s intended usage pattern: CLI-driven experiments with standard dataset/model presets.

12. **Environment and deployment**
   - `environment.yml` and `docs/Dockerfile` define supported execution environments.
   - The README notes DDP support via `torchrun`, so multi-GPU training is a runtime concern in the architecture.
   - The codebase targets local GPU training/inference rather than a server-style deployment model.

13. **Docs and notebooks as onboarding surfaces**
   - `docs/` explains datasets, Docker, FAQs, and code structure.
   - `CycleGAN.ipynb` and `pix2pix.ipynb` act as interactive examples, but they sit on top of the same underlying Python modules.
   - These assets are important for adoption, but they do not introduce separate runtime subsystems.

Overall, the architecture is a modular GAN research framework: a common CLI/config layer drives dataset loading, task-specific model selection, training/inference execution, and HTML/W&B visualization, with preprocessing and evaluation handled by adjacent scripts.

Main entry points: [`train.py`](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/train.py) (train.py), [`test.py`](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/test.py) (test.py), [`data/__init__.py`](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/__init__.py) (data init), [`models/__init__.py`](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/__init__.py) (models init)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_train_py["train.py<br/>entrypoint<br/>[train.py]"]
  node_test_py["test.py<br/>entrypoint<br/>[test.py]"]
end

subgraph group_config["Config"]
  node_base_options["Base opts<br/>CLI schema<br/>[base_options.py]"]
  node_train_options["Train opts<br/>CLI schema<br/>[train_options.py]"]
  node_test_options["Test opts<br/>CLI schema<br/>[test_options.py]"]
end

subgraph group_data["Data"]
  node_data_init["data init<br/>dataset registry<br/>[__init__.py]"]
  node_base_dataset["Base dataset<br/>dataset base<br/>[base_dataset.py]"]
  node_aligned_dataset["Aligned<br/>paired loader<br/>[aligned_dataset.py]"]
  node_unaligned_dataset["Unaligned<br/>unpaired loader"]
  node_single_dataset["Single<br/>inference loader<br/>[single_dataset.py]"]
  node_colorization_dataset["Colorization<br/>task loader"]
end

subgraph group_models["Models"]
  node_models_init["models init<br/>model registry<br/>[__init__.py]"]
  node_base_model["Base model<br/>model base<br/>[base_model.py]"]
  node_cycle_gan_model["CycleGAN<br/>GAN model<br/>[cycle_gan_model.py]"]
  node_pix2pix_model["pix2pix<br/>GAN model<br/>[pix2pix_model.py]"]
  node_colorization_model["Colorization model<br/>task model"]
  node_test_model["Test model<br/>inference model<br/>[test_model.py]"]
  node_networks["Networks<br/>architectures<br/>[networks.py]"]
end

subgraph group_utils["Utils"]
  node_visualizer["Visualizer<br/>reporting<br/>[visualizer.py]"]
  node_html_util["HTML<br/>report output<br/>[html.py]"]
  node_image_pool["Image pool<br/>stabilizer<br/>[image_pool.py]"]
  node_util_core["Core util<br/>helpers<br/>[util.py]"]
  node_get_data["Get data<br/>download helper<br/>[get_data.py]"]
end

subgraph group_ops["Ops"]
  node_dataset_prep[("Dataset prep<br/>preprocessing<br/>[combine_A_and_B.py]")]
  node_eval_cityscapes["Cityscapes eval<br/>benchmarking<br/>[evaluate.py]"]
  node_train_scripts["Train scripts<br/>wrappers<br/>[train_cyclegan.sh]"]
  node_test_scripts["Test scripts<br/>wrappers<br/>[test_cyclegan.sh]"]
  node_env_files["Env setup<br/>environment<br/>[environment.yml]"]
end

node_train_py -->|"parses"| node_base_options
node_train_py -->|"extends"| node_train_options
node_train_py -->|"builds"| node_data_init
node_train_py -->|"builds"| node_models_init
node_train_py -->|"logs"| node_visualizer
node_test_py -->|"parses"| node_base_options
node_test_py -->|"extends"| node_test_options
node_test_py -->|"builds"| node_data_init
node_test_py -->|"builds"| node_models_init
node_test_py -->|"exports"| node_visualizer
node_base_options -->|"specializes"| node_train_options
node_base_options -->|"specializes"| node_test_options
node_data_init -->|"registers"| node_base_dataset
node_data_init -->|"selects"| node_aligned_dataset
node_data_init -->|"selects"| node_unaligned_dataset
node_data_init -->|"selects"| node_single_dataset
node_data_init -->|"selects"| node_colorization_dataset
node_models_init -->|"registers"| node_base_model
node_models_init -->|"selects"| node_cycle_gan_model
node_models_init -->|"selects"| node_pix2pix_model
node_models_init -->|"selects"| node_colorization_model
node_models_init -->|"selects"| node_test_model
node_cycle_gan_model -->|"uses"| node_networks
node_pix2pix_model -->|"uses"| node_networks
node_colorization_model -->|"uses"| node_networks
node_base_model -.->|"depends on"| node_networks
node_cycle_gan_model -->|"uses"| node_image_pool
node_train_py -.->|"trains"| node_base_model
node_test_py -.->|"runs"| node_test_model
node_visualizer -->|"renders"| node_html_util
node_visualizer -->|"uses"| node_util_core
node_dataset_prep -.->|"prepares"| node_util_core
node_dataset_prep -.->|"downloads"| node_get_data
node_eval_cityscapes -.->|"benchmarks"| node_networks
node_train_scripts -.->|"wraps"| node_train_py
node_test_scripts -.->|"wraps"| node_test_py
node_env_files -.->|"supports"| node_train_py
node_env_files -.->|"supports"| node_test_py

click node_train_py "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/train.py"
click node_test_py "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/test.py"
click node_base_options "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/options/base_options.py"
click node_train_options "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/options/train_options.py"
click node_test_options "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/options/test_options.py"
click node_data_init "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/__init__.py"
click node_base_dataset "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/base_dataset.py"
click node_aligned_dataset "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/aligned_dataset.py"
click node_unaligned_dataset "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/unaligned_dataset.py"
click node_single_dataset "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/single_dataset.py"
click node_colorization_dataset "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/colorization_dataset.py"
click node_models_init "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/__init__.py"
click node_base_model "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/base_model.py"
click node_cycle_gan_model "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/cycle_gan_model.py"
click node_pix2pix_model "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/pix2pix_model.py"
click node_colorization_model "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/colorization_model.py"
click node_test_model "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/test_model.py"
click node_networks "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/networks.py"
click node_visualizer "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/visualizer.py"
click node_html_util "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/html.py"
click node_image_pool "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/image_pool.py"
click node_util_core "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/util.py"
click node_get_data "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/get_data.py"
click node_dataset_prep "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/datasets/combine_A_and_B.py"
click node_eval_cityscapes "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/scripts/eval_cityscapes/evaluate.py"
click node_train_scripts "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/scripts/train_cyclegan.sh"
click node_test_scripts "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/scripts/test_cyclegan.sh"
click node_env_files "https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/environment.yml"

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_train_py,node_test_py toneBlue
class node_base_options,node_train_options,node_test_options toneAmber
class node_data_init,node_base_dataset,node_aligned_dataset,node_unaligned_dataset,node_single_dataset,node_colorization_dataset toneMint
class node_models_init,node_base_model,node_cycle_gan_model,node_pix2pix_model,node_colorization_model,node_test_model,node_networks toneRose
class node_visualizer,node_html_util,node_image_pool,node_util_core,node_get_data toneIndigo
class node_dataset_prep,node_eval_cityscapes,node_train_scripts,node_test_scripts,node_env_files toneTeal
```

## Components

### Runtime

Top-level training and inference entrypoints

- [train.py](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/train.py): entrypoint · Training runner · `train.py`
- [test.py](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/test.py): entrypoint · Inference runner · `test.py`

### Config

CLI options and experiment wiring

- [Base opts](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/options/base_options.py): CLI schema · Shared command-line options · `options/base_options.py`
- [Train opts](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/options/train_options.py): CLI schema · Training-specific options · `options/train_options.py`
- [Test opts](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/options/test_options.py): CLI schema · Inference-specific options · `options/test_options.py`

### Data

Dataset loaders and preprocessing

- [data init](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/__init__.py): dataset registry · Dataset factory and registration · `data/__init__.py`
- [Base dataset](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/base_dataset.py): dataset base · Shared dataset interface · `data/base_dataset.py`
- [Aligned](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/aligned_dataset.py): paired loader · Paired A/B examples · `data/aligned_dataset.py`
- [Unaligned](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/unaligned_dataset.py): unpaired loader · Unpaired domain translation · `data/unaligned_dataset.py`
- [Single](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/single_dataset.py): inference loader · One-sided or single-domain inputs · `data/single_dataset.py`
- [Colorization](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/data/colorization_dataset.py): task loader · Colorization-specific inputs · `data/colorization_dataset.py`

### Models

GAN variants and shared network code

- [models init](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/__init__.py): model registry · Model factory and selection · `models/__init__.py`
- [Base model](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/base_model.py): model base · Lifecycle, persistence, device handling · `models/base_model.py`
- [CycleGAN](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/cycle_gan_model.py): GAN model · Unpaired translation model · `models/cycle_gan_model.py`
- [pix2pix](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/pix2pix_model.py): GAN model · Paired translation model · `models/pix2pix_model.py`
- [Colorization model](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/colorization_model.py): task model · Colorization training/inference · `models/colorization_model.py`
- [Test model](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/test_model.py): inference model · Inference-only model wrapper · `models/test_model.py`
- [Networks](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/models/networks.py): architectures · Generators, discriminators, losses · `models/networks.py`

### Utils

Visualization and shared helpers

- [Visualizer](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/visualizer.py): reporting · Training and test output logging · `util/visualizer.py`
- [HTML](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/html.py): report output · Browser-viewable result pages · `util/html.py`
- [Image pool](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/image_pool.py): stabilizer · Replay buffer for discriminator updates · `util/image_pool.py`
- [Core util](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/util.py): helpers · Image and file utilities · `util/util.py`
- [Get data](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/util/get_data.py): download helper · Dataset retrieval utilities · `util/get_data.py`

### Ops

Scripts, evaluation, and environment support

- [Dataset prep](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/datasets/combine_A_and_B.py): preprocessing · Download and dataset preparation scripts · `datasets/combine_A_and_B.py`
- [Cityscapes eval](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/scripts/eval_cityscapes/evaluate.py): benchmarking · External evaluation pipeline · `scripts/eval_cityscapes/evaluate.py`
- [Train scripts](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/scripts/train_cyclegan.sh): wrappers · Shell wrappers for training presets · `scripts/train_cyclegan.sh`
- [Test scripts](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/scripts/test_cyclegan.sh): wrappers · Shell wrappers for inference presets · `scripts/test_cyclegan.sh`
- [Env setup](https://github.com/junyanz/pytorch-cyclegan-and-pix2pix/blob/master/environment.yml): environment · Conda and Docker definitions · `environment.yml`

## Connections

- train.py → Base opts: parses · Training CLI starts with shared options
- train.py → Train opts: extends · Training adds task-specific flags
- train.py → data init: builds · Selects dataset implementation
- train.py → models init: builds · Selects model implementation
- train.py → Visualizer: logs · Emits HTML/W&B progress views
- test.py → Base opts: parses · Inference CLI starts with shared options
- test.py → Test opts: extends · Inference adds evaluation flags
- test.py → data init: builds · Selects inference dataset
- test.py → models init: builds · Selects inference model
- test.py → Visualizer: exports · Writes visual outputs
- Base opts → Train opts: specializes · Training options inherit shared contract
- Base opts → Test opts: specializes · Test options inherit shared contract
- data init → Base dataset: registers · Common dataset abstraction
- data init → Aligned: selects · Paired dataset mode
- data init → Unaligned: selects · Unpaired dataset mode
- data init → Single: selects · Single-image or one-sided mode
- data init → Colorization: selects · Colorization task mode
- models init → Base model: registers · Shared model lifecycle
- models init → CycleGAN: selects · CycleGAN task model
- models init → pix2pix: selects · pix2pix task model
- models init → Colorization model: selects · Colorization task model
- models init → Test model: selects · Inference-only model
- CycleGAN → Networks: uses · Builds generators/discriminators
- pix2pix → Networks: uses · Builds generator/discriminator stack
- Colorization model → Networks: uses · Reuses shared network blocks
- Base model → Networks: depends on · Model base delegates architecture details
- CycleGAN → Image pool: uses · Replay buffer for discriminator updates
- train.py → Base model: trains · Model class owns optimization loop
- test.py → Test model: runs · Inference uses the test-time wrapper
- Visualizer → HTML: renders · Creates browser pages
- Visualizer → Core util: uses · Shared formatting and file helpers
- Dataset prep → Core util: prepares · Dataset conversion helpers
- Dataset prep → Get data: downloads · Fetches standard benchmark data
- Cityscapes eval → Networks: benchmarks · Uses model assets for evaluation workflow
- Train scripts → train.py: wraps · Convenience presets for training
- Test scripts → test.py: wraps · Convenience presets for inference
- Env setup → train.py: supports · Defines runtime environment
- Env setup → test.py: supports · Defines runtime environment
