pytorch-CycleGAN-and-pix2pix

junyanz长期未更新 有专家点评

PyTorch 版 CycleGAN 与 pix2pix 实现,提供图像到图像翻译的完整训练与测试流程,支持无配对和有配对数据转换。

Image-to-Image Translation in PyTorch

暂无版本25,280star +5 / 7 天6,555fork同步于 2026-11-10 23:24 UTC

专家点评

上手难度:进阶更新于 2026-11-11

PyTorch 里做无配对图像翻译的参考实现,顶替你自己从论文复现 CycleGAN 的那两周

适用场景

  • 研究复现与消融:要改 loss、换生成器结构、对比 patchGAN 感受野,这套代码的 model 模板和 options 分层是照着改的,自己搭一套反而要先花时间对齐论文细节
  • 小样本风格迁移/域适应:手头只有两个域各几百张无配对图,CycleGAN 路径能直接跑,换 diffusion 方案要重做数据配对和训练预算
  • 教学与基线:给新人讲 GAN 训练循环、讲 pix2pix 的配对监督,仓库里 train/test 脚本和预训练权重能当天出图,不用先训
  • 论文对比实验的 baseline:需要和已有工作在同一套数据加载与评估口径下比,直接复用它的 dataset 模板比自己写更省事

踩坑

  • 用预训练模型推理时,pix2pix 和自训模型必须显式传 --netG/--norm/--no_dropout 对齐生成器结构,否则加载权重会静默错配或直接报 shape 错;README 里写了但很容易跳过
  • 预训练权重和数据集要单独跑下载脚本,clone 完直接 test 会因缺文件失败;离线环境要提前把模型和数据集搬进去
  • issue 里反复出现环境类报错(torchvision 的 InterpolationMode 缺失、下载连接错误),说明依赖版本漂移是这套老代码的常态;按 environment.yml 建环境别自己拼版本
  • 高反应 issue 集中在 PatchGAN 判别器的原理提问上,说明这套代码默认读者懂 GAN 细节,改判别器前得先读论文,不能指望注释

同类对比

要的是高质量、可控的生成而不是 GAN 训练流程本身,看 diffusers 生态的 img2img/inpainting 管线;要的是现代无配对翻译且能接受更大训练成本,看 CUT/FastCUT 这类后续工作,它们针对 CycleGAN 的显存与训练时长问题做了改动

怎么上手

由 README 提炼,执行前请确认
git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
cd pytorch-CycleGAN-and-pix2pix
conda env create -f environment.yml
conda activate pytorch-img2img
硬件要求按 README 说明

README 未说明具体显存要求;支持单机多卡 GPU 训练(DDP),未列出 GPU 厂商或型号。

需要 CUDA多卡 / 张量并行
下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

PyTorch 版 CycleGAN 与 pix2pix,做无配对和有配对的图像到图像翻译

PyTorch 版 CycleGAN 与 pix2pix 实现,提供图像到图像翻译的完整训练与测试流程,支持无配对和有配对数据转换。

核心能力

  • 实现无配对翻译的 CycleGAN 与配对翻译的 pix2pix
  • 提供训练与测试脚本,支持自定义模型和数据集模板
  • 支持单机多卡 DDP 训练(torchrun --nproc_per_node)
  • 提供预训练模型下载脚本,可直接推理生成结果
  • 训练过程可选用 --use_wandb 记录到 W&B 面板

容易踩的坑

