# neuralchen/simswap architecture

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

- Interactive diagram: https://gitdiagram.com/neuralchen/simswap
- Repository: https://github.com/neuralchen/simswap

## Overview

##### Main Components
a. Data Layer
- Raw face datasets (e.g., VGGFace2-224, VGGFace2-HQ) stored locally or on cloud drives
- Pretrained weight artifacts (arcface_checkpoint.tar, SimSwap checkpoints)
b. Preprocessing Layer
- insightface_func: face detection & cropping (single & multi)
- parsing_model: face‐parsing network (resnet-based segmentation)
- util modules (alignment, resizing, normalization)
c. Core Modeling Layer
- Feature extractor: ArcFace model (arcface_models.py)
- Generator: fs_networks*.py variants (224, 512, fix)
- Projected Discriminator: pg_modules/projected_discriminator.py
- Loss & projection heads: projectionhead.py, base_model.py
d. Orchestration Layer
- options/…: command‐line argument parsing for train.py & predict.py
- train.py & predict.py: entry points for training and inference
- Notebook interfaces: train.ipynb, MultiSpecific.ipynb, SimSwap colab.ipynb
e. Post-processing & Visualization
- util/video_swap*.py, util/add_watermark.py, util/html.py, util/visualizer.py
- Output directory: frames/videos, HTML gallery
f. Deployment/Integration
- cog.yaml: deployment spec for Replicate/Docker
- download-weights.sh: bootstrap script
- docs/guidance: user docs & sample webpage

##### Relationships & Data Flows
- Data Layer → Preprocessing: videos/images are fed to face detector → cropped faces → aligned & parsed
- Preprocessing → Core Modeling (Training): batches of aligned, parsed faces → generator + discriminator → backprop → model artifacts
- Preprocessing → Core Modeling (Inference): feed source & target crops into generator → produce swapped face
- Core Modeling (Inference) → Post-processing: blend swapped face back into original frame → add watermark or mask → write out video/images
- Orchestration layer (CLI/notebooks) invokes pipelines in correct sequence, passing in options and I/O paths

