Skip to content

Commit 3ba6c16

Browse files
authored
Adding CycleGAN_Pytorch (pclubiitk#26)
* Add files via upload * Update README.md * Add files via upload * Delete README.md * Delete utils.py * Delete model.py * Delete main.py * Delete acc.png * Delete anchor.png * Delete bounding.png * Delete mix2.png * Delete result.png * Delete result2.png * Delete result3.png * Add files via upload * Add files via upload * Update main.py * Delete CycleGAN_loss.png * Delete discriminator_layers.png * Delete epoch30.jpg * Delete epoch40.jpg * Delete generator.png * Delete loss.png * Delete README.md * Delete dataset.sh * Delete main.py * Delete model.py * Delete utils.py * Add files via upload * Update main.py * Add files via upload * Add files via upload
1 parent 882a1c7 commit 3ba6c16

12 files changed

+804
-0
lines changed

CycleGAN_PyTorch/README.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Pytorch Implementation of CycleGAN
2+
Download Dataset(horse2zebra,summer2winter_yosemite)
3+
```bash
4+
$ sh ./dataset.sh horse2zebra
5+
```
6+
## Usage
7+
To run training
8+
```bash
9+
$ python main.py --training True --epochs 40
10+
```
11+
For testing
12+
```bash
13+
$ python main.py --testing True
14+
```
15+
> **_NOTE:_** on Colab Notebook use following command:
16+
```python
17+
!git clone link-to-repo
18+
%run main.py --training True --epochs 40
19+
%run main.py --testing True
20+
```
21+
## Help log
22+
```
23+
usage: main.py [-h] [--epochs EPOCHS] [--decay_epoch DECAY_EPOCH]
24+
[--batch_size BATCH_SIZE] [--lr LR] [--load_height LOAD_HEIGHT]
25+
[--load_width LOAD_WIDTH] [--gpu_ids GPU_IDS]
26+
[--crop_height CROP_HEIGHT] [--crop_width CROP_WIDTH]
27+
[--lamda LAMDA] [--idt_coef IDT_COEF] [--training TRAINING]
28+
[--testing TESTING] [--results_dir RESULTS_DIR]
29+
[--dataset_dir DATASET_DIR] [--checkpoint_dir CHECKPOINT_DIR]
30+
[--norm NORM] [--no_dropout] [--ngf NGF] [--ndf NDF]
31+
[--gen_net GEN_NET] [--dis_net DIS_NET]
32+
33+
cycleGAN PyTorch
34+
35+
optional arguments:
36+
-h, --help show this help message and exit
37+
--epochs EPOCHS
38+
--decay_epoch DECAY_EPOCH
39+
--batch_size BATCH_SIZE
40+
--lr LR
41+
--load_height LOAD_HEIGHT
42+
--load_width LOAD_WIDTH
43+
--gpu_ids GPU_IDS
44+
--crop_height CROP_HEIGHT
45+
--crop_width CROP_WIDTH
46+
--lamda LAMDA
47+
--idt_coef IDT_COEF
48+
--training TRAINING
49+
--testing TESTING
50+
--results_dir RESULTS_DIR
51+
--dataset_dir DATASET_DIR
52+
--checkpoint_dir CHECKPOINT_DIR
53+
--norm NORM instance normalization or batch normalization
54+
--no_dropout no dropout for the generator
55+
--ngf NGF # of gen filters in first conv layer
56+
--ndf NDF # of discrim filters in first conv layer
57+
--gen_net GEN_NET
58+
--dis_net DIS_NET
59+
60+
```
61+
## Contributed by:
62+
* [Mridul Dubey](https://github.com/mridul911)
63+
## References
64+
65+
* **Title**: Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks
66+
* **Authors**: Jun-Yan Zhu, Taesung Park, Phillip Isola, Alexei A. Efros
67+
* **Link**: https://arxiv.org/pdf/1703.10593.pdf
68+
* **Year**: 2017
69+
70+
# Summary
71+
72+
## **Introduction**
73+
CycleGANs give us a way to learn the mapping between one image domain and another using an unsupervised approach. A CycleGAN is designed for image-to-image translation and it learns from unpaired training data. This means that in order to train a generator to translate images from domain X to domain Y , we do not have to have exact correspondences between individual images in those domains. For example, in the paper that introduced CycleGANs, the authors are able to translate between images of horses and zebras, even though there are no images of a zebra in exactly the same position as a horse or with exactly the same background, etc. Thus, CycleGANs enable learning a mapping from one domain X to another domain Y without having to find perfectly-matched, training pairs!
74+
## **Model**
75+
CycleGAN is a Generative Adversarial Network (GAN) that uses two generators and two discriminators.We call one generator G, and have it convert images from the X domain to the Y domain. The other generator is called F, and converts images from Y to X.Each generator has a corresponding discriminator, which attempts to tell apart its synthesized images from real ones.
76+
### **Discriminators**
77+
The discriminators are PatchGANs, fully convolutional neural networks that look at a “patch” of the input image, and output the probability of the patch being “real”. This is both more computationally efficient than trying to look at the entire input image, and is also more effective — it allows the discriminator to focus on more surface-level features, like texture, which is often the sort of thing being changed in an image translation task.
78+
79+
The discriminators, DX and DY , in this CycleGAN are convolutional neural networks that see an image and attempt to classify it as real or fake. In this case, real is indicated by an output close to 1 and fake as close to 0. The discriminators have the following architecture
80+
![4](./assets/discriminator_layers.png)
81+
### **Generators**
82+
83+
Each CycleGAN generator has three sections: an encoder, a transformer, and a decoder. The input image is fed directly into the encoder, which shrinks the representation size while increasing the number of channels. The encoder is composed of three convolution layers. The resulting activation is then passed to the transformer, a series of six residual blocks. It is then expanded again by the decoder, which uses two transpose convolutions to enlarge the representation size, and one output layer to produce the final image in RGB.
84+
![4](./assets/generator.png)
85+
86+
## **Loss Functions**
87+
Finding the discriminator and the generator losses are key to getting a CycleGAN to train.
88+
* The CycleGAN contains two mapping functions G:X→Y and F:Y→X , and associated adversarial discriminators DY and DX . (a) DY encourages G to translate X into outputs indistinguishable from domain Y , and vice versa for DX and F .
89+
* To further regularize the mappings, we introduce two cycle consistency losses that capture the intuition that if we translate from one domain to the other and back again we should arrive at where we started. (b) Forward cycle-consistency loss and (c) backward cycle-consistency loss.
90+
![4](./assets/CYCLEGAN_loss.png)
91+
### **Discriminator Losses**
92+
The discriminator losses will be mean squared errors between the output of the discriminator, given an image, and the target value, 0 or 1, depending on whether it should classify that image as fake or real. For example, for a real image, x, we can train DX by looking at how close it is to recognizing and image x as real using the mean squared error:
93+
94+
out_x = D_X(x)
95+
96+
real_err = torch.mean((out_x-1)**2)
97+
### **Generator Losses**
98+
Calculating the generator losses will look somewhat similar to calculating the discriminator loss; there will still be steps in which you generate fake images that look like they belong to the set of X images but are based on real images in set Y , and vice versa. You'll compute the "real loss" on those generated images by looking at the output of the discriminator as it's applied to these fake images; this time, your generator aims to make the discriminator classify these fake images as real images.
99+
### **Cycle Consistency Loss**
100+
In addition to the adversarial losses, the generator loss terms will also include the cycle consistency loss. This loss is a measure of how good a reconstructed image is, when compared to an original image.
101+
102+
Say you have a fake, generated image, x_hat, and a real image, y. You can get a reconstructed y_hat by applying G_XtoY(x_hat) = y_hat and then check to see if this reconstruction y_hat and the orginal image y match. For this, we recommed calculating the L1 loss, which is an absolute difference, between reconstructed and real images. You may also choose to multiply this loss by some weight value lambda_weight to convey its importance.
103+
104+
The total generator loss will be the sum of the generator losses and the forward and backward cycle consistency losses.
105+
![4](./assets/loss.png)
106+
# **Results**
107+
108+
## Images after 40 epochs(horse2zebra)
109+
( Real - Generated - Reconstructed)
110+
111+
![4](./assets/epoch40.jpg)
112+
## Images after 30 epochs(horse2zebra)
113+
(Real - Generated - Reconstructed)
114+
![4](./assets/epoch30.jpg)
115+
116+
**Train for more epochs for better result(default)**
140 KB
Loading

CycleGAN_PyTorch/assets/diff.png

97.3 KB
Loading
Loading

CycleGAN_PyTorch/assets/epoch30.jpg

88.9 KB
Loading

CycleGAN_PyTorch/assets/epoch40.jpg

79.6 KB
Loading

CycleGAN_PyTorch/assets/generator.png

152 KB
Loading

CycleGAN_PyTorch/assets/loss.png

64 KB
Loading

CycleGAN_PyTorch/dataset.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mkdir datasets
2+
FILE=$1
3+
4+
if [[ $FILE != "ae_photos" && $FILE != "apple2orange" && $FILE != "summer2winter_yosemite" && $FILE != "horse2zebra" && $FILE != "monet2photo" && $FILE != "cezanne2photo" && $FILE != "ukiyoe2photo" && $FILE != "vangogh2photo" && $FILE != "maps" && $FILE != "cityscapes" && $FILE != "facades" && $FILE != "iphone2dslr_flower" && $FILE != "ae_photos" ]]; then
5+
echo "Available datasets are: apple2orange, summer2winter_yosemite, horse2zebra, monet2photo, cezanne2photo, ukiyoe2photo, vangogh2photo, maps, cityscapes, facades, iphone2dslr_flower, ae_photos"
6+
exit 1
7+
fi
8+
9+
URL=https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets/$FILE.zip
10+
ZIP_FILE=./datasets/$FILE.zip
11+
TARGET_DIR=./datasets/$FILE/
12+
wget -N $URL -O $ZIP_FILE
13+
mkdir $TARGET_DIR
14+
unzip $ZIP_FILE -d ./datasets/
15+
rm $ZIP_FILE

0 commit comments

Comments
 (0)