A simple code of CycleGAN which is easy to read is implemented by TensorFlow
Please see the detail about the paper CycleGAN from here Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks.The method of original pix2pix needs lots of paired datasets. For example, sketch-to-cat translation, we should have the datasets about many cats and its corresponding sketches in pixelwise. Sometimes, paired datasets are very rare. Just like man-to-woman translation for pix2pix it's impossible, but the method of this paper(CycleGAN) it's become possible. The most innovation point of this paper is about that the kinds of datasets don't need paired image, instead it just need two domains of different kinds of samples. For example, one domain is about man, another is about woman.
The main method of this paper, please see the image below(from the paper). Inspiration of "Cycle" is from language to language translationDual Learning for Machine Translation. For example, French-to-English and English-to-French, they called it dual learning. In CycleGAN, datasets domain X and datasets domain Y translate each other, like this X-to-Y and Y-to-X.
In this code, we don't use the loss function of LSGAN, instead we use WGAN which has been proved that it can yield high quality results and faster convergence rate, the problem of mode collapse of GAN also be solved by WGAN. But WGAN has shotcoming that it must satisfy the 1-Lipschitz condition. WGAN-GP solve the problem of WGAN about 1-Lipschitz condition by gradient penalty, but it has a problem that the computation of gradient penalty cost too much time in training phase. In this code, we don't use gradient penalty, instead we use spectral Normalization which is published in ICLR2018, it not only can yield high quality results like WGAN-GP, but also cost less time in training phase.
Firstly, you should install python and install some packages of python like tensorflow, numpy, scipy, pillow etc.
Please use these commands to install:
- pip install tensorflow
- pip install numpy
- pip install scipy
- pip install pillow
Secondly, putting two domains of datasets into the folder X and Y. For example, put the datasets about man into X, and put the datasets about woman into Y. I will provide one datasets about man2woman, which is selected from CelebA. There are about 8000 images for man, and 10000 images for woman and the images i had croped and resized it to 256x256. Please download the datasets from here BaiDuYun
Because of my poor device, i just train the CycleGAN for 13 epochs, the result of man2woman as shown below, the result of woman2man looks not really well, so i don't show it.