# aliaksandrsiarohin/first-order-model architecture

> Architecture diagram of the GitHub repository aliaksandrsiarohin/first-order-model, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aliaksandrsiarohin/first-order-model
- Repository: https://github.com/aliaksandrsiarohin/first-order-model

## Overview

This repository implements first-order motion transfer: users provide a source image or video and a driving video, then the model transfers motion to produce an animation. The main runtime entry point, `run.py`, selects training, reconstruction, or animation; `demo.py` provides direct source/driving inference. Training loads paired video frames, optimizes keypoint, generator, and discriminator models, and logs checkpoints. The README also documents dataset preparation, crop suggestions, and supervised face-swap as an adaptation. Some subsystem links rely on the README or SOURCE INDEX because their implementations were not excerpted.

Main entry points: [`animate.py`](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/animate.py) (Video animation), [`frames_dataset.py`](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/frames_dataset.py) (Frame datasets), [`run.py`](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/run.py) (Mode dispatcher), [`train.py`](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/train.py) (Training loop)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime workflows"]
  node_run["Mode dispatcher<br/>[run.py]"]
  node_demo["Animation demo<br/>[demo.py]"]
  node_animate["Video animation<br/>[animate.py]"]
  node_reconstruction["Reconstruction<br/>[reconstruction.py]"]
  node_train["Training loop<br/>[train.py]"]
end

subgraph group_data["Video data"]
  node_dataset["Frame datasets<br/>[frames_dataset.py]"]
  node_augmentation["Clip transforms<br/>[augmentation.py]"]
  node_taichi["Taichi loader<br/>[load_videos.py]"]
  node_crop["Crop suggestions<br/>[crop-video.py]"]
end

subgraph group_models["Motion models"]
  node_kp["Keypoint detector"]
  node_dense["Dense motion<br/>[dense_motion.py]"]
  node_generator["Occlusion-aware generator<br/>[generator.py]"]
  node_discriminator["Discriminator<br/>[discriminator.py]"]
  node_losses["Model losses<br/>[model.py]"]
  node_modelutil["Network blocks<br/>[util.py]"]
end

subgraph group_support["Training support"]
  node_logger["Logger and visualizer<br/>[logger.py]"]
  node_syncnorm["Synchronized normalization<br/>[batchnorm.py]"]
  node_synccomm["Normalization communication<br/>[comm.py]"]
  node_syncreplicate["Parallel replication<br/>[replicate.py]"]
end

node_user(("User"))
node_checkpoint[("Model checkpoint")]
node_result["Animation or reconstruction"]
node_datasetfiles[("Video datasets")]
node_facealign["Face alignment library"]

node_user -->|"selects mode"| node_run
node_user -->|"provides inputs"| node_demo
node_run -->|"dispatches training"| node_train
node_run -->|"dispatches animation"| node_animate
node_run -->|"dispatches reconstruction"| node_reconstruction
node_run -->|"loads data"| node_dataset
node_demo -->|"detects keypoints"| node_kp
node_demo -->|"generates frames"| node_generator
node_demo -->|"uses animation logic"| node_animate
node_animate -->|"pairs videos"| node_dataset
node_animate -->|"detects motion"| node_kp
node_animate -->|"generates frames"| node_generator
node_animate -->|"writes animation"| node_result
node_reconstruction -->|"loads data"| node_dataset
node_reconstruction -->|"writes reconstruction"| node_result
node_dataset -->|"transforms clips"| node_augmentation
node_datasetfiles -->|"provides videos"| node_dataset
node_user -->|"requests crop guidance"| node_crop
node_crop -.->|"uses"| node_facealign
node_user -->|"prepares Taichi data"| node_taichi
node_dataset -->|"supplies batches"| node_train
node_train -->|"computes objectives"| node_losses
node_losses -->|"evaluates detector"| node_kp
node_losses -->|"evaluates generator"| node_generator
node_losses -->|"evaluates discriminator"| node_discriminator
node_generator -->|"estimates deformation"| node_dense
node_generator -->|"uses blocks"| node_modelutil
node_dense -->|"uses geometry operations"| node_modelutil
node_kp -->|"uses geometry operations"| node_modelutil
node_discriminator -->|"uses blocks"| node_modelutil
node_modelutil -->|"normalizes activations"| node_syncnorm
node_syncnorm -->|"synchronizes statistics"| node_synccomm
node_syncreplicate -->|"registers replicas"| node_syncnorm
node_train -->|"logs training"| node_logger
node_animate -->|"visualizes frames"| node_logger
node_train -->|"saves checkpoints"| node_checkpoint
node_checkpoint -->|"loads model state"| node_animate
node_checkpoint -->|"loads model state"| node_reconstruction
node_user -->|"provides pretrained weights"| node_checkpoint