需 PyTorch 2.4+,旧版本要用 pytorch0.3.1 分支;用预训练模型前要先用脚本单独下载模型和对应数据集;用 pix2pix 或自训模型推理时需显式指定 --netG、--norm、--no_dropout 以匹配生成器结构。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    CycleGAN and pix2pix in PyTorch

    Udpate in 2025: we recently updated the code to support Python 3.11 and PyTorch 2.4. It also supports DDP for single-machine multiple-GPU training. (Please use torchrun --nproc_per_node=4 train.py ...)

    New: Please check out img2img-turbo repo that includes both pix2pix-turbo and CycleGAN-Turbo. Our new one-step image-to-image translation methods can support both paired and unpaired training and produce better results by leveraging the pre-trained StableDiffusion-Turbo model. The inference time for 512x512 image is 0.29 sec on A6000 and 0.11 sec on A100.

    Please check out contrastive-unpaired-translation (CUT), our new unpaired image-to-image translation model that enables fast and memory-efficient training.

    We provide PyTorch implementations for both unpaired and paired image-to-image translation.

    The code was written by Jun-Yan Zhu and Taesung Park, and supported by Tongzhou Wang.

    This PyTorch implementation produces results comparable to or better than our original Torch software. If you would like to reproduce the same results as in the papers, check out the original CycleGAN Torch and pix2pix Torch code in Lua/Torch.

    Note: The current software works well with PyTorch 2.4+. Check out the older branch that supports PyTorch 0.1-0.3.

    You may find useful information in training/test tips and frequently asked questions. To implement custom models and datasets, check out our templates. To help users better understand and adapt our codebase, we provide an overview of the code structure of this repository.

    CycleGAN: Project | Paper | Torch | Tensorflow Core Tutorial | PyTorch Colab

    Pix2pix: Project | Paper | Torch | Tensorflow Core Tutorial | PyTorch Colab

    EdgesCats Demo | pix2pix-tensorflow | by Christopher Hesse

    If you use this code for your research, please cite:

    Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks. Jun-Yan Zhu*, Taesung Park*, Phillip Isola, Alexei A. Efros. In ICCV 2017. (* equal contributions) [Bibtex]

    Image-to-Image Translation with Conditional Adversarial Networks. Phillip Isola, Jun-Yan Zhu, Tinghui Zhou, Alexei A. Efros. In CVPR 2017. [Bibtex]

    Talks and Course

    pix2pix slides: keynote | pdf, CycleGAN slides: pptx | pdf

    CycleGAN course assignment code and handout designed by Prof. Roger Grosse for CSC321 "Intro to Neural Networks and Machine Learning" at University of Toronto. Please contact the instructor if you would like to adopt it in your course.

    Colab Notebook

    TensorFlow Core CycleGAN Tutorial: Google Colab | Code

    TensorFlow Core pix2pix Tutorial: Google Colab | Code

    PyTorch Colab notebook: CycleGAN and pix2pix

    ZeroCostDL4Mic Colab notebook: CycleGAN and pix2pix

    Other implementations

    CycleGAN

    pix2pix

    Prerequisites

    • Linux or macOS
    • Python 3
    • CPU or NVIDIA GPU + CUDA CuDNN

    Getting Started

    Installation

    • Clone this repo:
    git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
    cd pytorch-CycleGAN-and-pix2pix
    
    • Install PyTorch and other dependencies. For Conda users, you can create a new Conda environment by
    conda env create -f environment.yml
    

    and then activate the environment by

    conda activate pytorch-img2img
    
    • For Docker users, we provide the pre-built Docker image and Dockerfile. Please refer to our Docker page.
    • For Repl users, please click Run on Repl.it.

    CycleGAN train/test

    • Download a CycleGAN dataset (e.g. maps):
    bash ./datasets/download_cyclegan_dataset.sh maps
    
    • To log training progress and test images to W&B dashboard, set the --use_wandb flag with training script
    • Train a model:
    #!./scripts/train_cyclegan.sh
    python train.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan --use_wandb
    

    To see more intermediate results, check out ./checkpoints/maps_cyclegan/web/index.html.

    • Test the model:
    #!./scripts/test_cyclegan.sh
    python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan
    
    • The test results will be saved to a html file here: ./results/maps_cyclegan/latest_test/index.html.

    pix2pix train/test

    • Download a pix2pix dataset (e.g.facades):
    bash ./datasets/download_pix2pix_dataset.sh facades
    
    • To log training progress and test images to W&B dashboard, set the --use_wandb flag with training script
    • Train a model:
    #!./scripts/train_pix2pix.sh
    python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA  --use_wandb
    

    To see more intermediate results, check out ./checkpoints/facades_pix2pix/web/index.html.

    • Test the model (bash ./scripts/test_pix2pix.sh):
    #!./scripts/test_pix2pix.sh
    python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA
    
    • The test results will be saved to a html file here: ./results/facades_pix2pix/test_latest/index.html. You can find more scripts at scripts directory.
    • To train and test pix2pix-based colorization models, please add --model colorization and --dataset_mode colorization. See our training tips for more details.

    Apply a pre-trained model (CycleGAN)

    • You can download a pretrained model (e.g. horse2zebra) with the following script:
    bash ./scripts/download_cyclegan_model.sh horse2zebra
    
    • The pretrained model is saved at ./checkpoints/{name}_pretrained/latest_net_G.pth. Check here for all the available CycleGAN models.
    • To test the model, you also need to download the horse2zebra dataset:
    bash ./datasets/download_cyclegan_dataset.sh horse2zebra
    
    • Then generate the results using
    python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout
    
    • The option --model test is used for generating results of CycleGAN only for one side. This option will automatically set --dataset_mode single, which only loads the images from one set. On the contrary, using --model cycle_gan requires loading and generating results in both directions, which is sometimes unnecessary. The results will be saved at ./results/. Use --results_dir {directory_path_to_save_result} to specify the results directory.

    • For pix2pix and your own models, you need to explicitly specify --netG, --norm, --no_dropout to match the generator architecture of the trained model. See this FAQ for more details.

    Apply a pre-trained model (pix2pix)

    Download a pre-trained model with ./scripts/download_pix2pix_model.sh.

    • Check here for all the available pix2pix models. For example, if you would like to download label2photo model on the Facades dataset,
    bash ./scripts/download_pix2pix_model.sh facades_label2photo
    
    • Download the pix2pix facades datasets:
    bash ./datasets/download_pix2pix_dataset.sh facades
    
    • Then generate the results using
    python test.py --dataroot ./datasets/facades/ --direction BtoA --model pix2pix --name facades_label2photo_pretrained
    
    • Note that we specified --direction BtoA as Facades dataset's A to B direction is photos to labels.

    • If you would like to apply a pre-trained model to a collection of input images (rather than image pairs), please use --model test option. See ./scripts/test_single.sh for how to apply a model to Facade label maps (stored in the directory facades/testB).

    • See a list of currently available models at ./scripts/download_pix2pix_model.sh

    Multi-GPU training

    To train a model on multiple GPUs, please use torchrun --nproc_per_node=4 train.py ... instead of python train.py .... We also need to use synchronized batchnorm by setting --norm sync_batch (or --norm sync_instance for instance normgalization). The --norm batch is not compatible with DDP.

    Docker

    We provide the pre-built Docker image and Dockerfile that can run this code repo. See docker.

    Datasets

    Download pix2pix/CycleGAN datasets and create your own datasets.

    Training/Test Tips

    Best practice for training and testing your models.

    Frequently Asked Questions

    Before you post a new question, please first look at the above Q & A and existing GitHub issues.

    Custom Model and Dataset

    If you plan to implement custom models and dataset for your new applications, we provide a dataset template and a model template as a starting point.

    Code structure

    To help users better understand and use our code, we briefly overview the functionality and implementation of each package and each module.

    Pull Request

    You are always welcome to contribute to this repository by sending a pull request. Please run flake8 --ignore E501 . and pytest scripts/test_before_push.py -v before you commit the code. Please also update the code structure overview accordingly if you add or remove files.

    Citation

    If you use this code for your research, please cite our papers.

    @inproceedings{CycleGAN2017,
      title={Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks},
      author={Zhu, Jun-Yan and Park, Taesung and Isola, Phillip and Efros, Alexei A},
      booktitle={Computer Vision (ICCV), 2017 IEEE International Conference on},
      year={2017}
    }
    
    
    @inproceedings{isola2017image,
      title={Image-to-Image Translation with Conditional Adversarial Networks},
      author={Isola, Phillip and Zhu, Jun-Yan and Zhou, Tinghui and Efros, Alexei A},
      booktitle={Computer Vision and Pattern Recognition (CVPR), 2017 IEEE Conference on},
      year={2017}
    }
    

    Other Languages

    Spanish

    Related Projects

    img2img-turbo contrastive-unpaired-translation (CUT) CycleGAN-Torch | pix2pix-Torch | pix2pixHD| BicycleGAN | vid2vid | SPADE/GauGAN iGAN | GAN Dissection | GAN Paint

    Cat Paper Collection

    If you love cats, and love reading cool graphics, vision, and learning papers, please check out the Cat Paper Collection.

    Acknowledgments

    Our code is inspired by pytorch-DCGAN.