pytorch-CycleGAN-and-pix2pix README

The project's README from github.com/junyanz/pytorch-CycleGAN-and-pix2pix. As one markdown file: README.md.




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

[Tensorflow] (by Harry Yang), [Tensorflow] (by Archit Rathore), [Tensorflow] (by Van Huy), [Tensorflow] (by Xiaowei Hu), [Tensorflow2] (by Zhenliang He), [TensorLayer1.0] (by luoxier), [TensorLayer2.0] (by zsdonghao), [Chainer] (by Yanghua Jin), [Minimal PyTorch] (by yunjey), [Mxnet] (by Ldpe2G), [lasagne/Keras] (by tjwei), [Keras] (by Simon Karlsson), [OneFlow] (by Ldpe2G)

pix2pix

[Tensorflow] (by Christopher Hesse), [Tensorflow] (by Eyyüb Sariu), [Tensorflow (face2face)] (by Dat Tran), [Tensorflow (film)] (by Arthur Juliani), [Tensorflow (zi2zi)] (by Yuchen Tian), [Chainer] (by mattya), [tf/torch/keras/lasagne] (by tjwei), [Pytorch] (by taey16)

Prerequisites

Getting Started

Installation

git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
cd pytorch-CycleGAN-and-pix2pix
conda env create -f environment.yml

and then activate the environment by

conda activate pytorch-img2img

CycleGAN train/test

bash ./datasets/download_cyclegan_dataset.sh maps
#!./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.

#!./scripts/test_cyclegan.sh
python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan

pix2pix train/test

bash ./datasets/download_pix2pix_dataset.sh facades
#!./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.

#!./scripts/test_pix2pix.sh
python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA

Apply a pre-trained model (CycleGAN)

bash ./scripts/download_cyclegan_model.sh horse2zebra
bash ./datasets/download_cyclegan_dataset.sh horse2zebra
python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout

Apply a pre-trained model (pix2pix)

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

bash ./scripts/download_pix2pix_model.sh facades_label2photo
bash ./datasets/download_pix2pix_dataset.sh facades
python test.py --dataroot ./datasets/facades/ --direction BtoA --model pix2pix --name facades_label2photo_pretrained

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

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.

pytorch-CycleGAN-and-pix2pix (junyanz/pytorch-CycleGAN-and-pix2pix)

Searching Database…

这是做什么的?两个经典图像到图像翻译模型 CycleGAN 和 pix2pix 的 PyTorch 开源实现。它们能把一种图像转换成另一种,比如把马变成斑马,或者把建筑标注图变成照片。
CycleGAN 和 pix2pix 是两个不同的模型,我该在什么时候用哪个?CycleGAN 从两批不需要配对的图像集合中学习转换,因此不需要一一对应的图片;pix2pix 则需要用你数据集中输入和输出一一对应的图片对来训练。
我怎么把它跑起来?克隆仓库,安装依赖,用自带的脚本下载数据集,然后运行训练和测试脚本——测试结果会保存到 ./results/ 下的一个 HTML 页面里。另外还提供预构建的 Docker 镜像和 Colab 笔记本。
我需要先装好哪些东西?需要 Python 3 和 PyTorch 2.4+;项目提供了 Conda 环境文件来配置依赖,如果你愿意,也有预构建的 Docker 和 Repl 可选。
在 Windows 上能运行吗?运行前提里只列出了 Linux 或 macOS,所以 Windows 不在官方支持范围内。
我需要 NVIDIA 显卡吗?不需要——运行前提里 CPU 也是一种选择,不过如果想让训练时间合理一些,还是需要一块支持 CUDA CuDNN 的 NVIDIA 显卡。
我不自己训练,能直接试用吗?可以——预训练模型用自带的脚本就能下载,比如 CycleGAN 的 horse2zebra,或 pix2pix 在 Facades 上的 label2photo。模型文件会下载到 ./checkpoints/horse2zebra_pretrained/latest_net_G.pth,结果写入 ./results/ 下的一个 HTML 页面。用 Facades 模型时必须加上 --direction BtoA 参数,因为它的 A 到 B 方向是从照片到标注图。
我想用自己收集的图片来训练,该怎么做?用自带脚本下载数据集,或者准备你自己的数据,然后运行训练脚本即可。中间结果会写到 ./checkpoints 下的一个 HTML 页面;加上 --use_wandb 参数还可以用 W&B 记录训练日志;如果你想要自定义的网络结构或数据加载器,项目还提供了模型模板和数据集模板。
训练支持多张显卡吗?支持——从 2025 年更新开始,可以在单机上用 torchrun --nproc_per_node=4 train.py 启动 DDP,并且必须把 --norm 换成 sync_batch(或 sync_instance),因为普通的批归一化和 DDP 不兼容。
同一批作者有没有更快的替代方案?有——img2img-turbo 基于 StableDiffusion-Turbo 实现一步式图像到图像翻译,同时支持配对和非配对训练:在 A6000 上每张 512x512 图片只需 0.29 秒,在 A100 上只需 0.11 秒。作者还推荐了 CUT,用于快速且省显存的非配对训练。
Creator
junyanz
Size
8.3 MB
Stars
25.3K
Updated
CommitAug 06
Age
9y
Commits · 90 Days