click node_run "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/run.py"
click node_demo "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/demo.py"
click node_animate "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/animate.py"
click node_reconstruction "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/reconstruction.py"
click node_dataset "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/frames_dataset.py"
click node_augmentation "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/augmentation.py"
click node_taichi "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/data/taichi-loading/load_videos.py"
click node_crop "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/crop-video.py"
click node_train "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/train.py"
click node_kp "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/keypoint_detector.py"
click node_dense "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/dense_motion.py"
click node_generator "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/generator.py"
click node_discriminator "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/discriminator.py"
click node_losses "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/model.py"
click node_modelutil "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/util.py"
click node_logger "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/logger.py"
click node_syncnorm "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/sync_batchnorm/batchnorm.py"
click node_synccomm "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/sync_batchnorm/comm.py"
click node_syncreplicate "https://github.com/aliaksandrsiarohin/first-order-model/blob/master/sync_batchnorm/replicate.py"

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_run,node_demo,node_animate,node_reconstruction,node_train,node_user toneBlue
class node_dataset,node_augmentation,node_taichi,node_crop,node_checkpoint,node_datasetfiles toneAmber
class node_kp,node_dense,node_generator,node_discriminator,node_losses,node_modelutil toneMint
class node_logger,node_syncnorm,node_synccomm,node_syncreplicate toneRose
class node_result,node_facealign toneIndigo
```

## Components

### Runtime workflows

- [Mode dispatcher](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/run.py): `run.py`
- [Animation demo](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/demo.py): `demo.py`
- [Video animation](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/animate.py): `animate.py`
- [Reconstruction](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/reconstruction.py): `reconstruction.py`
- [Training loop](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/train.py): `train.py`

### Video data

- [Frame datasets](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/frames_dataset.py): `frames_dataset.py`
- [Clip transforms](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/augmentation.py): `augmentation.py`
- [Taichi loader](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/data/taichi-loading/load_videos.py): `data/taichi-loading/load_videos.py`
- [Crop suggestions](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/crop-video.py): `crop-video.py`

### Motion models

- [Keypoint detector](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/keypoint_detector.py): `modules/keypoint_detector.py`
- [Dense motion](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/dense_motion.py): `modules/dense_motion.py`
- [Occlusion-aware generator](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/generator.py): `modules/generator.py`
- [Discriminator](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/discriminator.py): `modules/discriminator.py`
- [Model losses](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/model.py): `modules/model.py`
- [Network blocks](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/modules/util.py): `modules/util.py`

### Training support

- [Logger and visualizer](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/logger.py): `logger.py`
- [Synchronized normalization](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/sync_batchnorm/batchnorm.py): `sync_batchnorm/batchnorm.py`
- [Normalization communication](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/sync_batchnorm/comm.py): `sync_batchnorm/comm.py`
- [Parallel replication](https://github.com/aliaksandrsiarohin/first-order-model/blob/master/sync_batchnorm/replicate.py): `sync_batchnorm/replicate.py`

### Other components

- **User**
- **Model checkpoint**
- **Animation or reconstruction**
- **Video datasets**
- **Face alignment library**

## Connections

- User → Mode dispatcher: selects mode (evidence: `README.md`)
- User → Animation demo: provides inputs (evidence: `README.md`)
- Mode dispatcher → Training loop: dispatches training (evidence: `run.py`)
- Mode dispatcher → Video animation: dispatches animation (evidence: `run.py`)
- Mode dispatcher → Reconstruction: dispatches reconstruction (evidence: `run.py`)
- Mode dispatcher → Frame datasets: loads data (evidence: `run.py`)
- Animation demo → Keypoint detector: detects keypoints (evidence: `demo.py`)
- Animation demo → Occlusion-aware generator: generates frames (evidence: `demo.py`)
- Animation demo → Video animation: uses animation logic (evidence: `demo.py`)
- Video animation → Frame datasets: pairs videos (evidence: `animate.py`)
- Video animation → Keypoint detector: detects motion (evidence: `animate.py`)
- Video animation → Occlusion-aware generator: generates frames (evidence: `animate.py`)
- Video animation → Animation or reconstruction: writes animation (evidence: `README.md`)
- Reconstruction → Frame datasets: loads data (evidence: `run.py`)
- Reconstruction → Animation or reconstruction: writes reconstruction (evidence: `README.md`)
- Frame datasets → Clip transforms: transforms clips (evidence: `frames_dataset.py`)
- Video datasets → Frame datasets: provides videos (evidence: `README.md`)
- User → Crop suggestions: requests crop guidance (evidence: `README.md`)
- Crop suggestions → Face alignment library: uses (evidence: `README.md`)
- User → Taichi loader: prepares Taichi data (evidence: `README.md`)
- Frame datasets → Training loop: supplies batches (evidence: `train.py`)
- Training loop → Model losses: computes objectives (evidence: `train.py`)
- Model losses → Keypoint detector: evaluates detector (evidence: `modules/model.py`)
- Model losses → Occlusion-aware generator: evaluates generator (evidence: `modules/model.py`)
- Model losses → Discriminator: evaluates discriminator (evidence: `modules/model.py`)
- Occlusion-aware generator → Dense motion: estimates deformation (evidence: `modules/generator.py`)
- Occlusion-aware generator → Network blocks: uses blocks (evidence: `modules/generator.py`)
- Dense motion → Network blocks: uses geometry operations (evidence: `modules/dense_motion.py`)
- Keypoint detector → Network blocks: uses geometry operations (evidence: `modules/keypoint_detector.py`)
- Discriminator → Network blocks: uses blocks (evidence: `modules/discriminator.py`)
- Network blocks → Synchronized normalization: normalizes activations (evidence: `modules/util.py`)
- Synchronized normalization → Normalization communication: synchronizes statistics (evidence: `sync_batchnorm/batchnorm.py`)
- Parallel replication → Synchronized normalization: registers replicas (evidence: `sync_batchnorm/__init__.py`)
- Training loop → Logger and visualizer: logs training (evidence: `train.py`)
- Video animation → Logger and visualizer: visualizes frames (evidence: `animate.py`)
- Training loop → Model checkpoint: saves checkpoints
- Model checkpoint → Video animation: loads model state (evidence: `animate.py`)
- Model checkpoint → Reconstruction: loads model state (evidence: `reconstruction.py`)
- User → Model checkpoint: provides pretrained weights (evidence: `README.md`)