##### Architectural Patterns & Design Principles
- Modular separation by function: “insightface_func,” “parsing_model,” “models,” “pg_modules,” “util”
- ML pipeline pattern: clearly defined stages (preprocess → model → postprocess)
- Config-driven CLI: options/*.py supply hyperparameters and file paths
- Extensibility: multiple network variants can be plugged via config.py and BaseModel

##### Technologies & Frameworks
- Python ≥3.6
- PyTorch ≥1.5; TorchVision
- NumPy, OpenCV, Pillow, moviepy, imageio
- insightface library for detection & recognition
- timm (0.5.4) for backbone nets
- Docker/Replicate (via cog.yaml) for containerized inference

##### Must-Haves
- Callouts for external dependencies (Google Drive links, Docker, Colab notebooks)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Layer
    subgraph "Data Layer" 
        direction TB
        Data_Datasets["Raw Face Datasets (VGGFace2, etc.)"]:::data
        Data_WeightsScript["download-weights.sh"]:::data
        Data_CogSpec["cog.yaml"]:::data
    end

    %% Preprocessing Layer
    subgraph "Preprocessing Layer"
        direction TB
        FD_Single["Face Detect & Crop Single"]:::pre
        click FD_Single "https://github.com/neuralchen/simswap/blob/main/insightface_func/face_detect_crop_single.py"
        FD_Multi["Face Detect & Crop Multi"]:::pre
        click FD_Multi "https://github.com/neuralchen/simswap/blob/main/insightface_func/face_detect_crop_multi.py"
        Face_Align["Face Alignment Utils"]:::pre
        click Face_Align "https://github.com/neuralchen/simswap/blob/main/insightface_func/utils/face_align_ffhqandnewarc.py"
        Parsing_Model["Parsing Model (model.py)"]:::pre
        click Parsing_Model "https://github.com/neuralchen/simswap/blob/main/parsing_model/model.py"
        Parsing_ResNet["Parsing ResNet (resnet.py)"]:::pre
        click Parsing_ResNet "https://github.com/neuralchen/simswap/blob/main/parsing_model/resnet.py"
        Util_Modules["Util: alignment/resizing/norm"]:::pre
        click Util_Modules "https://github.com/neuralchen/simswap/blob/main/util/util.py"
        click Util_Modules "https://github.com/neuralchen/simswap/blob/main/util/norm.py"
        click Util_Modules "https://github.com/neuralchen/simswap/blob/main/util/reverse2original.py"
        click Util_Modules "https://github.com/neuralchen/simswap/blob/main/util/image_pool.py"
    end

    %% Core Modeling Layer
    subgraph "Core Modeling Layer"
        direction TB
        ArcFace["ArcFace Feature Extractor"]:::model
        click ArcFace "https://github.com/neuralchen/simswap/blob/main/models/arcface_models.py"
        Generator_224["Generator fs_networks.py"]:::model
        click Generator_224 "https://github.com/neuralchen/simswap/blob/main/models/fs_networks.py"
        Generator_512["Generator fs_networks_512.py"]:::model
        click Generator_512 "https://github.com/neuralchen/simswap/blob/main/models/fs_networks_512.py"
        Generator_Fix["Generator fs_networks_fix.py"]:::model
        click Generator_Fix "https://github.com/neuralchen/simswap/blob/main/models/fs_networks_fix.py"
        Discriminator["Projected Discriminator"]:::model
        click Discriminator "https://github.com/neuralchen/simswap/blob/main/pg_modules/projected_discriminator.py"
        BaseModel["BaseModel & Loss"]:::model
        click BaseModel "https://github.com/neuralchen/simswap/blob/main/models/base_model.py"
        ProjectionHead["Projection Head"]:::model
        click ProjectionHead "https://github.com/neuralchen/simswap/blob/main/models/projectionhead.py"
        Model_Utils["Additional Model Utilities"]:::model
        click Model_Utils "https://github.com/neuralchen/simswap/blob/main/models/fs_model.py"
        click Model_Utils "https://github.com/neuralchen/simswap/blob/main/models/projected_model.py"
        click Model_Utils "https://github.com/neuralchen/simswap/blob/main/models/pix2pixHD_model.py"
        click Model_Utils "https://github.com/neuralchen/simswap/blob/main/models/models.py"
    end

    %% Orchestration Layer
    subgraph "Orchestration Layer"
        direction TB
        CLI_Options["Options (base/train/test)"]:::orch
        click CLI_Options "https://github.com/neuralchen/simswap/blob/main/options/base_options.py"
        click CLI_Options "https://github.com/neuralchen/simswap/blob/main/options/train_options.py"
        click CLI_Options "https://github.com/neuralchen/simswap/blob/main/options/test_options.py"
        Scripts["train.py & predict.py"]:::orch
        click Scripts "https://github.com/neuralchen/simswap/blob/main/train.py"
        click Scripts "https://github.com/neuralchen/simswap/blob/main/predict.py"
        Notebooks["Notebooks Interfaces"]:::orch
        click Notebooks "https://github.com/neuralchen/simswap/blob/main/train.ipynb"
        click Notebooks "https://github.com/neuralchen/simswap/blob/main/MultiSpecific.ipynb"
        click Notebooks "https://github.com/neuralchen/simswap/blob/main/SimSwap colab.ipynb"
    end

    %% Post-processing Layer
    subgraph "Post-processing & Visualization"
        direction TB
        VideoSwap["videoswap.py"]:::post
        click VideoSwap "https://github.com/neuralchen/simswap/blob/main/util/videoswap.py"
        VideoSwap_Multi["videoswap_multispecific.py"]:::post
        click VideoSwap_Multi "https://github.com/neuralchen/simswap/blob/main/util/videoswap_multispecific.py"
        VideoSwap_Specific["videoswap_specific.py"]:::post
        click VideoSwap_Specific "https://github.com/neuralchen/simswap/blob/main/util/videoswap_specific.py"
        Watermark["add_watermark.py"]:::post
        click Watermark "https://github.com/neuralchen/simswap/blob/main/util/add_watermark.py"
        HTML_Out["html.py"]:::post
        click HTML_Out "https://github.com/neuralchen/simswap/blob/main/util/html.py"
        Visualizer["visualizer.py"]:::post
        click Visualizer "https://github.com/neuralchen/simswap/blob/main/util/visualizer.py"
        Plot["plot.py"]:::post
        click Plot "https://github.com/neuralchen/simswap/blob/main/util/plot.py"
        Heatmap["save_heatmap.py"]:::post
        click Heatmap "https://github.com/neuralchen/simswap/blob/main/util/save_heatmap.py"
    end

    %% Deployment Layer
    subgraph "Deployment & Integration"
        direction TB
        Docs_Prep["docs/guidance/preparation.md"]:::deploy
        click Docs_Prep "https://github.com/neuralchen/simswap/blob/main/docs/guidance/preparation.md"
        Docs_Usage["docs/guidance/usage.md"]:::deploy
        click Docs_Usage "https://github.com/neuralchen/simswap/blob/main/docs/guidance/usage.md"
        Docs_Index["docs/index.html"]:::deploy
        click Docs_Index "https://github.com/neuralchen/simswap/blob/main/docs/index.html"
        Docs_CSS["docs/css/"]:::deploy
        click Docs_CSS "https://github.com/neuralchen/simswap/tree/main/docs/css/"
        Docs_JS["docs/js/"]:::deploy
        click Docs_JS "https://github.com/neuralchen/simswap/tree/main/docs/js/"
        Docs_IMG["docs/img/"]:::deploy
        click Docs_IMG "https://github.com/neuralchen/simswap/tree/main/docs/img/"
        Logo["simswaplogo/"]:::deploy
        click Logo "https://github.com/neuralchen/simswap/tree/main/simswaplogo/"
    end

    %% Data flows
    Data_Datasets -->|feeds images| FD_Single
    Data_Datasets -->|feeds images| FD_Multi
    FD_Single -->|crops| Util_Modules
    FD_Multi -->|crops| Util_Modules
    Util_Modules --> Parsing_Model
    Util_Modules --> Parsing_ResNet
    Parsing_Model --> ArcFace
    Parsing_ResNet --> ArcFace

    %% Training flow
    ArcFace --> Generator_224
    ArcFace --> Generator_512
    ArcFace --> Generator_Fix
    Generator_224 --> Discriminator
    Generator_512 --> Discriminator
    Generator_Fix --> Discriminator
    Discriminator --> BaseModel
    Generator_224 --> BaseModel
    ProjectionHead --> BaseModel
    BaseModel --> Model_Utils

    %% Inference flow
    FD_Single --> Generator_224
    FD_Single --> Generator_512
    FD_Single --> Generator_Fix
    Generator_224 --> VideoSwap
    Generator_512 --> VideoSwap
    Generator_Fix --> VideoSwap
    VideoSwap -->|blended frames| Watermark
    Watermark --> HTML_Out
    HTML_Out --> Visualizer

    %% Orchestration
    CLI_Options --> Scripts
    Scripts --> FD_Single
    Scripts --> Parsing_Model
    Scripts --> Generator_224
    Scripts --> VideoSwap
    Notebooks --> Scripts

    %% Deployment invocation
    Data_WeightsScript --> Scripts
    Data_CogSpec --> Scripts
    Docs_Prep --> Docs_Index
    Docs_Usage --> Docs_Index

    %% Styles
    classDef data fill:#ADD8E6;
    classDef pre fill:#90EE90;
    classDef model fill:#FFA500;
    classDef post fill:#DDA0DD;
    classDef orch fill:#FFB6C1;
    classDef deploy fill:#D3D3D3;
```
