Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
takerum committed Feb 2, 2018
0 parents commit ab97f6b
Show file tree
Hide file tree
Showing 56 changed files with 4,796 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LICENSE.md
@@ -0,0 +1,9 @@
IT License

Copyright (c) 2018 Preferred Networks, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
82 changes: 82 additions & 0 deletions README.md
@@ -0,0 +1,82 @@
[//]: <links>
[sngans]: https://openreview.net/forum?id=B1QRgziT-
[pcgans]: https://openreview.net/forum?id=ByS1VpgRZ

# GANs with spectral normalization and projection discriminator
*NOTE: The setup and example code in this README are for training GANs on **single GPU**.*
*The models used in the example code are smaller than the ones used in the [papers](https://github.pfidev.jp/miyato/sngans_projection#references).*
*Please go to [**link**](https://github.pfidev.jp/miyato/sngans_projection/blob/master/README_paper.md) if you are looking for how to reproduce the results in the papers.*

<img src="https://github.pfidev.jp/miyato/gan_imagenet/blob/master/demo/dog_and_cat_1x1_short.gif" width="128">

Official Chainer implementation for conditional image generation on ILSCRC2012 dataset (ImageNet) with [spectral normalization][sngans] and [projection discrimiantor][pcgans].

### Demo movies

Consecutive category morphing movies:
- (5x5 panels 128px images) https://www.youtube.com/watch?v=q3yy5Fxs7Lc
- (10x10 panels 128px images) https://www.youtube.com/watch?v=83D_3WXpPjQ

### Other materials
- [Generated images](https://drive.google.com/drive/folders/1ZzQctZ-loDf9wHJHX90xNN02-_BCYtB-?usp=sharing)
- [from the model trained on all ImageNet images (1K categories), 128px](https://drive.google.com/drive/folders/1Mr-fYW0-9QbwKYlIaiFUtgcN6n9qhY8l?usp=sharing)
- [from the model trained on dog and cat images (143 categories), 128px](https://drive.google.com/drive/folders/1yA3xWJqWRvhnhkvJsKF3Xbb-2LO4JrJw?usp=sharing)
- [Pretrained models](https://drive.google.com/drive/folders/1xZoL48uFOCnTxNGdknEYqE5YX0ZyoUej?usp=sharing)
- [Movies](https://drive.google.com/drive/folders/1yhV8_VbOcs2rkiMTstO4RHqp4YRnzg6c?usp=sharing)
- 4 corner category morph.

<img src="https://github.pfidev.jp/miyato/gan_imagenet/blob/master/demo/interpolated_images_4.png" width="432"> <img src="https://github.pfidev.jp/miyato/gan_imagenet/blob/master/demo/interpolated_images_24.png" width="432">

### References
- Takeru Miyato, Toshiki Kataoka, Masanori Koyama, Yuichi Yoshida. *Spectral Normalization for Generative Adversarial Networks*. ICLR2018. [OpenReview][sngans]
- Takeru Miyato, Masanori Koyama. *cGANs with Projection Discriminator*. ICLR2018. [OpenReview][pcgans]

## Setup

### Install required python libraries:

`pip install -r requirements.txt`
### Download ImageNet dataset
Please download ILSVRC2012 dataset from http://image-net.org/download-images

### Preprocess dataset:
```
cd datasets
IMAGENET_TRAIN_DIR=/path/to/imagenet/train/
PREPROCESSED_DATA_DIR=/path/to/save_dir/
bash preprocess.sh $IMAGENET_TRAIN_DIR $PREPROCESSED_DATA_DIR
# Make the list of image-label pairs for dog and cat images (143 categories, 180373 images).
puthon imagenet_dog_and_cat.py $PREPROCESSED_DATA_DIR
```
### Download inception model:

`python source/inception/download.py --outfile=datasets/inception_model`

## Training examples

### Spectral normalization + projection discriminator for 64x64 dog and cat images:
```
LOGDIR = /path/to/logdir
CONFIG = configs/sn_projection_dog_and_cat_64.yml
python train.py --config=$CONFIG --results_dir=$LOGDIR --data_dir=$PREPROCESSED_DATA_DIR
```

### Spectral normalization + projection discriminator for 64x64 all ImageNet images:
```
LOGDIR = /path/to/logdir
CONFIG = configs/sn_projection_64.yml
python train.py --config=$CONFIG --results_dir=$LOGDIR --data_dir=$PREPROCESSED_DATA_DIR
```

## Evaluation
### Calculate inception score (with the original OpenAI implementation)
```
python evaluations/calc_inception_score.py --config=$CONFIG --snapshot=${LOGDIR}/ResNetGenerator_<iterations>.npz --results_dir=${LOGDIR}/inception_score --splits=10 --tf
```

### Generate images and save them in `${LOGDIR}/gen_images`
```
python evaluations/gen_images.py --config=$CONFIG --snapshot=${LOGDIR}/ResNetGenerator_<iterations>.npz --results_dir=${LOGDIR}/gen_images
```


94 changes: 94 additions & 0 deletions README_paper.md
@@ -0,0 +1,94 @@
# The code for reproducing the ImageNet results in the ICLR2018 papers; [spectral normalization][sngans] and [projection discrimiantor][pcgans]

Official Chainer implementation for reproducing the results of conditional image generation on ILSCRC2012 dataset (ImageNet) with [spectral normalization][sngans] and [projection discrimiantor][pcgans].

### References
- Takeru Miyato, Toshiki Kataoka, Masanori Koyama, Yuichi Yoshida. *Spectral Normalization for Generative Adversarial Networks*. ICLR2018. [OpenReview][sngans]
- Takeru Miyato, Masanori Koyama. *cGANs with Projection Discriminator*. ICLR2018. [OpenReview][pcgans]

## Setup
### Install OpenMPI and NCCL (required for multi-GPU training with [ChainerMN](https://github.com/chainer/chainermn))
Please see the following installation guide: https://chainermn.readthedocs.io/en/latest/installation/guide.html#requirements

(Note: we provide the single GPU training code [here](https://github.pfidev.jp/miyato/sngans_projection/blob/master/README_paper.md#training), but we have not checked the peformance of the models trained on single GPU.
All of the results showed in [the papers](https://github.pfidev.jp/miyato/gan_imagenet#references) are produced by the models trained on 4 GPUs)
### Install required python libraries:

`pip install -r requirements_paper.txt`
### Download ImageNet dataset
Please download ILSVRC2012 dataset from http://image-net.org/download-images

### Preprocess dataset:
```
cd datasets
IMAGENET_TRAIN_DIR=/path/to/imagenet/train/
PREPROCESSED_DATA_DIR=/path/to/save_dir/
bash preprocess.sh $IMAGENET_TRAIN_DIR $PREPROCESSED_DATA_DIR
# Make the list of image-label pairs for all images (1000 categories, 1281167 images).
python imagenet.py $PREPROCESSED_DATA_DIR
# (optional) Make the list of image-label pairs for dog and cat images (143 categories, 180373 images).
puthon imagenet_dog_and_cat.py $PREPROCESSED_DATA_DIR
```
### Download inception model:

`python source/inception/download.py --outfile=datasets/inception_model`

## Training

### Spectral normalization + projection discriminator for 128x128 all ImageNet images:
```
LOGDIR = /path/to/logdir/
CONFIG = configs/sn_projection_dog_and_cat.yml
# multi-GPU
mpiexec -n 4 python train_mn.py --config=configs/sn_projection.yml --results_dir=$LOGDIR --data_dir=$PREPROCESSED_DATA_DIR
# single-GPU
python train.py --config=$CONFIG --results_dir=$LOGDIR --data_dir=$PREPROCESSED_DATA_DIR
```
- [pretrained models](https://drive.google.com/drive/folders/1m04Db3HbN10Tz5XHqiPpIg8kw23fkbSi)
- [generated images at 450K iterations](https://drive.google.com/drive/folders/1Mr-fYW0-9QbwKYlIaiFUtgcN6n9qhY8l) (Inception score:29.7, Intra class FID:103.1)
- [generated images at 850K iterations](https://drive.google.com/drive/folders/1-PbUUnrII9vUmcTUwYVYUtiwjiixbXpP) (Inception score:36.8, Intra class FID:92.4)
- Examples of generated images at 450K iterations:

![image](https://github.pfidev.jp/miyato/gan_imagenet/blob/master/demo/various_images.jpg)


### Spectral normalization + concat discriminator for 128x128 all ImageNet images:
```
LOGDIR = /path/to/logdir/
CONFIG = configs/sn_projection_dog_and_cat.yml
# multi-GPU
mpiexec -n 4 python train_mn.py --config=configs/sn_concat --results_dir=$LOGDIR --data_dir=$PREPROCESSED_DATA_DIR
# single-GPU
python train.py --config=$CONFIG --results_dir=$LOGDIR --data_dir=$PREPROCESSED_DATA_DIR
```
- [pretrained models](https://drive.google.com/drive/folders/1xInDUt8nFkq7VWUeIgnqvu2o2ZcsBB-2)
- [generated images at 450K iterations](https://drive.google.com/drive/folders/11TGLERZsfuVavfgV-dVtYJsUznq2mVIL) (Inception score:21.1, Intra class FID:141.2)

### (optional) Spectral normalization + projection discriminator for 128x128 dog and cat images:
```
LOGDIR = /path/to/logdir/
CONFIG = configs/sn_projection_dog_and_cat.yml
# multi-GPU
mpiexec -n 4 python train_mn.py --config=configs/sn_projection_dog_and_cat.yml --results_dir=$LOGDIR --data_dir=$PREPROCESSED_DATA_DIR
# single-GPU
python train.py --config=$CONFIG --results_dir=$LOGDIR --data_dir=$PREPROCESSED_DATA_DIR
```
- [pretrained models](https://drive.google.com/drive/folders/1wKMG6ontP8ZKdBYOA8l-z_JQUUpuA7XA)
- [generated images](https://drive.google.com/drive/folders/1yA3xWJqWRvhnhkvJsKF3Xbb-2LO4JrJw?usp=sharing) (Inception score: 28.2)

## Evaluations

### Calculate inception score (with the original OpenAI implementation)
```
python evaluations/calc_inception_score.py --config=./configs/sn_projection.yml --snapshot=${LOGDIR}/ResNetGenerator_<iterations>.npz --results_dir=${LOGDIR}/inception_score --splits=10 --tf
```

### Generate images and save them in `${LOGDIR}/gen_images`
```
python evaluations/gen_images.py --config=./configs/sn_projection.yml --snapshot=${LOGDIR}/ResNetGenerator_<iterations>.npz --results_dir=${LOGDIR}/gen_images
```
(If you want to use the pretrained model for the image generation, please download the pretraind model from [link](https://drive.google.com/drive/folders/1xZoL48uFOCnTxNGdknEYqE5YX0ZyoUej?usp=sharing) and set the `snapshot` argument to the path to the downloaded pretrained model file (.npz).)


[sngans]: https://openreview.net/forum?id=B1QRgziT-
[pcgans]: https://openreview.net/forum?id=ByS1VpgRZ
Empty file added __init__.py
Empty file.
48 changes: 48 additions & 0 deletions configs/sn_concat.yml
@@ -0,0 +1,48 @@
batchsize: 16
iteration: 450000
iteration_decay_start: 400000
seed: 0
display_interval: 100
snapshot_interval: 10000
evaluation_interval: 5000

models:
generator:
fn: gen_models/resnet.py
name: ResNetGeneratorImageNet
args:
dim_z: 128
bottom_width: 4
ch: 64
n_classes: 1000


discriminator:
fn: dis_models/snresnet.py
name: SNResNetConcatDiscriminator
args:
ch: 64
n_classes: 1000

dataset:
dataset_fn: datasets/imagenet.py
dataset_name: ImageNetDataset
args:
path: datasets/image_list.txt
size: 128
resize_method: bilinear
augmentation: False

adam:
alpha: 0.0002
beta1: 0.0
beta2: 0.9

updater:
fn: updater.py
name: Updater
args:
n_dis: 5
n_gen_samples: 32
conditional: True
loss_type: hinge
48 changes: 48 additions & 0 deletions configs/sn_projection.yml
@@ -0,0 +1,48 @@
batchsize: 16
iteration: 450000
iteration_decay_start: 400000
seed: 0
display_interval: 100
snapshot_interval: 10000
evaluation_interval: 5000

models:
generator:
fn: gen_models/resnet.py
name: ResNetGeneratorImageNet
args:
dim_z: 128
bottom_width: 4
ch: 64
n_classes: 1000


discriminator:
fn: dis_models/snresnet.py
name: SNResNetProjectionDiscriminator
args:
ch: 64
n_classes: 1000

dataset:
dataset_fn: datasets/imagenet.py
dataset_name: ImageNetDataset
args:
path: datasets/image_list.txt
size: 128
resize_method: bilinear
augmentation: False

adam:
alpha: 0.0002
beta1: 0.0
beta2: 0.9

updater:
fn: updater.py
name: Updater
args:
n_dis: 5
n_gen_samples: 32
conditional: True
loss_type: hinge
48 changes: 48 additions & 0 deletions configs/sn_projection_64.yml
@@ -0,0 +1,48 @@
batchsize: 64
iteration: 250000
iteration_decay_start: 00000
seed: 0
display_interval: 100
snapshot_interval: 10000
evaluation_interval: 5000

models:
generator:
fn: gen_models/resnet_64.py
name: ResNetGeneratorImageNet
args:
dim_z: 128
bottom_width: 4
ch: 64
n_classes: 1000


discriminator:
fn: dis_models/snresnet_64.py
name: SNResNetProjectionDiscriminator
args:
ch: 64
n_classes: 1000

dataset:
dataset_fn: datasets/imagenet.py
dataset_name: ImageNetDataset
args:
path: datasets/image_list.txt
size: 64
resize_method: bilinear
augmentation: False

adam:
alpha: 0.0002
beta1: 0.0
beta2: 0.9

updater:
fn: updater.py
name: Updater
args:
n_dis: 5
n_gen_samples: 64
conditional: True
loss_type: hinge
48 changes: 48 additions & 0 deletions configs/sn_projection_dog_and_cat.yml
@@ -0,0 +1,48 @@
batchsize: 16
iteration: 450000
iteration_decay_start: 400000
seed: 0
display_interval: 100
snapshot_interval: 10000
evaluation_interval: 5000

models:
generator:
fn: gen_models/resnet_small.py
name: ResNetGeneratorImageNet
args:
dim_z: 128
bottom_width: 4
ch: 64
n_classes: 143


discriminator:
fn: dis_models/snresnet_small.py
name: SNResNetProjectionDiscriminator
args:
ch: 64
n_classes: 143

dataset:
dataset_fn: datasets/imagenet_dog_and_cat.py
dataset_name: ImageNetDogAndCatDataset
args:
path: datasets/image_list_dog_and_cat.txt
size: 128
resize_method: bilinear
augmentation: True

adam:
alpha: 0.0002
beta1: 0.0
beta2: 0.9

updater:
fn: updater.py
name: Updater
args:
n_dis: 5
n_gen_samples: 32
conditional: True
loss_type: hinge

0 comments on commit ab97f6b

Please sign in to comment.