From e8e07a3f0eac98956a18b86c45c7190e41ae59f3 Mon Sep 17 00:00:00 2001
From: Nataniel Ruiz
Date: Wed, 25 Dec 2019 19:08:33 -0400
Subject: [PATCH] next
---
cyclegan/.gitignore | 3 +
cyclegan/CycleGAN.ipynb | 255 ++++++++
cyclegan/README.md | 292 +++++----
cyclegan/data/__init__.py | 93 +++
cyclegan/data/aligned_data_loader.lua | 44 --
cyclegan/data/aligned_dataset.py | 60 ++
cyclegan/data/base_data_loader.lua | 53 --
cyclegan/data/base_dataset.py | 157 +++++
cyclegan/data/colorization_dataset.py | 68 ++
cyclegan/data/data.lua | 103 ---
cyclegan/data/data_util.lua | 24 -
cyclegan/data/dataset.lua | 398 ------------
cyclegan/data/donkey_folder.lua | 200 ------
cyclegan/data/image_folder.py | 67 ++
cyclegan/data/single_dataset.py | 40 ++
cyclegan/data/template_dataset.py | 75 +++
cyclegan/data/unaligned_data_loader.lua | 49 --
cyclegan/data/unaligned_dataset.py | 71 ++
cyclegan/docs/Dockerfile | 13 +
cyclegan/docs/README_es.md | 238 +++++++
cyclegan/docs/datasets.md | 44 ++
cyclegan/docs/docker.md | 38 ++
cyclegan/docs/overview.md | 45 ++
cyclegan/docs/qa.md | 110 ++++
cyclegan/docs/tips.md | 74 +++
cyclegan/environment.yml | 14 +
.../test_vangogh_style_on_ae_photos.sh | 19 -
cyclegan/examples/train_maps.sh | 26 -
cyclegan/models/__init__.py | 67 ++
cyclegan/models/architectures.lua | 384 -----------
cyclegan/models/base_model.lua | 66 --
cyclegan/models/base_model.py | 229 +++++++
cyclegan/models/bigan_model.lua | 254 --------
cyclegan/models/colorization_model.py | 68 ++
cyclegan/models/content_gan_model.lua | 201 ------
cyclegan/models/cycle_gan_model.lua | 324 ---------
cyclegan/models/cycle_gan_model.py | 194 ++++++
cyclegan/models/networks.py | 615 ++++++++++++++++++
cyclegan/models/one_direction_test_model.lua | 79 ---
cyclegan/models/pix2pix_model.lua | 228 -------
cyclegan/models/pix2pix_model.py | 127 ++++
cyclegan/models/template_model.py | 99 +++
cyclegan/models/test_model.py | 69 ++
cyclegan/options.lua | 143 ----
cyclegan/options/__init__.py | 1 +
cyclegan/options/base_options.py | 136 ++++
cyclegan/options/test_options.py | 24 +
cyclegan/options/train_options.py | 40 ++
cyclegan/pix2pix.ipynb | 265 ++++++++
cyclegan/pretrained_models/download_model.sh | 10 -
cyclegan/pretrained_models/download_vgg.sh | 6 -
cyclegan/requirements.txt | 4 +
cyclegan/scripts/conda_deps.sh | 4 +
cyclegan/scripts/download_cyclegan_model.sh | 11 +
cyclegan/scripts/download_pix2pix_model.sh | 10 +
cyclegan/scripts/edges/PostprocessHED.m | 77 +++
cyclegan/scripts/edges/batch_hed.py | 81 +++
.../caffemodel/deploy.prototxt} | 434 ++++++++----
.../scripts/eval_cityscapes/cityscapes.py | 141 ++++
.../scripts/eval_cityscapes/download_fcn8s.sh | 3 +
cyclegan/scripts/eval_cityscapes/evaluate.py | 68 ++
cyclegan/scripts/eval_cityscapes/util.py | 42 ++
cyclegan/scripts/install_deps.sh | 3 +
cyclegan/scripts/test_before_push.py | 51 ++
cyclegan/scripts/test_colorization.sh | 2 +
cyclegan/scripts/test_cyclegan.sh | 2 +
cyclegan/scripts/test_pix2pix.sh | 2 +
cyclegan/scripts/test_single.sh | 2 +
cyclegan/scripts/train_colorization.sh | 2 +
cyclegan/scripts/train_cyclegan.sh | 2 +
cyclegan/scripts/train_pix2pix.sh | 2 +
cyclegan/test.lua | 142 ----
cyclegan/test.py | 69 ++
cyclegan/train.lua | 178 -----
cyclegan/train.py | 78 +++
cyclegan/util/InstanceNormalization.lua | 106 ---
cyclegan/util/VGG_preprocess.lua | 27 -
cyclegan/util/__init__.py | 1 +
cyclegan/util/content_loss.lua | 87 ---
cyclegan/util/cudnn_convert_custom.lua | 72 --
cyclegan/util/get_data.py | 110 ++++
cyclegan/util/html.py | 86 +++
cyclegan/util/image_pool.lua | 59 --
cyclegan/util/image_pool.py | 54 ++
cyclegan/util/plot_util.lua | 44 --
cyclegan/util/util.lua | 266 --------
cyclegan/util/util.py | 103 +++
cyclegan/util/visualizer.lua | 63 --
cyclegan/util/visualizer.py | 221 +++++++
89 files changed, 5089 insertions(+), 3922 deletions(-)
create mode 100644 cyclegan/CycleGAN.ipynb
create mode 100644 cyclegan/data/__init__.py
delete mode 100644 cyclegan/data/aligned_data_loader.lua
create mode 100644 cyclegan/data/aligned_dataset.py
delete mode 100644 cyclegan/data/base_data_loader.lua
create mode 100644 cyclegan/data/base_dataset.py
create mode 100644 cyclegan/data/colorization_dataset.py
delete mode 100644 cyclegan/data/data.lua
delete mode 100644 cyclegan/data/data_util.lua
delete mode 100644 cyclegan/data/dataset.lua
delete mode 100644 cyclegan/data/donkey_folder.lua
create mode 100644 cyclegan/data/image_folder.py
create mode 100644 cyclegan/data/single_dataset.py
create mode 100644 cyclegan/data/template_dataset.py
delete mode 100644 cyclegan/data/unaligned_data_loader.lua
create mode 100644 cyclegan/data/unaligned_dataset.py
create mode 100644 cyclegan/docs/Dockerfile
create mode 100644 cyclegan/docs/README_es.md
create mode 100644 cyclegan/docs/datasets.md
create mode 100644 cyclegan/docs/docker.md
create mode 100644 cyclegan/docs/overview.md
create mode 100644 cyclegan/docs/qa.md
create mode 100644 cyclegan/docs/tips.md
create mode 100644 cyclegan/environment.yml
delete mode 100644 cyclegan/examples/test_vangogh_style_on_ae_photos.sh
delete mode 100644 cyclegan/examples/train_maps.sh
create mode 100644 cyclegan/models/__init__.py
delete mode 100644 cyclegan/models/architectures.lua
delete mode 100644 cyclegan/models/base_model.lua
create mode 100644 cyclegan/models/base_model.py
delete mode 100644 cyclegan/models/bigan_model.lua
create mode 100644 cyclegan/models/colorization_model.py
delete mode 100644 cyclegan/models/content_gan_model.lua
delete mode 100644 cyclegan/models/cycle_gan_model.lua
create mode 100644 cyclegan/models/cycle_gan_model.py
create mode 100644 cyclegan/models/networks.py
delete mode 100644 cyclegan/models/one_direction_test_model.lua
delete mode 100644 cyclegan/models/pix2pix_model.lua
create mode 100644 cyclegan/models/pix2pix_model.py
create mode 100644 cyclegan/models/template_model.py
create mode 100644 cyclegan/models/test_model.py
delete mode 100644 cyclegan/options.lua
create mode 100644 cyclegan/options/__init__.py
create mode 100644 cyclegan/options/base_options.py
create mode 100644 cyclegan/options/test_options.py
create mode 100644 cyclegan/options/train_options.py
create mode 100644 cyclegan/pix2pix.ipynb
delete mode 100644 cyclegan/pretrained_models/download_model.sh
delete mode 100644 cyclegan/pretrained_models/download_vgg.sh
create mode 100644 cyclegan/requirements.txt
create mode 100644 cyclegan/scripts/conda_deps.sh
create mode 100644 cyclegan/scripts/download_cyclegan_model.sh
create mode 100644 cyclegan/scripts/download_pix2pix_model.sh
create mode 100755 cyclegan/scripts/edges/PostprocessHED.m
create mode 100755 cyclegan/scripts/edges/batch_hed.py
rename cyclegan/{pretrained_models/places_vgg.prototxt => scripts/eval_cityscapes/caffemodel/deploy.prototxt} (57%)
mode change 100644 => 100755
create mode 100755 cyclegan/scripts/eval_cityscapes/cityscapes.py
create mode 100755 cyclegan/scripts/eval_cityscapes/download_fcn8s.sh
create mode 100755 cyclegan/scripts/eval_cityscapes/evaluate.py
create mode 100755 cyclegan/scripts/eval_cityscapes/util.py
create mode 100644 cyclegan/scripts/install_deps.sh
create mode 100644 cyclegan/scripts/test_before_push.py
create mode 100644 cyclegan/scripts/test_colorization.sh
create mode 100755 cyclegan/scripts/test_cyclegan.sh
create mode 100755 cyclegan/scripts/test_pix2pix.sh
create mode 100755 cyclegan/scripts/test_single.sh
create mode 100644 cyclegan/scripts/train_colorization.sh
create mode 100755 cyclegan/scripts/train_cyclegan.sh
create mode 100755 cyclegan/scripts/train_pix2pix.sh
delete mode 100644 cyclegan/test.lua
create mode 100644 cyclegan/test.py
delete mode 100644 cyclegan/train.lua
create mode 100644 cyclegan/train.py
delete mode 100644 cyclegan/util/InstanceNormalization.lua
delete mode 100644 cyclegan/util/VGG_preprocess.lua
create mode 100644 cyclegan/util/__init__.py
delete mode 100644 cyclegan/util/content_loss.lua
delete mode 100644 cyclegan/util/cudnn_convert_custom.lua
create mode 100644 cyclegan/util/get_data.py
create mode 100644 cyclegan/util/html.py
delete mode 100644 cyclegan/util/image_pool.lua
create mode 100644 cyclegan/util/image_pool.py
delete mode 100644 cyclegan/util/plot_util.lua
delete mode 100644 cyclegan/util/util.lua
create mode 100644 cyclegan/util/util.py
delete mode 100644 cyclegan/util/visualizer.lua
create mode 100644 cyclegan/util/visualizer.py
diff --git a/cyclegan/.gitignore b/cyclegan/.gitignore
index 4a26633..4fdef3e 100644
--- a/cyclegan/.gitignore
+++ b/cyclegan/.gitignore
@@ -1,3 +1,5 @@
+.DS_Store
+debug*
datasets/
checkpoints/
results/
@@ -38,3 +40,4 @@ test/.coverage
*/**/*.dylib*
test/data/legacy_serialized.pt
*~
+.idea
diff --git a/cyclegan/CycleGAN.ipynb b/cyclegan/CycleGAN.ipynb
new file mode 100644
index 0000000..590a2a1
--- /dev/null
+++ b/cyclegan/CycleGAN.ipynb
@@ -0,0 +1,255 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "name": "CycleGAN",
+ "provenance": [],
+ "collapsed_sections": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "accelerator": "GPU"
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "5VIGyIus8Vr7",
+ "colab_type": "text"
+ },
+ "source": [
+ "Take a look at the [repository](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix) for more information"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "7wNjDKdQy35h",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Install"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "TRm-USlsHgEV",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "Pt3igws3eiVp",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "import os\n",
+ "os.chdir('pytorch-CycleGAN-and-pix2pix/')"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "z1EySlOXwwoa",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!pip install -r requirements.txt"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "8daqlgVhw29P",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Datasets\n",
+ "\n",
+ "Download one of the official datasets with:\n",
+ "\n",
+ "- `bash ./datasets/download_cyclegan_dataset.sh [apple2orange, orange2apple, summer2winter_yosemite, winter2summer_yosemite, horse2zebra, zebra2horse, monet2photo, style_monet, style_cezanne, style_ukiyoe, style_vangogh, sat2map, map2sat, cityscapes_photo2label, cityscapes_label2photo, facades_photo2label, facades_label2photo, iphone2dslr_flower]`\n",
+ "\n",
+ "Or use your own dataset by creating the appropriate folders and adding in the images.\n",
+ "\n",
+ "- Create a dataset folder under `/dataset` for your dataset.\n",
+ "- Create subfolders `testA`, `testB`, `trainA`, and `trainB` under your dataset's folder. Place any images you want to transform from a to b (cat2dog) in the `testA` folder, images you want to transform from b to a (dog2cat) in the `testB` folder, and do the same for the `trainA` and `trainB` folders."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "vrdOettJxaCc",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!bash ./datasets/download_cyclegan_dataset.sh horse2zebra"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "gdUz4116xhpm",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Pretrained models\n",
+ "\n",
+ "Download one of the official pretrained models with:\n",
+ "\n",
+ "- `bash ./scripts/download_cyclegan_model.sh [apple2orange, orange2apple, summer2winter_yosemite, winter2summer_yosemite, horse2zebra, zebra2horse, monet2photo, style_monet, style_cezanne, style_ukiyoe, style_vangogh, sat2map, map2sat, cityscapes_photo2label, cityscapes_label2photo, facades_photo2label, facades_label2photo, iphone2dslr_flower]`\n",
+ "\n",
+ "Or add your own pretrained model to `./checkpoints/{NAME}_pretrained/latest_net_G.pt`"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "B75UqtKhxznS",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!bash ./scripts/download_cyclegan_model.sh horse2zebra"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "yFw1kDQBx3LN",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Training\n",
+ "\n",
+ "- `python train.py --dataroot ./datasets/horse2zebra --name horse2zebra --model cycle_gan`\n",
+ "\n",
+ "Change the `--dataroot` and `--name` to your own dataset's path and model's name. Use `--gpu_ids 0,1,..` to train on multiple GPUs and `--batch_size` to change the batch size. I've found that a batch size of 16 fits onto 4 V100s and can finish training an epoch in ~90s.\n",
+ "\n",
+ "Once your model has trained, copy over the last checkpoint to a format that the testing model can automatically detect:\n",
+ "\n",
+ "Use `cp ./checkpoints/horse2zebra/latest_net_G_A.pth ./checkpoints/horse2zebra/latest_net_G.pth` if you want to transform images from class A to class B and `cp ./checkpoints/horse2zebra/latest_net_G_B.pth ./checkpoints/horse2zebra/latest_net_G.pth` if you want to transform images from class B to class A.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "0sp7TCT2x9dB",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!python train.py --dataroot ./datasets/horse2zebra --name horse2zebra --model cycle_gan"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "9UkcaFZiyASl",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Testing\n",
+ "\n",
+ "- `python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout`\n",
+ "\n",
+ "Change the `--dataroot` and `--name` to be consistent with your trained model's configuration.\n",
+ "\n",
+ "> from https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix:\n",
+ "> 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.\n",
+ "\n",
+ "> For your own experiments, you might want to specify --netG, --norm, --no_dropout to match the generator architecture of the trained model."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "uCsKkEq0yGh0",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "OzSKIPUByfiN",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Visualize"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "9Mgg8raPyizq",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "import matplotlib.pyplot as plt\n",
+ "\n",
+ "img = plt.imread('./results/horse2zebra_pretrained/test_latest/images/n02381460_1010_fake.png')\n",
+ "plt.imshow(img)"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "0G3oVH9DyqLQ",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "import matplotlib.pyplot as plt\n",
+ "\n",
+ "img = plt.imread('./results/horse2zebra_pretrained/test_latest/images/n02381460_1010_real.png')\n",
+ "plt.imshow(img)"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ }
+ ]
+}
\ No newline at end of file
diff --git a/cyclegan/README.md b/cyclegan/README.md
index 9f8f579..114409e 100644
--- a/cyclegan/README.md
+++ b/cyclegan/README.md
@@ -2,201 +2,207 @@
-# CycleGAN
-### [PyTorch](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix) | [project page](https://junyanz.github.io/CycleGAN/) | [paper](https://arxiv.org/pdf/1703.10593.pdf)
+# CycleGAN and pix2pix in PyTorch
-Torch implementation for learning an image-to-image translation (i.e. [pix2pix](https://github.com/phillipi/pix2pix)) **without** input-output pairs, for example:
+We provide PyTorch implementations for both unpaired and paired image-to-image translation.
+
+The code was written by [Jun-Yan Zhu](https://github.com/junyanz) and [Taesung Park](https://github.com/taesung), and supported by [Tongzhou Wang](https://ssnl.github.io/).
+
+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](https://github.com/junyanz/CycleGAN) and [pix2pix Torch](https://github.com/phillipi/pix2pix) code
+
+**Note**: The current software works well with PyTorch 0.41+. Check out the older [branch](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/tree/pytorch0.3.1) that supports PyTorch 0.1-0.3.
+
+You may find useful information in [training/test tips](docs/tips.md) and [frequently asked questions](docs/qa.md). To implement custom models and datasets, check out our [templates](#custom-model-and-dataset). To help users better understand and adapt our codebase, we provide an [overview](docs/overview.md) of the code structure of this repository.
+
+**CycleGAN: [Project](https://junyanz.github.io/CycleGAN/) | [Paper](https://arxiv.org/pdf/1703.10593.pdf) | [Torch](https://github.com/junyanz/CycleGAN) |
+[Tensorflow Core Tutorial](https://www.tensorflow.org/tutorials/generative/cyclegan) | [PyTorch Colab](https://colab.research.google.com/github/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/CycleGAN.ipynb)**
+
+
+
+**Pix2pix: [Project](https://phillipi.github.io/pix2pix/) | [Paper](https://arxiv.org/pdf/1611.07004.pdf) | [Torch](https://github.com/phillipi/pix2pix) |
+[Tensorflow Core Tutorial](https://www.tensorflow.org/tutorials/generative/cyclegan) | [PyTorch Colab](https://colab.research.google.com/github/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/pix2pix.ipynb)**
+
+
+**[EdgesCats Demo](https://affinelayer.com/pixsrv/) | [pix2pix-tensorflow](https://github.com/affinelayer/pix2pix-tensorflow) | by [Christopher Hesse](https://twitter.com/christophrhesse)**
-
+
-[Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks](https://junyanz.github.io/CycleGAN/)
- [Jun-Yan Zhu](https://people.eecs.berkeley.edu/~junyanz/)\*, [Taesung Park](https://taesung.me/)\*, [Phillip Isola](http://web.mit.edu/phillipi/), [Alexei A. Efros](https://people.eecs.berkeley.edu/~efros/)
- Berkeley AI Research Lab, UC Berkeley
- In ICCV 2017. (* equal contributions)
+If you use this code for your research, please cite:
-This package includes CycleGAN, [pix2pix](https://github.com/phillipi/pix2pix), as well as other methods like [BiGAN](https://arxiv.org/abs/1605.09782)/[ALI](https://ishmaelbelghazi.github.io/ALI/) and Apple's paper [S+U learning](https://arxiv.org/pdf/1612.07828.pdf).
-The code was written by [Jun-Yan Zhu](https://github.com/junyanz) and [Taesung Park](https://github.com/taesung).
-**Update**: Please check out [PyTorch](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix) implementation for CycleGAN and pix2pix.
-The PyTorch version is under active development and can produce results comparable or better than this Torch version.
+Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks.
+[Jun-Yan Zhu](https://people.eecs.berkeley.edu/~junyanz/)\*, [Taesung Park](https://taesung.me/)\*, [Phillip Isola](https://people.eecs.berkeley.edu/~isola/), [Alexei A. Efros](https://people.eecs.berkeley.edu/~efros). In ICCV 2017. (* equal contributions) [[Bibtex]](https://junyanz.github.io/CycleGAN/CycleGAN.txt)
-## Other implementations:
+
+Image-to-Image Translation with Conditional Adversarial Networks.
+[Phillip Isola](https://people.eecs.berkeley.edu/~isola), [Jun-Yan Zhu](https://people.eecs.berkeley.edu/~junyanz), [Tinghui Zhou](https://people.eecs.berkeley.edu/~tinghuiz), [Alexei A. Efros](https://people.eecs.berkeley.edu/~efros). In CVPR 2017. [[Bibtex]](http://people.csail.mit.edu/junyanz/projects/pix2pix/pix2pix.bib)
+
+## Talks and Course
+pix2pix slides: [keynote](http://efrosgans.eecs.berkeley.edu/CVPR18_slides/pix2pix.key) | [pdf](http://efrosgans.eecs.berkeley.edu/CVPR18_slides/pix2pix.pdf),
+CycleGAN slides: [pptx](http://efrosgans.eecs.berkeley.edu/CVPR18_slides/CycleGAN.pptx) | [pdf](http://efrosgans.eecs.berkeley.edu/CVPR18_slides/CycleGAN.pdf)
+
+CycleGAN course assignment [code](http://www.cs.toronto.edu/~rgrosse/courses/csc321_2018/assignments/a4-code.zip) and [handout](http://www.cs.toronto.edu/~rgrosse/courses/csc321_2018/assignments/a4-handout.pdf) designed by Prof. [Roger Grosse](http://www.cs.toronto.edu/~rgrosse/) for [CSC321](http://www.cs.toronto.edu/~rgrosse/courses/csc321_2018/) "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](https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/generative/cyclegan.ipynb) | [Code](https://github.com/tensorflow/docs/blob/master/site/en/tutorials/generative/cyclegan.ipynb)
+
+TensorFlow Core pix2pix Tutorial: [Google Colab](https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/generative/pix2pix.ipynb) | [Code](https://github.com/tensorflow/docs/blob/master/site/en/tutorials/generative/pix2pix.ipynb)
+
+PyTorch Colab notebook: [CycleGAN](https://colab.research.google.com/github/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/CycleGAN.ipynb) and [pix2pix](https://colab.research.google.com/github/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/pix2pix.ipynb)
+
+## Other implementations
+### CycleGAN
[Tensorflow] (by Harry Yang),
[Tensorflow] (by Archit Rathore),
[Tensorflow] (by Van Huy),
-[Tensorflow] (by Xiaowei Hu),
+[Tensorflow] (by Xiaowei Hu),
[Tensorflow-simple] (by Zhenliang He),
- [TensorLayer] (by luoxier),
+ [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)
+[lasagne/Keras] (by tjwei),
+[Keras] (by Simon Karlsson)
+
-## Applications
-### Monet Paintings to Photos
-
-
-### Collection Style Transfer
-
-
-### Object Transfiguration
-
-
-### Season Transfer
-
-
-### Photo Enhancement: Narrow depth of field
-
-
-
+### 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
-- Linux or OSX
-- NVIDIA GPU + CUDA CuDNN (CPU mode and CUDA without CuDNN may work with minimal modification, but untested)
-- For MAC users, you need the Linux/GNU commands `gfind` and `gwc`, which can be installed with `brew install findutils coreutils`.
+- Linux or macOS
+- Python 3
+- CPU or NVIDIA GPU + CUDA CuDNN
## Getting Started
### Installation
-- Install torch and dependencies from https://github.com/torch/distro
-- Install torch packages `nngraph`, `class`, `display`
-```bash
-luarocks install nngraph
-luarocks install class
-luarocks install https://raw.githubusercontent.com/szym/display/master/display-scm-0.rockspec
-```
+
- Clone this repo:
```bash
-git clone https://github.com/junyanz/CycleGAN
-cd CycleGAN
+git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
+cd pytorch-CycleGAN-and-pix2pix
```
-### Apply a Pre-trained Model
-- Download the test photos (taken by [Alexei Efros](https://www.flickr.com/photos/aaefros)):
-```
-bash ./datasets/download_dataset.sh ae_photos
-```
-- Download the pre-trained model `style_cezanne` (For CPU model, use `style_cezanne_cpu`):
-```
-bash ./pretrained_models/download_model.sh style_cezanne
-```
-- Now, let's generate Paul Cézanne style images:
-```
-DATA_ROOT=./datasets/ae_photos name=style_cezanne_pretrained model=one_direction_test phase=test loadSize=256 fineSize=256 resize_or_crop="scale_width" th test.lua
-```
-The test results will be saved to `./results/style_cezanne_pretrained/latest_test/index.html`.
-Please refer to [Model Zoo](#model-zoo) for more pre-trained models.
-`./examples/test_vangogh_style_on_ae_photos.sh` is an example script that downloads the pretrained Van Gogh style network and runs it on Efros's photos.
+- Install [PyTorch](http://pytorch.org) and 0.4+ and other dependencies (e.g., torchvision, [visdom](https://github.com/facebookresearch/visdom) and [dominate](https://github.com/Knio/dominate)).
+ - For pip users, please type the command `pip install -r requirements.txt`.
+ - For Conda users, we provide a installation script `./scripts/conda_deps.sh`. Alternatively, you can create a new Conda environment using `conda env create -f environment.yml`.
+ - For Docker users, we provide the pre-built Docker image and Dockerfile. Please refer to our [Docker](docs/docker.md) page.
-### Train
-- Download a dataset (e.g. zebra and horse images from ImageNet):
+### CycleGAN train/test
+- Download a CycleGAN dataset (e.g. maps):
```bash
-bash ./datasets/download_dataset.sh horse2zebra
+bash ./datasets/download_cyclegan_dataset.sh maps
```
+- To view training results and loss plots, run `python -m visdom.server` and click the URL http://localhost:8097.
- Train a model:
```bash
-DATA_ROOT=./datasets/horse2zebra name=horse2zebra_model th train.lua
+#!./scripts/train_cyclegan.sh
+python train.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan
```
-- (CPU only) The same training command without using a GPU or CUDNN. Setting the environment variables ```gpu=0 cudnn=0``` forces CPU only
+To see more intermediate results, check out `./checkpoints/maps_cyclegan/web/index.html`.
+- Test the model:
```bash
-DATA_ROOT=./datasets/horse2zebra name=horse2zebra_model gpu=0 cudnn=0 th train.lua
+#!./scripts/test_cyclegan.sh
+python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan
```
-- (Optionally) start the display server to view results as the model trains. (See [Display UI](#display-ui) for more details):
+- 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](http://cmp.felk.cvut.cz/~tylecr1/facade/)):
```bash
-th -ldisplay.start 8000 0.0.0.0
+bash ./datasets/download_pix2pix_dataset.sh facades
```
-
-### Test
-- Finally, test the model:
+- To view training results and loss plots, run `python -m visdom.server` and click the URL http://localhost:8097.
+- Train a model:
```bash
-DATA_ROOT=./datasets/horse2zebra name=horse2zebra_model phase=test th test.lua
+#!./scripts/train_pix2pix.sh
+python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA
```
-The test results will be saved to an HTML file here: `./results/horse2zebra_model/latest_test/index.html`.
+To see more intermediate results, check out `./checkpoints/facades_pix2pix/web/index.html`.
-
-## Model Zoo
-Download the pre-trained models with the following script. The model will be saved to `./checkpoints/model_name/latest_net_G.t7`.
+- Test the model (`bash ./scripts/test_pix2pix.sh`):
```bash
-bash ./pretrained_models/download_model.sh model_name
+#!./scripts/test_pix2pix.sh
+python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA
```
-- `orange2apple` (orange -> apple) and `apple2orange`: trained on ImageNet categories `apple` and `orange`.
-- `horse2zebra` (horse -> zebra) and `zebra2horse` (zebra -> horse): trained on ImageNet categories `horse` and `zebra`.
-- `style_monet` (landscape photo -> Monet painting style), `style_vangogh` (landscape photo -> Van Gogh painting style), `style_ukiyoe` (landscape photo -> Ukiyo-e painting style), `style_cezanne` (landscape photo -> Cezanne painting style): trained on paintings and Flickr landscape photos.
-- `monet2photo` (Monet paintings -> real landscape): trained on paintings and Flickr landscape photographs.
-- `cityscapes_photo2label` (street scene -> label) and `cityscapes_label2photo` (label -> street scene): trained on the Cityscapes dataset.
-- `map2sat` (map -> aerial photo) and `sat2map` (aerial photo -> map): trained on Google maps.
-- `iphone2dslr_flower` (iPhone photos of flowers -> DSLR photos of flowers): trained on Flickr photos.
+- 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](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md#notes-on-colorization) for more details.
-CPU models can be downloaded using:
+### Apply a pre-trained model (CycleGAN)
+- You can download a pretrained model (e.g. horse2zebra) with the following script:
```bash
-bash pretrained_models/download_model.sh _cpu
+bash ./scripts/download_cyclegan_model.sh horse2zebra
```
-, where `` can be `horse2zebra`, `style_monet`, etc. You just need to append `_cpu` to the target model.
-
-## Training and Test Details
-To train a model,
+- The pretrained model is saved at `./checkpoints/{name}_pretrained/latest_net_G.pth`. Check [here](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/scripts/download_cyclegan_model.sh#L3) for all the available CycleGAN models.
+- To test the model, you also need to download the horse2zebra dataset:
```bash
-DATA_ROOT=/path/to/data/ name=expt_name th train.lua
+bash ./datasets/download_cyclegan_dataset.sh horse2zebra
```
-Models are saved to `./checkpoints/expt_name` (can be changed by passing `checkpoint_dir=your_dir` in train.lua).
-See `opt_train` in `options.lua` for additional training options.
-To test the model,
+- Then generate the results using
```bash
-DATA_ROOT=/path/to/data/ name=expt_name phase=test th test.lua
+python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout
```
-This will run the model named `expt_name` in both directions on all images in `/path/to/data/testA` and `/path/to/data/testB`.
-A webpage with result images will be saved to `./results/expt_name` (can be changed by passing `results_dir=your_dir` in test.lua).
-See `opt_test` in `options.lua` for additional test options. Please use `model=one_direction_test` if you only would like to generate outputs of the trained network in only one direction, and specify `which_direction=AtoB` or `which_direction=BtoA` to set the direction.
+- 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.
-There are other options that can be used. For example, you can specify `resize_or_crop=crop` option to avoid resizing the image to squares. This is indeed how we trained GTA2Cityscapes model in the projet [webpage](https://junyanz.github.io/CycleGAN/) and [Cycada](https://arxiv.org/pdf/1711.03213.pdf) model. We prepared the images at 1024px resolution, and used `resize_or_crop=crop fineSize=360` to work with the cropped images of size 360x360. We also used `lambda_identity=1.0`.
+- For your own experiments, you might want to specify `--netG`, `--norm`, `--no_dropout` to match the generator architecture of the trained model.
-## Datasets
-Download the datasets using the following script. Many of the datasets were collected by other researchers. Please cite their papers if you use the data.
+### Apply a pre-trained model (pix2pix)
+Download a pre-trained model with `./scripts/download_pix2pix_model.sh`.
+
+- Check [here](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/scripts/download_pix2pix_model.sh#L3) for all the available pix2pix models. For example, if you would like to download label2photo model on the Facades dataset,
```bash
-bash ./datasets/download_dataset.sh dataset_name
+bash ./scripts/download_pix2pix_model.sh facades_label2photo
```
-- `facades`: 400 images from the [CMP Facades dataset](http://cmp.felk.cvut.cz/~tylecr1/facade/). [[Citation](datasets/bibtex/facades.tex)]
-- `cityscapes`: 2975 images from the [Cityscapes training set](https://www.cityscapes-dataset.com/). [[Citation](datasets/bibtex/cityscapes.tex)]. Note: Due to license issue, we do not host the dataset on our repo. Please download the dataset directly from the Cityscapes webpage. Please refer to `./datasets/prepare_cityscapes_dataset.py` for more detail.
-- `maps`: 1096 training images scraped from Google Maps.
-- `horse2zebra`: 939 horse images and 1177 zebra images downloaded from [ImageNet](http://www.image-net.org/) using the keywords `wild horse` and `zebra`
-- `apple2orange`: 996 apple images and 1020 orange images downloaded from [ImageNet](http://www.image-net.org/) using the keywords `apple` and `navel orange`.
-- `summer2winter_yosemite`: 1273 summer Yosemite images and 854 winter Yosemite images were downloaded using Flickr API. See more details in our paper.
-- `monet2photo`, `vangogh2photo`, `ukiyoe2photo`, `cezanne2photo`: The art images were downloaded from [Wikiart](https://www.wikiart.org/). The real photos are downloaded from Flickr using the combination of the tags *landscape* and *landscapephotography*. The training set size of each class is Monet:1074, Cezanne:584, Van Gogh:401, Ukiyo-e:1433, Photographs:6853.
-- `iphone2dslr_flower`: both classes of images were downloaded from Flickr. The training set size of each class is iPhone:1813, DSLR:3316. See more details in our paper.
-
-
-## Display UI
-Optionally, for displaying images during training and test, use the [display package](https://github.com/szym/display).
-
-- Install it with: `luarocks install https://raw.githubusercontent.com/szym/display/master/display-scm-0.rockspec`
-- Then start the server with: `th -ldisplay.start`
-- Open this URL in your browser: [http://localhost:8000](http://localhost:8000)
-
-By default, the server listens on localhost. Pass `0.0.0.0` to allow external connections on any interface:
+- Download the pix2pix facades datasets:
```bash
-th -ldisplay.start 8000 0.0.0.0
+bash ./datasets/download_pix2pix_dataset.sh facades
```
-Then open `http://(hostname):(port)/` in your browser to load the remote desktop.
+- Then generate the results using
+```bash
+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.
-## Setup Training and Test data
-To train CycleGAN model on your own datasets, you need to create a data folder with two subdirectories `trainA` and `trainB` that contain images from domain A and B. You can test your model on your training set by setting ``phase='train'`` in `test.lua`. You can also create subdirectories `testA` and `testB` if you have test data.
+- 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`).
-You should **not** expect our method to work on just any random combination of input and output datasets (e.g. `cats<->keyboards`). From our experiments, we find it works better if two datasets share similar visual content. For example, `landscape painting<->landscape photographs` works much better than `portrait painting <-> landscape photographs`. `zebras<->horses` achieves compelling results while `cats<->dogs` completely fails. See the following section for more discussion.
+- See a list of currently available models at `./scripts/download_pix2pix_model.sh`
+## [Docker](docs/docker.md)
+We provide the pre-built Docker image and Dockerfile that can run this code repo. See [docker](docs/docker.md).
-## Failure cases
-
+## [Datasets](docs/datasets.md)
+Download pix2pix/CycleGAN datasets and create your own datasets.
-Our model does not work well when the test image is rather different from the images on which the model is trained, as is the case in the figure to the left (we trained on horses and zebras without riders, but test here one a horse with a rider). See additional typical failure cases [here](https://junyanz.github.io/CycleGAN/images/failures.jpg). On translation tasks that involve color and texture changes, like many of those reported above, the method often succeeds. We have also explored tasks that require geometric changes, with little success. For example, on the task of `dog<->cat` transfiguration, the learned translation degenerates into making minimal changes to the input. We also observe a lingering gap between the results achievable with paired training data and those achieved by our unpaired method. In some cases, this gap may be very hard -- or even impossible,-- to close: for example, our method sometimes permutes the labels for tree and building in the output of the cityscapes photos->labels task.
+## [Training/Test Tips](docs/tips.md)
+Best practice for training and testing your models.
+## [Frequently Asked Questions](docs/qa.md)
+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](data/template_dataset.py) and a model [template](models/template_model.py) as a starting point.
+
+## [Code structure](docs/overview.md)
+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](https://help.github.com/articles/about-pull-requests/).
+Please run `flake8 --ignore E501 .` and `python ./scripts/test_before_push.py` before you commit the code. Please also update the code structure [overview](docs/overview.md) accordingly if you add or remove files.
## Citation
-If you use this code for your research, please cite our [paper](https://junyanz.github.io/CycleGAN/):
-
+If you use this code for your research, please cite our papers.
```
@inproceedings{CycleGAN2017,
title={Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networkss},
@@ -205,16 +211,26 @@ If you use this code for your research, please cite our [paper](https://junyanz.
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](docs/README_es.md)
-## Related Projects:
-[pix2pix](https://github.com/phillipi/pix2pix): Image-to-image translation using conditional adversarial nets
-[iGAN](https://github.com/junyanz/iGAN): Interactive Image Generation via Generative Adversarial Networks
+## Related Projects
+**[CycleGAN-Torch](https://github.com/junyanz/CycleGAN) |
+[pix2pix-Torch](https://github.com/phillipi/pix2pix) | [pix2pixHD](https://github.com/NVIDIA/pix2pixHD)|
+[BicycleGAN](https://github.com/junyanz/BicycleGAN) | [vid2vid](https://tcwang0509.github.io/vid2vid/) | [SPADE/GauGAN](https://github.com/NVlabs/SPADE)**
+**[iGAN](https://github.com/junyanz/iGAN) | [GAN Dissection](https://github.com/CSAILVision/GANDissect) | [GAN Paint](http://ganpaint.io/)**
## Cat Paper Collection
-If you love cats, and love reading cool graphics, vision, and ML papers, please check out the Cat Paper [Collection](https://github.com/junyanz/CatPapers).
-
+If you love cats, and love reading cool graphics, vision, and learning papers, please check out the Cat Paper [Collection](https://github.com/junyanz/CatPapers).
## Acknowledgments
-Code borrows from [pix2pix](https://github.com/phillipi/pix2pix) and [DCGAN](https://github.com/soumith/dcgan.torch). The data loader is modified from [DCGAN](https://github.com/soumith/dcgan.torch) and [Context-Encoder](https://github.com/pathak22/context-encoder). The generative network is adopted from [neural-style](https://github.com/jcjohnson/neural-style) with [Instance Normalization](https://github.com/DmitryUlyanov/texture_nets/blob/master/InstanceNormalization.lua).
+Our code is inspired by [pytorch-DCGAN](https://github.com/pytorch/examples/tree/master/dcgan).
diff --git a/cyclegan/data/__init__.py b/cyclegan/data/__init__.py
new file mode 100644
index 0000000..8cb6186
--- /dev/null
+++ b/cyclegan/data/__init__.py
@@ -0,0 +1,93 @@
+"""This package includes all the modules related to data loading and preprocessing
+
+ To add a custom dataset class called 'dummy', you need to add a file called 'dummy_dataset.py' and define a subclass 'DummyDataset' inherited from BaseDataset.
+ You need to implement four functions:
+ -- <__init__>: initialize the class, first call BaseDataset.__init__(self, opt).
+ -- <__len__>: return the size of dataset.
+ -- <__getitem__>: get a data point from data loader.
+ -- : (optionally) add dataset-specific options and set default options.
+
+Now you can use the dataset class by specifying flag '--dataset_mode dummy'.
+See our template dataset class 'template_dataset.py' for more details.
+"""
+import importlib
+import torch.utils.data
+from data.base_dataset import BaseDataset
+
+
+def find_dataset_using_name(dataset_name):
+ """Import the module "data/[dataset_name]_dataset.py".
+
+ In the file, the class called DatasetNameDataset() will
+ be instantiated. It has to be a subclass of BaseDataset,
+ and it is case-insensitive.
+ """
+ dataset_filename = "data." + dataset_name + "_dataset"
+ datasetlib = importlib.import_module(dataset_filename)
+
+ dataset = None
+ target_dataset_name = dataset_name.replace('_', '') + 'dataset'
+ for name, cls in datasetlib.__dict__.items():
+ if name.lower() == target_dataset_name.lower() \
+ and issubclass(cls, BaseDataset):
+ dataset = cls
+
+ if dataset is None:
+ raise NotImplementedError("In %s.py, there should be a subclass of BaseDataset with class name that matches %s in lowercase." % (dataset_filename, target_dataset_name))
+
+ return dataset
+
+
+def get_option_setter(dataset_name):
+ """Return the static method of the dataset class."""
+ dataset_class = find_dataset_using_name(dataset_name)
+ return dataset_class.modify_commandline_options
+
+
+def create_dataset(opt):
+ """Create a dataset given the option.
+
+ This function wraps the class CustomDatasetDataLoader.
+ This is the main interface between this package and 'train.py'/'test.py'
+
+ Example:
+ >>> from data import create_dataset
+ >>> dataset = create_dataset(opt)
+ """
+ data_loader = CustomDatasetDataLoader(opt)
+ dataset = data_loader.load_data()
+ return dataset
+
+
+class CustomDatasetDataLoader():
+ """Wrapper class of Dataset class that performs multi-threaded data loading"""
+
+ def __init__(self, opt):
+ """Initialize this class
+
+ Step 1: create a dataset instance given the name [dataset_mode]
+ Step 2: create a multi-threaded data loader.
+ """
+ self.opt = opt
+ dataset_class = find_dataset_using_name(opt.dataset_mode)
+ self.dataset = dataset_class(opt)
+ print("dataset [%s] was created" % type(self.dataset).__name__)
+ self.dataloader = torch.utils.data.DataLoader(
+ self.dataset,
+ batch_size=opt.batch_size,
+ shuffle=not opt.serial_batches,
+ num_workers=int(opt.num_threads))
+
+ def load_data(self):
+ return self
+
+ def __len__(self):
+ """Return the number of data in the dataset"""
+ return min(len(self.dataset), self.opt.max_dataset_size)
+
+ def __iter__(self):
+ """Return a batch of data"""
+ for i, data in enumerate(self.dataloader):
+ if i * self.opt.batch_size >= self.opt.max_dataset_size:
+ break
+ yield data
diff --git a/cyclegan/data/aligned_data_loader.lua b/cyclegan/data/aligned_data_loader.lua
deleted file mode 100644
index e2637e0..0000000
--- a/cyclegan/data/aligned_data_loader.lua
+++ /dev/null
@@ -1,44 +0,0 @@
---------------------------------------------------------------------------------
--- Subclass of BaseDataLoader that provides data from two datasets.
--- The samples from the datasets are aligned
--- The datasets are of the same size
---------------------------------------------------------------------------------
-require 'data.base_data_loader'
-
-local class = require 'class'
-data_util = paths.dofile('data_util.lua')
-
-AlignedDataLoader = class('AlignedDataLoader', 'BaseDataLoader')
-
-function AlignedDataLoader:__init(conf)
- BaseDataLoader.__init(self, conf)
- conf = conf or {}
-end
-
-function AlignedDataLoader:name()
- return 'AlignedDataLoader'
-end
-
-function AlignedDataLoader:Initialize(opt)
- opt.align_data = 1
- self.idx_A = {1, opt.input_nc}
- self.idx_B = {opt.input_nc+1, opt.input_nc+opt.output_nc}
- local nc = 3--opt.input_nc + opt.output_nc
- self.data = data_util.load_dataset('', opt, nc)
-end
-
--- actually fetches the data
--- |return|: a table of two tables, each corresponding to
--- the batch for dataset A and dataset B
-function AlignedDataLoader:LoadBatchForAllDatasets()
- local batch_data, path = self.data:getBatch()
- local batchA = batch_data[{ {}, self.idx_A, {}, {} }]
- local batchB = batch_data[{ {}, self.idx_B, {}, {} }]
-
- return batchA, batchB, path, path
-end
-
--- returns the size of each dataset
-function AlignedDataLoader:size(dataset)
- return self.data:size()
-end
diff --git a/cyclegan/data/aligned_dataset.py b/cyclegan/data/aligned_dataset.py
new file mode 100644
index 0000000..cce2be3
--- /dev/null
+++ b/cyclegan/data/aligned_dataset.py
@@ -0,0 +1,60 @@
+import os.path
+from data.base_dataset import BaseDataset, get_params, get_transform
+from data.image_folder import make_dataset
+from PIL import Image
+
+
+class AlignedDataset(BaseDataset):
+ """A dataset class for paired image dataset.
+
+ It assumes that the directory '/path/to/data/train' contains image pairs in the form of {A,B}.
+ During test time, you need to prepare a directory '/path/to/data/test'.
+ """
+
+ def __init__(self, opt):
+ """Initialize this dataset class.
+
+ Parameters:
+ opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
+ """
+ BaseDataset.__init__(self, opt)
+ self.dir_AB = os.path.join(opt.dataroot, opt.phase) # get the image directory
+ self.AB_paths = sorted(make_dataset(self.dir_AB, opt.max_dataset_size)) # get image paths
+ assert(self.opt.load_size >= self.opt.crop_size) # crop_size should be smaller than the size of loaded image
+ self.input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc
+ self.output_nc = self.opt.input_nc if self.opt.direction == 'BtoA' else self.opt.output_nc
+
+ def __getitem__(self, index):
+ """Return a data point and its metadata information.
+
+ Parameters:
+ index - - a random integer for data indexing
+
+ Returns a dictionary that contains A, B, A_paths and B_paths
+ A (tensor) - - an image in the input domain
+ B (tensor) - - its corresponding image in the target domain
+ A_paths (str) - - image paths
+ B_paths (str) - - image paths (same as A_paths)
+ """
+ # read a image given a random integer index
+ AB_path = self.AB_paths[index]
+ AB = Image.open(AB_path).convert('RGB')
+ # split AB image into A and B
+ w, h = AB.size
+ w2 = int(w / 2)
+ A = AB.crop((0, 0, w2, h))
+ B = AB.crop((w2, 0, w, h))
+
+ # apply the same transform to both A and B
+ transform_params = get_params(self.opt, A.size)
+ A_transform = get_transform(self.opt, transform_params, grayscale=(self.input_nc == 1))
+ B_transform = get_transform(self.opt, transform_params, grayscale=(self.output_nc == 1))
+
+ A = A_transform(A)
+ B = B_transform(B)
+
+ return {'A': A, 'B': B, 'A_paths': AB_path, 'B_paths': AB_path}
+
+ def __len__(self):
+ """Return the total number of images in the dataset."""
+ return len(self.AB_paths)
diff --git a/cyclegan/data/base_data_loader.lua b/cyclegan/data/base_data_loader.lua
deleted file mode 100644
index 65fa830..0000000
--- a/cyclegan/data/base_data_loader.lua
+++ /dev/null
@@ -1,53 +0,0 @@
---------------------------------------------------------------------------------
--- Base Class for Providing Data
---------------------------------------------------------------------------------
-
-local class = require 'class'
-require 'torch'
-
-BaseDataLoader = class('BaseDataLoader')
-
-function BaseDataLoader:__init(conf)
- conf = conf or {}
- self.data_tm = torch.Timer()
-end
-
-function BaseDataLoader:name()
- return 'BaseDataLoader'
-end
-
-function BaseDataLoader:Initialize(opt)
-end
-
--- actually fetches the data
--- |return|: a table of two tables, each corresponding to
--- the batch for dataset A and dataset B
-function BaseDataLoader:LoadBatchForAllDatasets()
- return {},{},{},{}
-end
-
--- returns the next batch
--- a wrapper of getBatch(), which is meant to be overriden by subclasses
--- |return|: a table of two tables, each corresponding to
--- the batch for dataset A and dataset B
-function BaseDataLoader:GetNextBatch()
- self.data_tm:reset()
- self.data_tm:resume()
- local dataA, dataB, pathA, pathB = self:LoadBatchForAllDatasets()
- self.data_tm:stop()
- return dataA, dataB, pathA, pathB
-end
-
-function BaseDataLoader:time_elapsed_to_fetch_data()
- return self.data_tm:time().real
-end
-
--- returns the size of each dataset
-function BaseDataLoader:size(dataset)
- return 0
-end
-
-
-
-
-
diff --git a/cyclegan/data/base_dataset.py b/cyclegan/data/base_dataset.py
new file mode 100644
index 0000000..ae434b7
--- /dev/null
+++ b/cyclegan/data/base_dataset.py
@@ -0,0 +1,157 @@
+"""This module implements an abstract base class (ABC) 'BaseDataset' for datasets.
+
+It also includes common transformation functions (e.g., get_transform, __scale_width), which can be later used in subclasses.
+"""
+import random
+import numpy as np
+import torch.utils.data as data
+from PIL import Image
+import torchvision.transforms as transforms
+from abc import ABC, abstractmethod
+
+
+class BaseDataset(data.Dataset, ABC):
+ """This class is an abstract base class (ABC) for datasets.
+
+ To create a subclass, you need to implement the following four functions:
+ -- <__init__>: initialize the class, first call BaseDataset.__init__(self, opt).
+ -- <__len__>: return the size of dataset.
+ -- <__getitem__>: get a data point.
+ -- : (optionally) add dataset-specific options and set default options.
+ """
+
+ def __init__(self, opt):
+ """Initialize the class; save the options in the class
+
+ Parameters:
+ opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
+ """
+ self.opt = opt
+ self.root = opt.dataroot
+
+ @staticmethod
+ def modify_commandline_options(parser, is_train):
+ """Add new dataset-specific options, and rewrite default values for existing options.
+
+ Parameters:
+ parser -- original option parser
+ is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.
+
+ Returns:
+ the modified parser.
+ """
+ return parser
+
+ @abstractmethod
+ def __len__(self):
+ """Return the total number of images in the dataset."""
+ return 0
+
+ @abstractmethod
+ def __getitem__(self, index):
+ """Return a data point and its metadata information.
+
+ Parameters:
+ index - - a random integer for data indexing
+
+ Returns:
+ a dictionary of data with their names. It ususally contains the data itself and its metadata information.
+ """
+ pass
+
+
+def get_params(opt, size):
+ w, h = size
+ new_h = h
+ new_w = w
+ if opt.preprocess == 'resize_and_crop':
+ new_h = new_w = opt.load_size
+ elif opt.preprocess == 'scale_width_and_crop':
+ new_w = opt.load_size
+ new_h = opt.load_size * h // w
+
+ x = random.randint(0, np.maximum(0, new_w - opt.crop_size))
+ y = random.randint(0, np.maximum(0, new_h - opt.crop_size))
+
+ flip = random.random() > 0.5
+
+ return {'crop_pos': (x, y), 'flip': flip}
+
+
+def get_transform(opt, params=None, grayscale=False, method=Image.BICUBIC, convert=True):
+ transform_list = []
+ if grayscale:
+ transform_list.append(transforms.Grayscale(1))
+ if 'resize' in opt.preprocess:
+ osize = [opt.load_size, opt.load_size]
+ transform_list.append(transforms.Resize(osize, method))
+ elif 'scale_width' in opt.preprocess:
+ transform_list.append(transforms.Lambda(lambda img: __scale_width(img, opt.load_size, method)))
+
+ if 'crop' in opt.preprocess:
+ if params is None:
+ transform_list.append(transforms.RandomCrop(opt.crop_size))
+ else:
+ transform_list.append(transforms.Lambda(lambda img: __crop(img, params['crop_pos'], opt.crop_size)))
+
+ if opt.preprocess == 'none':
+ transform_list.append(transforms.Lambda(lambda img: __make_power_2(img, base=4, method=method)))
+
+ if not opt.no_flip:
+ if params is None:
+ transform_list.append(transforms.RandomHorizontalFlip())
+ elif params['flip']:
+ transform_list.append(transforms.Lambda(lambda img: __flip(img, params['flip'])))
+
+ if convert:
+ transform_list += [transforms.ToTensor()]
+ if grayscale:
+ transform_list += [transforms.Normalize((0.5,), (0.5,))]
+ else:
+ transform_list += [transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
+ return transforms.Compose(transform_list)
+
+
+def __make_power_2(img, base, method=Image.BICUBIC):
+ ow, oh = img.size
+ h = int(round(oh / base) * base)
+ w = int(round(ow / base) * base)
+ if (h == oh) and (w == ow):
+ return img
+
+ __print_size_warning(ow, oh, w, h)
+ return img.resize((w, h), method)
+
+
+def __scale_width(img, target_width, method=Image.BICUBIC):
+ ow, oh = img.size
+ if (ow == target_width):
+ return img
+ w = target_width
+ h = int(target_width * oh / ow)
+ return img.resize((w, h), method)
+
+
+def __crop(img, pos, size):
+ ow, oh = img.size
+ x1, y1 = pos
+ tw = th = size
+ if (ow > tw or oh > th):
+ return img.crop((x1, y1, x1 + tw, y1 + th))
+ return img
+
+
+def __flip(img, flip):
+ if flip:
+ return img.transpose(Image.FLIP_LEFT_RIGHT)
+ return img
+
+
+def __print_size_warning(ow, oh, w, h):
+ """Print warning information about image size(only print once)"""
+ if not hasattr(__print_size_warning, 'has_printed'):
+ print("The image size needs to be a multiple of 4. "
+ "The loaded image size was (%d, %d), so it was adjusted to "
+ "(%d, %d). This adjustment will be done to all images "
+ "whose sizes are not multiples of 4" % (ow, oh, w, h))
+ __print_size_warning.has_printed = True
diff --git a/cyclegan/data/colorization_dataset.py b/cyclegan/data/colorization_dataset.py
new file mode 100644
index 0000000..ba0a8d7
--- /dev/null
+++ b/cyclegan/data/colorization_dataset.py
@@ -0,0 +1,68 @@
+import os.path
+from data.base_dataset import BaseDataset, get_transform
+from data.image_folder import make_dataset
+from skimage import color # require skimage
+from PIL import Image
+import numpy as np
+import torchvision.transforms as transforms
+
+
+class ColorizationDataset(BaseDataset):
+ """This dataset class can load a set of natural images in RGB, and convert RGB format into (L, ab) pairs in Lab color space.
+
+ This dataset is required by pix2pix-based colorization model ('--model colorization')
+ """
+ @staticmethod
+ def modify_commandline_options(parser, is_train):
+ """Add new dataset-specific options, and rewrite default values for existing options.
+
+ Parameters:
+ parser -- original option parser
+ is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.
+
+ Returns:
+ the modified parser.
+
+ By default, the number of channels for input image is 1 (L) and
+ the number of channels for output image is 2 (ab). The direction is from A to B
+ """
+ parser.set_defaults(input_nc=1, output_nc=2, direction='AtoB')
+ return parser
+
+ def __init__(self, opt):
+ """Initialize this dataset class.
+
+ Parameters:
+ opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
+ """
+ BaseDataset.__init__(self, opt)
+ self.dir = os.path.join(opt.dataroot, opt.phase)
+ self.AB_paths = sorted(make_dataset(self.dir, opt.max_dataset_size))
+ assert(opt.input_nc == 1 and opt.output_nc == 2 and opt.direction == 'AtoB')
+ self.transform = get_transform(self.opt, convert=False)
+
+ def __getitem__(self, index):
+ """Return a data point and its metadata information.
+
+ Parameters:
+ index - - a random integer for data indexing
+
+ Returns a dictionary that contains A, B, A_paths and B_paths
+ A (tensor) - - the L channel of an image
+ B (tensor) - - the ab channels of the same image
+ A_paths (str) - - image paths
+ B_paths (str) - - image paths (same as A_paths)
+ """
+ path = self.AB_paths[index]
+ im = Image.open(path).convert('RGB')
+ im = self.transform(im)
+ im = np.array(im)
+ lab = color.rgb2lab(im).astype(np.float32)
+ lab_t = transforms.ToTensor()(lab)
+ A = lab_t[[0], ...] / 50.0 - 1.0
+ B = lab_t[[1, 2], ...] / 110.0
+ return {'A': A, 'B': B, 'A_paths': path, 'B_paths': path}
+
+ def __len__(self):
+ """Return the total number of images in the dataset."""
+ return len(self.AB_paths)
diff --git a/cyclegan/data/data.lua b/cyclegan/data/data.lua
deleted file mode 100644
index d43586e..0000000
--- a/cyclegan/data/data.lua
+++ /dev/null
@@ -1,103 +0,0 @@
---[[
- This data loader is a modified version of the one from dcgan.torch
- (see https://github.com/soumith/dcgan.torch/blob/master/data/data.lua).
-
- Copyright (c) 2016, Deepak Pathak [See LICENSE file for details]
-]]--
-
-local Threads = require 'threads'
-Threads.serialization('threads.sharedserialize')
-
-local data = {}
-
-local result = {}
-local unpack = unpack and unpack or table.unpack
-
-function data.new(n, opt_)
- opt_ = opt_ or {}
- local self = {}
- for k,v in pairs(data) do
- self[k] = v
- end
-
- local donkey_file = 'donkey_folder.lua'
--- print('n..' .. n)
- if n > 0 then
- local options = opt_
- self.threads = Threads(n,
- function() require 'torch' end,
- function(idx)
- opt = options
- tid = idx
- local seed = (opt.manualSeed and opt.manualSeed or 0) + idx
- torch.manualSeed(seed)
- torch.setnumthreads(1)
- print(string.format('Starting donkey with id: %d seed: %d', tid, seed))
- assert(options, 'options not found')
- assert(opt, 'opt not given')
- print(opt)
- paths.dofile(donkey_file)
- end
-
- )
- else
- if donkey_file then paths.dofile(donkey_file) end
--- print('empty threads')
- self.threads = {}
- function self.threads:addjob(f1, f2) f2(f1()) end
- function self.threads:dojob() end
- function self.threads:synchronize() end
- end
-
- local nSamples = 0
- self.threads:addjob(function() return trainLoader:size() end,
- function(c) nSamples = c end)
- self.threads:synchronize()
- self._size = nSamples
-
- for i = 1, n do
- self.threads:addjob(self._getFromThreads,
- self._pushResult)
- end
--- print(self.threads)
- return self
-end
-
-function data._getFromThreads()
- assert(opt.batchSize, 'opt.batchSize not found')
- return trainLoader:sample(opt.batchSize)
-end
-
-function data._pushResult(...)
- local res = {...}
- if res == nil then
- self.threads:synchronize()
- end
- result[1] = res
-end
-
-
-
-function data:getBatch()
- -- queue another job
- self.threads:addjob(self._getFromThreads, self._pushResult)
- self.threads:dojob()
- local res = result[1]
-
- img_data = res[1]
- img_paths = res[3]
-
- result[1] = nil
- if torch.type(img_data) == 'table' then
- img_data = unpack(img_data)
- end
-
-
- return img_data, img_paths
-end
-
-function data:size()
- return self._size
-end
-
-return data
diff --git a/cyclegan/data/data_util.lua b/cyclegan/data/data_util.lua
deleted file mode 100644
index 319560a..0000000
--- a/cyclegan/data/data_util.lua
+++ /dev/null
@@ -1,24 +0,0 @@
-local data_util = {}
-
-require 'torch'
--- options = require '../options.lua'
--- load dataset from the file system
--- |name|: name of the dataset. It's currently either 'A' or 'B'
-function data_util.load_dataset(name, opt, nc)
- local tensortype = torch.getdefaulttensortype()
- torch.setdefaulttensortype('torch.FloatTensor')
-
- local new_opt = options.clone(opt)
- new_opt.manualSeed = torch.random(1, 10000) -- fix seed
- new_opt.nc = nc
- torch.manualSeed(new_opt.manualSeed)
- local data_loader = paths.dofile('../data/data.lua')
- new_opt.phase = new_opt.phase .. name
- local data = data_loader.new(new_opt.nThreads, new_opt)
- print("Dataset Size " .. name .. ": ", data:size())
-
- torch.setdefaulttensortype(tensortype)
- return data
-end
-
-return data_util
diff --git a/cyclegan/data/dataset.lua b/cyclegan/data/dataset.lua
deleted file mode 100644
index 45328d3..0000000
--- a/cyclegan/data/dataset.lua
+++ /dev/null
@@ -1,398 +0,0 @@
---[[
- Copyright (c) 2015-present, Facebook, Inc.
- All rights reserved.
-
- This source code is licensed under the BSD-style license found in the
- LICENSE file in the root directory of this source tree. An additional grant
- of patent rights can be found in the PATENTS file in the same directory.
-]]--
-
-require 'torch'
-torch.setdefaulttensortype('torch.FloatTensor')
-local ffi = require 'ffi'
-local class = require('pl.class')
-local dir = require 'pl.dir'
-local tablex = require 'pl.tablex'
-local argcheck = require 'argcheck'
-require 'sys'
-require 'xlua'
-require 'image'
-
-local dataset = torch.class('dataLoader')
-
-local initcheck = argcheck{
- pack=true,
- help=[[
- A dataset class for images in a flat folder structure (folder-name is class-name).
- Optimized for extremely large datasets (upwards of 14 million images).
- Tested only on Linux (as it uses command-line linux utilities to scale up)
-]],
- {check=function(paths)
- local out = true;
- for k,v in ipairs(paths) do
- if type(v) ~= 'string' then
- print('paths can only be of string input');
- out = false
- end
- end
- return out
- end,
- name="paths",
- type="table",
- help="Multiple paths of directories with images"},
-
- {name="sampleSize",
- type="table",
- help="a consistent sample size to resize the images"},
-
- {name="split",
- type="number",
- help="Percentage of split to go to Training"
- },
- {name="serial_batches",
- type="number",
- help="if randomly sample training images"},
-
- {name="samplingMode",
- type="string",
- help="Sampling mode: random | balanced ",
- default = "balanced"},
-
- {name="verbose",
- type="boolean",
- help="Verbose mode during initialization",
- default = false},
-
- {name="loadSize",
- type="table",
- help="a size to load the images to, initially",
- opt = true},
-
- {name="forceClasses",
- type="table",
- help="If you want this loader to map certain classes to certain indices, "
- .. "pass a classes table that has {classname : classindex} pairs."
- .. " For example: {3 : 'dog', 5 : 'cat'}"
- .. "This function is very useful when you want two loaders to have the same "
- .. "class indices (trainLoader/testLoader for example)",
- opt = true},
-
- {name="sampleHookTrain",
- type="function",
- help="applied to sample during training(ex: for lighting jitter). "
- .. "It takes the image path as input",
- opt = true},
-
- {name="sampleHookTest",
- type="function",
- help="applied to sample during testing",
- opt = true},
-}
-
-function dataset:__init(...)
-
- -- argcheck
- local args = initcheck(...)
- print(args)
- for k,v in pairs(args) do self[k] = v end
-
- if not self.loadSize then self.loadSize = self.sampleSize; end
-
- if not self.sampleHookTrain then self.sampleHookTrain = self.defaultSampleHook end
- if not self.sampleHookTest then self.sampleHookTest = self.defaultSampleHook end
- self.image_count = 1
--- print('image_count_init', self.image_count)
- -- find class names
- self.classes = {}
- local classPaths = {}
- if self.forceClasses then
- for k,v in pairs(self.forceClasses) do
- self.classes[k] = v
- classPaths[k] = {}
- end
- end
- local function tableFind(t, o) for k,v in pairs(t) do if v == o then return k end end end
- -- loop over each paths folder, get list of unique class names,
- -- also store the directory paths per class
- -- for each class,
- for k,path in ipairs(self.paths) do
--- print('path', path)
- local dirs = {} -- hack
- dirs[1] = path
--- local dirs = dir.getdirectories(path);
- for k,dirpath in ipairs(dirs) do
- local class = paths.basename(dirpath)
- local idx = tableFind(self.classes, class)
--- print(class)
--- print(idx)
- if not idx then
- table.insert(self.classes, class)
- idx = #self.classes
- classPaths[idx] = {}
- end
- if not tableFind(classPaths[idx], dirpath) then
- table.insert(classPaths[idx], dirpath);
- end
- end
- end
-
- self.classIndices = {}
- for k,v in ipairs(self.classes) do
- self.classIndices[v] = k
- end
-
- -- define command-line tools, try your best to maintain OSX compatibility
- local wc = 'wc'
- local cut = 'cut'
- local find = 'find -H' -- if folder name is symlink, do find inside it after dereferencing
-
- if ffi.os == 'OSX' then
- wc = 'gwc'
- cut = 'gcut'
- find = 'gfind'
- end
- ----------------------------------------------------------------------
- -- Options for the GNU find command
- local extensionList = {'jpg', 'png','JPG','PNG','JPEG', 'ppm', 'PPM', 'bmp', 'BMP'}
- local findOptions = ' -iname "*.' .. extensionList[1] .. '"'
- for i=2,#extensionList do
- findOptions = findOptions .. ' -o -iname "*.' .. extensionList[i] .. '"'
- end
-
- -- find the image path names
- self.imagePath = torch.CharTensor() -- path to each image in dataset
- self.imageClass = torch.LongTensor() -- class index of each image (class index in self.classes)
- self.classList = {} -- index of imageList to each image of a particular class
- self.classListSample = self.classList -- the main list used when sampling data
-
- print('running "find" on each class directory, and concatenate all'
- .. ' those filenames into a single file containing all image paths for a given class')
- -- so, generates one file per class
- local classFindFiles = {}
- for i=1,#self.classes do
- classFindFiles[i] = os.tmpname()
- end
- local combinedFindList = os.tmpname();
-
- local tmpfile = os.tmpname()
- local tmphandle = assert(io.open(tmpfile, 'w'))
- -- iterate over classes
- for i, class in ipairs(self.classes) do
- -- iterate over classPaths
- for j,path in ipairs(classPaths[i]) do
- local command = find .. ' "' .. path .. '" ' .. findOptions
- .. ' >>"' .. classFindFiles[i] .. '" \n'
- tmphandle:write(command)
- end
- end
- io.close(tmphandle)
- os.execute('bash ' .. tmpfile)
- os.execute('rm -f ' .. tmpfile)
-
- print('now combine all the files to a single large file')
- local tmpfile = os.tmpname()
- local tmphandle = assert(io.open(tmpfile, 'w'))
- -- concat all finds to a single large file in the order of self.classes
- for i=1,#self.classes do
- local command = 'cat "' .. classFindFiles[i] .. '" >>' .. combinedFindList .. ' \n'
- tmphandle:write(command)
- end
- io.close(tmphandle)
- os.execute('bash ' .. tmpfile)
- os.execute('rm -f ' .. tmpfile)
-
- --==========================================================================
- print('load the large concatenated list of sample paths to self.imagePath')
- local cmd = wc .. " -L '"
- .. combinedFindList .. "' |"
- .. cut .. " -f1 -d' '"
- print('cmd..' .. cmd)
- local maxPathLength = tonumber(sys.fexecute(wc .. " -L '"
- .. combinedFindList .. "' |"
- .. cut .. " -f1 -d' '")) + 1
- local length = tonumber(sys.fexecute(wc .. " -l '"
- .. combinedFindList .. "' |"
- .. cut .. " -f1 -d' '"))
- assert(length > 0, "Could not find any image file in the given input paths")
- assert(maxPathLength > 0, "paths of files are length 0?")
- self.imagePath:resize(length, maxPathLength):fill(0)
- local s_data = self.imagePath:data()
- local count = 0
- for line in io.lines(combinedFindList) do
- ffi.copy(s_data, line)
- s_data = s_data + maxPathLength
- if self.verbose and count % 10000 == 0 then
- xlua.progress(count, length)
- end;
- count = count + 1
- end
-
- self.numSamples = self.imagePath:size(1)
- if self.verbose then print(self.numSamples .. ' samples found.') end
- --==========================================================================
- print('Updating classList and imageClass appropriately')
- self.imageClass:resize(self.numSamples)
- local runningIndex = 0
- for i=1,#self.classes do
- if self.verbose then xlua.progress(i, #(self.classes)) end
- local length = tonumber(sys.fexecute(wc .. " -l '"
- .. classFindFiles[i] .. "' |"
- .. cut .. " -f1 -d' '"))
- if length == 0 then
- error('Class has zero samples')
- else
- self.classList[i] = torch.linspace(runningIndex + 1, runningIndex + length, length):long()
- self.imageClass[{{runningIndex + 1, runningIndex + length}}]:fill(i)
- end
- runningIndex = runningIndex + length
- end
-
- --==========================================================================
- -- clean up temporary files
- print('Cleaning up temporary files')
- local tmpfilelistall = ''
- for i=1,#(classFindFiles) do
- tmpfilelistall = tmpfilelistall .. ' "' .. classFindFiles[i] .. '"'
- if i % 1000 == 0 then
- os.execute('rm -f ' .. tmpfilelistall)
- tmpfilelistall = ''
- end
- end
- os.execute('rm -f ' .. tmpfilelistall)
- os.execute('rm -f "' .. combinedFindList .. '"')
- --==========================================================================
-
- if self.split == 100 then
- self.testIndicesSize = 0
- else
- print('Splitting training and test sets to a ratio of '
- .. self.split .. '/' .. (100-self.split))
- self.classListTrain = {}
- self.classListTest = {}
- self.classListSample = self.classListTrain
- local totalTestSamples = 0
- -- split the classList into classListTrain and classListTest
- for i=1,#self.classes do
- local list = self.classList[i]
- local count = self.classList[i]:size(1)
- local splitidx = math.floor((count * self.split / 100) + 0.5) -- +round
- local perm = torch.randperm(count)
- self.classListTrain[i] = torch.LongTensor(splitidx)
- for j=1,splitidx do
- self.classListTrain[i][j] = list[perm[j]]
- end
- if splitidx == count then -- all samples were allocated to train set
- self.classListTest[i] = torch.LongTensor()
- else
- self.classListTest[i] = torch.LongTensor(count-splitidx)
- totalTestSamples = totalTestSamples + self.classListTest[i]:size(1)
- local idx = 1
- for j=splitidx+1,count do
- self.classListTest[i][idx] = list[perm[j]]
- idx = idx + 1
- end
- end
- end
- -- Now combine classListTest into a single tensor
- self.testIndices = torch.LongTensor(totalTestSamples)
- self.testIndicesSize = totalTestSamples
- local tdata = self.testIndices:data()
- local tidx = 0
- for i=1,#self.classes do
- local list = self.classListTest[i]
- if list:dim() ~= 0 then
- local ldata = list:data()
- for j=0,list:size(1)-1 do
- tdata[tidx] = ldata[j]
- tidx = tidx + 1
- end
- end
- end
- end
-end
-
--- size(), size(class)
-function dataset:size(class, list)
- list = list or self.classList
- if not class then
- return self.numSamples
- elseif type(class) == 'string' then
- return list[self.classIndices[class]]:size(1)
- elseif type(class) == 'number' then
- return list[class]:size(1)
- end
-end
-
--- getByClass
-function dataset:getByClass(class)
- local index = 0
- if self.serial_batches == 1 then
- index = math.fmod(self.image_count-1, self.classListSample[class]:nElement())+1
- self.image_count = self.image_count +1
- else
- index = math.ceil(torch.uniform() * self.classListSample[class]:nElement())
- end
-
- local imgpath = ffi.string(torch.data(self.imagePath[self.classListSample[class][index]]))
- return self:sampleHookTrain(imgpath), imgpath
-end
-
--- converts a table of samples (and corresponding labels) to a clean tensor
-local function tableToOutput(self, dataTable, scalarTable)
- local data, scalarLabels, labels
- if opt.resize_or_crop == 'crop' or opt.resize_or_crop == 'scale_width' or opt.resize_or_crop == 'scale_height' then
- assert(#scalarTable == 1)
- data = torch.Tensor(1,
- dataTable[1]:size(1), dataTable[1]:size(2), dataTable[1]:size(3))
- data[1]:copy(dataTable[1])
- scalarLabels = torch.LongTensor(#scalarTable):fill(-1111)
- else
- local quantity = #scalarTable
- data = torch.Tensor(quantity,
- self.sampleSize[1], self.sampleSize[2], self.sampleSize[3])
- scalarLabels = torch.LongTensor(quantity):fill(-1111)
- for i=1,#dataTable do
- data[i]:copy(dataTable[i])
- scalarLabels[i] = scalarTable[i]
- end
- end
- return data, scalarLabels
-end
-
--- sampler, samples from the training set.
-function dataset:sample(quantity)
- assert(quantity)
- local dataTable = {}
- local scalarTable = {}
- local samplePaths = {}
- for i=1,quantity do
- local class = torch.random(1, #self.classes)
- local out, imgpath = self:getByClass(class)
- table.insert(dataTable, out)
- table.insert(scalarTable, class)
- samplePaths[i] = imgpath
- end
-
- local data, scalarLabels = tableToOutput(self, dataTable, scalarTable)
- return data, scalarLabels, samplePaths-- filePaths
-end
-
-function dataset:get(i1, i2)
- local indices = torch.range(i1, i2);
- local quantity = i2 - i1 + 1;
- assert(quantity > 0)
- -- now that indices has been initialized, get the samples
- local dataTable = {}
- local scalarTable = {}
- for i=1,quantity do
- -- load the sample
- local imgpath = ffi.string(torch.data(self.imagePath[indices[i]]))
- local out = self:sampleHookTest(imgpath)
- table.insert(dataTable, out)
- table.insert(scalarTable, self.imageClass[indices[i]])
- end
- local data, scalarLabels = tableToOutput(self, dataTable, scalarTable)
- return data, scalarLabels
-end
-
-return dataset
diff --git a/cyclegan/data/donkey_folder.lua b/cyclegan/data/donkey_folder.lua
deleted file mode 100644
index 803b9ea..0000000
--- a/cyclegan/data/donkey_folder.lua
+++ /dev/null
@@ -1,200 +0,0 @@
-
---[[
- This data loader is a modified version of the one from dcgan.torch
- (see https://github.com/soumith/dcgan.torch/blob/master/data/donkey_folder.lua).
- Copyright (c) 2016, Deepak Pathak [See LICENSE file for details]
- Copyright (c) 2015-present, Facebook, Inc.
- All rights reserved.
- This source code is licensed under the BSD-style license found in the
- LICENSE file in the root directory of this source tree. An additional grant
- of patent rights can be found in the PATENTS file in the same directory.
-]]--
-
-require 'image'
-paths.dofile('dataset.lua')
--- This file contains the data-loading logic and details.
--- It is run by each data-loader thread.
-------------------------------------------
--------- COMMON CACHES and PATHS
--- Check for existence of opt.data
-if opt.DATA_ROOT then
- opt.data = paths.concat(opt.DATA_ROOT, opt.phase)
-else
- print(os.getenv('DATA_ROOT'))
- opt.data = paths.concat(os.getenv('DATA_ROOT'), opt.phase)
-end
-
-if not paths.dirp(opt.data) then
- error('Did not find directory: ' .. opt.data)
-end
-
--- a cache file of the training metadata (if doesnt exist, will be created)
-local cache_prefix = opt.data:gsub('/', '_')
-os.execute(('mkdir -p %s'):format(opt.cache_dir))
-local trainCache = paths.concat(opt.cache_dir, cache_prefix .. '_trainCache.t7')
-
---------------------------------------------------------------------------------------------
-local input_nc = opt.nc -- input channels
-local loadSize = {input_nc, opt.loadSize}
-local sampleSize = {input_nc, opt.fineSize}
-
-local function loadImage(path)
- local input = image.load(path, 3, 'float')
- local h = input:size(2)
- local w = input:size(3)
-
- local imA = image.crop(input, 0, 0, w/2, h)
- imA = image.scale(imA, loadSize[2], loadSize[2])
- local imB = image.crop(input, w/2, 0, w, h)
- imB = image.scale(imB, loadSize[2], loadSize[2])
-
- local perm = torch.LongTensor{3, 2, 1}
- imA = imA:index(1, perm)
- imA = imA:mul(2):add(-1)
- imB = imB:index(1, perm)
- imB = imB:mul(2):add(-1)
-
- assert(imA:max()<=1,"A: badly scaled inputs")
- assert(imA:min()>=-1,"A: badly scaled inputs")
- assert(imB:max()<=1,"B: badly scaled inputs")
- assert(imB:min()>=-1,"B: badly scaled inputs")
-
-
- local oW = sampleSize[2]
- local oH = sampleSize[2]
- local iH = imA:size(2)
- local iW = imA:size(3)
-
- if iH~=oH then
- h1 = math.ceil(torch.uniform(1e-2, iH-oH))
- end
-
- if iW~=oW then
- w1 = math.ceil(torch.uniform(1e-2, iW-oW))
- end
- if iH ~= oH or iW ~= oW then
- imA = image.crop(imA, w1, h1, w1 + oW, h1 + oH)
- imB = image.crop(imB, w1, h1, w1 + oW, h1 + oH)
- end
-
- if opt.flip == 1 and torch.uniform() > 0.5 then
- imA = image.hflip(imA)
- imB = image.hflip(imB)
- end
-
- local concatenated = torch.cat(imA,imB,1)
-
- return concatenated
-end
-
-
-local function loadSingleImage(path)
- local im = image.load(path, input_nc, 'float')
- if opt.resize_or_crop == 'resize_and_crop' then
- im = image.scale(im, loadSize[2], loadSize[2])
- end
- if input_nc == 3 then
- local perm = torch.LongTensor{3, 2, 1}
- im = im:index(1, perm)--:mul(256.0): brg, rgb
- im = im:mul(2):add(-1)
- end
- assert(im:max()<=1,"A: badly scaled inputs")
- assert(im:min()>=-1,"A: badly scaled inputs")
-
- local oW = sampleSize[2]
- local oH = sampleSize[2]
- local iH = im:size(2)
- local iW = im:size(3)
- if (opt.resize_or_crop == 'resize_and_crop' ) then
- local h1, w1 = 0, 0
- if iH~=oH then
- h1 = math.ceil(torch.uniform(1e-2, iH-oH))
- end
- if iW~=oW then
- w1 = math.ceil(torch.uniform(1e-2, iW-oW))
- end
- if iH ~= oH or iW ~= oW then
- im = image.crop(im, w1, h1, w1 + oW, h1 + oH)
- end
- elseif (opt.resize_or_crop == 'combined') then
- local sH = math.min(math.ceil(oH * torch.uniform(1+1e-2, 2.0-1e-2)), iH-1e-2)
- local sW = math.min(math.ceil(oW * torch.uniform(1+1e-2, 2.0-1e-2)), iW-1e-2)
- local h1 = math.ceil(torch.uniform(1e-2, iH-sH))
- local w1 = math.ceil(torch.uniform(1e-2, iW-sW))
- im = image.crop(im, w1, h1, w1 + sW, h1 + sH)
- im = image.scale(im, oW, oH)
- elseif (opt.resize_or_crop == 'crop') then
- local w = math.min(math.min(oH, iH),iW)
- w = math.floor(w/4)*4
- local x = math.floor(torch.uniform(0, iW - w))
- local y = math.floor(torch.uniform(0, iH - w))
- im = image.crop(im, x, y, x+w, y+w)
- elseif (opt.resize_or_crop == 'scale_width') then
- w = oW
- h = torch.floor(iH * oW/iW)
- im = image.scale(im, w, h)
- elseif (opt.resize_or_crop == 'scale_height') then
- h = oH
- w = torch.floor(iW * oH / iH)
- im = image.scale(im, w, h)
- end
-
- if opt.flip == 1 and torch.uniform() > 0.5 then
- im = image.hflip(im)
- end
-
- return im
-
-end
-
--- channel-wise mean and std. Calculate or load them from disk later in the script.
-local mean,std
---------------------------------------------------------------------------------
--- Hooks that are used for each image that is loaded
-
--- function to load the image, jitter it appropriately (random crops etc.)
-local trainHook_singleimage = function(self, path)
- collectgarbage()
- -- print('load single image')
- local im = loadSingleImage(path)
- return im
-end
-
--- function that loads images that have juxtaposition
--- of two images from two domains
-local trainHook_doubleimage = function(self, path)
- -- print('load double image')
- collectgarbage()
-
- local im = loadImage(path)
- return im
-end
-
-
-if opt.align_data > 0 then
- sample_nc = input_nc*2
- trainHook = trainHook_doubleimage
-else
- sample_nc = input_nc
- trainHook = trainHook_singleimage
-end
-
-trainLoader = dataLoader{
- paths = {opt.data},
- loadSize = {input_nc, loadSize[2], loadSize[2]},
- sampleSize = {sample_nc, sampleSize[2], sampleSize[2]},
- split = 100,
- serial_batches = opt.serial_batches,
- verbose = true
- }
-
-trainLoader.sampleHookTrain = trainHook
-collectgarbage()
-
--- do some sanity checks on trainLoader
-do
- local class = trainLoader.imageClass
- local nClasses = #trainLoader.classes
- assert(class:max() <= nClasses, "class logic has error")
- assert(class:min() >= 1, "class logic has error")
-end
diff --git a/cyclegan/data/image_folder.py b/cyclegan/data/image_folder.py
new file mode 100644
index 0000000..d0b4b30
--- /dev/null
+++ b/cyclegan/data/image_folder.py
@@ -0,0 +1,67 @@
+"""A modified image folder class
+
+We modify the official PyTorch image folder (https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py)
+so that this class can load images from both current directory and its subdirectories.
+"""
+
+import torch.utils.data as data
+
+from PIL import Image
+import os
+import os.path
+
+IMG_EXTENSIONS = [
+ '.jpg', '.JPG', '.jpeg', '.JPEG',
+ '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP',
+ '.tif', '.TIF', '.tiff', '.TIFF',
+]
+
+
+def is_image_file(filename):
+ return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)
+
+
+def make_dataset(dir, max_dataset_size=float("inf")):
+ images = []
+ assert os.path.isdir(dir), '%s is not a valid directory' % dir
+
+ for root, _, fnames in sorted(os.walk(dir)):
+ for fname in fnames:
+ if is_image_file(fname):
+ path = os.path.join(root, fname)
+ images.append(path)
+ return images[:min(max_dataset_size, len(images))]
+
+
+def default_loader(path):
+ return Image.open(path).convert('RGB')
+
+
+class ImageFolder(data.Dataset):
+
+ def __init__(self, root, transform=None, return_paths=False,
+ loader=default_loader):
+ imgs = make_dataset(root)
+ if len(imgs) == 0:
+ raise(RuntimeError("Found 0 images in: " + root + "\n"
+ "Supported image extensions are: " +
+ ",".join(IMG_EXTENSIONS)))
+
+ self.root = root
+ self.imgs = imgs
+ self.transform = transform
+ self.return_paths = return_paths
+ self.loader = loader
+
+ def __getitem__(self, index):
+ path = self.imgs[index]
+ img = self.loader(path)
+ if self.transform is not None:
+ img = self.transform(img)
+ if self.return_paths:
+ return img, path
+ else:
+ return img
+
+ def __len__(self):
+ return len(self.imgs)
diff --git a/cyclegan/data/single_dataset.py b/cyclegan/data/single_dataset.py
new file mode 100644
index 0000000..9a5c323
--- /dev/null
+++ b/cyclegan/data/single_dataset.py
@@ -0,0 +1,40 @@
+from data.base_dataset import BaseDataset, get_transform
+from data.image_folder import make_dataset
+from PIL import Image
+
+
+class SingleDataset(BaseDataset):
+ """This dataset class can load a set of images specified by the path --dataroot /path/to/data.
+
+ It can be used for generating CycleGAN results only for one side with the model option '-model test'.
+ """
+
+ def __init__(self, opt):
+ """Initialize this dataset class.
+
+ Parameters:
+ opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
+ """
+ BaseDataset.__init__(self, opt)
+ self.A_paths = sorted(make_dataset(opt.dataroot, opt.max_dataset_size))
+ input_nc = self.opt.output_nc if self.opt.direction == 'BtoA' else self.opt.input_nc
+ self.transform = get_transform(opt, grayscale=(input_nc == 1))
+
+ def __getitem__(self, index):
+ """Return a data point and its metadata information.
+
+ Parameters:
+ index - - a random integer for data indexing
+
+ Returns a dictionary that contains A and A_paths
+ A(tensor) - - an image in one domain
+ A_paths(str) - - the path of the image
+ """
+ A_path = self.A_paths[index]
+ A_img = Image.open(A_path).convert('RGB')
+ A = self.transform(A_img)
+ return {'A': A, 'A_paths': A_path}
+
+ def __len__(self):
+ """Return the total number of images in the dataset."""
+ return len(self.A_paths)
diff --git a/cyclegan/data/template_dataset.py b/cyclegan/data/template_dataset.py
new file mode 100644
index 0000000..bfdf16b
--- /dev/null
+++ b/cyclegan/data/template_dataset.py
@@ -0,0 +1,75 @@
+"""Dataset class template
+
+This module provides a template for users to implement custom datasets.
+You can specify '--dataset_mode template' to use this dataset.
+The class name should be consistent with both the filename and its dataset_mode option.
+The filename should be _dataset.py
+The class name should be Dataset.py
+You need to implement the following functions:
+ -- : Add dataset-specific options and rewrite default values for existing options.
+ -- <__init__>: Initialize this dataset class.
+ -- <__getitem__>: Return a data point and its metadata information.
+ -- <__len__>: Return the number of images.
+"""
+from data.base_dataset import BaseDataset, get_transform
+# from data.image_folder import make_dataset
+# from PIL import Image
+
+
+class TemplateDataset(BaseDataset):
+ """A template dataset class for you to implement custom datasets."""
+ @staticmethod
+ def modify_commandline_options(parser, is_train):
+ """Add new dataset-specific options, and rewrite default values for existing options.
+
+ Parameters:
+ parser -- original option parser
+ is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.
+
+ Returns:
+ the modified parser.
+ """
+ parser.add_argument('--new_dataset_option', type=float, default=1.0, help='new dataset option')
+ parser.set_defaults(max_dataset_size=10, new_dataset_option=2.0) # specify dataset-specific default values
+ return parser
+
+ def __init__(self, opt):
+ """Initialize this dataset class.
+
+ Parameters:
+ opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
+
+ A few things can be done here.
+ - save the options (have been done in BaseDataset)
+ - get image paths and meta information of the dataset.
+ - define the image transformation.
+ """
+ # save the option and dataset root
+ BaseDataset.__init__(self, opt)
+ # get the image paths of your dataset;
+ self.image_paths = [] # You can call sorted(make_dataset(self.root, opt.max_dataset_size)) to get all the image paths under the directory self.root
+ # define the default transform function. You can use ; You can also define your custom transform function
+ self.transform = get_transform(opt)
+
+ def __getitem__(self, index):
+ """Return a data point and its metadata information.
+
+ Parameters:
+ index -- a random integer for data indexing
+
+ Returns:
+ a dictionary of data with their names. It usually contains the data itself and its metadata information.
+
+ Step 1: get a random image path: e.g., path = self.image_paths[index]
+ Step 2: load your data from the disk: e.g., image = Image.open(path).convert('RGB').
+ Step 3: convert your data to a PyTorch tensor. You can use helpder functions such as self.transform. e.g., data = self.transform(image)
+ Step 4: return a data point as a dictionary.
+ """
+ path = 'temp' # needs to be a string
+ data_A = None # needs to be a tensor
+ data_B = None # needs to be a tensor
+ return {'data_A': data_A, 'data_B': data_B, 'path': path}
+
+ def __len__(self):
+ """Return the total number of images."""
+ return len(self.image_paths)
diff --git a/cyclegan/data/unaligned_data_loader.lua b/cyclegan/data/unaligned_data_loader.lua
deleted file mode 100644
index 526ba73..0000000
--- a/cyclegan/data/unaligned_data_loader.lua
+++ /dev/null
@@ -1,49 +0,0 @@
---------------------------------------------------------------------------------
--- Subclass of BaseDataLoader that provides data from two datasets.
--- The samples from the datasets are not aligned.
--- The datasets can have different sizes
---------------------------------------------------------------------------------
-require 'data.base_data_loader'
-
-local class = require 'class'
-data_util = paths.dofile('data_util.lua')
-
-UnalignedDataLoader = class('UnalignedDataLoader', 'BaseDataLoader')
-
-function UnalignedDataLoader:__init(conf)
- BaseDataLoader.__init(self, conf)
- conf = conf or {}
-end
-
-function UnalignedDataLoader:name()
- return 'UnalignedDataLoader'
-end
-
-function UnalignedDataLoader:Initialize(opt)
- opt.align_data = 0
- self.dataA = data_util.load_dataset('A', opt, opt.input_nc)
- self.dataB = data_util.load_dataset('B', opt, opt.output_nc)
-end
-
--- actually fetches the data
--- |return|: a table of two tables, each corresponding to
--- the batch for dataset A and dataset B
-function UnalignedDataLoader:LoadBatchForAllDatasets()
- local batchA, pathA = self.dataA:getBatch()
- local batchB, pathB = self.dataB:getBatch()
- return batchA, batchB, pathA, pathB
-end
-
--- returns the size of each dataset
-function UnalignedDataLoader:size(dataset)
- if dataset == 'A' then
- return self.dataA:size()
- end
-
- if dataset == 'B' then
- return self.dataB:size()
- end
-
- return math.max(self.dataA:size(), self.dataB:size())
- -- return the size of the largest dataset by default
-end
diff --git a/cyclegan/data/unaligned_dataset.py b/cyclegan/data/unaligned_dataset.py
new file mode 100644
index 0000000..832bc88
--- /dev/null
+++ b/cyclegan/data/unaligned_dataset.py
@@ -0,0 +1,71 @@
+import os.path
+from data.base_dataset import BaseDataset, get_transform
+from data.image_folder import make_dataset
+from PIL import Image
+import random
+
+
+class UnalignedDataset(BaseDataset):
+ """
+ This dataset class can load unaligned/unpaired datasets.
+
+ It requires two directories to host training images from domain A '/path/to/data/trainA'
+ and from domain B '/path/to/data/trainB' respectively.
+ You can train the model with the dataset flag '--dataroot /path/to/data'.
+ Similarly, you need to prepare two directories:
+ '/path/to/data/testA' and '/path/to/data/testB' during test time.
+ """
+
+ def __init__(self, opt):
+ """Initialize this dataset class.
+
+ Parameters:
+ opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
+ """
+ BaseDataset.__init__(self, opt)
+ self.dir_A = os.path.join(opt.dataroot, opt.phase + 'A') # create a path '/path/to/data/trainA'
+ self.dir_B = os.path.join(opt.dataroot, opt.phase + 'B') # create a path '/path/to/data/trainB'
+
+ self.A_paths = sorted(make_dataset(self.dir_A, opt.max_dataset_size)) # load images from '/path/to/data/trainA'
+ self.B_paths = sorted(make_dataset(self.dir_B, opt.max_dataset_size)) # load images from '/path/to/data/trainB'
+ self.A_size = len(self.A_paths) # get the size of dataset A
+ self.B_size = len(self.B_paths) # get the size of dataset B
+ btoA = self.opt.direction == 'BtoA'
+ input_nc = self.opt.output_nc if btoA else self.opt.input_nc # get the number of channels of input image
+ output_nc = self.opt.input_nc if btoA else self.opt.output_nc # get the number of channels of output image
+ self.transform_A = get_transform(self.opt, grayscale=(input_nc == 1))
+ self.transform_B = get_transform(self.opt, grayscale=(output_nc == 1))
+
+ def __getitem__(self, index):
+ """Return a data point and its metadata information.
+
+ Parameters:
+ index (int) -- a random integer for data indexing
+
+ Returns a dictionary that contains A, B, A_paths and B_paths
+ A (tensor) -- an image in the input domain
+ B (tensor) -- its corresponding image in the target domain
+ A_paths (str) -- image paths
+ B_paths (str) -- image paths
+ """
+ A_path = self.A_paths[index % self.A_size] # make sure index is within then range
+ if self.opt.serial_batches: # make sure index is within then range
+ index_B = index % self.B_size
+ else: # randomize the index for domain B to avoid fixed pairs.
+ index_B = random.randint(0, self.B_size - 1)
+ B_path = self.B_paths[index_B]
+ A_img = Image.open(A_path).convert('RGB')
+ B_img = Image.open(B_path).convert('RGB')
+ # apply image transformation
+ A = self.transform_A(A_img)
+ B = self.transform_B(B_img)
+
+ return {'A': A, 'B': B, 'A_paths': A_path, 'B_paths': B_path}
+
+ def __len__(self):
+ """Return the total number of images in the dataset.
+
+ As we have two datasets with potentially different number of images,
+ we take a maximum of
+ """
+ return max(self.A_size, self.B_size)
diff --git a/cyclegan/docs/Dockerfile b/cyclegan/docs/Dockerfile
new file mode 100644
index 0000000..99cb43d
--- /dev/null
+++ b/cyclegan/docs/Dockerfile
@@ -0,0 +1,13 @@
+FROM nvidia/cuda:9.0-base
+
+RUN apt update && apt install -y wget unzip curl bzip2 git
+RUN curl -LO http://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh
+RUN bash Miniconda-latest-Linux-x86_64.sh -p /miniconda -b
+RUN rm Miniconda-latest-Linux-x86_64.sh
+ENV PATH=/miniconda/bin:${PATH}
+RUN conda update -y conda
+
+RUN conda install -y pytorch torchvision -c pytorch
+RUN mkdir /workspace/ && cd /workspace/ && git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix.git && cd pytorch-CycleGAN-and-pix2pix && pip install -r requirements.txt
+
+WORKDIR /workspace
\ No newline at end of file
diff --git a/cyclegan/docs/README_es.md b/cyclegan/docs/README_es.md
new file mode 100644
index 0000000..4ac01a5
--- /dev/null
+++ b/cyclegan/docs/README_es.md
@@ -0,0 +1,238 @@
+
+
+
+
+# CycleGAN y pix2pix en PyTorch
+
+Implementacion en PyTorch de Unpaired Image-to-Image Translation.
+
+Este codigo fue escrito por [Jun-Yan Zhu](https://github.com/junyanz) y [Taesung Park](https://github.com/taesung), y con ayuda de [Tongzhou Wang](https://ssnl.github.io/).
+
+Esta implementacion de PyTorch produce resultados comparables o mejores que nuestros original software de Torch. Si te gustaria producir los mismos resultados que en documento oficial, echa un vistazo al codigo original [CycleGAN Torch](https://github.com/junyanz/CycleGAN) y [pix2pix Torch](https://github.com/phillipi/pix2pix)
+
+**Aviso**: El software actual funciona correctamente en PyTorch 0.41+. Para soporte en PyTorch 0.1-0.3: [branch](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/tree/pytorch0.3.1).
+
+Puede encontrar información útil en [training/test tips](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md) y [preguntas frecuentes](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/qa.md). Para implementar modelos y conjuntos de datos personalizados, consulte nuestro [templates](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/README_es.md#modelo-y-dataset-personalizado). Para ayudar a los usuarios a comprender y adaptar mejor nuestra base de código, proporcionamos un [overview](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/overview.md) de la estructura de código de este repositorio.
+
+**CycleGAN: [Proyecto](https://junyanz.github.io/CycleGAN/) | [PDF](https://arxiv.org/pdf/1703.10593.pdf) | [Torch](https://github.com/junyanz/CycleGAN) |
+[Guia de Tensorflow Core](https://www.tensorflow.org/tutorials/generative/cyclegan) | [PyTorch Colab](https://colab.research.google.com/github/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/CycleGAN.ipynb)**
+
+
+
+**Pix2pix: [Proyeto](https://phillipi.github.io/pix2pix/) | [PDF](https://arxiv.org/pdf/1611.07004.pdf) | [Torch](https://github.com/phillipi/pix2pix) |
+[Guia de Tensorflow Core](https://www.tensorflow.org/tutorials/generative/cyclegan) | [PyTorch Colab](https://colab.research.google.com/github/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/pix2pix.ipynb)**
+
+
+
+
+**[EdgesCats Demo](https://affinelayer.com/pixsrv/) | [pix2pix-tensorflow](https://github.com/affinelayer/pix2pix-tensorflow) | por [Christopher Hesse](https://twitter.com/christophrhesse)**
+
+
+
+Si usa este código para su investigación, cite:
+
+Unpaired Image-to-Image Translation usando Cycle-Consistent Adversarial Networks.
+[Jun-Yan Zhu](https://people.eecs.berkeley.edu/~junyanz/)\*, [Taesung Park](https://taesung.me/)\*, [Phillip Isola](https://people.eecs.berkeley.edu/~isola/), [Alexei A. Efros](https://people.eecs.berkeley.edu/~efros). In ICCV 2017. (* contribucion igualitaria) [[Bibtex]](https://junyanz.github.io/CycleGAN/CycleGAN.txt)
+
+
+Image-to-Image Translation usando Conditional Adversarial Networks.
+[Phillip Isola](https://people.eecs.berkeley.edu/~isola), [Jun-Yan Zhu](https://people.eecs.berkeley.edu/~junyanz), [Tinghui Zhou](https://people.eecs.berkeley.edu/~tinghuiz), [Alexei A. Efros](https://people.eecs.berkeley.edu/~efros). In CVPR 2017. [[Bibtex]](http://people.csail.mit.edu/junyanz/projects/pix2pix/pix2pix.bib)
+
+## Charlas y curso
+Presentacion en PowerPoint de Pix2pix: [keynote](http://efrosgans.eecs.berkeley.edu/CVPR18_slides/pix2pix.key) | [pdf](http://efrosgans.eecs.berkeley.edu/CVPR18_slides/pix2pix.pdf),
+Presentacion en PowerPoint de CycleGAN: [pptx](http://efrosgans.eecs.berkeley.edu/CVPR18_slides/CycleGAN.pptx) | [pdf](http://efrosgans.eecs.berkeley.edu/CVPR18_slides/CycleGAN.pdf)
+
+Asignación del curso CycleGAN [codigo](http://www.cs.toronto.edu/~rgrosse/courses/csc321_2018/assignments/a4-code.zip) y [handout](http://www.cs.toronto.edu/~rgrosse/courses/csc321_2018/assignments/a4-handout.pdf) diseñado por el Prof. [Roger Grosse](http://www.cs.toronto.edu/~rgrosse/) for [CSC321](http://www.cs.toronto.edu/~rgrosse/courses/csc321_2018/) "Intro to Neural Networks and Machine Learning" en la universidad de Toronto. Póngase en contacto con el instructor si desea adoptarlo en su curso.
+
+## Colab Notebook
+TensorFlow Core CycleGAN Tutorial: [Google Colab](https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/generative/cyclegan.ipynb) | [Codigo](https://github.com/tensorflow/docs/blob/master/site/en/tutorials/generative/cyclegan.ipynb)
+
+Guia de TensorFlow Core pix2pix : [Google Colab](https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/generative/pix2pix.ipynb) | [Codigo](https://github.com/tensorflow/docs/blob/master/site/en/tutorials/generative/pix2pix.ipynb)
+
+PyTorch Colab notebook: [CycleGAN](https://colab.research.google.com/github/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/CycleGAN.ipynb) y [pix2pix](https://colab.research.google.com/github/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/pix2pix.ipynb)
+
+## Otras implementaciones
+### CycleGAN
+ [Tensorflow] (por Harry Yang),
+[Tensorflow] (por Archit Rathore),
+[Tensorflow] (por Van Huy),
+[Tensorflow] (por Xiaowei Hu),
+ [Tensorflow-simple] (por Zhenliang He),
+ [TensorLayer] (por luoxier),
+[Chainer] (por Yanghua Jin),
+[Minimal PyTorch] (por yunjey),
+[Mxnet] (por Ldpe2G),
+[lasagne/Keras] (por tjwei),
+[Keras] (por Simon Karlsson)
+
+
+
+### pix2pix
+ [Tensorflow] (por Christopher Hesse),
+[Tensorflow] (por Eyyüb Sariu),
+ [Tensorflow (face2face)] (por Dat Tran),
+ [Tensorflow (film)] (por Arthur Juliani),
+[Tensorflow (zi2zi)] (por Yuchen Tian),
+[Chainer] (por mattya),
+[tf/torch/keras/lasagne] (por tjwei),
+[Pytorch] (por taey16)
+
+
+
+## Requerimientos
+- Linux o macOS
+- Python 3
+- CPU o NVIDIA GPU usando CUDA CuDNN
+
+## Inicio
+### Instalación
+
+- Clone este repositorio:
+```bash
+git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
+cd pytorch-CycleGAN-and-pix2pix
+```
+
+- Instale [PyTorch](http://pytorch.org) 0.4+ y sus otras dependencias (e.g., torchvision, [visdom](https://github.com/facebookresearch/visdom) y [dominate](https://github.com/Knio/dominate)).
+ - Para uso de pip, por favor escriba el comando `pip install -r requirements.txt`.
+ - Para uso de Conda, proporcionamos un script de instalación `./scripts/conda_deps.sh`. De forma alterna, puede crear un nuevo entorno Conda usando `conda env create -f environment.yml`.
+ - Para uso de Docker, Proporcionamos la imagen Docker y el archivo Docker preconstruidos. Por favor, consulte nuestra página
+ [Docker](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/docker.md).
+
+### CycleGAN entreanimiento/test
+- Descargar el dataset de CycleGAN (e.g. maps):
+```bash
+bash ./datasets/download_cyclegan_dataset.sh maps
+```
+- Para ver los resultados del entrenamiento y las gráficas de pérdidas, `python -m visdom.server` y haga clic en la URL
+ http://localhost:8097.
+- Entrenar el modelo:
+```bash
+#!./scripts/train_cyclegan.sh
+python train.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan
+```
+Para ver más resultados intermedios, consulte `./checkpoints/maps_cyclegan/web/index.html`.
+- Pruebe el modelo:
+```bash
+#!./scripts/test_cyclegan.sh
+python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan
+```
+-Los resultados de la prueba se guardarán en un archivo html aquí: `./results/maps_cyclegan/latest_test/index.html`.
+
+### pix2pix entrenamiento/test
+- Descargue el dataset de pix2pix (e.g.[facades](http://cmp.felk.cvut.cz/~tylecr1/facade/)):
+```bash
+bash ./datasets/download_pix2pix_dataset.sh facades
+```
+- Para ver los resultados del entrenamiento y las gráficas de pérdidas `python -m visdom.server`, haga clic en la URL http://localhost:8097.
+- Para entrenar el modelo:
+```bash
+#!./scripts/train_pix2pix.sh
+python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA
+```
+Para ver más resultados intermedios, consulte `./checkpoints/facades_pix2pix/web/index.html`.
+
+- Pruebe el modelo (`bash ./scripts/test_pix2pix.sh`):
+```bash
+#!./scripts/test_pix2pix.sh
+python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA
+```
+- Los resultados de la prueba se guardarán en un archivo html aquí: `./results/facades_pix2pix/test_latest/index.html`. Puede encontrar más scripts en `scripts` directory.
+- Para entrenar y probar modelos de colorización basados en pix2pix, agregue la linea `--model colorization` y `--dataset_mode colorization`. Para más detalles de nuestro entrenamiento [tips](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md#notes-on-colorization).
+
+### Aplicar un modelo pre-entrenado (CycleGAN)
+- Puedes descargar un modelo previamente entrenado (e.g. horse2zebra) con el siguiente script:
+```bash
+bash ./scripts/download_cyclegan_model.sh horse2zebra
+```
+- El modelo pre-entrenado se guarda en `./checkpoints/{name}_pretrained/latest_net_G.pth`. Revise [aqui](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/scripts/download_cyclegan_model.sh#L3) para todos los modelos CycleGAN disponibles.
+
+- Para probar el modelo, también debe descargar el dataset horse2zebra:
+```bash
+bash ./datasets/download_cyclegan_dataset.sh horse2zebra
+```
+
+- Luego genere los resultados usando:
+```bash
+python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout
+```
+- La opcion `--model test` ise usa para generar resultados de CycleGAN de un solo lado. Esta opción configurará automáticamente
+ `--dataset_mode single`, carga solo las imágenes de un conjunto. Por el contrario, el uso de `--model cycle_gan` requiere cargar y generar resultados en ambas direcciones, lo que a veces es innecesario. Los resultados se guardarán en `./results/`. Use `--results_dir {directory_path_to_save_result}` para especificar el directorio de resultados.
+
+- Para sus propios experimentos, es posible que desee especificar `--netG`, `--norm`, `--no_dropout` para que coincida con la arquitectura del generador del modelo entrenado.
+
+### Aplicar un modelo pre-entrenado (pix2pix)
+Descargue un modelo pre-entrenado con `./scripts/download_pix2pix_model.sh`.
+
+- Revise [aqui](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/scripts/download_pix2pix_model.sh#L3) para todos los modelos pix2pix disponibles. Por ejemplo, si desea descargar el modelo label2photo en el dataset:
+```bash
+bash ./scripts/download_pix2pix_model.sh facades_label2photo
+```
+- Descarga el dataset facades de pix2pix:
+```bash
+bash ./datasets/download_pix2pix_dataset.sh facades
+```
+- Luego genere los resultados usando:
+```bash
+python test.py --dataroot ./datasets/facades/ --direction BtoA --model pix2pix --name facades_label2photo_pretrained
+```
+- Tenga en cuenta que `--direction BtoA` como Facades dataset's, son direcciones A o B para etiquetado de fotos.
+
+- Si desea aplicar un modelo previamente entrenado a una colección de imágenes de entrada (en lugar de pares de imágenes), use la opcion `--model test`. Vea `./scripts/test_single.sh` obre cómo aplicar un modelo a Facade label maps (almacenados en el directorio `facades/testB`).
+
+- Vea una lista de los modelos disponibles actualmente en `./scripts/download_pix2pix_model.sh`
+
+## [Docker](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/docker.md)
+Proporcionamos la imagen Docker y el archivo Docker preconstruidos que pueden ejecutar este repositorio de código. Ver [docker](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/docker.md).
+
+## [Datasets](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/datasets.md)
+Descargue los conjuntos de datos pix2pix / CycleGAN y cree sus propios conjuntos de datos.
+
+## [Entretanimiento/Test Tips](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md)
+Las mejores prácticas para entrenar y probar sus modelos.
+
+## [Preguntas frecuentes](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/qa.md)
+Antes de publicar una nueva pregunta, primero mire las preguntas y respuestas anteriores y los problemas existentes de GitHub.
+
+## Modelo y Dataset personalizado
+Si planea implementar modelos y conjuntos de datos personalizados para sus nuevas aplicaciones, proporcionamos un conjunto de datos [template](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/data/template_dataset.py) y un modelo [template](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/models/template_model.py) como punto de partida.
+
+
+## [Estructura de codigo](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/overview.md)
+Para ayudar a los usuarios a comprender mejor y usar nuestro código, presentamos brevemente la funcionalidad e implementación de cada paquete y cada módulo.
+
+## Solicitud de Pull
+Siempre puede contribuir a este repositorio enviando un [pull request](https://help.github.com/articles/about-pull-requests/).
+Por favor ejecute `flake8 --ignore E501 .` y `python ./scripts/test_before_push.py` antes de realizar un Pull en el código, asegure de también actualizar la estructura del código [overview](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/overview.md) en consecuencia si agrega o elimina archivos.
+
+
+## Citación
+Si utiliza este código para su investigación, cite nuestros documentos.
+```
+@inproceedings{CycleGAN2017,
+ title={Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networkss},
+ 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}
+}
+```
+
+## Proyectos relacionados
+**[CycleGAN-Torch](https://github.com/junyanz/CycleGAN) |
+[pix2pix-Torch](https://github.com/phillipi/pix2pix) | [pix2pixHD](https://github.com/NVIDIA/pix2pixHD)|
+[BicycleGAN](https://github.com/junyanz/BicycleGAN) | [vid2vid](https://tcwang0509.github.io/vid2vid/) | [SPADE/GauGAN](https://github.com/NVlabs/SPADE)**
+**[iGAN](https://github.com/junyanz/iGAN) | [GAN Dissection](https://github.com/CSAILVision/GANDissect) | [GAN Paint](http://ganpaint.io/)**
+
+## Cat Paper Collection
+Si amas a los gatos y te encanta leer gráficos geniales, computer vision y documentos de aprendizaje, echa un vistazo a Cat Paper [Collection](https://github.com/junyanz/CatPapers).
+
+## Agradecimientos
+Nuestro código fue inspirado en [pytorch-DCGAN](https://github.com/pytorch/examples/tree/master/dcgan).
diff --git a/cyclegan/docs/datasets.md b/cyclegan/docs/datasets.md
new file mode 100644
index 0000000..b53c7db
--- /dev/null
+++ b/cyclegan/docs/datasets.md
@@ -0,0 +1,44 @@
+
+
+### CycleGAN Datasets
+Download the CycleGAN datasets using the following script. Some of the datasets are collected by other researchers. Please cite their papers if you use the data.
+```bash
+bash ./datasets/download_cyclegan_dataset.sh dataset_name
+```
+- `facades`: 400 images from the [CMP Facades dataset](http://cmp.felk.cvut.cz/~tylecr1/facade). [[Citation](../datasets/bibtex/facades.tex)]
+- `cityscapes`: 2975 images from the [Cityscapes training set](https://www.cityscapes-dataset.com). [[Citation](../datasets/bibtex/cityscapes.tex)]. Note: Due to license issue, we cannot directly provide the Cityscapes dataset. Please download the Cityscapes dataset from [https://cityscapes-dataset.com](https://cityscapes-dataset.com) and use the script `./datasets/prepare_cityscapes_dataset.py`.
+- `maps`: 1096 training images scraped from Google Maps.
+- `horse2zebra`: 939 horse images and 1177 zebra images downloaded from [ImageNet](http://www.image-net.org) using keywords `wild horse` and `zebra`
+- `apple2orange`: 996 apple images and 1020 orange images downloaded from [ImageNet](http://www.image-net.org) using keywords `apple` and `navel orange`.
+- `summer2winter_yosemite`: 1273 summer Yosemite images and 854 winter Yosemite images were downloaded using Flickr API. See more details in our paper.
+- `monet2photo`, `vangogh2photo`, `ukiyoe2photo`, `cezanne2photo`: The art images were downloaded from [Wikiart](https://www.wikiart.org/). The real photos are downloaded from Flickr using the combination of the tags *landscape* and *landscapephotography*. The training set size of each class is Monet:1074, Cezanne:584, Van Gogh:401, Ukiyo-e:1433, Photographs:6853.
+- `iphone2dslr_flower`: both classes of images were downlaoded from Flickr. The training set size of each class is iPhone:1813, DSLR:3316. See more details in our paper.
+
+To train a model on your own datasets, you need to create a data folder with two subdirectories `trainA` and `trainB` that contain images from domain A and B. You can test your model on your training set by setting `--phase train` in `test.py`. You can also create subdirectories `testA` and `testB` if you have test data.
+
+You should **not** expect our method to work on just any random combination of input and output datasets (e.g. `cats<->keyboards`). From our experiments, we find it works better if two datasets share similar visual content. For example, `landscape painting<->landscape photographs` works much better than `portrait painting <-> landscape photographs`. `zebras<->horses` achieves compelling results while `cats<->dogs` completely fails.
+
+### pix2pix datasets
+Download the pix2pix datasets using the following script. Some of the datasets are collected by other researchers. Please cite their papers if you use the data.
+```bash
+bash ./datasets/download_pix2pix_dataset.sh dataset_name
+```
+- `facades`: 400 images from [CMP Facades dataset](http://cmp.felk.cvut.cz/~tylecr1/facade). [[Citation](../datasets/bibtex/facades.tex)]
+- `cityscapes`: 2975 images from the [Cityscapes training set](https://www.cityscapes-dataset.com). [[Citation](../datasets/bibtex/cityscapes.tex)]
+- `maps`: 1096 training images scraped from Google Maps
+- `edges2shoes`: 50k training images from [UT Zappos50K dataset](http://vision.cs.utexas.edu/projects/finegrained/utzap50k). Edges are computed by [HED](https://github.com/s9xie/hed) edge detector + post-processing. [[Citation](datasets/bibtex/shoes.tex)]
+- `edges2handbags`: 137K Amazon Handbag images from [iGAN project](https://github.com/junyanz/iGAN). Edges are computed by [HED](https://github.com/s9xie/hed) edge detector + post-processing. [[Citation](datasets/bibtex/handbags.tex)]
+- `night2day`: around 20K natural scene images from [Transient Attributes dataset](http://transattr.cs.brown.edu/) [[Citation](datasets/bibtex/transattr.tex)]. To train a `day2night` pix2pix model, you need to add `--direction BtoA`.
+
+We provide a python script to generate pix2pix training data in the form of pairs of images {A,B}, where A and B are two different depictions of the same underlying scene. For example, these might be pairs {label map, photo} or {bw image, color image}. Then we can learn to translate A to B or B to A:
+
+Create folder `/path/to/data` with subfolders `A` and `B`. `A` and `B` should each have their own subfolders `train`, `val`, `test`, etc. In `/path/to/data/A/train`, put training images in style A. In `/path/to/data/B/train`, put the corresponding images in style B. Repeat same for other data splits (`val`, `test`, etc).
+
+Corresponding images in a pair {A,B} must be the same size and have the same filename, e.g., `/path/to/data/A/train/1.jpg` is considered to correspond to `/path/to/data/B/train/1.jpg`.
+
+Once the data is formatted this way, call:
+```bash
+python datasets/combine_A_and_B.py --fold_A /path/to/data/A --fold_B /path/to/data/B --fold_AB /path/to/data
+```
+
+This will combine each pair of images (A,B) into a single image file, ready for training.
diff --git a/cyclegan/docs/docker.md b/cyclegan/docs/docker.md
new file mode 100644
index 0000000..74043e3
--- /dev/null
+++ b/cyclegan/docs/docker.md
@@ -0,0 +1,38 @@
+# Docker image with pytorch-CycleGAN-and-pix2pix
+
+We provide both Dockerfile and pre-built Docker container that can run this code repo.
+
+## Prerequisite
+
+- Install [docker-ce](https://docs.docker.com/install/linux/docker-ce/ubuntu/)
+- Install [nvidia-docker](https://github.com/NVIDIA/nvidia-docker#quickstart)
+
+## Running pre-built Dockerfile
+
+- Pull the pre-built docker file
+
+```bash
+docker pull taesungp/pytorch-cyclegan-and-pix2pix
+```
+
+- Start an interactive docker session. `-p 8097:8097` option is needed if you want to run `visdom` server on the Docker container.
+
+```bash
+nvidia-docker run -it -p 8097:8097 taesungp/pytorch-cyclegan-and-pix2pix
+```
+
+- Now you are in the Docker environment. Go to our code repo and start running things.
+```bash
+cd /workspace/pytorch-CycleGAN-and-pix2pix
+bash datasets/download_pix2pix_dataset.sh facades
+python -m visdom.server &
+bash scripts/train_pix2pix.sh
+```
+
+## Running with Dockerfile
+
+We also posted the [Dockerfile](Dockerfile). To generate the pre-built file, download the Dockerfile in this directory and run
+```bash
+docker build -t [target_tag] .
+```
+in the directory that contains the Dockerfile.
diff --git a/cyclegan/docs/overview.md b/cyclegan/docs/overview.md
new file mode 100644
index 0000000..5db2ae9
--- /dev/null
+++ b/cyclegan/docs/overview.md
@@ -0,0 +1,45 @@
+## Overview of Code Structure
+To help users better understand and use our codebase, we briefly overview the functionality and implementation of each package and each module. Please see the documentation in each file for more details. If you have questions, you may find useful information in [training/test tips](tips.md) and [frequently asked questions](qa.md).
+
+[train.py](../train.py) is a general-purpose training script. It works for various models (with option `--model`: e.g., `pix2pix`, `cyclegan`, `colorization`) and different datasets (with option `--dataset_mode`: e.g., `aligned`, `unaligned`, `single`, `colorization`). See the main [README](.../README.md) and [training/test tips](tips.md) for more details.
+
+[test.py](../test.py) is a general-purpose test script. Once you have trained your model with `train.py`, you can use this script to test the model. It will load a saved model from `--checkpoints_dir` and save the results to `--results_dir`. See the main [README](.../README.md) and [training/test tips](tips.md) for more details.
+
+
+[data](../data) directory contains all the modules related to data loading and preprocessing. To add a custom dataset class called `dummy`, you need to add a file called `dummy_dataset.py` and define a subclass `DummyDataset` inherited from `BaseDataset`. You need to implement four functions: `__init__` (initialize the class, you need to first call `BaseDataset.__init__(self, opt)`), `__len__` (return the size of dataset), `__getitem__` (get a data point), and optionally `modify_commandline_options` (add dataset-specific options and set default options). Now you can use the dataset class by specifying flag `--dataset_mode dummy`. See our template dataset [class](../data/template_dataset.py) for an example. Below we explain each file in details.
+
+* [\_\_init\_\_.py](../data/__init__.py) implements the interface between this package and training and test scripts. `train.py` and `test.py` call `from data import create_dataset` and `dataset = create_dataset(opt)` to create a dataset given the option `opt`.
+* [base_dataset.py](../data/base_dataset.py) implements an abstract base class ([ABC](https://docs.python.org/3/library/abc.html)) for datasets. It also includes common transformation functions (e.g., `get_transform`, `__scale_width`), which can be later used in subclasses.
+* [image_folder.py](../data/image_folder.py) implements an image folder class. We modify the official PyTorch image folder [code](https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py) so that this class can load images from both the current directory and its subdirectories.
+* [template_dataset.py](../data/template_dataset.py) provides a dataset template with detailed documentation. Check out this file if you plan to implement your own dataset.
+* [aligned_dataset.py](../data/aligned_dataset.py) includes a dataset class that can load image pairs. It assumes a single image directory `/path/to/data/train`, which contains image pairs in the form of {A,B}. See [here](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md#prepare-your-own-datasets-for-pix2pix) on how to prepare aligned datasets. During test time, you need to prepare a directory `/path/to/data/test` as test data.
+* [unaligned_dataset.py](../data/unaligned_dataset.py) includes a dataset class that can load unaligned/unpaired datasets. It assumes that two directories to host training images from domain A `/path/to/data/trainA` and from domain B `/path/to/data/trainB` respectively. Then you can train the model with the dataset flag `--dataroot /path/to/data`. Similarly, you need to prepare two directories `/path/to/data/testA` and `/path/to/data/testB` during test time.
+* [single_dataset.py](../data/single_dataset.py) includes a dataset class that can load a set of single images specified by the path `--dataroot /path/to/data`. It can be used for generating CycleGAN results only for one side with the model option `-model test`.
+* [colorization_dataset.py](../data/colorization_dataset.py) implements a dataset class that can load a set of nature images in RGB, and convert RGB format into (L, ab) pairs in [Lab](https://en.wikipedia.org/wiki/CIELAB_color_space) color space. It is required by pix2pix-based colorization model (`--model colorization`).
+
+
+[models](../models) directory contains modules related to objective functions, optimizations, and network architectures. To add a custom model class called `dummy`, you need to add a file called `dummy_model.py` and define a subclass `DummyModel` inherited from `BaseModel`. You need to implement four functions: `__init__` (initialize the class; you need to first call `BaseModel.__init__(self, opt)`), `set_input` (unpack data from dataset and apply preprocessing), `forward` (generate intermediate results), `optimize_parameters` (calculate loss, gradients, and update network weights), and optionally `modify_commandline_options` (add model-specific options and set default options). Now you can use the model class by specifying flag `--model dummy`. See our template model [class](../models/template_model.py) for an example. Below we explain each file in details.
+
+* [\_\_init\_\_.py](../models/__init__.py) implements the interface between this package and training and test scripts. `train.py` and `test.py` call `from models import create_model` and `model = create_model(opt)` to create a model given the option `opt`. You also need to call `model.setup(opt)` to properly initialize the model.
+* [base_model.py](../models/base_model.py) implements an abstract base class ([ABC](https://docs.python.org/3/library/abc.html)) for models. It also includes commonly used helper functions (e.g., `setup`, `test`, `update_learning_rate`, `save_networks`, `load_networks`), which can be later used in subclasses.
+* [template_model.py](../models/template_model.py) provides a model template with detailed documentation. Check out this file if you plan to implement your own model.
+* [pix2pix_model.py](../models/pix2pix_model.py) implements the pix2pix [model](https://phillipi.github.io/pix2pix/), for learning a mapping from input images to output images given paired data. The model training requires `--dataset_mode aligned` dataset. By default, it uses a `--netG unet256` [U-Net](https://arxiv.org/pdf/1505.04597.pdf) generator, a `--netD basic` discriminator (PatchGAN), and a `--gan_mode vanilla` GAN loss (standard cross-entropy objective).
+* [colorization_model.py](../models/colorization_model.py) implements a subclass of `Pix2PixModel` for image colorization (black & white image to colorful image). The model training requires `-dataset_model colorization` dataset. It trains a pix2pix model, mapping from L channel to ab channels in [Lab](https://en.wikipedia.org/wiki/CIELAB_color_space) color space. By default, the `colorization` dataset will automatically set `--input_nc 1` and `--output_nc 2`.
+* [cycle_gan_model.py](../models/cycle_gan_model.py) implements the CycleGAN [model](https://junyanz.github.io/CycleGAN/), for learning image-to-image translation without paired data. The model training requires `--dataset_mode unaligned` dataset. By default, it uses a `--netG resnet_9blocks` ResNet generator, a `--netD basic` discriminator (PatchGAN introduced by pix2pix), and a least-square GANs [objective](https://arxiv.org/abs/1611.04076) (`--gan_mode lsgan`).
+* [networks.py](../models/networks.py) module implements network architectures (both generators and discriminators), as well as normalization layers, initialization methods, optimization scheduler (i.e., learning rate policy), and GAN objective function (`vanilla`, `lsgan`, `wgangp`).
+* [test_model.py](../models/test_model.py) implements a model that can be used to generate CycleGAN results for only one direction. This model will automatically set `--dataset_mode single`, which only loads the images from one set. See the test [instruction](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix#apply-a-pre-trained-model-cyclegan) for more details.
+
+[options](../options) directory includes our option modules: training options, test options, and basic options (used in both training and test). `TrainOptions` and `TestOptions` are both subclasses of `BaseOptions`. They will reuse the options defined in `BaseOptions`.
+* [\_\_init\_\_.py](../options/__init__.py) is required to make Python treat the directory `options` as containing packages,
+* [base_options.py](../options/base_options.py) includes options that are used in both training and test. It also implements a few helper functions such as parsing, printing, and saving the options. It also gathers additional options defined in `modify_commandline_options` functions in both dataset class and model class.
+* [train_options.py](../options/train_options.py) includes options that are only used during training time.
+* [test_options.py](../options/test_options.py) includes options that are only used during test time.
+
+
+[util](../util) directory includes a miscellaneous collection of useful helper functions.
+ * [\_\_init\_\_.py](../util/__init__.py) is required to make Python treat the directory `util` as containing packages,
+ * [get_data.py](../util/get_data.py) provides a Python script for downloading CycleGAN and pix2pix datasets. Alternatively, You can also use bash scripts such as [download_pix2pix_model.sh](../scripts/download_pix2pix_model.sh) and [download_cyclegan_model.sh](../scripts/download_cyclegan_model.sh).
+ * [html.py](../util/html.py) implements a module that saves images into a single HTML file. It consists of functions such as `add_header` (add a text header to the HTML file), `add_images` (add a row of images to the HTML file), `save` (save the HTML to the disk). It is based on Python library `dominate`, a Python library for creating and manipulating HTML documents using a DOM API.
+ * [image_pool.py](../util/image_pool.py) implements an image buffer that stores previously generated images. This buffer enables us to update discriminators using a history of generated images rather than the ones produced by the latest generators. The original idea was discussed in this [paper](http://openaccess.thecvf.com/content_cvpr_2017/papers/Shrivastava_Learning_From_Simulated_CVPR_2017_paper.pdf). The size of the buffer is controlled by the flag `--pool_size`.
+ * [visualizer.py](../util/visualizer.py) includes several functions that can display/save images and print/save logging information. It uses a Python library `visdom` for display and a Python library `dominate` (wrapped in `HTML`) for creating HTML files with images.
+ * [util.py](../util/util.py) consists of simple helper functions such as `tensor2im` (convert a tensor array to a numpy image array), `diagnose_network` (calculate and print the mean of average absolute value of gradients), and `mkdirs` (create multiple directories).
diff --git a/cyclegan/docs/qa.md b/cyclegan/docs/qa.md
new file mode 100644
index 0000000..9842318
--- /dev/null
+++ b/cyclegan/docs/qa.md
@@ -0,0 +1,110 @@
+## Frequently Asked Questions
+Before you post a new question, please first look at the following Q & A and existing GitHub issues. You may also want to read [Training/Test tips](docs/tips.md) for more suggestions.
+
+#### Connection Error:HTTPConnectionPool ([#230](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/230), [#24](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/24), [#38](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/38))
+Similar error messages include “Failed to establish a new connection/Connection refused”.
+
+Please start the visdom server before starting the training:
+```bash
+python -m visdom.server
+```
+To install the visdom, you can use the following command:
+```bash
+pip install visdom
+```
+You can also disable the visdom by setting `--display_id 0`.
+
+#### My PyTorch errors on CUDA related code.
+Try to run the following code snippet to make sure that CUDA is working (assuming using PyTorch >= 0.4):
+```python
+import torch
+torch.cuda.init()
+print(torch.randn(1, device='cuda'))
+```
+
+If you met an error, it is likely that your PyTorch build does not work with CUDA, e.g., it is installl from the official MacOS binary, or you have a GPU that is too old and not supported anymore. You may run the the code with CPU using `--gpu_ids -1`.
+
+#### TypeError: Object of type 'Tensor' is not JSON serializable ([#258](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/258))
+Similar errors: AttributeError: module 'torch' has no attribute 'device' ([#314](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/314))
+
+The current code only works with PyTorch 0.4+. An earlier PyTorch version can often cause the above errors.
+
+#### ValueError: empty range for randrange() ([#390](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/390), [#376](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/376), [#194](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/194))
+Similar error messages include "ConnectionRefusedError: [Errno 111] Connection refused"
+
+It is related to data augmentation step. It often happens when you use `--preprocess crop`. The program will crop random `crop_size x crop_size` patches out of the input training images. But if some of your image sizes (e.g., `256x384`) are smaller than the `crop_size` (e.g., 512), you will get this error. A simple fix will be to use other data augmentation methods such as `resize_and_crop` or `scale_width_and_crop`. Our program will automatically resize the images according to `load_size` before apply `crop_size x crop_size` cropping. Make sure that `load_size >= crop_size`.
+
+
+#### Can I continue/resume my training? ([#350](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/350), [#275](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/275), [#234](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/234), [#87](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/87))
+You can use the option `--continue_train`. Also set `--epoch_count` to specify a different starting epoch count. See more discussion in [training/test tips](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md#trainingtest-tips).
+
+#### Why does my training loss not converge? ([#335](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/335), [#164](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/164), [#30](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/30))
+Many GAN losses do not converge (exception: WGAN, WGAN-GP, etc. ) due to the nature of minimax optimization. For DCGAN and LSGAN objective, it is quite normal for the G and D losses to go up and down. It should be fine as long as they do not blow up.
+
+#### How can I make it work for my own data (e.g., 16-bit png, tiff, hyperspectral images)? ([#309](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/309), [#320](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/), [#202](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/202))
+The current code only supports RGB and grayscale images. If you would like to train the model on other data types, please follow the following steps:
+
+- change the parameters `--input_nc` and `--output_nc` to the number of channels in your input/output images.
+- Write your own custom data loader (It is easy as long as you know how to load your data with python). If you write a new data loader class, you need to change the flag `--dataset_mode` accordingly. Alternatively, you can modify the existing data loader. For aligned datasets, change this [line](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/data/aligned_dataset.py#L41); For unaligned datasets, change these two [lines](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/data/unaligned_dataset.py#L57).
+
+- If you use visdom and HTML to visualize the results, you may also need to change the visualization code.
+
+#### Multi-GPU Training ([#327](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/327), [#292](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/292), [#137](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/137), [#35](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/35))
+You can use Multi-GPU training by setting `--gpu_ids` (e.g., `--gpu_ids 0,1,2,3` for the first four GPUs on your machine.) To fully utilize all the GPUs, you need to increase your batch size. Try `--batch_size 4`, `--batch_size 16`, or even a larger batch_size. Each GPU will process batch_size/#GPUs images. The optimal batch size depends on the number of GPUs you have, GPU memory per GPU, and the resolution of your training images.
+
+We also recommend that you use the instance normalization for multi-GPU training by setting `--norm instance`. The current batch normalization might not work for multi-GPUs as the batchnorm parameters are not shared across different GPUs. Advanced users can try [synchronized batchnorm](https://github.com/vacancy/Synchronized-BatchNorm-PyTorch).
+
+
+#### Can I run the model on CPU? ([#310](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/310))
+Yes, you can set `--gpu_ids -1`. See [training/test tips](tips.md) for more details.
+
+
+#### Are pre-trained models available? ([#10](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/10))
+Yes, you can download pretrained models with the bash script `./scripts/download_cyclegan_model.sh`. See [here](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix#apply-a-pre-trained-model-cyclegan) for more details. We are slowly adding more models to the repo.
+
+#### Out of memory ([#174](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/174))
+CycleGAN is more memory-intensive than pix2pix as it requires two generators and two discriminators. If you would like to produce high-resolution images, you can do the following.
+
+- During training, train CycleGAN on cropped images of the training set. Please be careful not to change the aspect ratio or the scale of the original image, as this can lead to the training/test gap. You can usually do this by using `--preprocess crop` option, or `--preprocess scale_width_and_crop`.
+
+- Then at test time, you can load only one generator to produce the results in a single direction. This greatly saves GPU memory as you are not loading the discriminators and the other generator in the opposite direction. You can probably take the whole image as input. You can do this using `--model test --dataroot [path to the directory that contains your test images (e.g., ./datasets/horse2zebra/trainA)] --model_suffix _A --preprocess none`. You can use either `--preprocess none` or `--preprocess scale_width --crop_size [your_desired_image_width]`. Please see the [model_suffix](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/models/test_model.py#L16) and [preprocess](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/data/base_dataset.py#L24) for more details.
+
+#### RuntimeError: Error(s) in loading state_dict ([#812](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/812), [#671](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/671),[#461](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/461), [#296](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/296))
+If you get the above errors when loading the generator during test time, you probably have used different network configurations for training and test. There are a few things to check: (1) the network architecture `--netG`: you will get an error if you use `--netG unet256` during training, and use `--netG resnet_6blocks` during test. Make sure that the flag is the same. (2) the normalization parameters `--norm`: we use different default `--norm` parameters for `--model cycle_gan`, `--model pix2pix`, and `--model test`. They might be different from the one you used in your training time. Make sure that you add the `--norm` flag in your test code. (3) If you use dropout during training time, make sure that you use the same Dropout setting in your test. Check the flag `--no_dropout`.
+
+#### What is the identity loss? ([#322](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/322), [#373](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/373), [#362](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/pull/362))
+We use the identity loss for our photo to painting application. The identity loss can regularize the generator to be close to an identity mapping when fed with real samples from the *target* domain. If something already looks like from the target domain, you should preserve the image without making additional changes. The generator trained with this loss will often be more conservative for unknown content. Please see more details in Sec 5.2 ''Photo generation from paintings'' and Figure 12 in the CycleGAN [paper](https://arxiv.org/pdf/1703.10593.pdf). The loss was first proposed in the Equation 6 of the prior work [[Taigman et al., 2017]](https://arxiv.org/pdf/1611.02200.pdf).
+
+#### The color gets inverted from the beginning of training ([#249](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/249))
+The authors also observe that the generator unnecessarily inverts the color of the input image early in training, and then never learns to undo the inversion. In this case, you can try two things.
+
+- First, try using identity loss `--lambda_identity 1.0` or `--lambda_identity 0.1`. We observe that the identity loss makes the generator to be more conservative and make fewer unnecessary changes. However, because of this, the change may not be as dramatic.
+
+- Second, try smaller variance when initializing weights by changing `--init_gain`. We observe that smaller variance in weight initialization results in less color inversion.
+
+#### For labels2photo Cityscapes evaluation, why does the pretrained FCN-8s model not work well on the original Cityscapes input images? ([#150](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/150))
+The model was trained on 256x256 images that are resized/upsampled to 1024x2048, so expected input images to the network are very blurry. The purpose of the resizing was to 1) keep the label maps in the original high resolution untouched and 2) avoid the need of changing the standard FCN training code for Cityscapes.
+
+#### How do I get the `ground-truth` numbers on the labels2photo Cityscapes evaluation? ([#150](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/150))
+You need to resize the original Cityscapes images to 256x256 before running the evaluation code.
+
+
+#### Using resize-conv to reduce checkerboard artifacts ([#190](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/190), [#64](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/64))
+This Distill [blog](https://distill.pub/2016/deconv-checkerboard/) discussed one of the potential causes of the checkerboard artifacts. You can fix that issue by switching from "deconvolution" to nearest-neighbor upsampling followed by regular convolution. Here is one implementation provided by [@SsnL](https://github.com/SsnL). You can replace the ConvTranspose2d with the following layers.
+```python
+nn.Upsample(scale_factor = 2, mode='bilinear'),
+nn.ReflectionPad2d(1),
+nn.Conv2d(ngf * mult, int(ngf * mult / 2), kernel_size=3, stride=1, padding=0),
+```
+We have also noticed that sometimes the checkboard artifacts will go away if you train long enough. Maybe you can try training your model a bit longer.
+
+#### pix2pix/CycleGAN has no random noise z ([#152](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/152))
+The current pix2pix/CycleGAN model does not take z as input. In both pix2pix and CycleGAN, we tried to add z to the generator: e.g., adding z to a latent state, concatenating with a latent state, applying dropout, etc., but often found the output did not vary significantly as a function of z. Conditional GANs do not need noise as long as the input is sufficiently complex so that the input can kind of play the role of noise. Without noise, the mapping is deterministic.
+
+Please check out the following papers that show ways of getting z to actually have a substantial effect: e.g., [BicycleGAN](https://github.com/junyanz/BicycleGAN), [AugmentedCycleGAN](https://arxiv.org/abs/1802.10151), [MUNIT](https://arxiv.org/abs/1804.04732), [DRIT](https://arxiv.org/pdf/1808.00948.pdf), etc.
+
+#### Experiment details (e.g., BW->color) ([#306](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/issues/306))
+You can find more training details and hyperparameter settings in the appendix of [CycleGAN](https://arxiv.org/abs/1703.10593) and [pix2pix](https://arxiv.org/abs/1611.07004) papers.
+
+#### Results with [Cycada](https://arxiv.org/pdf/1711.03213.pdf)
+We generated the [result of translating GTA images to Cityscapes-style images](https://junyanz.github.io/CycleGAN/) using our Torch repo. Our PyTorch and Torch implementation seemed to produce a little bit different results, although we have not measured the FCN score using the pytorch-trained model. To reproduce the result of Cycada, please use the Torch repo for now.
diff --git a/cyclegan/docs/tips.md b/cyclegan/docs/tips.md
new file mode 100644
index 0000000..189fe2a
--- /dev/null
+++ b/cyclegan/docs/tips.md
@@ -0,0 +1,74 @@
+## Training/test Tips
+#### Training/test options
+Please see `options/train_options.py` and `options/base_options.py` for the training flags; see `options/test_options.py` and `options/base_options.py` for the test flags. There are some model-specific flags as well, which are added in the model files, such as `--lambda_A` option in `model/cycle_gan_model.py`. The default values of these options are also adjusted in the model files.
+#### CPU/GPU (default `--gpu_ids 0`)
+Please set`--gpu_ids -1` to use CPU mode; set `--gpu_ids 0,1,2` for multi-GPU mode. You need a large batch size (e.g., `--batch_size 32`) to benefit from multiple GPUs.
+
+#### Visualization
+During training, the current results can be viewed using two methods. First, if you set `--display_id` > 0, the results and loss plot will appear on a local graphics web server launched by [visdom](https://github.com/facebookresearch/visdom). To do this, you should have `visdom` installed and a server running by the command `python -m visdom.server`. The default server URL is `http://localhost:8097`. `display_id` corresponds to the window ID that is displayed on the `visdom` server. The `visdom` display functionality is turned on by default. To avoid the extra overhead of communicating with `visdom` set `--display_id -1`. Second, the intermediate results are saved to `[opt.checkpoints_dir]/[opt.name]/web/` as an HTML file. To avoid this, set `--no_html`.
+
+#### Preprocessing
+ Images can be resized and cropped in different ways using `--preprocess` option. The default option `'resize_and_crop'` resizes the image to be of size `(opt.load_size, opt.load_size)` and does a random crop of size `(opt.crop_size, opt.crop_size)`. `'crop'` skips the resizing step and only performs random cropping. `'scale_width'` resizes the image to have width `opt.crop_size` while keeping the aspect ratio. `'scale_width_and_crop'` first resizes the image to have width `opt.load_size` and then does random cropping of size `(opt.crop_size, opt.crop_size)`. `'none'` tries to skip all these preprocessing steps. However, if the image size is not a multiple of some number depending on the number of downsamplings of the generator, you will get an error because the size of the output image may be different from the size of the input image. Therefore, `'none'` option still tries to adjust the image size to be a multiple of 4. You might need a bigger adjustment if you change the generator architecture. Please see `data/base_datset.py` do see how all these were implemented.
+
+#### Fine-tuning/resume training
+To fine-tune a pre-trained model, or resume the previous training, use the `--continue_train` flag. The program will then load the model based on `epoch`. By default, the program will initialize the epoch count as 1. Set `--epoch_count ` to specify a different starting epoch count.
+
+
+#### Prepare your own datasets for CycleGAN
+You need to create two directories to host images from domain A `/path/to/data/trainA` and from domain B `/path/to/data/trainB`. Then you can train the model with the dataset flag `--dataroot /path/to/data`. Optionally, you can create hold-out test datasets at `/path/to/data/testA` and `/path/to/data/testB` to test your model on unseen images.
+
+#### Prepare your own datasets for pix2pix
+Pix2pix's training requires paired data. We provide a python script to generate training data in the form of pairs of images {A,B}, where A and B are two different depictions of the same underlying scene. For example, these might be pairs {label map, photo} or {bw image, color image}. Then we can learn to translate A to B or B to A:
+
+Create folder `/path/to/data` with subdirectories `A` and `B`. `A` and `B` should each have their own subdirectories `train`, `val`, `test`, etc. In `/path/to/data/A/train`, put training images in style A. In `/path/to/data/B/train`, put the corresponding images in style B. Repeat same for other data splits (`val`, `test`, etc).
+
+Corresponding images in a pair {A,B} must be the same size and have the same filename, e.g., `/path/to/data/A/train/1.jpg` is considered to correspond to `/path/to/data/B/train/1.jpg`.
+
+Once the data is formatted this way, call:
+```bash
+python datasets/combine_A_and_B.py --fold_A /path/to/data/A --fold_B /path/to/data/B --fold_AB /path/to/data
+```
+
+This will combine each pair of images (A,B) into a single image file, ready for training.
+
+
+#### About image size
+ Since the generator architecture in CycleGAN involves a series of downsampling / upsampling operations, the size of the input and output image may not match if the input image size is not a multiple of 4. As a result, you may get a runtime error because the L1 identity loss cannot be enforced with images of different size. Therefore, we slightly resize the image to become multiples of 4 even with `--preprocess none` option. For the same reason, `--crop_size` needs to be a multiple of 4.
+
+#### Training/Testing with high res images
+CycleGAN is quite memory-intensive as four networks (two generators and two discriminators) need to be loaded on one GPU, so a large image cannot be entirely loaded. In this case, we recommend training with cropped images. For example, to generate 1024px results, you can train with `--preprocess scale_width_and_crop --load_size 1024 --crop_size 360`, and test with `--preprocess scale_width --load_size 1024`. This way makes sure the training and test will be at the same scale. At test time, you can afford higher resolution because you don’t need to load all networks.
+
+#### Training/Testing with rectangular images
+Both pix2pix and CycleGAN can work for rectangular images. To make them work, you need to use different preprocessing flags. Let's say that you are working with `360x256` images. During training, you can specify `--preprocess crop` and `--crop_size 256`. This will allow your model to be trained on randomly cropped `256x256` images during training time. During test time, you can apply the model on `360x256` images with the flag `--preprocess none`.
+
+There are practical restrictions regarding image sizes for each generator architecture. For `unet256`, it only supports images whose width and height are divisible by 256. For `unet128`, the width and height need to be divisible by 128. For `resnet_6blocks` and `resnet_9blocks`, the width and height need to be divisible by 4.
+
+#### About loss curve
+Unfortunately, the loss curve does not reveal much information in training GANs, and CycleGAN is no exception. To check whether the training has converged or not, we recommend periodically generating a few samples and looking at them.
+
+#### About batch size
+For all experiments in the paper, we set the batch size to be 1. If there is room for memory, you can use higher batch size with batch norm or instance norm. (Note that the default batchnorm does not work well with multi-GPU training. You may consider using [synchronized batchnorm](https://github.com/vacancy/Synchronized-BatchNorm-PyTorch) instead). But please be aware that it can impact the training. In particular, even with Instance Normalization, different batch sizes can lead to different results. Moreover, increasing `--crop_size` may be a good alternative to increasing the batch size.
+
+
+#### Notes on Colorization
+No need to run `combine_A_and_B.py` for colorization. Instead, you need to prepare natural images and set `--dataset_mode colorization` and `--model colorization` in the script. The program will automatically convert each RGB image into Lab color space, and create `L -> ab` image pair during the training. Also set `--input_nc 1` and `--output_nc 2`. The training and test directory should be organized as `/your/data/train` and `your/data/test`. See example scripts `scripts/train_colorization.sh` and `scripts/test_colorization` for more details.
+
+#### Notes on Extracting Edges
+We provide python and Matlab scripts to extract coarse edges from photos. Run `scripts/edges/batch_hed.py` to compute [HED](https://github.com/s9xie/hed) edges. Run `scripts/edges/PostprocessHED.m` to simplify edges with additional post-processing steps. Check the code documentation for more details.
+
+#### Evaluating Labels2Photos on Cityscapes
+We provide scripts for running the evaluation of the Labels2Photos task on the Cityscapes **validation** set. We assume that you have installed `caffe` (and `pycaffe`) in your system. If not, see the [official website](http://caffe.berkeleyvision.org/installation.html) for installation instructions. Once `caffe` is successfully installed, download the pre-trained FCN-8s semantic segmentation model (512MB) by running
+```bash
+bash ./scripts/eval_cityscapes/download_fcn8s.sh
+```
+Then make sure `./scripts/eval_cityscapes/` is in your system's python path. If not, run the following command to add it
+```bash
+export PYTHONPATH=${PYTHONPATH}:./scripts/eval_cityscapes/
+```
+Now you can run the following command to evaluate your predictions:
+```bash
+python ./scripts/eval_cityscapes/evaluate.py --cityscapes_dir /path/to/original/cityscapes/dataset/ --result_dir /path/to/your/predictions/ --output_dir /path/to/output/directory/
+```
+Images stored under `--result_dir` should contain your model predictions on the Cityscapes **validation** split, and have the original Cityscapes naming convention (e.g., `frankfurt_000001_038418_leftImg8bit.png`). The script will output a text file under `--output_dir` containing the metric.
+
+**Further notes**: The pre-trained model is **not** supposed to work on Cityscapes in the original resolution (1024x2048) as it was trained on 256x256 images that are upsampled to 1024x2048. The purpose of the resizing was to 1) keep the label maps in the original high resolution untouched and 2) avoid the need of changing the standard FCN training code for Cityscapes. To get the *ground-truth* numbers in the paper, you need to resize the original Cityscapes images to 256x256 before running the evaluation code.
diff --git a/cyclegan/environment.yml b/cyclegan/environment.yml
new file mode 100644
index 0000000..12ed68c
--- /dev/null
+++ b/cyclegan/environment.yml
@@ -0,0 +1,14 @@
+name: pytorch-CycleGAN-and-pix2pix
+channels:
+- peterjc123
+- defaults
+dependencies:
+- python=3.5.5
+- pytorch=0.4.1
+- scipy
+- pip:
+ - dominate==2.3.1
+ - git+https://github.com/pytorch/vision.git
+ - Pillow==5.0.0
+ - numpy==1.14.1
+ - visdom==0.1.7
diff --git a/cyclegan/examples/test_vangogh_style_on_ae_photos.sh b/cyclegan/examples/test_vangogh_style_on_ae_photos.sh
deleted file mode 100644
index 60ce1bb..0000000
--- a/cyclegan/examples/test_vangogh_style_on_ae_photos.sh
+++ /dev/null
@@ -1,19 +0,0 @@
-#!/bin/sh
-
-## This script download the dataset and pre-trained network,
-## and generates style transferred images.
-
-# Download the dataset. The downloaded dataset is stored in ./datasets/${DATASET_NAME}
-DATASET_NAME='ae_photos'
-bash ./datasets/download_dataset.sh $DATASET_NAME
-
-# Download the pre-trained model. The downloaded model is stored in ./models/${MODEL_NAME}_pretrained/latest_net_G.t7
-MODEL_NAME='style_vangogh'
-bash ./pretrained_models/download_model.sh $MODEL_NAME
-
-# Run style transfer using the downloaded dataset and model
-DATA_ROOT=./datasets/$DATASET_NAME name=${MODEL_NAME}_pretrained model=one_direction_test phase=test how_many='all' loadSize=256 fineSize=256 resize_or_crop='scale_width' th test.lua
-
-if [ $? == 0 ]; then
- echo "The result can be viewed at ./results/${MODEL_NAME}_pretrained/latest_test/index.html"
-fi
diff --git a/cyclegan/examples/train_maps.sh b/cyclegan/examples/train_maps.sh
deleted file mode 100644
index 7c266ac..0000000
--- a/cyclegan/examples/train_maps.sh
+++ /dev/null
@@ -1,26 +0,0 @@
-DB_NAME='maps'
-GPU_ID=1
-DISPLAY_ID=1
-NET_G=resnet_6blocks
-NET_D=basic
-MODEL=cycle_gan
-SAVE_EPOCH=5
-ALIGN_DATA=0
-LAMBDA=10
-NF=64
-
-
-EXPR_NAME=${DB_NAME}_${MODEL}_${LAMBDA}
-
-CHECKPOINT_DIR=./checkpoints/
-LOG_FILE=${CHECKPOINT_DIR}${EXPR_NAME}/log.txt
-mkdir -p ${CHECKPOINT_DIR}${EXPR_NAME}
-
-DATA_ROOT=./datasets/$DB_NAME align_data=$ALIGN_DATA use_lsgan=1 \
-which_direction='AtoB' display_plot=$PLOT pool_size=50 niter=100 niter_decay=100 \
-which_model_netG=$NET_G which_model_netD=$NET_D model=$MODEL lr=0.0002 print_freq=200 lambda_A=$LAMBDA lambda_B=$LAMBDA \
-loadSize=143 fineSize=128 gpu=$GPU_ID display_winsize=128 \
-name=$EXPR_NAME flip=1 save_epoch_freq=$SAVE_EPOCH \
-continue_train=0 display_id=$DISPLAY_ID \
-checkpoints_dir=$CHECKPOINT_DIR\
- th train.lua | tee -a $LOG_FILE
diff --git a/cyclegan/models/__init__.py b/cyclegan/models/__init__.py
new file mode 100644
index 0000000..fc01113
--- /dev/null
+++ b/cyclegan/models/__init__.py
@@ -0,0 +1,67 @@
+"""This package contains modules related to objective functions, optimizations, and network architectures.
+
+To add a custom model class called 'dummy', you need to add a file called 'dummy_model.py' and define a subclass DummyModel inherited from BaseModel.
+You need to implement the following five functions:
+ -- <__init__>: initialize the class; first call BaseModel.__init__(self, opt).
+ -- : unpack data from dataset and apply preprocessing.
+ -- : produce intermediate results.
+ -- : calculate loss, gradients, and update network weights.
+ -- : (optionally) add model-specific options and set default options.
+
+In the function <__init__>, you need to define four lists:
+ -- self.loss_names (str list): specify the training losses that you want to plot and save.
+ -- self.model_names (str list): define networks used in our training.
+ -- self.visual_names (str list): specify the images that you want to display and save.
+ -- self.optimizers (optimizer list): define and initialize optimizers. You can define one optimizer for each network. If two networks are updated at the same time, you can use itertools.chain to group them. See cycle_gan_model.py for an usage.
+
+Now you can use the model class by specifying flag '--model dummy'.
+See our template model class 'template_model.py' for more details.
+"""
+
+import importlib
+from models.base_model import BaseModel
+
+
+def find_model_using_name(model_name):
+ """Import the module "models/[model_name]_model.py".
+
+ In the file, the class called DatasetNameModel() will
+ be instantiated. It has to be a subclass of BaseModel,
+ and it is case-insensitive.
+ """
+ model_filename = "models." + model_name + "_model"
+ modellib = importlib.import_module(model_filename)
+ model = None
+ target_model_name = model_name.replace('_', '') + 'model'
+ for name, cls in modellib.__dict__.items():
+ if name.lower() == target_model_name.lower() \
+ and issubclass(cls, BaseModel):
+ model = cls
+
+ if model is None:
+ print("In %s.py, there should be a subclass of BaseModel with class name that matches %s in lowercase." % (model_filename, target_model_name))
+ exit(0)
+
+ return model
+
+
+def get_option_setter(model_name):
+ """Return the static method of the model class."""
+ model_class = find_model_using_name(model_name)
+ return model_class.modify_commandline_options
+
+
+def create_model(opt):
+ """Create a model given the option.
+
+ This function warps the class CustomDatasetDataLoader.
+ This is the main interface between this package and 'train.py'/'test.py'
+
+ Example:
+ >>> from models import create_model
+ >>> model = create_model(opt)
+ """
+ model = find_model_using_name(opt.model)
+ instance = model(opt)
+ print("model [%s] was created" % type(instance).__name__)
+ return instance
diff --git a/cyclegan/models/architectures.lua b/cyclegan/models/architectures.lua
deleted file mode 100644
index 8b83cea..0000000
--- a/cyclegan/models/architectures.lua
+++ /dev/null
@@ -1,384 +0,0 @@
-require 'nngraph'
-
-
-----------------------------------------------------------------------------
-local function weights_init(m)
- local name = torch.type(m)
- if name:find('Convolution') then
- m.weight:normal(0.0, 0.02)
- m.bias:fill(0)
- elseif name:find('Normalization') then
- if m.weight then m.weight:normal(1.0, 0.02) end
- if m.bias then m.bias:fill(0) end
- end
-end
-
-
-normalization = nil
-
-function set_normalization(norm)
-if norm == 'instance' then
- require 'util.InstanceNormalization'
- print('use InstanceNormalization')
- normalization = nn.InstanceNormalization
-elseif norm == 'batch' then
- print('use SpatialBatchNormalization')
- normalization = nn.SpatialBatchNormalization
-end
-end
-
-function defineG(input_nc, output_nc, ngf, which_model_netG, nz, arch)
- local netG = nil
- if which_model_netG == "encoder_decoder" then netG = defineG_encoder_decoder(input_nc, output_nc, ngf)
- elseif which_model_netG == "unet128" then netG = defineG_unet128(input_nc, output_nc, ngf)
- elseif which_model_netG == "unet256" then netG = defineG_unet256(input_nc, output_nc, ngf)
- elseif which_model_netG == "resnet_6blocks" then netG = defineG_resnet_6blocks(input_nc, output_nc, ngf)
- elseif which_model_netG == "resnet_9blocks" then netG = defineG_resnet_9blocks(input_nc, output_nc, ngf)
- else error("unsupported netG model")
- end
- netG:apply(weights_init)
-
- return netG
-end
-
-function defineD(input_nc, ndf, which_model_netD, n_layers_D, use_sigmoid)
- local netD = nil
- if which_model_netD == "basic" then netD = defineD_basic(input_nc, ndf, use_sigmoid)
- elseif which_model_netD == "imageGAN" then netD = defineD_imageGAN(input_nc, ndf, use_sigmoid)
- elseif which_model_netD == "n_layers" then netD = defineD_n_layers(input_nc, ndf, n_layers_D, use_sigmoid)
- else error("unsupported netD model")
- end
- netD:apply(weights_init)
-
- return netD
-end
-
-function defineG_encoder_decoder(input_nc, output_nc, ngf)
- -- input is (nc) x 256 x 256
- local e1 = - nn.SpatialConvolution(input_nc, ngf, 4, 4, 2, 2, 1, 1)
- -- input is (ngf) x 128 x 128
- local e2 = e1 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf, ngf * 2, 4, 4, 2, 2, 1, 1) - normalization(ngf * 2)
- -- input is (ngf * 2) x 64 x 64
- local e3 = e2 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 2, ngf * 4, 4, 4, 2, 2, 1, 1) - normalization(ngf * 4)
- -- input is (ngf * 4) x 32 x 32
- local e4 = e3 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 4, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 16 x 16
- local e5 = e4 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 8 x 8
- local e6 = e5 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 4 x 4
- local e7 = e6 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 2 x 2
- local e8 = e7 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) -- normalization(ngf * 8)
- -- input is (ngf * 8) x 1 x 1
-
- local d1 = e8 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8) - nn.Dropout(0.5)
- -- input is (ngf * 8) x 2 x 2
- local d2 = d1 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8) - nn.Dropout(0.5)
- -- input is (ngf * 8) x 4 x 4
- local d3 = d2 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8) - nn.Dropout(0.5)
- -- input is (ngf * 8) x 8 x 8
- local d4 = d3 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 16 x 16
- local d5 = d4 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8, ngf * 4, 4, 4, 2, 2, 1, 1) - normalization(ngf * 4)
- -- input is (ngf * 4) x 32 x 32
- local d6 = d5 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 4, ngf * 2, 4, 4, 2, 2, 1, 1) - normalization(ngf * 2)
- -- input is (ngf * 2) x 64 x 64
- local d7 = d6 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 2, ngf, 4, 4, 2, 2, 1, 1) - normalization(ngf)
- -- input is (ngf) x128 x 128
- local d8 = d7 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf, output_nc, 4, 4, 2, 2, 1, 1)
- -- input is (nc) x 256 x 256
- local o1 = d8 - nn.Tanh()
-
- local netG = nn.gModule({e1},{o1})
- return netG
-end
-
-
-function defineG_unet128(input_nc, output_nc, ngf)
- local netG = nil
- -- input is (nc) x 128 x 128
- local e1 = - nn.SpatialConvolution(input_nc, ngf, 4, 4, 2, 2, 1, 1)
- -- input is (ngf) x 64 x 64
- local e2 = e1 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf, ngf * 2, 4, 4, 2, 2, 1, 1) - normalization(ngf * 2)
- -- input is (ngf * 2) x 32 x 32
- local e3 = e2 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 2, ngf * 4, 4, 4, 2, 2, 1, 1) - normalization(ngf * 4)
- -- input is (ngf * 4) x 16 x 16
- local e4 = e3 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 4, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 8 x 8
- local e5 = e4 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 4 x 4
- local e6 = e5 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 2 x 2
- local e7 = e6 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) -- normalization(ngf * 8)
- -- input is (ngf * 8) x 1 x 1
-
- local d1_ = e7 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8) - nn.Dropout(0.5)
- -- input is (ngf * 8) x 2 x 2
- local d1 = {d1_,e6} - nn.JoinTable(2)
- local d2_ = d1 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8 * 2, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8) - nn.Dropout(0.5)
- -- input is (ngf * 8) x 4 x 4
- local d2 = {d2_,e5} - nn.JoinTable(2)
- local d3_ = d2 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8 * 2, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8) - nn.Dropout(0.5)
- -- input is (ngf * 8) x 8 x 8
- local d3 = {d3_,e4} - nn.JoinTable(2)
- local d4_ = d3 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8 * 2, ngf * 4, 4, 4, 2, 2, 1, 1) - normalization(ngf * 4)
- -- input is (ngf * 8) x 16 x 16
- local d4 = {d4_,e3} - nn.JoinTable(2)
- local d5_ = d4 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 4 * 2, ngf * 2, 4, 4, 2, 2, 1, 1) - normalization(ngf * 2)
- -- input is (ngf * 4) x 32 x 32
- local d5 = {d5_,e2} - nn.JoinTable(2)
- local d6_ = d5 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 2 * 2, ngf, 4, 4, 2, 2, 1, 1) - normalization(ngf)
- -- input is (ngf * 2) x 64 x 64
- local d6 = {d6_,e1} - nn.JoinTable(2)
-
- local d7 = d6 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 2, output_nc, 4, 4, 2, 2, 1, 1)
- -- input is (nc) x 128 x 128
-
- local o1 = d7 - nn.Tanh()
- local netG = nn.gModule({e1},{o1})
- return netG
-end
-
-
-function defineG_unet256(input_nc, output_nc, ngf)
- local netG = nil
- -- input is (nc) x 256 x 256
- local e1 = - nn.SpatialConvolution(input_nc, ngf, 4, 4, 2, 2, 1, 1)
- -- input is (ngf) x 128 x 128
- local e2 = e1 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf, ngf * 2, 4, 4, 2, 2, 1, 1) - normalization(ngf * 2)
- -- input is (ngf * 2) x 64 x 64
- local e3 = e2 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 2, ngf * 4, 4, 4, 2, 2, 1, 1) - normalization(ngf * 4)
- -- input is (ngf * 4) x 32 x 32
- local e4 = e3 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 4, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 16 x 16
- local e5 = e4 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 8 x 8
- local e6 = e5 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 4 x 4
- local e7 = e6 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 2 x 2
- local e8 = e7 - nn.LeakyReLU(0.2, true) - nn.SpatialConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) -- - normalization(ngf * 8)
- -- input is (ngf * 8) x 1 x 1
-
- local d1_ = e8 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8) - nn.Dropout(0.5)
- -- input is (ngf * 8) x 2 x 2
- local d1 = {d1_,e7} - nn.JoinTable(2)
- local d2_ = d1 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8 * 2, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8) - nn.Dropout(0.5)
- -- input is (ngf * 8) x 4 x 4
- local d2 = {d2_,e6} - nn.JoinTable(2)
- local d3_ = d2 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8 * 2, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8) - nn.Dropout(0.5)
- -- input is (ngf * 8) x 8 x 8
- local d3 = {d3_,e5} - nn.JoinTable(2)
- local d4_ = d3 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8 * 2, ngf * 8, 4, 4, 2, 2, 1, 1) - normalization(ngf * 8)
- -- input is (ngf * 8) x 16 x 16
- local d4 = {d4_,e4} - nn.JoinTable(2)
- local d5_ = d4 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 8 * 2, ngf * 4, 4, 4, 2, 2, 1, 1) - normalization(ngf * 4)
- -- input is (ngf * 4) x 32 x 32
- local d5 = {d5_,e3} - nn.JoinTable(2)
- local d6_ = d5 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 4 * 2, ngf * 2, 4, 4, 2, 2, 1, 1) - normalization(ngf * 2)
- -- input is (ngf * 2) x 64 x 64
- local d6 = {d6_,e2} - nn.JoinTable(2)
- local d7_ = d6 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 2 * 2, ngf, 4, 4, 2, 2, 1, 1) - normalization(ngf)
- -- input is (ngf) x128 x 128
- local d7 = {d7_,e1} - nn.JoinTable(2)
- local d8 = d7 - nn.ReLU(true) - nn.SpatialFullConvolution(ngf * 2, output_nc, 4, 4, 2, 2, 1, 1)
- -- input is (nc) x 256 x 256
-
- local o1 = d8 - nn.Tanh()
- local netG = nn.gModule({e1},{o1})
- return netG
-end
-
---------------------------------------------------------------------------------
--- Justin Johnson's model from https://github.com/jcjohnson/fast-neural-style/
---------------------------------------------------------------------------------
-
-local function build_conv_block(dim, padding_type)
- local conv_block = nn.Sequential()
- local p = 0
- if padding_type == 'reflect' then
- conv_block:add(nn.SpatialReflectionPadding(1, 1, 1, 1))
- elseif padding_type == 'replicate' then
- conv_block:add(nn.SpatialReplicationPadding(1, 1, 1, 1))
- elseif padding_type == 'zero' then
- p = 1
- end
- conv_block:add(nn.SpatialConvolution(dim, dim, 3, 3, 1, 1, p, p))
- conv_block:add(normalization(dim))
- conv_block:add(nn.ReLU(true))
- if padding_type == 'reflect' then
- conv_block:add(nn.SpatialReflectionPadding(1, 1, 1, 1))
- elseif padding_type == 'replicate' then
- conv_block:add(nn.SpatialReplicationPadding(1, 1, 1, 1))
- end
- conv_block:add(nn.SpatialConvolution(dim, dim, 3, 3, 1, 1, p, p))
- conv_block:add(normalization(dim))
- return conv_block
-end
-
-
-local function build_res_block(dim, padding_type)
- local conv_block = build_conv_block(dim, padding_type)
- local res_block = nn.Sequential()
- local concat = nn.ConcatTable()
- concat:add(conv_block)
- concat:add(nn.Identity())
-
- res_block:add(concat):add(nn.CAddTable())
- return res_block
-end
-
-function defineG_resnet_6blocks(input_nc, output_nc, ngf)
- padding_type = 'reflect'
- local ks = 3
- local netG = nil
- local f = 7
- local p = (f - 1) / 2
- local data = -nn.Identity()
- local e1 = data - nn.SpatialReflectionPadding(p, p, p, p) - nn.SpatialConvolution(input_nc, ngf, f, f, 1, 1) - normalization(ngf) - nn.ReLU(true)
- local e2 = e1 - nn.SpatialConvolution(ngf, ngf*2, ks, ks, 2, 2, 1, 1) - normalization(ngf*2) - nn.ReLU(true)
- local e3 = e2 - nn.SpatialConvolution(ngf*2, ngf*4, ks, ks, 2, 2, 1, 1) - normalization(ngf*4) - nn.ReLU(true)
- local d1 = e3 - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type)
- - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type)
- local d2 = d1 - nn.SpatialFullConvolution(ngf*4, ngf*2, ks, ks, 2, 2, 1, 1,1,1) - normalization(ngf*2) - nn.ReLU(true)
- local d3 = d2 - nn.SpatialFullConvolution(ngf*2, ngf, ks, ks, 2, 2, 1, 1,1,1) - normalization(ngf) - nn.ReLU(true)
- local d4 = d3 - nn.SpatialReflectionPadding(p, p, p, p) - nn.SpatialConvolution(ngf, output_nc, f, f, 1, 1) - nn.Tanh()
- netG = nn.gModule({data},{d4})
- return netG
-end
-
-function defineG_resnet_9blocks(input_nc, output_nc, ngf)
- padding_type = 'reflect'
- local ks = 3
- local netG = nil
- local f = 7
- local p = (f - 1) / 2
- local data = -nn.Identity()
- local e1 = data - nn.SpatialReflectionPadding(p, p, p, p) - nn.SpatialConvolution(input_nc, ngf, f, f, 1, 1) - normalization(ngf) - nn.ReLU(true)
- local e2 = e1 - nn.SpatialConvolution(ngf, ngf*2, ks, ks, 2, 2, 1, 1) - normalization(ngf*2) - nn.ReLU(true)
- local e3 = e2 - nn.SpatialConvolution(ngf*2, ngf*4, ks, ks, 2, 2, 1, 1) - normalization(ngf*4) - nn.ReLU(true)
- local d1 = e3 - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type)
- - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type)
- - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type) - build_res_block(ngf*4, padding_type)
- local d2 = d1 - nn.SpatialFullConvolution(ngf*4, ngf*2, ks, ks, 2, 2, 1, 1,1,1) - normalization(ngf*2) - nn.ReLU(true)
- local d3 = d2 - nn.SpatialFullConvolution(ngf*2, ngf, ks, ks, 2, 2, 1, 1,1,1) - normalization(ngf) - nn.ReLU(true)
- local d4 = d3 - nn.SpatialReflectionPadding(p, p, p, p) - nn.SpatialConvolution(ngf, output_nc, f, f, 1, 1) - nn.Tanh()
- netG = nn.gModule({data},{d4})
- return netG
-end
-
-function defineD_imageGAN(input_nc, ndf, use_sigmoid)
- local netD = nn.Sequential()
-
- -- input is (nc) x 256 x 256
- netD:add(nn.SpatialConvolution(input_nc, ndf, 4, 4, 2, 2, 1, 1))
- netD:add(nn.LeakyReLU(0.2, true))
- -- state size: (ndf) x 128 x 128
- netD:add(nn.SpatialConvolution(ndf, ndf * 2, 4, 4, 2, 2, 1, 1))
- netD:add(nn.SpatialBatchNormalization(ndf * 2)):add(nn.LeakyReLU(0.2, true))
- -- state size: (ndf*2) x 64 x 64
- netD:add(nn.SpatialConvolution(ndf * 2, ndf*4, 4, 4, 2, 2, 1, 1))
- netD:add(nn.SpatialBatchNormalization(ndf * 4)):add(nn.LeakyReLU(0.2, true))
- -- state size: (ndf*4) x 32 x 32
- netD:add(nn.SpatialConvolution(ndf * 4, ndf * 8, 4, 4, 2, 2, 1, 1))
- netD:add(nn.SpatialBatchNormalization(ndf * 8)):add(nn.LeakyReLU(0.2, true))
- -- state size: (ndf*8) x 16 x 16
- netD:add(nn.SpatialConvolution(ndf * 8, ndf * 8, 4, 4, 2, 2, 1, 1))
- netD:add(nn.SpatialBatchNormalization(ndf * 8)):add(nn.LeakyReLU(0.2, true))
- -- state size: (ndf*8) x 8 x 8
- netD:add(nn.SpatialConvolution(ndf * 8, ndf * 8, 4, 4, 2, 2, 1, 1))
- netD:add(nn.SpatialBatchNormalization(ndf * 8)):add(nn.LeakyReLU(0.2, true))
- -- state size: (ndf*8) x 4 x 4
- netD:add(nn.SpatialConvolution(ndf * 8, 1, 4, 4, 2, 2, 1, 1))
- -- state size: 1 x 1 x 1
- if use_sigmoid then
- netD:add(nn.Sigmoid())
- end
-
- return netD
-end
-
-
-
-function defineD_basic(input_nc, ndf, use_sigmoid)
- n_layers = 3
- return defineD_n_layers(input_nc, ndf, n_layers, use_sigmoid)
-end
-
--- rf=1
-function defineD_pixelGAN(input_nc, ndf, use_sigmoid)
-
- local netD = nn.Sequential()
-
- -- input is (nc) x 256 x 256
- netD:add(nn.SpatialConvolution(input_nc, ndf, 1, 1, 1, 1, 0, 0))
- netD:add(nn.LeakyReLU(0.2, true))
- -- state size: (ndf) x 256 x 256
- netD:add(nn.SpatialConvolution(ndf, ndf * 2, 1, 1, 1, 1, 0, 0))
- netD:add(normalization(ndf * 2)):add(nn.LeakyReLU(0.2, true))
- -- state size: (ndf*2) x 256 x 256
- netD:add(nn.SpatialConvolution(ndf * 2, 1, 1, 1, 1, 1, 0, 0))
- -- state size: 1 x 256 x 256
- if use_sigmoid then
- netD:add(nn.Sigmoid())
- -- state size: 1 x 30 x 30
- end
-
- return netD
-end
-
--- if n=0, then use pixelGAN (rf=1)
--- else rf is 16 if n=1
--- 34 if n=2
--- 70 if n=3
--- 142 if n=4
--- 286 if n=5
--- 574 if n=6
-function defineD_n_layers(input_nc, ndf, n_layers, use_sigmoid, kw, dropout_ratio)
-
- if dropout_ratio == nil then
- dropout_ratio = 0.0
- end
-
- if kw == nil then
- kw = 4
- end
- padw = math.ceil((kw-1)/2)
-
- if n_layers==0 then
- return defineD_pixelGAN(input_nc, ndf, use_sigmoid)
- else
-
- local netD = nn.Sequential()
-
- -- input is (nc) x 256 x 256
- -- print('input_nc', input_nc)
- netD:add(nn.SpatialConvolution(input_nc, ndf, kw, kw, 2, 2, padw, padw))
- netD:add(nn.LeakyReLU(0.2, true))
-
- local nf_mult = 1
- local nf_mult_prev = 1
- for n = 1, n_layers-1 do
- nf_mult_prev = nf_mult
- nf_mult = math.min(2^n,8)
- netD:add(nn.SpatialConvolution(ndf * nf_mult_prev, ndf * nf_mult, kw, kw, 2, 2, padw,padw))
- netD:add(normalization(ndf * nf_mult)):add(nn.Dropout(dropout_ratio))
- netD:add(nn.LeakyReLU(0.2, true))
- end
-
- -- state size: (ndf*M) x N x N
- nf_mult_prev = nf_mult
- nf_mult = math.min(2^n_layers,8)
- netD:add(nn.SpatialConvolution(ndf * nf_mult_prev, ndf * nf_mult, kw, kw, 1, 1, padw, padw))
- netD:add(normalization(ndf * nf_mult)):add(nn.LeakyReLU(0.2, true))
- -- state size: (ndf*M*2) x (N-1) x (N-1)
- netD:add(nn.SpatialConvolution(ndf * nf_mult, 1, kw, kw, 1, 1, padw,padw))
- -- state size: 1 x (N-2) x (N-2)
- if use_sigmoid then
- netD:add(nn.Sigmoid())
- end
- -- state size: 1 x (N-2) x (N-2)
- return netD
- end
-end
diff --git a/cyclegan/models/base_model.lua b/cyclegan/models/base_model.lua
deleted file mode 100644
index fedd4bd..0000000
--- a/cyclegan/models/base_model.lua
+++ /dev/null
@@ -1,66 +0,0 @@
---------------------------------------------------------------------------------
--- Base Class for Providing Models
---------------------------------------------------------------------------------
-
-local class = require 'class'
-
-BaseModel = class('BaseModel')
-
-function BaseModel:__init(conf)
- conf = conf or {}
-end
-
--- Returns the name of the model
-function BaseModel:model_name()
- return 'DoesNothingModel'
-end
-
--- Defines models and networks
-function BaseModel:Initialize(opt)
- models = {}
- return models
-end
-
--- Runs the forward pass of the network
-function BaseModel:Forward(input, opt)
- output = {}
- return output
-end
-
--- Runs the backprop gradient descent
--- Corresponds to a single batch of data
-function BaseModel:OptimizeParameters(opt)
-end
-
--- This function can be used to reset momentum after each epoch
-function BaseModel:RefreshParameters(opt)
-end
-
--- This function can be used to reset momentum after each epoch
-function BaseModel:UpdateLearningRate(opt)
-end
--- Save the current model to the file system
-function BaseModel:Save(prefix, opt)
-end
-
--- returns a string that describes the current errors
-function BaseModel:GetCurrentErrorDescription()
- return "No Error exists in BaseModel"
-end
-
--- returns current errors
-function BaseModel:GetCurrentErrors(opt)
- return {}
-end
-
--- returns a table of image/label pairs that describe
--- the current results.
--- |return|: a table of table. List of image/label pairs
-function BaseModel:GetCurrentVisuals(opt, size)
- return {}
-end
-
--- returns a string that describes the display plot configuration
-function BaseModel:DisplayPlot(opt)
- return {}
-end
diff --git a/cyclegan/models/base_model.py b/cyclegan/models/base_model.py
new file mode 100644
index 0000000..307c2ba
--- /dev/null
+++ b/cyclegan/models/base_model.py
@@ -0,0 +1,229 @@
+import os
+import torch
+from collections import OrderedDict
+from abc import ABC, abstractmethod
+from . import networks
+
+
+class BaseModel(ABC):
+ """This class is an abstract base class (ABC) for models.
+ To create a subclass, you need to implement the following five functions:
+ -- <__init__>: initialize the class; first call BaseModel.__init__(self, opt).
+ -- : unpack data from dataset and apply preprocessing.
+ -- : produce intermediate results.
+ -- : calculate losses, gradients, and update network weights.
+ -- : (optionally) add model-specific options and set default options.
+ """
+
+ def __init__(self, opt):
+ """Initialize the BaseModel class.
+
+ Parameters:
+ opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
+
+ When creating your custom class, you need to implement your own initialization.
+ In this fucntion, you should first call
+ Then, you need to define four lists:
+ -- self.loss_names (str list): specify the training losses that you want to plot and save.
+ -- self.model_names (str list): specify the images that you want to display and save.
+ -- self.visual_names (str list): define networks used in our training.
+ -- self.optimizers (optimizer list): define and initialize optimizers. You can define one optimizer for each network. If two networks are updated at the same time, you can use itertools.chain to group them. See cycle_gan_model.py for an example.
+ """
+ self.opt = opt
+ self.gpu_ids = opt.gpu_ids
+ self.isTrain = opt.isTrain
+ self.device = torch.device('cuda:{}'.format(self.gpu_ids[0])) if self.gpu_ids else torch.device('cpu') # get device name: CPU or GPU
+ self.save_dir = os.path.join(opt.checkpoints_dir, opt.name) # save all the checkpoints to save_dir
+ if opt.preprocess != 'scale_width': # with [scale_width], input images might have different sizes, which hurts the performance of cudnn.benchmark.
+ torch.backends.cudnn.benchmark = True
+ self.loss_names = []
+ self.model_names = []
+ self.visual_names = []
+ self.optimizers = []
+ self.image_paths = []
+ self.metric = 0 # used for learning rate policy 'plateau'
+
+ @staticmethod
+ def modify_commandline_options(parser, is_train):
+ """Add new model-specific options, and rewrite default values for existing options.
+
+ Parameters:
+ parser -- original option parser
+ is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.
+
+ Returns:
+ the modified parser.
+ """
+ return parser
+
+ @abstractmethod
+ def set_input(self, input):
+ """Unpack input data from the dataloader and perform necessary pre-processing steps.
+
+ Parameters:
+ input (dict): includes the data itself and its metadata information.
+ """
+ pass
+
+ @abstractmethod
+ def forward(self):
+ """Run forward pass; called by both functions and ."""
+ pass
+
+ @abstractmethod
+ def optimize_parameters(self):
+ """Calculate losses, gradients, and update network weights; called in every training iteration"""
+ pass
+
+ def setup(self, opt):
+ """Load and print networks; create schedulers
+
+ Parameters:
+ opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
+ """
+ if self.isTrain:
+ self.schedulers = [networks.get_scheduler(optimizer, opt) for optimizer in self.optimizers]
+ if not self.isTrain or opt.continue_train:
+ load_suffix = 'iter_%d' % opt.load_iter if opt.load_iter > 0 else opt.epoch
+ self.load_networks(load_suffix)
+ self.print_networks(opt.verbose)
+
+ def eval(self):
+ """Make models eval mode during test time"""
+ for name in self.model_names:
+ if isinstance(name, str):
+ net = getattr(self, 'net' + name)
+ net.eval()
+
+ def test(self):
+ """Forward function used in test time.
+
+ This function wraps function in no_grad() so we don't save intermediate steps for backprop
+ It also calls to produce additional visualization results
+ """
+ with torch.no_grad():
+ self.forward()
+ self.compute_visuals()
+
+ def compute_visuals(self):
+ """Calculate additional output images for visdom and HTML visualization"""
+ pass
+
+ def get_image_paths(self):
+ """ Return image paths that are used to load current data"""
+ return self.image_paths
+
+ def update_learning_rate(self):
+ """Update learning rates for all the networks; called at the end of every epoch"""
+ for scheduler in self.schedulers:
+ if self.opt.lr_policy == 'plateau':
+ scheduler.step(self.metric)
+ else:
+ scheduler.step()
+
+ lr = self.optimizers[0].param_groups[0]['lr']
+ print('learning rate = %.7f' % lr)
+
+ def get_current_visuals(self):
+ """Return visualization images. train.py will display these images with visdom, and save the images to a HTML"""
+ visual_ret = OrderedDict()
+ for name in self.visual_names:
+ if isinstance(name, str):
+ visual_ret[name] = getattr(self, name)
+ return visual_ret
+
+ def get_current_losses(self):
+ """Return traning losses / errors. train.py will print out these errors on console, and save them to a file"""
+ errors_ret = OrderedDict()
+ for name in self.loss_names:
+ if isinstance(name, str):
+ errors_ret[name] = float(getattr(self, 'loss_' + name)) # float(...) works for both scalar tensor and float number
+ return errors_ret
+
+ def save_networks(self, epoch):
+ """Save all the networks to the disk.
+
+ Parameters:
+ epoch (int) -- current epoch; used in the file name '%s_net_%s.pth' % (epoch, name)
+ """
+ for name in self.model_names:
+ if isinstance(name, str):
+ save_filename = '%s_net_%s.pth' % (epoch, name)
+ save_path = os.path.join(self.save_dir, save_filename)
+ net = getattr(self, 'net' + name)
+
+ if len(self.gpu_ids) > 0 and torch.cuda.is_available():
+ torch.save(net.module.cpu().state_dict(), save_path)
+ net.cuda(self.gpu_ids[0])
+ else:
+ torch.save(net.cpu().state_dict(), save_path)
+
+ def __patch_instance_norm_state_dict(self, state_dict, module, keys, i=0):
+ """Fix InstanceNorm checkpoints incompatibility (prior to 0.4)"""
+ key = keys[i]
+ if i + 1 == len(keys): # at the end, pointing to a parameter/buffer
+ if module.__class__.__name__.startswith('InstanceNorm') and \
+ (key == 'running_mean' or key == 'running_var'):
+ if getattr(module, key) is None:
+ state_dict.pop('.'.join(keys))
+ if module.__class__.__name__.startswith('InstanceNorm') and \
+ (key == 'num_batches_tracked'):
+ state_dict.pop('.'.join(keys))
+ else:
+ self.__patch_instance_norm_state_dict(state_dict, getattr(module, key), keys, i + 1)
+
+ def load_networks(self, epoch):
+ """Load all the networks from the disk.
+
+ Parameters:
+ epoch (int) -- current epoch; used in the file name '%s_net_%s.pth' % (epoch, name)
+ """
+ for name in self.model_names:
+ if isinstance(name, str):
+ load_filename = '%s_net_%s.pth' % (epoch, name)
+ load_path = os.path.join(self.save_dir, load_filename)
+ net = getattr(self, 'net' + name)
+ if isinstance(net, torch.nn.DataParallel):
+ net = net.module
+ print('loading the model from %s' % load_path)
+ # if you are using PyTorch newer than 0.4 (e.g., built from
+ # GitHub source), you can remove str() on self.device
+ state_dict = torch.load(load_path, map_location=str(self.device))
+ if hasattr(state_dict, '_metadata'):
+ del state_dict._metadata
+
+ # patch InstanceNorm checkpoints prior to 0.4
+ for key in list(state_dict.keys()): # need to copy keys here because we mutate in loop
+ self.__patch_instance_norm_state_dict(state_dict, net, key.split('.'))
+ net.load_state_dict(state_dict)
+
+ def print_networks(self, verbose):
+ """Print the total number of parameters in the network and (if verbose) network architecture
+
+ Parameters:
+ verbose (bool) -- if verbose: print the network architecture
+ """
+ print('---------- Networks initialized -------------')
+ for name in self.model_names:
+ if isinstance(name, str):
+ net = getattr(self, 'net' + name)
+ num_params = 0
+ for param in net.parameters():
+ num_params += param.numel()
+ if verbose:
+ print(net)
+ print('[Network %s] Total number of parameters : %.3f M' % (name, num_params / 1e6))
+ print('-----------------------------------------------')
+
+ def set_requires_grad(self, nets, requires_grad=False):
+ """Set requies_grad=Fasle for all the networks to avoid unnecessary computations
+ Parameters:
+ nets (network list) -- a list of networks
+ requires_grad (bool) -- whether the networks require gradients or not
+ """
+ if not isinstance(nets, list):
+ nets = [nets]
+ for net in nets:
+ if net is not None:
+ for param in net.parameters():
+ param.requires_grad = requires_grad
diff --git a/cyclegan/models/bigan_model.lua b/cyclegan/models/bigan_model.lua
deleted file mode 100644
index 7bd6df8..0000000
--- a/cyclegan/models/bigan_model.lua
+++ /dev/null
@@ -1,254 +0,0 @@
-local class = require 'class'
-require 'models.base_model'
-require 'models.architectures'
-require 'util.image_pool'
-util = paths.dofile('../util/util.lua')
-content = paths.dofile('../util/content_loss.lua')
-
-BiGANModel = class('BiGANModel', 'BaseModel')
-
-function BiGANModel:__init(conf)
- BaseModel.__init(self, conf)
- conf = conf or {}
-end
-
-function BiGANModel:model_name()
- return 'BiGANModel'
-end
-
-function BiGANModel:InitializeStates(use_wgan)
- optimState = {learningRate=opt.lr, beta1=opt.beta1,}
- return optimState
-end
--- Defines models and networks
-function BiGANModel:Initialize(opt)
- if opt.test == 0 then
- self.realABPool = ImagePool(opt.pool_size)
- self.fakeABPool = ImagePool(opt.pool_size)
- end
- -- define tensors
- local d_input_nc = opt.input_nc + opt.output_nc
- self.real_AB = torch.Tensor(opt.batchSize, d_input_nc, opt.fineSize, opt.fineSize)
- self.fake_AB = torch.Tensor(opt.batchSize, d_input_nc, opt.fineSize, opt.fineSize)
- -- load/define models
- self.criterionGAN = nn.MSECriterion()
-
- local netG, netE, netD = nil, nil, nil
- if opt.continue_train == 1 then
- if opt.test == 1 then -- which_epoch option exists in test mode
- netG = util.load_test_model('G', opt)
- netE = util.load_test_model('E', opt)
- netD = util.load_test_model('D', opt)
- else
- netG = util.load_model('G', opt)
- netE = util.load_model('E', opt)
- netD = util.load_model('D', opt)
- end
- else
- -- netG_test = defineG(opt.input_nc, opt.output_nc, opt.ngf, "resnet_unet", opt.arch)
- -- os.exit()
- netD = defineD(d_input_nc, opt.ndf, opt.which_model_netD, opt.n_layers_D, false) -- no sigmoid layer
- print('netD...', netD)
- netG = defineG(opt.input_nc, opt.output_nc, opt.ngf, opt.which_model_netG, opt.arch)
- print('netG...', netG)
- netE = defineG(opt.output_nc, opt.input_nc, opt.ngf, opt.which_model_netG, opt.arch)
- print('netE...', netE)
-
- end
-
- self.netD = netD
- self.netG = netG
- self.netE = netE
-
- -- define real/fake labels
- netD_output_size = self.netD:forward(self.real_AB):size()
- self.fake_label = torch.Tensor(netD_output_size):fill(0.0)
- self.real_label = torch.Tensor(netD_output_size):fill(1.0) -- no soft smoothing
-
- self.optimStateD = self:InitializeStates()
- self.optimStateG = self:InitializeStates()
- self.optimStateE = self:InitializeStates()
- self.A_idx = {{}, {1, opt.input_nc}, {}, {}}
- self.B_idx = {{}, {opt.input_nc+1, opt.input_nc+opt.output_nc}, {}, {}}
- self:RefreshParameters()
-
- print('---------- # Learnable Parameters --------------')
- print(('G = %d'):format(self.parametersG:size(1)))
- print(('E = %d'):format(self.parametersE:size(1)))
- print(('D = %d'):format(self.parametersD:size(1)))
- print('------------------------------------------------')
- -- os.exit()
-end
-
--- Runs the forward pass of the network and
--- saves the result to member variables of the class
-function BiGANModel:Forward(input, opt)
- if opt.which_direction == 'BtoA' then
- local temp = input.real_A
- input.real_A = input.real_B
- input.real_B = temp
- end
- self.real_AB[self.A_idx]:copy(input.real_A)
- self.fake_AB[self.B_idx]:copy(input.real_B)
- self.real_A = self.real_AB[self.A_idx]
- self.real_B = self.fake_AB[self.B_idx]
- self.fake_B = self.netG:forward(self.real_A):clone()
- self.fake_A = self.netE:forward(self.real_B):clone()
- self.real_AB[self.B_idx]:copy(self.fake_B) -- real_AB: real_A, fake_B -> real_label
- self.fake_AB[self.A_idx]:copy(self.fake_A) -- fake_AB: fake_A, real_B -> fake_label
- -- if opt.test == 0 then
- -- self.real_AB = self.realABPool:Query(self.real_AB) -- batch history
- -- self.fake_AB = self.fakeABPool:Query(self.fake_AB) -- batch history
- -- end
-end
-
--- create closure to evaluate f(X) and df/dX of discriminator
-function BiGANModel:fDx_basic(x, gradParams, netD, real_AB, fake_AB, opt)
- util.BiasZero(netD)
- gradParams:zero()
- -- Real log(D_A(B))
- local output = netD:forward(real_AB):clone()
- local errD_real = self.criterionGAN:forward(output, self.real_label)
- local df_do = self.criterionGAN:backward(output, self.real_label)
- netD:backward(real_AB, df_do)
- -- Fake + log(1 - D_A(G(A)))
- output = netD:forward(fake_AB):clone()
- local errD_fake = self.criterionGAN:forward(output, self.fake_label)
- local df_do2 = self.criterionGAN:backward(output, self.fake_label)
- netD:backward(fake_AB, df_do2)
- -- Compute loss
- local errD = (errD_real + errD_fake) / 2.0
- return errD, gradParams
-end
-
-
-function BiGANModel:fDx(x, opt)
- -- use image pool that stores the old fake images
- real_AB = self.realABPool:Query(self.real_AB)
- fake_AB = self.fakeABPool:Query(self.fake_AB)
- self.errD, gradParams = self:fDx_basic(x, self.gradParametersD, self.netD, real_AB, fake_AB, opt)
- return self.errD, gradParams
-end
-
-
-
-function BiGANModel:fGx_basic(x, netG, netD, gradParametersG, opt)
- util.BiasZero(netG)
- util.BiasZero(netD)
- gradParametersG:zero()
-
- -- First. G(A) should fake the discriminator
- local output = netD:forward(self.real_AB):clone()
- local errG = self.criterionGAN:forward(output, self.fake_label)
- local dgan_loss_dd = self.criterionGAN:backward(output, self.fake_label)
- local dgan_loss_do = netD:updateGradInput(self.real_AB, dgan_loss_dd)
- netG:backward(self.real_A, dgan_loss_do[self.B_idx]) -- real_AB: real_A, fake_B -> real_label
- return gradParametersG, errG
-end
-
-
-function BiGANModel:fGx(x, opt)
- self.gradParametersG, self.errG = self:fGx_basic(x, self.netG, self.netD,
- self.gradParametersG, opt)
- return self.errG, self.gradParametersG
-end
-
-
-function BiGANModel:fEx_basic(x, netE, netD, gradParametersE, opt)
- util.BiasZero(netE)
- util.BiasZero(netD)
- gradParametersE:zero()
-
- -- First. G(A) should fake the discriminator
- local output = netD:forward(self.fake_AB):clone()
- local errE= self.criterionGAN:forward(output, self.real_label)
- local dgan_loss_dd = self.criterionGAN:backward(output, self.real_label)
- local dgan_loss_do = netD:updateGradInput(self.fake_AB, dgan_loss_dd)
- netE:backward(self.real_B, dgan_loss_do[self.A_idx])-- fake_AB: fake_A, real_B -> fake_label
- return gradParametersE, errE
-end
-
-
-function BiGANModel:fEx(x, opt)
- self.gradParametersE, self.errE = self:fEx_basic(x, self.netE, self.netD,
- self.gradParametersE, opt)
- return self.errE, self.gradParametersE
-end
-
-
-function BiGANModel:OptimizeParameters(opt)
- local fG = function(x) return self:fGx(x, opt) end
- local fE = function(x) return self:fEx(x, opt) end
- local fD = function(x) return self:fDx(x, opt) end
- optim.adam(fD, self.parametersD, self.optimStateD)
- optim.adam(fG, self.parametersG, self.optimStateG)
- optim.adam(fE, self.parametersE, self.optimStateE)
-end
-
-function BiGANModel:RefreshParameters()
- self.parametersD, self.gradParametersD = nil, nil -- nil them to avoid spiking memory
- self.parametersG, self.gradParametersG = nil, nil
- self.parametersE, self.gradParametersE = nil, nil
- -- define parameters of optimization
- self.parametersD, self.gradParametersD = self.netD:getParameters()
- self.parametersG, self.gradParametersG = self.netG:getParameters()
- self.parametersE, self.gradParametersE = self.netE:getParameters()
-end
-
-function BiGANModel:Save(prefix, opt)
- util.save_model(self.netG, prefix .. '_net_G.t7', 1)
- util.save_model(self.netE, prefix .. '_net_E.t7', 1)
- util.save_model(self.netD, prefix .. '_net_D.t7', 1)
-end
-
-function BiGANModel:GetCurrentErrorDescription()
- description = ('D: %.4f G: %.4f E: %.4f'):format(
- self.errD and self.errD or -1,
- self.errG and self.errG or -1,
- self.errE and self.errE or -1)
- return description
-end
-
-function BiGANModel:GetCurrentErrors()
- local errors = {errD=self.errD, errG=self.errG, errE=self.errE}
- return errors
-end
-
--- returns a string that describes the display plot configuration
-function BiGANModel:DisplayPlot(opt)
- return 'errD,errG,errE'
-end
-function BiGANModel:UpdateLearningRate(opt)
- local lrd = opt.lr / opt.niter_decay
- local old_lr = self.optimStateD['learningRate']
- local lr = old_lr - lrd
- self.optimStateD['learningRate'] = lr
- self.optimStateG['learningRate'] = lr
- self.optimStateE['learningRate'] = lr
- print(('update learning rate: %f -> %f'):format(old_lr, lr))
-end
-
-local function MakeIm3(im)
- -- print('before im_size', im:size())
- local im3 = nil
- if im:size(2) == 1 then
- im3 = torch.repeatTensor(im, 1,3,1,1)
- else
- im3 = im
- end
- -- print('after im_size', im:size())
- -- print('after im3_size', im3:size())
- return im3
-end
-function BiGANModel:GetCurrentVisuals(opt, size)
- if not size then
- size = opt.display_winsize
- end
-
- local visuals = {}
- table.insert(visuals, {img=MakeIm3(self.real_A), label='real_A'})
- table.insert(visuals, {img=MakeIm3(self.fake_B), label='fake_B'})
- table.insert(visuals, {img=MakeIm3(self.real_B), label='real_B'})
- table.insert(visuals, {img=MakeIm3(self.fake_A), label='fake_A'})
- return visuals
-end
diff --git a/cyclegan/models/colorization_model.py b/cyclegan/models/colorization_model.py
new file mode 100644
index 0000000..2b4a127
--- /dev/null
+++ b/cyclegan/models/colorization_model.py
@@ -0,0 +1,68 @@
+from .pix2pix_model import Pix2PixModel
+import torch
+from skimage import color # used for lab2rgb
+import numpy as np
+
+
+class ColorizationModel(Pix2PixModel):
+ """This is a subclass of Pix2PixModel for image colorization (black & white image -> colorful images).
+
+ The model training requires '-dataset_model colorization' dataset.
+ It trains a pix2pix model, mapping from L channel to ab channels in Lab color space.
+ By default, the colorization dataset will automatically set '--input_nc 1' and '--output_nc 2'.
+ """
+ @staticmethod
+ def modify_commandline_options(parser, is_train=True):
+ """Add new dataset-specific options, and rewrite default values for existing options.
+
+ Parameters:
+ parser -- original option parser
+ is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.
+
+ Returns:
+ the modified parser.
+
+ By default, we use 'colorization' dataset for this model.
+ See the original pix2pix paper (https://arxiv.org/pdf/1611.07004.pdf) and colorization results (Figure 9 in the paper)
+ """
+ Pix2PixModel.modify_commandline_options(parser, is_train)
+ parser.set_defaults(dataset_mode='colorization')
+ return parser
+
+ def __init__(self, opt):
+ """Initialize the class.
+
+ Parameters:
+ opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
+
+ For visualization, we set 'visual_names' as 'real_A' (input real image),
+ 'real_B_rgb' (ground truth RGB image), and 'fake_B_rgb' (predicted RGB image)
+ We convert the Lab image 'real_B' (inherited from Pix2pixModel) to a RGB image 'real_B_rgb'.
+ we convert the Lab image 'fake_B' (inherited from Pix2pixModel) to a RGB image 'fake_B_rgb'.
+ """
+ # reuse the pix2pix model
+ Pix2PixModel.__init__(self, opt)
+ # specify the images to be visualized.
+ self.visual_names = ['real_A', 'real_B_rgb', 'fake_B_rgb']
+
+ def lab2rgb(self, L, AB):
+ """Convert an Lab tensor image to a RGB numpy output
+ Parameters:
+ L (1-channel tensor array): L channel images (range: [-1, 1], torch tensor array)
+ AB (2-channel tensor array): ab channel images (range: [-1, 1], torch tensor array)
+
+ Returns:
+ rgb (RGB numpy image): rgb output images (range: [0, 255], numpy array)
+ """
+ AB2 = AB * 110.0
+ L2 = (L + 1.0) * 50.0
+ Lab = torch.cat([L2, AB2], dim=1)
+ Lab = Lab[0].data.cpu().float().numpy()
+ Lab = np.transpose(Lab.astype(np.float64), (1, 2, 0))
+ rgb = color.lab2rgb(Lab) * 255
+ return rgb
+
+ def compute_visuals(self):
+ """Calculate additional output images for visdom and HTML visualization"""
+ self.real_B_rgb = self.lab2rgb(self.real_A, self.real_B)
+ self.fake_B_rgb = self.lab2rgb(self.real_A, self.fake_B)
diff --git a/cyclegan/models/content_gan_model.lua b/cyclegan/models/content_gan_model.lua
deleted file mode 100644
index a3e7059..0000000
--- a/cyclegan/models/content_gan_model.lua
+++ /dev/null
@@ -1,201 +0,0 @@
-local class = require 'class'
-require 'models.base_model'
-require 'models.architectures'
-require 'util.image_pool'
-util = paths.dofile('../util/util.lua')
-content = paths.dofile('../util/content_loss.lua')
-
-ContentGANModel = class('ContentGANModel', 'BaseModel')
-
-function ContentGANModel:__init(conf)
- BaseModel.__init(self, conf)
- conf = conf or {}
-end
-
-function ContentGANModel:model_name()
- return 'ContentGANModel'
-end
-
-function ContentGANModel:InitializeStates()
- local optimState = {learningRate=opt.lr, beta1=opt.beta1,}
- return optimState
-end
--- Defines models and networks
-function ContentGANModel:Initialize(opt)
- if opt.test == 0 then
- self.fakePool = ImagePool(opt.pool_size)
- end
- -- define tensors
- self.real_A = torch.Tensor(opt.batchSize, opt.input_nc, opt.fineSize, opt.fineSize)
- self.fake_B = torch.Tensor(opt.batchSize, opt.output_nc, opt.fineSize, opt.fineSize)
- self.real_B = self.fake_B:clone() --torch.Tensor(opt.batchSize, opt.output_nc, opt.fineSize, opt.fineSize)
-
- -- load/define models
- self.criterionGAN = nn.MSECriterion()
- self.criterionContent = nn.AbsCriterion()
- self.contentFunc = content.defineContent(opt.content_loss, opt.layer_name)
- self.netG, self.netD = nil, nil
- if opt.continue_train == 1 then
- if opt.which_epoch then -- which_epoch option exists in test mode
- self.netG = util.load_test_model('G_A', opt)
- self.netD = util.load_test_model('D_A', opt)
- else
- self.netG = util.load_model('G_A', opt)
- self.netD = util.load_model('D_A', opt)
- end
- else
- self.netG = defineG(opt.input_nc, opt.output_nc, opt.ngf, opt.which_model_netG)
- print('netG...', self.netG)
- self.netD = defineD(opt.output_nc, opt.ndf, opt.which_model_netD, opt.n_layers_D, false)
- print('netD...', self.netD)
- end
- -- define real/fake labels
- netD_output_size = self.netD:forward(self.real_A):size()
- self.fake_label = torch.Tensor(netD_output_size):fill(0.0)
- self.real_label = torch.Tensor(netD_output_size):fill(1.0) -- no soft smoothing
- self.optimStateD = self:InitializeStates()
- self.optimStateG = self:InitializeStates()
- self:RefreshParameters()
- print('---------- # Learnable Parameters --------------')
- print(('G = %d'):format(self.parametersG:size(1)))
- print(('D = %d'):format(self.parametersD:size(1)))
- print('------------------------------------------------')
- -- os.exit()
-end
-
--- Runs the forward pass of the network and
--- saves the result to member variables of the class
-function ContentGANModel:Forward(input, opt)
- if opt.which_direction == 'BtoA' then
- local temp = input.real_A
- input.real_A = input.real_B
- input.real_B = temp
- end
-
- self.real_A:copy(input.real_A)
- self.real_B:copy(input.real_B)
- self.fake_B = self.netG:forward(self.real_A):clone()
- -- output = {self.fake_B}
- output = {}
- -- if opt.test == 1 then
-
- -- end
- return output
-end
-
--- create closure to evaluate f(X) and df/dX of discriminator
-function ContentGANModel:fDx_basic(x, gradParams, netD, netG,
- real_target, fake_target, opt)
- util.BiasZero(netD)
- util.BiasZero(netG)
- gradParams:zero()
-
- local errD_real, errD_rec, errD_fake, errD = 0, 0, 0, 0
- -- Real log(D_A(B))
- local output = netD:forward(real_target)
- errD_real = self.criterionGAN:forward(output, self.real_label)
- df_do = self.criterionGAN:backward(output, self.real_label)
- netD:backward(real_target, df_do)
-
- -- Fake + log(1 - D_A(G_A(A)))
- output = netD:forward(fake_target)
- errD_fake = self.criterionGAN:forward(output, self.fake_label)
- df_do = self.criterionGAN:backward(output, self.fake_label)
- netD:backward(fake_target, df_do)
- errD = (errD_real + errD_fake) / 2.0
- -- print('errD', errD
- return errD, gradParams
-end
-
-
-function ContentGANModel:fDx(x, opt)
- fake_B = self.fakePool:Query(self.fake_B)
- self.errD, gradParams = self:fDx_basic(x, self.gradparametersD, self.netD, self.netG,
- self.real_B, fake_B, opt)
- return self.errD, gradParams
-end
-
-function ContentGANModel:fGx_basic(x, netG_source, netD_source, real_source, real_target, fake_target,
- gradParametersG_source, opt)
- util.BiasZero(netD_source)
- util.BiasZero(netG_source)
- gradParametersG_source:zero()
- -- GAN loss
- -- local df_d_GAN = torch.zeros(fake_target:size())
- -- local errGAN = 0
- -- local errRec = 0
- --- Domain GAN loss: D_A(G_A(A))
- local output = netD_source.output -- [hack] forward was already executed in fDx, so save computation netD_source:forward(fake_B) ---
- local errGAN = self.criterionGAN:forward(output, self.real_label)
- local df_do = self.criterionGAN:backward(output, self.real_label)
- local df_d_GAN = netD_source:updateGradInput(fake_target, df_do) ---:narrow(2,fake_AB:size(2)-output_nc+1, output_nc)
-
- -- content loss
- -- print('content_loss', opt.content_loss)
- -- function content.lossUpdate(criterionContent, real_source, fake_target, contentFunc, loss_type, weight)
- local errContent, df_d_content = content.lossUpdate(self.criterionContent, real_source, fake_target, self.contentFunc, opt.content_loss, opt.lambda_A)
- netG_source:forward(real_source)
- netG_source:backward(real_source, df_d_GAN + df_d_content)
- -- print('errD', errGAN)
- return gradParametersG_source, errGAN, errContent
-end
-
-function ContentGANModel:fGx(x, opt)
- self.gradparametersG, self.errG, self.errCont =
- self:fGx_basic(x, self.netG, self.netD,
- self.real_A, self.real_B, self.fake_B,
- self.gradparametersG, opt)
- return self.errG, self.gradparametersG
-end
-
-function ContentGANModel:OptimizeParameters(opt)
- local fDx = function(x) return self:fDx(x, opt) end
- local fGx = function(x) return self:fGx(x, opt) end
- optim.adam(fDx, self.parametersD, self.optimStateD)
- optim.adam(fGx, self.parametersG, self.optimStateG)
-end
-
-function ContentGANModel:RefreshParameters()
- self.parametersD, self.gradparametersD = nil, nil -- nil them to avoid spiking memory
- self.parametersG, self.gradparametersG = nil, nil
- -- define parameters of optimization
- self.parametersG, self.gradparametersG = self.netG:getParameters()
- self.parametersD, self.gradparametersD = self.netD:getParameters()
-end
-
-function ContentGANModel:Save(prefix, opt)
- util.save_model(self.netG, prefix .. '_net_G_A.t7', 1.0)
- util.save_model(self.netD, prefix .. '_net_D_A.t7', 1.0)
-end
-
-function ContentGANModel:GetCurrentErrorDescription()
- description = ('G: %.4f D: %.4f Content: %.4f'):format(self.errG and self.errG or -1,
- self.errD and self.errD or -1,
- self.errCont and self.errCont or -1)
- return description
-end
-
-
-function ContentGANModel:GetCurrentErrors()
- local errors = {errG=self.errG and self.errG or -1, errD=self.errD and self.errD or -1,
- errCont=self.errCont and self.errCont or -1}
- return errors
-end
-
--- returns a string that describes the display plot configuration
-function ContentGANModel:DisplayPlot(opt)
- return 'errG,errD,errCont'
-end
-
-
-function ContentGANModel:GetCurrentVisuals(opt, size)
- if not size then
- size = opt.display_winsize
- end
-
- local visuals = {}
- table.insert(visuals, {img=self.real_A, label='real_A'})
- table.insert(visuals, {img=self.fake_B, label='fake_B'})
- table.insert(visuals, {img=self.real_B, label='real_B'})
- return visuals
-end
diff --git a/cyclegan/models/cycle_gan_model.lua b/cyclegan/models/cycle_gan_model.lua
deleted file mode 100644
index c1489c4..0000000
--- a/cyclegan/models/cycle_gan_model.lua
+++ /dev/null
@@ -1,324 +0,0 @@
-local class = require 'class'
-require 'models.base_model'
-require 'models.architectures'
-require 'util.image_pool'
-
-util = paths.dofile('../util/util.lua')
-CycleGANModel = class('CycleGANModel', 'BaseModel')
-
-function CycleGANModel:__init(conf)
- BaseModel.__init(self, conf)
- conf = conf or {}
-end
-
-function CycleGANModel:model_name()
- return 'CycleGANModel'
-end
-
-function CycleGANModel:InitializeStates(use_wgan)
- optimState = {learningRate=opt.lr, beta1=opt.beta1,}
- return optimState
-end
--- Defines models and networks
-function CycleGANModel:Initialize(opt)
- if opt.test == 0 then
- self.fakeAPool = ImagePool(opt.pool_size)
- self.fakeBPool = ImagePool(opt.pool_size)
- end
- -- define tensors
- if opt.test == 0 then -- allocate tensors for training
- self.real_A = torch.Tensor(opt.batchSize, opt.input_nc, opt.fineSize, opt.fineSize)
- self.real_B = torch.Tensor(opt.batchSize, opt.output_nc, opt.fineSize, opt.fineSize)
- self.fake_A = torch.Tensor(opt.batchSize, opt.input_nc, opt.fineSize, opt.fineSize)
- self.fake_B = torch.Tensor(opt.batchSize, opt.output_nc, opt.fineSize, opt.fineSize)
- self.rec_A = torch.Tensor(opt.batchSize, opt.input_nc, opt.fineSize, opt.fineSize)
- self.rec_B = torch.Tensor(opt.batchSize, opt.output_nc, opt.fineSize, opt.fineSize)
- end
- -- load/define models
- local use_lsgan = ((opt.use_lsgan ~= nil) and (opt.use_lsgan == 1))
- if not use_lsgan then
- self.criterionGAN = nn.BCECriterion()
- else
- self.criterionGAN = nn.MSECriterion()
- end
- self.criterionRec = nn.AbsCriterion()
-
- local netG_A, netD_A, netG_B, netD_B = nil, nil, nil, nil
- if opt.continue_train == 1 then
- if opt.test == 1 then -- test mode
- netG_A = util.load_test_model('G_A', opt)
- netG_B = util.load_test_model('G_B', opt)
-
- --setup optnet to save a little bit of memory
- if opt.use_optnet == 1 then
- local sample_input = torch.randn(1, opt.input_nc, 2, 2)
- local optnet = require 'optnet'
- optnet.optimizeMemory(netG_A, sample_input, {inplace=true, reuseBuffers=true})
- optnet.optimizeMemory(netG_B, sample_input, {inplace=true, reuseBuffers=true})
- end
- else
- netG_A = util.load_model('G_A', opt)
- netG_B = util.load_model('G_B', opt)
- netD_A = util.load_model('D_A', opt)
- netD_B = util.load_model('D_B', opt)
- end
- else
- local use_sigmoid = (not use_lsgan)
- -- netG_test = defineG(opt.input_nc, opt.output_nc, opt.ngf, "resnet_unet", opt.arch)
- -- os.exit()
- netG_A = defineG(opt.input_nc, opt.output_nc, opt.ngf, opt.which_model_netG, opt.arch)
- print('netG_A...', netG_A)
- netD_A = defineD(opt.output_nc, opt.ndf, opt.which_model_netD, opt.n_layers_D, use_sigmoid) -- no sigmoid layer
- print('netD_A...', netD_A)
- netG_B = defineG(opt.output_nc, opt.input_nc, opt.ngf, opt.which_model_netG, opt.arch)
- print('netG_B...', netG_B)
- netD_B = defineD(opt.input_nc, opt.ndf, opt.which_model_netD, opt.n_layers_D, use_sigmoid) -- no sigmoid layer
- print('netD_B', netD_B)
- end
-
- self.netD_A = netD_A
- self.netG_A = netG_A
- self.netG_B = netG_B
- self.netD_B = netD_B
-
- -- define real/fake labels
- if opt.test == 0 then
- local D_A_size = self.netD_A:forward(self.real_B):size() -- hack: assume D_size_A = D_size_B
- self.fake_label_A = torch.Tensor(D_A_size):fill(0.0)
- self.real_label_A = torch.Tensor(D_A_size):fill(1.0) -- no soft smoothing
- local D_B_size = self.netD_B:forward(self.real_A):size() -- hack: assume D_size_A = D_size_B
- self.fake_label_B = torch.Tensor(D_B_size):fill(0.0)
- self.real_label_B = torch.Tensor(D_B_size):fill(1.0) -- no soft smoothing
- self.optimStateD_A = self:InitializeStates()
- self.optimStateG_A = self:InitializeStates()
- self.optimStateD_B = self:InitializeStates()
- self.optimStateG_B = self:InitializeStates()
- self:RefreshParameters()
- print('---------- # Learnable Parameters --------------')
- print(('G_A = %d'):format(self.parametersG_A:size(1)))
- print(('D_A = %d'):format(self.parametersD_A:size(1)))
- print(('G_B = %d'):format(self.parametersG_B:size(1)))
- print(('D_B = %d'):format(self.parametersD_B:size(1)))
- print('------------------------------------------------')
- end
-end
-
--- Runs the forward pass of the network and
--- saves the result to member variables of the class
-function CycleGANModel:Forward(input, opt)
- if opt.which_direction == 'BtoA' then
- local temp = input.real_A:clone()
- input.real_A = input.real_B:clone()
- input.real_B = temp
- end
-
- if opt.test == 0 then
- self.real_A:copy(input.real_A)
- self.real_B:copy(input.real_B)
- end
-
- if opt.test == 1 then -- forward for test
- if opt.gpu > 0 then
- self.real_A = input.real_A:cuda()
- self.real_B = input.real_B:cuda()
- else
- self.real_A = input.real_A:clone()
- self.real_B = input.real_B:clone()
- end
- self.fake_B = self.netG_A:forward(self.real_A):clone()
- self.fake_A = self.netG_B:forward(self.real_B):clone()
- self.rec_A = self.netG_B:forward(self.fake_B):clone()
- self.rec_B = self.netG_A:forward(self.fake_A):clone()
- end
-end
-
--- create closure to evaluate f(X) and df/dX of discriminator
-function CycleGANModel:fDx_basic(x, gradParams, netD, netG, real, fake, real_label, fake_label, opt)
- util.BiasZero(netD)
- util.BiasZero(netG)
- gradParams:zero()
- -- Real log(D_A(B))
- local output = netD:forward(real)
- local errD_real = self.criterionGAN:forward(output, real_label)
- local df_do = self.criterionGAN:backward(output, real_label)
- netD:backward(real, df_do)
- -- Fake + log(1 - D_A(G_A(A)))
- output = netD:forward(fake)
- local errD_fake = self.criterionGAN:forward(output, fake_label)
- local df_do2 = self.criterionGAN:backward(output, fake_label)
- netD:backward(fake, df_do2)
- -- Compute loss
- local errD = (errD_real + errD_fake) / 2.0
- return errD, gradParams
-end
-
-
-function CycleGANModel:fDAx(x, opt)
- -- use image pool that stores the old fake images
- fake_B = self.fakeBPool:Query(self.fake_B)
- self.errD_A, gradParams = self:fDx_basic(x, self.gradparametersD_A, self.netD_A, self.netG_A,
- self.real_B, fake_B, self.real_label_A, self.fake_label_A, opt)
- return self.errD_A, gradParams
-end
-
-
-function CycleGANModel:fDBx(x, opt)
- -- use image pool that stores the old fake images
- fake_A = self.fakeAPool:Query(self.fake_A)
- self.errD_B, gradParams = self:fDx_basic(x, self.gradparametersD_B, self.netD_B, self.netG_B,
- self.real_A, fake_A, self.real_label_B, self.fake_label_B, opt)
- return self.errD_B, gradParams
-end
-
-
-function CycleGANModel:fGx_basic(x, gradParams, netG, netD, netE, real, real2, real_label, lambda1, lambda2, opt)
- util.BiasZero(netD)
- util.BiasZero(netG)
- util.BiasZero(netE) -- inverse mapping
- gradParams:zero()
-
- -- G should be identity if real2 is fed.
- local errI = nil
- local identity = nil
- if opt.lambda_identity > 0 then
- identity = netG:forward(real2):clone()
- errI = self.criterionRec:forward(identity, real2) * lambda2 * opt.lambda_identity
- local didentity_loss_do = self.criterionRec:backward(identity, real2):mul(lambda2):mul(opt.lambda_identity)
- netG:backward(real2, didentity_loss_do)
- end
-
- --- GAN loss: D_A(G_A(A))
- local fake = netG:forward(real):clone()
- local output = netD:forward(fake)
- local errG = self.criterionGAN:forward(output, real_label)
- local df_do1 = self.criterionGAN:backward(output, real_label)
- local df_d_GAN = netD:updateGradInput(fake, df_do1) --
-
- -- forward cycle loss
- local rec = netE:forward(fake):clone()
- local errRec = self.criterionRec:forward(rec, real) * lambda1
- local df_do2 = self.criterionRec:backward(rec, real):mul(lambda1)
- local df_do_rec = netE:updateGradInput(fake, df_do2)
-
- netG:backward(real, df_d_GAN + df_do_rec)
-
- -- backward cycle loss
- local fake2 = netE:forward(real2)--:clone()
- local rec2 = netG:forward(fake2)--:clone()
- local errAdapt = self.criterionRec:forward(rec2, real2) * lambda2
- local df_do_coadapt = self.criterionRec:backward(rec2, real2):mul(lambda2)
- netG:backward(fake2, df_do_coadapt)
-
- return gradParams, errG, errRec, errI, fake, rec, identity
-end
-
-function CycleGANModel:fGAx(x, opt)
- self.gradparametersG_A, self.errG_A, self.errRec_A, self.errI_A, self.fake_B, self.rec_A, self.identity_B =
- self:fGx_basic(x, self.gradparametersG_A, self.netG_A, self.netD_A, self.netG_B, self.real_A, self.real_B,
- self.real_label_A, opt.lambda_A, opt.lambda_B, opt)
- return self.errG_A, self.gradparametersG_A
-end
-
-function CycleGANModel:fGBx(x, opt)
- self.gradparametersG_B, self.errG_B, self.errRec_B, self.errI_B, self.fake_A, self.rec_B, self.identity_A =
- self:fGx_basic(x, self.gradparametersG_B, self.netG_B, self.netD_B, self.netG_A, self.real_B, self.real_A,
- self.real_label_B, opt.lambda_B, opt.lambda_A, opt)
- return self.errG_B, self.gradparametersG_B
-end
-
-
-function CycleGANModel:OptimizeParameters(opt)
- local fDA = function(x) return self:fDAx(x, opt) end
- local fGA = function(x) return self:fGAx(x, opt) end
- local fDB = function(x) return self:fDBx(x, opt) end
- local fGB = function(x) return self:fGBx(x, opt) end
-
- optim.adam(fGA, self.parametersG_A, self.optimStateG_A)
- optim.adam(fDA, self.parametersD_A, self.optimStateD_A)
- optim.adam(fGB, self.parametersG_B, self.optimStateG_B)
- optim.adam(fDB, self.parametersD_B, self.optimStateD_B)
-end
-
-function CycleGANModel:RefreshParameters()
- self.parametersD_A, self.gradparametersD_A = nil, nil -- nil them to avoid spiking memory
- self.parametersG_A, self.gradparametersG_A = nil, nil
- self.parametersG_B, self.gradparametersG_B = nil, nil
- self.parametersD_B, self.gradparametersD_B = nil, nil
- -- define parameters of optimization
- self.parametersG_A, self.gradparametersG_A = self.netG_A:getParameters()
- self.parametersD_A, self.gradparametersD_A = self.netD_A:getParameters()
- self.parametersG_B, self.gradparametersG_B = self.netG_B:getParameters()
- self.parametersD_B, self.gradparametersD_B = self.netD_B:getParameters()
-end
-
-function CycleGANModel:Save(prefix, opt)
- util.save_model(self.netG_A, prefix .. '_net_G_A.t7', 1)
- util.save_model(self.netD_A, prefix .. '_net_D_A.t7', 1)
- util.save_model(self.netG_B, prefix .. '_net_G_B.t7', 1)
- util.save_model(self.netD_B, prefix .. '_net_D_B.t7', 1)
-end
-
-function CycleGANModel:GetCurrentErrorDescription()
- description = ('[A] G: %.4f D: %.4f Rec: %.4f I: %.4f || [B] G: %.4f D: %.4f Rec: %.4f I:%.4f'):format(
- self.errG_A and self.errG_A or -1,
- self.errD_A and self.errD_A or -1,
- self.errRec_A and self.errRec_A or -1,
- self.errI_A and self.errI_A or -1,
- self.errG_B and self.errG_B or -1,
- self.errD_B and self.errD_B or -1,
- self.errRec_B and self.errRec_B or -1,
- self.errI_B and self.errI_B or -1)
- return description
-end
-
-function CycleGANModel:GetCurrentErrors()
- local errors = {errG_A=self.errG_A, errD_A=self.errD_A, errRec_A=self.errRec_A, errI_A=self.errI_A,
- errG_B=self.errG_B, errD_B=self.errD_B, errRec_B=self.errRec_B, errI_B=self.errI_B}
- return errors
-end
-
--- returns a string that describes the display plot configuration
-function CycleGANModel:DisplayPlot(opt)
- if opt.lambda_identity > 0 then
- return 'errG_A,errD_A,errRec_A,errI_A,errG_B,errD_B,errRec_B,errI_B'
- else
- return 'errG_A,errD_A,errRec_A,errG_B,errD_B,errRec_B'
- end
-end
-
-function CycleGANModel:UpdateLearningRate(opt)
- local lrd = opt.lr / opt.niter_decay
- local old_lr = self.optimStateD_A['learningRate']
- local lr = old_lr - lrd
- self.optimStateD_A['learningRate'] = lr
- self.optimStateD_B['learningRate'] = lr
- self.optimStateG_A['learningRate'] = lr
- self.optimStateG_B['learningRate'] = lr
- print(('update learning rate: %f -> %f'):format(old_lr, lr))
-end
-
-local function MakeIm3(im)
- if im:size(2) == 1 then
- local im3 = torch.repeatTensor(im, 1,3,1,1)
- return im3
- else
- return im
- end
-end
-
-function CycleGANModel:GetCurrentVisuals(opt, size)
- local visuals = {}
- table.insert(visuals, {img=MakeIm3(self.real_A), label='real_A'})
- table.insert(visuals, {img=MakeIm3(self.fake_B), label='fake_B'})
- table.insert(visuals, {img=MakeIm3(self.rec_A), label='rec_A'})
- if opt.test == 0 and opt.lambda_identity > 0 then
- table.insert(visuals, {img=MakeIm3(self.identity_A), label='identity_A'})
- end
- table.insert(visuals, {img=MakeIm3(self.real_B), label='real_B'})
- table.insert(visuals, {img=MakeIm3(self.fake_A), label='fake_A'})
- table.insert(visuals, {img=MakeIm3(self.rec_B), label='rec_B'})
- if opt.test == 0 and opt.lambda_identity > 0 then
- table.insert(visuals, {img=MakeIm3(self.identity_B), label='identity_B'})
- end
- return visuals
-end
diff --git a/cyclegan/models/cycle_gan_model.py b/cyclegan/models/cycle_gan_model.py
new file mode 100644
index 0000000..15bb72d
--- /dev/null
+++ b/cyclegan/models/cycle_gan_model.py
@@ -0,0 +1,194 @@
+import torch
+import itertools
+from util.image_pool import ImagePool
+from .base_model import BaseModel
+from . import networks
+
+
+class CycleGANModel(BaseModel):
+ """
+ This class implements the CycleGAN model, for learning image-to-image translation without paired data.
+
+ The model training requires '--dataset_mode unaligned' dataset.
+ By default, it uses a '--netG resnet_9blocks' ResNet generator,
+ a '--netD basic' discriminator (PatchGAN introduced by pix2pix),
+ and a least-square GANs objective ('--gan_mode lsgan').
+
+ CycleGAN paper: https://arxiv.org/pdf/1703.10593.pdf
+ """
+ @staticmethod
+ def modify_commandline_options(parser, is_train=True):
+ """Add new dataset-specific options, and rewrite default values for existing options.
+
+ Parameters:
+ parser -- original option parser
+ is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.
+
+ Returns:
+ the modified parser.
+
+ For CycleGAN, in addition to GAN losses, we introduce lambda_A, lambda_B, and lambda_identity for the following losses.
+ A (source domain), B (target domain).
+ Generators: G_A: A -> B; G_B: B -> A.
+ Discriminators: D_A: G_A(A) vs. B; D_B: G_B(B) vs. A.
+ Forward cycle loss: lambda_A * ||G_B(G_A(A)) - A|| (Eqn. (2) in the paper)
+ Backward cycle loss: lambda_B * ||G_A(G_B(B)) - B|| (Eqn. (2) in the paper)
+ Identity loss (optional): lambda_identity * (||G_A(B) - B|| * lambda_B + ||G_B(A) - A|| * lambda_A) (Sec 5.2 "Photo generation from paintings" in the paper)
+ Dropout is not used in the original CycleGAN paper.
+ """
+ parser.set_defaults(no_dropout=True) # default CycleGAN did not use dropout
+ if is_train:
+ parser.add_argument('--lambda_A', type=float, default=10.0, help='weight for cycle loss (A -> B -> A)')
+ parser.add_argument('--lambda_B', type=float, default=10.0, help='weight for cycle loss (B -> A -> B)')
+ parser.add_argument('--lambda_identity', type=float, default=0.5, help='use identity mapping. Setting lambda_identity other than 0 has an effect of scaling the weight of the identity mapping loss. For example, if the weight of the identity loss should be 10 times smaller than the weight of the reconstruction loss, please set lambda_identity = 0.1')
+
+ return parser
+
+ def __init__(self, opt):
+ """Initialize the CycleGAN class.
+
+ Parameters:
+ opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
+ """
+ BaseModel.__init__(self, opt)
+ # specify the training losses you want to print out. The training/test scripts will call
+ self.loss_names = ['D_A', 'G_A', 'cycle_A', 'idt_A', 'D_B', 'G_B', 'cycle_B', 'idt_B']
+ # specify the images you want to save/display. The training/test scripts will call
+ visual_names_A = ['real_A', 'fake_B', 'rec_A']
+ visual_names_B = ['real_B', 'fake_A', 'rec_B']
+ if self.isTrain and self.opt.lambda_identity > 0.0: # if identity loss is used, we also visualize idt_B=G_A(B) ad idt_A=G_A(B)
+ visual_names_A.append('idt_B')
+ visual_names_B.append('idt_A')
+
+ self.visual_names = visual_names_A + visual_names_B # combine visualizations for A and B
+ # specify the models you want to save to the disk. The training/test scripts will call and .
+ if self.isTrain:
+ self.model_names = ['G_A', 'G_B', 'D_A', 'D_B']
+ else: # during test time, only load Gs
+ self.model_names = ['G_A', 'G_B']
+
+ # define networks (both Generators and discriminators)
+ # The naming is different from those used in the paper.
+ # Code (vs. paper): G_A (G), G_B (F), D_A (D_Y), D_B (D_X)
+ self.netG_A = networks.define_G(opt.input_nc, opt.output_nc, opt.ngf, opt.netG, opt.norm,
+ not opt.no_dropout, opt.init_type, opt.init_gain, self.gpu_ids)
+ self.netG_B = networks.define_G(opt.output_nc, opt.input_nc, opt.ngf, opt.netG, opt.norm,
+ not opt.no_dropout, opt.init_type, opt.init_gain, self.gpu_ids)
+
+ if self.isTrain: # define discriminators
+ self.netD_A = networks.define_D(opt.output_nc, opt.ndf, opt.netD,
+ opt.n_layers_D, opt.norm, opt.init_type, opt.init_gain, self.gpu_ids)
+ self.netD_B = networks.define_D(opt.input_nc, opt.ndf, opt.netD,
+ opt.n_layers_D, opt.norm, opt.init_type, opt.init_gain, self.gpu_ids)
+
+ if self.isTrain:
+ if opt.lambda_identity > 0.0: # only works when input and output images have the same number of channels
+ assert(opt.input_nc == opt.output_nc)
+ self.fake_A_pool = ImagePool(opt.pool_size) # create image buffer to store previously generated images
+ self.fake_B_pool = ImagePool(opt.pool_size) # create image buffer to store previously generated images
+ # define loss functions
+ self.criterionGAN = networks.GANLoss(opt.gan_mode).to(self.device) # define GAN loss.
+ self.criterionCycle = torch.nn.L1Loss()
+ self.criterionIdt = torch.nn.L1Loss()
+ # initialize optimizers; schedulers will be automatically created by function .
+ self.optimizer_G = torch.optim.Adam(itertools.chain(self.netG_A.parameters(), self.netG_B.parameters()), lr=opt.lr, betas=(opt.beta1, 0.999))
+ self.optimizer_D = torch.optim.Adam(itertools.chain(self.netD_A.parameters(), self.netD_B.parameters()), lr=opt.lr, betas=(opt.beta1, 0.999))
+ self.optimizers.append(self.optimizer_G)
+ self.optimizers.append(self.optimizer_D)
+
+ def set_input(self, input):
+ """Unpack input data from the dataloader and perform necessary pre-processing steps.
+
+ Parameters:
+ input (dict): include the data itself and its metadata information.
+
+ The option 'direction' can be used to swap domain A and domain B.
+ """
+ AtoB = self.opt.direction == 'AtoB'
+ self.real_A = input['A' if AtoB else 'B'].to(self.device)
+ self.real_B = input['B' if AtoB else 'A'].to(self.device)
+ self.image_paths = input['A_paths' if AtoB else 'B_paths']
+
+ def forward(self):
+ """Run forward pass; called by both functions and ."""
+ self.fake_B = self.netG_A(self.real_A) # G_A(A)
+ self.rec_A = self.netG_B(self.fake_B) # G_B(G_A(A))
+ self.fake_A = self.netG_B(self.real_B) # G_B(B)
+ self.rec_B = self.netG_A(self.fake_A) # G_A(G_B(B))
+
+ def backward_D_basic(self, netD, real, fake):
+ """Calculate GAN loss for the discriminator
+
+ Parameters:
+ netD (network) -- the discriminator D
+ real (tensor array) -- real images
+ fake (tensor array) -- images generated by a generator
+
+ Return the discriminator loss.
+ We also call loss_D.backward() to calculate the gradients.
+ """
+ # Real
+ pred_real = netD(real)
+ loss_D_real = self.criterionGAN(pred_real, True)
+ # Fake
+ pred_fake = netD(fake.detach())
+ loss_D_fake = self.criterionGAN(pred_fake, False)
+ # Combined loss and calculate gradients
+ loss_D = (loss_D_real + loss_D_fake) * 0.5
+ loss_D.backward()
+ return loss_D
+
+ def backward_D_A(self):
+ """Calculate GAN loss for discriminator D_A"""
+ fake_B = self.fake_B_pool.query(self.fake_B)
+ self.loss_D_A = self.backward_D_basic(self.netD_A, self.real_B, fake_B)
+
+ def backward_D_B(self):
+ """Calculate GAN loss for discriminator D_B"""
+ fake_A = self.fake_A_pool.query(self.fake_A)
+ self.loss_D_B = self.backward_D_basic(self.netD_B, self.real_A, fake_A)
+
+ def backward_G(self):
+ """Calculate the loss for generators G_A and G_B"""
+ lambda_idt = self.opt.lambda_identity
+ lambda_A = self.opt.lambda_A
+ lambda_B = self.opt.lambda_B
+ # Identity loss
+ if lambda_idt > 0:
+ # G_A should be identity if real_B is fed: ||G_A(B) - B||
+ self.idt_A = self.netG_A(self.real_B)
+ self.loss_idt_A = self.criterionIdt(self.idt_A, self.real_B) * lambda_B * lambda_idt
+ # G_B should be identity if real_A is fed: ||G_B(A) - A||
+ self.idt_B = self.netG_B(self.real_A)
+ self.loss_idt_B = self.criterionIdt(self.idt_B, self.real_A) * lambda_A * lambda_idt
+ else:
+ self.loss_idt_A = 0
+ self.loss_idt_B = 0
+
+ # GAN loss D_A(G_A(A))
+ self.loss_G_A = self.criterionGAN(self.netD_A(self.fake_B), True)
+ # GAN loss D_B(G_B(B))
+ self.loss_G_B = self.criterionGAN(self.netD_B(self.fake_A), True)
+ # Forward cycle loss || G_B(G_A(A)) - A||
+ self.loss_cycle_A = self.criterionCycle(self.rec_A, self.real_A) * lambda_A
+ # Backward cycle loss || G_A(G_B(B)) - B||
+ self.loss_cycle_B = self.criterionCycle(self.rec_B, self.real_B) * lambda_B
+ # combined loss and calculate gradients
+ self.loss_G = self.loss_G_A + self.loss_G_B + self.loss_cycle_A + self.loss_cycle_B + self.loss_idt_A + self.loss_idt_B
+ self.loss_G.backward()
+
+ def optimize_parameters(self):
+ """Calculate losses, gradients, and update network weights; called in every training iteration"""
+ # forward
+ self.forward() # compute fake images and reconstruction images.
+ # G_A and G_B
+ self.set_requires_grad([self.netD_A, self.netD_B], False) # Ds require no gradients when optimizing Gs
+ self.optimizer_G.zero_grad() # set G_A and G_B's gradients to zero
+ self.backward_G() # calculate gradients for G_A and G_B
+ self.optimizer_G.step() # update G_A and G_B's weights
+ # D_A and D_B
+ self.set_requires_grad([self.netD_A, self.netD_B], True)
+ self.optimizer_D.zero_grad() # set D_A and D_B's gradients to zero
+ self.backward_D_A() # calculate gradients for D_A
+ self.backward_D_B() # calculate graidents for D_B
+ self.optimizer_D.step() # update D_A and D_B's weights
diff --git a/cyclegan/models/networks.py b/cyclegan/models/networks.py
new file mode 100644
index 0000000..f516ced
--- /dev/null
+++ b/cyclegan/models/networks.py
@@ -0,0 +1,615 @@
+import torch
+import torch.nn as nn
+from torch.nn import init
+import functools
+from torch.optim import lr_scheduler
+
+
+###############################################################################
+# Helper Functions
+###############################################################################
+
+
+class Identity(nn.Module):
+ def forward(self, x):
+ return x
+
+
+def get_norm_layer(norm_type='instance'):
+ """Return a normalization layer
+
+ Parameters:
+ norm_type (str) -- the name of the normalization layer: batch | instance | none
+
+ For BatchNorm, we use learnable affine parameters and track running statistics (mean/stddev).
+ For InstanceNorm, we do not use learnable affine parameters. We do not track running statistics.
+ """
+ if norm_type == 'batch':
+ norm_layer = functools.partial(nn.BatchNorm2d, affine=True, track_running_stats=True)
+ elif norm_type == 'instance':
+ norm_layer = functools.partial(nn.InstanceNorm2d, affine=False, track_running_stats=False)
+ elif norm_type == 'none':
+ def norm_layer(x): return Identity()
+ else:
+ raise NotImplementedError('normalization layer [%s] is not found' % norm_type)
+ return norm_layer
+
+
+def get_scheduler(optimizer, opt):
+ """Return a learning rate scheduler
+
+ Parameters:
+ optimizer -- the optimizer of the network
+ opt (option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions.
+ opt.lr_policy is the name of learning rate policy: linear | step | plateau | cosine
+
+ For 'linear', we keep the same learning rate for the first epochs
+ and linearly decay the rate to zero over the next epochs.
+ For other schedulers (step, plateau, and cosine), we use the default PyTorch schedulers.
+ See https://pytorch.org/docs/stable/optim.html for more details.
+ """
+ if opt.lr_policy == 'linear':
+ def lambda_rule(epoch):
+ lr_l = 1.0 - max(0, epoch + opt.epoch_count - opt.n_epochs) / float(opt.n_epochs_decay + 1)
+ return lr_l
+ scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule)
+ elif opt.lr_policy == 'step':
+ scheduler = lr_scheduler.StepLR(optimizer, step_size=opt.lr_decay_iters, gamma=0.1)
+ elif opt.lr_policy == 'plateau':
+ scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.2, threshold=0.01, patience=5)
+ elif opt.lr_policy == 'cosine':
+ scheduler = lr_scheduler.CosineAnnealingLR(optimizer, T_max=opt.n_epochs, eta_min=0)
+ else:
+ return NotImplementedError('learning rate policy [%s] is not implemented', opt.lr_policy)
+ return scheduler
+
+
+def init_weights(net, init_type='normal', init_gain=0.02):
+ """Initialize network weights.
+
+ Parameters:
+ net (network) -- network to be initialized
+ init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
+ init_gain (float) -- scaling factor for normal, xavier and orthogonal.
+
+ We use 'normal' in the original pix2pix and CycleGAN paper. But xavier and kaiming might
+ work better for some applications. Feel free to try yourself.
+ """
+ def init_func(m): # define the initialization function
+ classname = m.__class__.__name__
+ if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
+ if init_type == 'normal':
+ init.normal_(m.weight.data, 0.0, init_gain)
+ elif init_type == 'xavier':
+ init.xavier_normal_(m.weight.data, gain=init_gain)
+ elif init_type == 'kaiming':
+ init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
+ elif init_type == 'orthogonal':
+ init.orthogonal_(m.weight.data, gain=init_gain)
+ else:
+ raise NotImplementedError('initialization method [%s] is not implemented' % init_type)
+ if hasattr(m, 'bias') and m.bias is not None:
+ init.constant_(m.bias.data, 0.0)
+ elif classname.find('BatchNorm2d') != -1: # BatchNorm Layer's weight is not a matrix; only normal distribution applies.
+ init.normal_(m.weight.data, 1.0, init_gain)
+ init.constant_(m.bias.data, 0.0)
+
+ print('initialize network with %s' % init_type)
+ net.apply(init_func) # apply the initialization function
+
+
+def init_net(net, init_type='normal', init_gain=0.02, gpu_ids=[]):
+ """Initialize a network: 1. register CPU/GPU device (with multi-GPU support); 2. initialize the network weights
+ Parameters:
+ net (network) -- the network to be initialized
+ init_type (str) -- the name of an initialization method: normal | xavier | kaiming | orthogonal
+ gain (float) -- scaling factor for normal, xavier and orthogonal.
+ gpu_ids (int list) -- which GPUs the network runs on: e.g., 0,1,2
+
+ Return an initialized network.
+ """
+ if len(gpu_ids) > 0:
+ assert(torch.cuda.is_available())
+ net.to(gpu_ids[0])
+ net = torch.nn.DataParallel(net, gpu_ids) # multi-GPUs
+ init_weights(net, init_type, init_gain=init_gain)
+ return net
+
+
+def define_G(input_nc, output_nc, ngf, netG, norm='batch', use_dropout=False, init_type='normal', init_gain=0.02, gpu_ids=[]):
+ """Create a generator
+
+ Parameters:
+ input_nc (int) -- the number of channels in input images
+ output_nc (int) -- the number of channels in output images
+ ngf (int) -- the number of filters in the last conv layer
+ netG (str) -- the architecture's name: resnet_9blocks | resnet_6blocks | unet_256 | unet_128
+ norm (str) -- the name of normalization layers used in the network: batch | instance | none
+ use_dropout (bool) -- if use dropout layers.
+ init_type (str) -- the name of our initialization method.
+ init_gain (float) -- scaling factor for normal, xavier and orthogonal.
+ gpu_ids (int list) -- which GPUs the network runs on: e.g., 0,1,2
+
+ Returns a generator
+
+ Our current implementation provides two types of generators:
+ U-Net: [unet_128] (for 128x128 input images) and [unet_256] (for 256x256 input images)
+ The original U-Net paper: https://arxiv.org/abs/1505.04597
+
+ Resnet-based generator: [resnet_6blocks] (with 6 Resnet blocks) and [resnet_9blocks] (with 9 Resnet blocks)
+ Resnet-based generator consists of several Resnet blocks between a few downsampling/upsampling operations.
+ We adapt Torch code from Justin Johnson's neural style transfer project (https://github.com/jcjohnson/fast-neural-style).
+
+
+ The generator has been initialized by . It uses RELU for non-linearity.
+ """
+ net = None
+ norm_layer = get_norm_layer(norm_type=norm)
+
+ if netG == 'resnet_9blocks':
+ net = ResnetGenerator(input_nc, output_nc, ngf, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=9)
+ elif netG == 'resnet_6blocks':
+ net = ResnetGenerator(input_nc, output_nc, ngf, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=6)
+ elif netG == 'unet_128':
+ net = UnetGenerator(input_nc, output_nc, 7, ngf, norm_layer=norm_layer, use_dropout=use_dropout)
+ elif netG == 'unet_256':
+ net = UnetGenerator(input_nc, output_nc, 8, ngf, norm_layer=norm_layer, use_dropout=use_dropout)
+ else:
+ raise NotImplementedError('Generator model name [%s] is not recognized' % netG)
+ return init_net(net, init_type, init_gain, gpu_ids)
+
+
+def define_D(input_nc, ndf, netD, n_layers_D=3, norm='batch', init_type='normal', init_gain=0.02, gpu_ids=[]):
+ """Create a discriminator
+
+ Parameters:
+ input_nc (int) -- the number of channels in input images
+ ndf (int) -- the number of filters in the first conv layer
+ netD (str) -- the architecture's name: basic | n_layers | pixel
+ n_layers_D (int) -- the number of conv layers in the discriminator; effective when netD=='n_layers'
+ norm (str) -- the type of normalization layers used in the network.
+ init_type (str) -- the name of the initialization method.
+ init_gain (float) -- scaling factor for normal, xavier and orthogonal.
+ gpu_ids (int list) -- which GPUs the network runs on: e.g., 0,1,2
+
+ Returns a discriminator
+
+ Our current implementation provides three types of discriminators:
+ [basic]: 'PatchGAN' classifier described in the original pix2pix paper.
+ It can classify whether 70×70 overlapping patches are real or fake.
+ Such a patch-level discriminator architecture has fewer parameters
+ than a full-image discriminator and can work on arbitrarily-sized images
+ in a fully convolutional fashion.
+
+ [n_layers]: With this mode, you cna specify the number of conv layers in the discriminator
+ with the parameter (default=3 as used in [basic] (PatchGAN).)
+
+ [pixel]: 1x1 PixelGAN discriminator can classify whether a pixel is real or not.
+ It encourages greater color diversity but has no effect on spatial statistics.
+
+ The discriminator has been initialized by . It uses Leakly RELU for non-linearity.
+ """
+ net = None
+ norm_layer = get_norm_layer(norm_type=norm)
+
+ if netD == 'basic': # default PatchGAN classifier
+ net = NLayerDiscriminator(input_nc, ndf, n_layers=3, norm_layer=norm_layer)
+ elif netD == 'n_layers': # more options
+ net = NLayerDiscriminator(input_nc, ndf, n_layers_D, norm_layer=norm_layer)
+ elif netD == 'pixel': # classify if each pixel is real or fake
+ net = PixelDiscriminator(input_nc, ndf, norm_layer=norm_layer)
+ else:
+ raise NotImplementedError('Discriminator model name [%s] is not recognized' % netD)
+ return init_net(net, init_type, init_gain, gpu_ids)
+
+
+##############################################################################
+# Classes
+##############################################################################
+class GANLoss(nn.Module):
+ """Define different GAN objectives.
+
+ The GANLoss class abstracts away the need to create the target label tensor
+ that has the same size as the input.
+ """
+
+ def __init__(self, gan_mode, target_real_label=1.0, target_fake_label=0.0):
+ """ Initialize the GANLoss class.
+
+ Parameters:
+ gan_mode (str) - - the type of GAN objective. It currently supports vanilla, lsgan, and wgangp.
+ target_real_label (bool) - - label for a real image
+ target_fake_label (bool) - - label of a fake image
+
+ Note: Do not use sigmoid as the last layer of Discriminator.
+ LSGAN needs no sigmoid. vanilla GANs will handle it with BCEWithLogitsLoss.
+ """
+ super(GANLoss, self).__init__()
+ self.register_buffer('real_label', torch.tensor(target_real_label))
+ self.register_buffer('fake_label', torch.tensor(target_fake_label))
+ self.gan_mode = gan_mode
+ if gan_mode == 'lsgan':
+ self.loss = nn.MSELoss()
+ elif gan_mode == 'vanilla':
+ self.loss = nn.BCEWithLogitsLoss()
+ elif gan_mode in ['wgangp']:
+ self.loss = None
+ else:
+ raise NotImplementedError('gan mode %s not implemented' % gan_mode)
+
+ def get_target_tensor(self, prediction, target_is_real):
+ """Create label tensors with the same size as the input.
+
+ Parameters:
+ prediction (tensor) - - tpyically the prediction from a discriminator
+ target_is_real (bool) - - if the ground truth label is for real images or fake images
+
+ Returns:
+ A label tensor filled with ground truth label, and with the size of the input
+ """
+
+ if target_is_real:
+ target_tensor = self.real_label
+ else:
+ target_tensor = self.fake_label
+ return target_tensor.expand_as(prediction)
+
+ def __call__(self, prediction, target_is_real):
+ """Calculate loss given Discriminator's output and grount truth labels.
+
+ Parameters:
+ prediction (tensor) - - tpyically the prediction output from a discriminator
+ target_is_real (bool) - - if the ground truth label is for real images or fake images
+
+ Returns:
+ the calculated loss.
+ """
+ if self.gan_mode in ['lsgan', 'vanilla']:
+ target_tensor = self.get_target_tensor(prediction, target_is_real)
+ loss = self.loss(prediction, target_tensor)
+ elif self.gan_mode == 'wgangp':
+ if target_is_real:
+ loss = -prediction.mean()
+ else:
+ loss = prediction.mean()
+ return loss
+
+
+def cal_gradient_penalty(netD, real_data, fake_data, device, type='mixed', constant=1.0, lambda_gp=10.0):
+ """Calculate the gradient penalty loss, used in WGAN-GP paper https://arxiv.org/abs/1704.00028
+
+ Arguments:
+ netD (network) -- discriminator network
+ real_data (tensor array) -- real images
+ fake_data (tensor array) -- generated images from the generator
+ device (str) -- GPU / CPU: from torch.device('cuda:{}'.format(self.gpu_ids[0])) if self.gpu_ids else torch.device('cpu')
+ type (str) -- if we mix real and fake data or not [real | fake | mixed].
+ constant (float) -- the constant used in formula ( | |gradient||_2 - constant)^2
+ lambda_gp (float) -- weight for this loss
+
+ Returns the gradient penalty loss
+ """
+ if lambda_gp > 0.0:
+ if type == 'real': # either use real images, fake images, or a linear interpolation of two.
+ interpolatesv = real_data
+ elif type == 'fake':
+ interpolatesv = fake_data
+ elif type == 'mixed':
+ alpha = torch.rand(real_data.shape[0], 1, device=device)
+ alpha = alpha.expand(real_data.shape[0], real_data.nelement() // real_data.shape[0]).contiguous().view(*real_data.shape)
+ interpolatesv = alpha * real_data + ((1 - alpha) * fake_data)
+ else:
+ raise NotImplementedError('{} not implemented'.format(type))
+ interpolatesv.requires_grad_(True)
+ disc_interpolates = netD(interpolatesv)
+ gradients = torch.autograd.grad(outputs=disc_interpolates, inputs=interpolatesv,
+ grad_outputs=torch.ones(disc_interpolates.size()).to(device),
+ create_graph=True, retain_graph=True, only_inputs=True)
+ gradients = gradients[0].view(real_data.size(0), -1) # flat the data
+ gradient_penalty = (((gradients + 1e-16).norm(2, dim=1) - constant) ** 2).mean() * lambda_gp # added eps
+ return gradient_penalty, gradients
+ else:
+ return 0.0, None
+
+
+class ResnetGenerator(nn.Module):
+ """Resnet-based generator that consists of Resnet blocks between a few downsampling/upsampling operations.
+
+ We adapt Torch code and idea from Justin Johnson's neural style transfer project(https://github.com/jcjohnson/fast-neural-style)
+ """
+
+ def __init__(self, input_nc, output_nc, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False, n_blocks=6, padding_type='reflect'):
+ """Construct a Resnet-based generator
+
+ Parameters:
+ input_nc (int) -- the number of channels in input images
+ output_nc (int) -- the number of channels in output images
+ ngf (int) -- the number of filters in the last conv layer
+ norm_layer -- normalization layer
+ use_dropout (bool) -- if use dropout layers
+ n_blocks (int) -- the number of ResNet blocks
+ padding_type (str) -- the name of padding layer in conv layers: reflect | replicate | zero
+ """
+ assert(n_blocks >= 0)
+ super(ResnetGenerator, self).__init__()
+ if type(norm_layer) == functools.partial:
+ use_bias = norm_layer.func == nn.InstanceNorm2d
+ else:
+ use_bias = norm_layer == nn.InstanceNorm2d
+
+ model = [nn.ReflectionPad2d(3),
+ nn.Conv2d(input_nc, ngf, kernel_size=7, padding=0, bias=use_bias),
+ norm_layer(ngf),
+ nn.ReLU(True)]
+
+ n_downsampling = 2
+ for i in range(n_downsampling): # add downsampling layers
+ mult = 2 ** i
+ model += [nn.Conv2d(ngf * mult, ngf * mult * 2, kernel_size=3, stride=2, padding=1, bias=use_bias),
+ norm_layer(ngf * mult * 2),
+ nn.ReLU(True)]
+
+ mult = 2 ** n_downsampling
+ for i in range(n_blocks): # add ResNet blocks
+
+ model += [ResnetBlock(ngf * mult, padding_type=padding_type, norm_layer=norm_layer, use_dropout=use_dropout, use_bias=use_bias)]
+
+ for i in range(n_downsampling): # add upsampling layers
+ mult = 2 ** (n_downsampling - i)
+ model += [nn.ConvTranspose2d(ngf * mult, int(ngf * mult / 2),
+ kernel_size=3, stride=2,
+ padding=1, output_padding=1,
+ bias=use_bias),
+ norm_layer(int(ngf * mult / 2)),
+ nn.ReLU(True)]
+ model += [nn.ReflectionPad2d(3)]
+ model += [nn.Conv2d(ngf, output_nc, kernel_size=7, padding=0)]
+ model += [nn.Tanh()]
+
+ self.model = nn.Sequential(*model)
+
+ def forward(self, input):
+ """Standard forward"""
+ return self.model(input)
+
+
+class ResnetBlock(nn.Module):
+ """Define a Resnet block"""
+
+ def __init__(self, dim, padding_type, norm_layer, use_dropout, use_bias):
+ """Initialize the Resnet block
+
+ A resnet block is a conv block with skip connections
+ We construct a conv block with build_conv_block function,
+ and implement skip connections in function.
+ Original Resnet paper: https://arxiv.org/pdf/1512.03385.pdf
+ """
+ super(ResnetBlock, self).__init__()
+ self.conv_block = self.build_conv_block(dim, padding_type, norm_layer, use_dropout, use_bias)
+
+ def build_conv_block(self, dim, padding_type, norm_layer, use_dropout, use_bias):
+ """Construct a convolutional block.
+
+ Parameters:
+ dim (int) -- the number of channels in the conv layer.
+ padding_type (str) -- the name of padding layer: reflect | replicate | zero
+ norm_layer -- normalization layer
+ use_dropout (bool) -- if use dropout layers.
+ use_bias (bool) -- if the conv layer uses bias or not
+
+ Returns a conv block (with a conv layer, a normalization layer, and a non-linearity layer (ReLU))
+ """
+ conv_block = []
+ p = 0
+ if padding_type == 'reflect':
+ conv_block += [nn.ReflectionPad2d(1)]
+ elif padding_type == 'replicate':
+ conv_block += [nn.ReplicationPad2d(1)]
+ elif padding_type == 'zero':
+ p = 1
+ else:
+ raise NotImplementedError('padding [%s] is not implemented' % padding_type)
+
+ conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias), norm_layer(dim), nn.ReLU(True)]
+ if use_dropout:
+ conv_block += [nn.Dropout(0.5)]
+
+ p = 0
+ if padding_type == 'reflect':
+ conv_block += [nn.ReflectionPad2d(1)]
+ elif padding_type == 'replicate':
+ conv_block += [nn.ReplicationPad2d(1)]
+ elif padding_type == 'zero':
+ p = 1
+ else:
+ raise NotImplementedError('padding [%s] is not implemented' % padding_type)
+ conv_block += [nn.Conv2d(dim, dim, kernel_size=3, padding=p, bias=use_bias), norm_layer(dim)]
+
+ return nn.Sequential(*conv_block)
+
+ def forward(self, x):
+ """Forward function (with skip connections)"""
+ out = x + self.conv_block(x) # add skip connections
+ return out
+
+
+class UnetGenerator(nn.Module):
+ """Create a Unet-based generator"""
+
+ def __init__(self, input_nc, output_nc, num_downs, ngf=64, norm_layer=nn.BatchNorm2d, use_dropout=False):
+ """Construct a Unet generator
+ Parameters:
+ input_nc (int) -- the number of channels in input images
+ output_nc (int) -- the number of channels in output images
+ num_downs (int) -- the number of downsamplings in UNet. For example, # if |num_downs| == 7,
+ image of size 128x128 will become of size 1x1 # at the bottleneck
+ ngf (int) -- the number of filters in the last conv layer
+ norm_layer -- normalization layer
+
+ We construct the U-Net from the innermost layer to the outermost layer.
+ It is a recursive process.
+ """
+ super(UnetGenerator, self).__init__()
+ # construct unet structure
+ unet_block = UnetSkipConnectionBlock(ngf * 8, ngf * 8, input_nc=None, submodule=None, norm_layer=norm_layer, innermost=True) # add the innermost layer
+ for i in range(num_downs - 5): # add intermediate layers with ngf * 8 filters
+ unet_block = UnetSkipConnectionBlock(ngf * 8, ngf * 8, input_nc=None, submodule=unet_block, norm_layer=norm_layer, use_dropout=use_dropout)
+ # gradually reduce the number of filters from ngf * 8 to ngf
+ unet_block = UnetSkipConnectionBlock(ngf * 4, ngf * 8, input_nc=None, submodule=unet_block, norm_layer=norm_layer)
+ unet_block = UnetSkipConnectionBlock(ngf * 2, ngf * 4, input_nc=None, submodule=unet_block, norm_layer=norm_layer)
+ unet_block = UnetSkipConnectionBlock(ngf, ngf * 2, input_nc=None, submodule=unet_block, norm_layer=norm_layer)
+ self.model = UnetSkipConnectionBlock(output_nc, ngf, input_nc=input_nc, submodule=unet_block, outermost=True, norm_layer=norm_layer) # add the outermost layer
+
+ def forward(self, input):
+ """Standard forward"""
+ return self.model(input)
+
+
+class UnetSkipConnectionBlock(nn.Module):
+ """Defines the Unet submodule with skip connection.
+ X -------------------identity----------------------
+ |-- downsampling -- |submodule| -- upsampling --|
+ """
+
+ def __init__(self, outer_nc, inner_nc, input_nc=None,
+ submodule=None, outermost=False, innermost=False, norm_layer=nn.BatchNorm2d, use_dropout=False):
+ """Construct a Unet submodule with skip connections.
+
+ Parameters:
+ outer_nc (int) -- the number of filters in the outer conv layer
+ inner_nc (int) -- the number of filters in the inner conv layer
+ input_nc (int) -- the number of channels in input images/features
+ submodule (UnetSkipConnectionBlock) -- previously defined submodules
+ outermost (bool) -- if this module is the outermost module
+ innermost (bool) -- if this module is the innermost module
+ norm_layer -- normalization layer
+ use_dropout (bool) -- if use dropout layers.
+ """
+ super(UnetSkipConnectionBlock, self).__init__()
+ self.outermost = outermost
+ if type(norm_layer) == functools.partial:
+ use_bias = norm_layer.func == nn.InstanceNorm2d
+ else:
+ use_bias = norm_layer == nn.InstanceNorm2d
+ if input_nc is None:
+ input_nc = outer_nc
+ downconv = nn.Conv2d(input_nc, inner_nc, kernel_size=4,
+ stride=2, padding=1, bias=use_bias)
+ downrelu = nn.LeakyReLU(0.2, True)
+ downnorm = norm_layer(inner_nc)
+ uprelu = nn.ReLU(True)
+ upnorm = norm_layer(outer_nc)
+
+ if outermost:
+ upconv = nn.ConvTranspose2d(inner_nc * 2, outer_nc,
+ kernel_size=4, stride=2,
+ padding=1)
+ down = [downconv]
+ up = [uprelu, upconv, nn.Tanh()]
+ model = down + [submodule] + up
+ elif innermost:
+ upconv = nn.ConvTranspose2d(inner_nc, outer_nc,
+ kernel_size=4, stride=2,
+ padding=1, bias=use_bias)
+ down = [downrelu, downconv]
+ up = [uprelu, upconv, upnorm]
+ model = down + up
+ else:
+ upconv = nn.ConvTranspose2d(inner_nc * 2, outer_nc,
+ kernel_size=4, stride=2,
+ padding=1, bias=use_bias)
+ down = [downrelu, downconv, downnorm]
+ up = [uprelu, upconv, upnorm]
+
+ if use_dropout:
+ model = down + [submodule] + up + [nn.Dropout(0.5)]
+ else:
+ model = down + [submodule] + up
+
+ self.model = nn.Sequential(*model)
+
+ def forward(self, x):
+ if self.outermost:
+ return self.model(x)
+ else: # add skip connections
+ return torch.cat([x, self.model(x)], 1)
+
+
+class NLayerDiscriminator(nn.Module):
+ """Defines a PatchGAN discriminator"""
+
+ def __init__(self, input_nc, ndf=64, n_layers=3, norm_layer=nn.BatchNorm2d):
+ """Construct a PatchGAN discriminator
+
+ Parameters:
+ input_nc (int) -- the number of channels in input images
+ ndf (int) -- the number of filters in the last conv layer
+ n_layers (int) -- the number of conv layers in the discriminator
+ norm_layer -- normalization layer
+ """
+ super(NLayerDiscriminator, self).__init__()
+ if type(norm_layer) == functools.partial: # no need to use bias as BatchNorm2d has affine parameters
+ use_bias = norm_layer.func == nn.InstanceNorm2d
+ else:
+ use_bias = norm_layer == nn.InstanceNorm2d
+
+ kw = 4
+ padw = 1
+ sequence = [nn.Conv2d(input_nc, ndf, kernel_size=kw, stride=2, padding=padw), nn.LeakyReLU(0.2, True)]
+ nf_mult = 1
+ nf_mult_prev = 1
+ for n in range(1, n_layers): # gradually increase the number of filters
+ nf_mult_prev = nf_mult
+ nf_mult = min(2 ** n, 8)
+ sequence += [
+ nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=2, padding=padw, bias=use_bias),
+ norm_layer(ndf * nf_mult),
+ nn.LeakyReLU(0.2, True)
+ ]
+
+ nf_mult_prev = nf_mult
+ nf_mult = min(2 ** n_layers, 8)
+ sequence += [
+ nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult, kernel_size=kw, stride=1, padding=padw, bias=use_bias),
+ norm_layer(ndf * nf_mult),
+ nn.LeakyReLU(0.2, True)
+ ]
+
+ sequence += [nn.Conv2d(ndf * nf_mult, 1, kernel_size=kw, stride=1, padding=padw)] # output 1 channel prediction map
+ self.model = nn.Sequential(*sequence)
+
+ def forward(self, input):
+ """Standard forward."""
+ return self.model(input)
+
+
+class PixelDiscriminator(nn.Module):
+ """Defines a 1x1 PatchGAN discriminator (pixelGAN)"""
+
+ def __init__(self, input_nc, ndf=64, norm_layer=nn.BatchNorm2d):
+ """Construct a 1x1 PatchGAN discriminator
+
+ Parameters:
+ input_nc (int) -- the number of channels in input images
+ ndf (int) -- the number of filters in the last conv layer
+ norm_layer -- normalization layer
+ """
+ super(PixelDiscriminator, self).__init__()
+ if type(norm_layer) == functools.partial: # no need to use bias as BatchNorm2d has affine parameters
+ use_bias = norm_layer.func == nn.InstanceNorm2d
+ else:
+ use_bias = norm_layer == nn.InstanceNorm2d
+
+ self.net = [
+ nn.Conv2d(input_nc, ndf, kernel_size=1, stride=1, padding=0),
+ nn.LeakyReLU(0.2, True),
+ nn.Conv2d(ndf, ndf * 2, kernel_size=1, stride=1, padding=0, bias=use_bias),
+ norm_layer(ndf * 2),
+ nn.LeakyReLU(0.2, True),
+ nn.Conv2d(ndf * 2, 1, kernel_size=1, stride=1, padding=0, bias=use_bias)]
+
+ self.net = nn.Sequential(*self.net)
+
+ def forward(self, input):
+ """Standard forward."""
+ return self.net(input)
diff --git a/cyclegan/models/one_direction_test_model.lua b/cyclegan/models/one_direction_test_model.lua
deleted file mode 100644
index b06c186..0000000
--- a/cyclegan/models/one_direction_test_model.lua
+++ /dev/null
@@ -1,79 +0,0 @@
-local class = require 'class'
-require 'models.base_model'
-require 'models.architectures'
-require 'util.image_pool'
-
-util = paths.dofile('../util/util.lua')
-OneDirectionTestModel = class('OneDirectionTestModel', 'BaseModel')
-
-function OneDirectionTestModel:__init(conf)
- BaseModel.__init(self, conf)
- conf = conf or {}
-end
-
-function OneDirectionTestModel:model_name()
- return 'OneDirectionTestModel'
-end
-
--- Defines models and networks
-function OneDirectionTestModel:Initialize(opt)
- -- define tensors
- self.real_A = torch.Tensor(opt.batchSize, opt.input_nc, opt.fineSize, opt.fineSize)
-
- -- load/define models
- self.netG_A = util.load_test_model('G', opt)
-
- -- setup optnet to save a bit of memory
- if opt.use_optnet == 1 then
- local optnet = require 'optnet'
- local sample_input = torch.randn(1, opt.input_nc, 2, 2)
- optnet.optimizeMemory(self.netG_A, sample_input, {inplace=true, reuseBuffers=true})
- end
-
- self:RefreshParameters()
-
- print('---------- # Learnable Parameters --------------')
- print(('G_A = %d'):format(self.parametersG_A:size(1)))
- print('------------------------------------------------')
-end
-
--- Runs the forward pass of the network and
--- saves the result to member variables of the class
-function OneDirectionTestModel:Forward(input, opt)
- if opt.which_direction == 'BtoA' then
- input.real_A = input.real_B:clone()
- end
-
- self.real_A = input.real_A:clone()
- if opt.gpu > 0 then
- self.real_A = self.real_A:cuda()
- end
-
- self.fake_B = self.netG_A:forward(self.real_A):clone()
-end
-
-function OneDirectionTestModel:RefreshParameters()
- self.parametersG_A, self.gradparametersG_A = nil, nil
- self.parametersG_A, self.gradparametersG_A = self.netG_A:getParameters()
-end
-
-
-local function MakeIm3(im)
- if im:size(2) == 1 then
- local im3 = torch.repeatTensor(im, 1,3,1,1)
- return im3
- else
- return im
- end
-end
-
-function OneDirectionTestModel:GetCurrentVisuals(opt, size)
- if not size then
- size = opt.display_winsize
- end
-
- local visuals = {}
- table.insert(visuals, {img=MakeIm3(self.real_A), label='real_A'})
- table.insert(visuals, {img=MakeIm3(self.fake_B), label='fake_B'})
- return visuals
-end
diff --git a/cyclegan/models/pix2pix_model.lua b/cyclegan/models/pix2pix_model.lua
deleted file mode 100644
index 0629937..0000000
--- a/cyclegan/models/pix2pix_model.lua
+++ /dev/null
@@ -1,228 +0,0 @@
-local class = require 'class'
-require 'models.base_model'
-require 'models.architectures'
-require 'util.image_pool'
-util = paths.dofile('../util/util.lua')
-Pix2PixModel = class('Pix2PixModel', 'BaseModel')
-
-function Pix2PixModel:__init(conf)
- conf = conf or {}
-end
-
--- Returns the name of the model
-function Pix2PixModel:model_name()
- return 'Pix2PixModel'
-end
-
-function Pix2PixModel:InitializeStates()
- return {learningRate=opt.lr, beta1=opt.beta1,}
-end
-
--- Defines models and networks
-function Pix2PixModel:Initialize(opt) -- use lsgan
- -- define tensors
- local d_input_nc = opt.input_nc + opt.output_nc
- self.real_AB = torch.Tensor(opt.batchSize, d_input_nc, opt.fineSize, opt.fineSize)
- self.fake_AB = torch.Tensor(opt.batchSize, d_input_nc, opt.fineSize, opt.fineSize)
- if opt.test == 0 then
- self.fakeABPool = ImagePool(opt.pool_size)
- end
- -- load/define models
- self.criterionGAN = nn.MSECriterion()
- self.criterionL1 = nn.AbsCriterion()
-
- local netG, netD = nil, nil
- if opt.continue_train == 1 then
- if opt.test == 1 then -- only load model G for test
- netG = util.load_test_model('G', opt)
- else
- netG = util.load_model('G', opt)
- netD = util.load_model('D', opt)
- end
- else
- netG = defineG(opt.input_nc, opt.output_nc, opt.ngf, opt.which_model_netG)
- netD = defineD(d_input_nc, opt.ndf, opt.which_model_netD, opt.n_layers_D, false) -- with sigmoid
- end
-
- self.netD = netD
- self.netG = netG
-
- -- define real/fake labels
- if opt.test == 0 then
- netD_output_size = self.netD:forward(self.real_AB):size()
- self.fake_label = torch.Tensor(netD_output_size):fill(0.0)
- self.real_label = torch.Tensor(netD_output_size):fill(1.0) -- no soft smoothing
-
- self.optimStateD = self:InitializeStates()
- self.optimStateG = self:InitializeStates()
-
- self:RefreshParameters()
-
- print('---------- # Learnable Parameters --------------')
- print(('G = %d'):format(self.parametersG:size(1)))
- print(('D = %d'):format(self.parametersD:size(1)))
- print('------------------------------------------------')
- end
-
- self.A_idx = {{}, {1, opt.input_nc}, {}, {}}
- self.B_idx = {{}, {opt.input_nc+1, opt.input_nc+opt.output_nc}, {}, {}}
-end
-
--- Runs the forward pass of the network
-function Pix2PixModel:Forward(input, opt)
- if opt.which_direction == 'BtoA' then
- local temp = input.real_A
- input.real_A = input.real_B
- input.real_B = temp
- end
-
- if opt.test == 0 then
- self.real_AB[self.A_idx]:copy(input.real_A)
- self.real_AB[self.B_idx]:copy(input.real_B)
- self.real_A = self.real_AB[self.A_idx]
- self.real_B = self.real_AB[self.B_idx]
-
- self.fake_AB[self.A_idx]:copy(self.real_A)
- self.fake_B = self.netG:forward(self.real_A):clone()
- self.fake_AB[self.B_idx]:copy(self.fake_B)
- else
- if opt.gpu > 0 then
- self.real_A = input.real_A:cuda()
- self.real_B = input.real_B:cuda()
- else
- self.real_A = input.real_A:clone()
- self.real_B = input.real_B:clone()
- end
- self.fake_B = self.netG:forward(self.real_A):clone()
- end
-end
-
--- create closure to evaluate f(X) and df/dX of discriminator
-function Pix2PixModel:fDx_basic(x, gradParams, netD, netG, real, fake, opt)
- util.BiasZero(netD)
- util.BiasZero(netG)
- gradParams:zero()
-
- -- Real log(D(B))
- local output = netD:forward(real)
- local errD_real = self.criterionGAN:forward(output, self.real_label)
- local df_do = self.criterionGAN:backward(output, self.real_label)
- netD:backward(real, df_do)
- -- Fake + log(1 - D(G(A)))
- output = netD:forward(fake)
- local errD_fake = self.criterionGAN:forward(output, self.fake_label)
- local df_do2 = self.criterionGAN:backward(output, self.fake_label)
- netD:backward(fake, df_do2)
- -- calculate loss
- local errD = (errD_real + errD_fake) / 2.0
- return errD, gradParams
-end
-
-
-function Pix2PixModel:fDx(x, opt)
- fake_AB = self.fakeABPool:Query(self.fake_AB)
- self.errD, gradParams = self:fDx_basic(x, self.gradParametersD, self.netD, self.netG,
- self.real_AB, fake_AB, opt)
- return self.errD, gradParams
-end
-
-function Pix2PixModel:fGx_basic(x, netG, netD, real, fake, gradParametersG, opt)
- util.BiasZero(netG)
- util.BiasZero(netD)
- gradParametersG:zero()
-
- -- First. G(A) should fake the discriminator
- local output = netD:forward(fake)
- local errG = self.criterionGAN:forward(output, self.real_label)
- local dgan_loss_dd = self.criterionGAN:backward(output, self.real_label)
- local dgan_loss_do = netD:updateGradInput(fake, dgan_loss_dd)
-
- -- Second. G(A) should be close to the real
- real_B = real[self.B_idx]
- real_A = real[self.A_idx]
- fake_B = fake[self.B_idx]
- local errL1 = self.criterionL1:forward(fake_B, real_B) * opt.lambda_A
- local dl1_loss_do = self.criterionL1:backward(fake_B, real_B) * opt.lambda_A
- netG:backward(real_A, dgan_loss_do[self.B_idx] + dl1_loss_do)
-
- return gradParametersG, errG, errL1
-end
-
-function Pix2PixModel:fGx(x, opt)
- self.gradParametersG, self.errG, self.errL1 = self:fGx_basic(x, self.netG, self.netD,
- self.real_AB, self.fake_AB, self.gradParametersG, opt)
- return self.errG, self.gradParametersG
-end
-
--- Runs the backprop gradient descent
--- Corresponds to a single batch of data
-function Pix2PixModel:OptimizeParameters(opt)
- local fD = function(x) return self:fDx(x, opt) end
- local fG = function(x) return self:fGx(x, opt) end
- optim.adam(fD, self.parametersD, self.optimStateD)
- optim.adam(fG, self.parametersG, self.optimStateG)
-end
-
--- This function can be used to reset momentum after each epoch
-function Pix2PixModel:RefreshParameters()
- self.parametersD, self.gradParametersD = nil, nil -- nil them to avoid spiking memory
- self.parametersG, self.gradParametersG = nil, nil
-
- -- define parameters of optimization
- self.parametersG, self.gradParametersG = self.netG:getParameters()
- self.parametersD, self.gradParametersD = self.netD:getParameters()
-end
-
--- This function updates the learning rate; lr for the first opt.niter iterations; graduatlly decreases the lr to 0 for the next opt.niter_decay iterations
-function Pix2PixModel:UpdateLearningRate(opt)
- local lrd = opt.lr / opt.niter_decay
- local old_lr = self.optimStateD['learningRate']
- local lr = old_lr - lrd
- self.optimStateD['learningRate'] = lr
- self.optimStateG['learningRate'] = lr
- print(('update learning rate: %f -> %f'):format(old_lr, lr))
-end
-
-
--- Save the current model to the file system
-function Pix2PixModel:Save(prefix, opt)
- util.save_model(self.netG, prefix .. '_net_G.t7', 1.0)
- util.save_model(self.netD, prefix .. '_net_D.t7', 1.0)
-end
-
--- returns a string that describes the current errors
-function Pix2PixModel:GetCurrentErrorDescription()
- description = ('G: %.4f D: %.4f L1: %.4f'):format(
- self.errG and self.errG or -1, self.errD and self.errD or -1, self.errL1 and self.errL1 or -1)
- return description
-
-end
-
-
--- returns a string that describes the display plot configuration
-function Pix2PixModel:DisplayPlot(opt)
- return 'errG,errD,errL1'
-end
-
-
--- returns current errors
-function Pix2PixModel:GetCurrentErrors()
- local errors = {errG=self.errG, errD=self.errD, errL1=self.errL1}
- return errors
-end
-
--- returns a table of image/label pairs that describe
--- the current results.
--- |return|: a table of table. List of image/label pairs
-function Pix2PixModel:GetCurrentVisuals(opt, size)
- if not size then
- size = opt.display_winsize
- end
-
- local visuals = {}
- table.insert(visuals, {img=self.real_A, label='real_A'})
- table.insert(visuals, {img=self.fake_B, label='fake_B'})
- table.insert(visuals, {img=self.real_B, label='real_B'})
-
- return visuals
-end
diff --git a/cyclegan/models/pix2pix_model.py b/cyclegan/models/pix2pix_model.py
new file mode 100644
index 0000000..939eb88
--- /dev/null
+++ b/cyclegan/models/pix2pix_model.py
@@ -0,0 +1,127 @@
+import torch
+from .base_model import BaseModel
+from . import networks
+
+
+class Pix2PixModel(BaseModel):
+ """ This class implements the pix2pix model, for learning a mapping from input images to output images given paired data.
+
+ The model training requires '--dataset_mode aligned' dataset.
+ By default, it uses a '--netG unet256' U-Net generator,
+ a '--netD basic' discriminator (PatchGAN),
+ and a '--gan_mode' vanilla GAN loss (the cross-entropy objective used in the orignal GAN paper).
+
+ pix2pix paper: https://arxiv.org/pdf/1611.07004.pdf
+ """
+ @staticmethod
+ def modify_commandline_options(parser, is_train=True):
+ """Add new dataset-specific options, and rewrite default values for existing options.
+
+ Parameters:
+ parser -- original option parser
+ is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.
+
+ Returns:
+ the modified parser.
+
+ For pix2pix, we do not use image buffer
+ The training objective is: GAN Loss + lambda_L1 * ||G(A)-B||_1
+ By default, we use vanilla GAN loss, UNet with batchnorm, and aligned datasets.
+ """
+ # changing the default values to match the pix2pix paper (https://phillipi.github.io/pix2pix/)
+ parser.set_defaults(norm='batch', netG='unet_256', dataset_mode='aligned')
+ if is_train:
+ parser.set_defaults(pool_size=0, gan_mode='vanilla')
+ parser.add_argument('--lambda_L1', type=float, default=100.0, help='weight for L1 loss')
+
+ return parser
+
+ def __init__(self, opt):
+ """Initialize the pix2pix class.
+
+ Parameters:
+ opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
+ """
+ BaseModel.__init__(self, opt)
+ # specify the training losses you want to print out. The training/test scripts will call
+ self.loss_names = ['G_GAN', 'G_L1', 'D_real', 'D_fake']
+ # specify the images you want to save/display. The training/test scripts will call
+ self.visual_names = ['real_A', 'fake_B', 'real_B']
+ # specify the models you want to save to the disk. The training/test scripts will call and
+ if self.isTrain:
+ self.model_names = ['G', 'D']
+ else: # during test time, only load G
+ self.model_names = ['G']
+ # define networks (both generator and discriminator)
+ self.netG = networks.define_G(opt.input_nc, opt.output_nc, opt.ngf, opt.netG, opt.norm,
+ not opt.no_dropout, opt.init_type, opt.init_gain, self.gpu_ids)
+
+ if self.isTrain: # define a discriminator; conditional GANs need to take both input and output images; Therefore, #channels for D is input_nc + output_nc
+ self.netD = networks.define_D(opt.input_nc + opt.output_nc, opt.ndf, opt.netD,
+ opt.n_layers_D, opt.norm, opt.init_type, opt.init_gain, self.gpu_ids)
+
+ if self.isTrain:
+ # define loss functions
+ self.criterionGAN = networks.GANLoss(opt.gan_mode).to(self.device)
+ self.criterionL1 = torch.nn.L1Loss()
+ # initialize optimizers; schedulers will be automatically created by function .
+ self.optimizer_G = torch.optim.Adam(self.netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
+ self.optimizer_D = torch.optim.Adam(self.netD.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
+ self.optimizers.append(self.optimizer_G)
+ self.optimizers.append(self.optimizer_D)
+
+ def set_input(self, input):
+ """Unpack input data from the dataloader and perform necessary pre-processing steps.
+
+ Parameters:
+ input (dict): include the data itself and its metadata information.
+
+ The option 'direction' can be used to swap images in domain A and domain B.
+ """
+ AtoB = self.opt.direction == 'AtoB'
+ self.real_A = input['A' if AtoB else 'B'].to(self.device)
+ self.real_B = input['B' if AtoB else 'A'].to(self.device)
+ self.image_paths = input['A_paths' if AtoB else 'B_paths']
+
+ def forward(self):
+ """Run forward pass; called by both functions and ."""
+ self.fake_B = self.netG(self.real_A) # G(A)
+
+ def backward_D(self):
+ """Calculate GAN loss for the discriminator"""
+ # Fake; stop backprop to the generator by detaching fake_B
+ fake_AB = torch.cat((self.real_A, self.fake_B), 1) # we use conditional GANs; we need to feed both input and output to the discriminator
+ pred_fake = self.netD(fake_AB.detach())
+ self.loss_D_fake = self.criterionGAN(pred_fake, False)
+ # Real
+ real_AB = torch.cat((self.real_A, self.real_B), 1)
+ pred_real = self.netD(real_AB)
+ self.loss_D_real = self.criterionGAN(pred_real, True)
+ # combine loss and calculate gradients
+ self.loss_D = (self.loss_D_fake + self.loss_D_real) * 0.5
+ self.loss_D.backward()
+
+ def backward_G(self):
+ """Calculate GAN and L1 loss for the generator"""
+ # First, G(A) should fake the discriminator
+ fake_AB = torch.cat((self.real_A, self.fake_B), 1)
+ pred_fake = self.netD(fake_AB)
+ self.loss_G_GAN = self.criterionGAN(pred_fake, True)
+ # Second, G(A) = B
+ self.loss_G_L1 = self.criterionL1(self.fake_B, self.real_B) * self.opt.lambda_L1
+ # combine loss and calculate gradients
+ self.loss_G = self.loss_G_GAN + self.loss_G_L1
+ self.loss_G.backward()
+
+ def optimize_parameters(self):
+ self.forward() # compute fake images: G(A)
+ # update D
+ self.set_requires_grad(self.netD, True) # enable backprop for D
+ self.optimizer_D.zero_grad() # set D's gradients to zero
+ self.backward_D() # calculate gradients for D
+ self.optimizer_D.step() # update D's weights
+ # update G
+ self.set_requires_grad(self.netD, False) # D requires no gradients when optimizing G
+ self.optimizer_G.zero_grad() # set G's gradients to zero
+ self.backward_G() # calculate graidents for G
+ self.optimizer_G.step() # udpate G's weights
diff --git a/cyclegan/models/template_model.py b/cyclegan/models/template_model.py
new file mode 100644
index 0000000..68cdaf6
--- /dev/null
+++ b/cyclegan/models/template_model.py
@@ -0,0 +1,99 @@
+"""Model class template
+
+This module provides a template for users to implement custom models.
+You can specify '--model template' to use this model.
+The class name should be consistent with both the filename and its model option.
+The filename should be _dataset.py
+The class name should be Dataset.py
+It implements a simple image-to-image translation baseline based on regression loss.
+Given input-output pairs (data_A, data_B), it learns a network netG that can minimize the following L1 loss:
+ min_ ||netG(data_A) - data_B||_1
+You need to implement the following functions:
+ : Add model-specific options and rewrite default values for existing options.
+ <__init__>: Initialize this model class.
+ : Unpack input data and perform data pre-processing.
+ : Run forward pass. This will be called by both and .
+ : Update network weights; it will be called in every training iteration.
+"""
+import torch
+from .base_model import BaseModel
+from . import networks
+
+
+class TemplateModel(BaseModel):
+ @staticmethod
+ def modify_commandline_options(parser, is_train=True):
+ """Add new model-specific options and rewrite default values for existing options.
+
+ Parameters:
+ parser -- the option parser
+ is_train -- if it is training phase or test phase. You can use this flag to add training-specific or test-specific options.
+
+ Returns:
+ the modified parser.
+ """
+ parser.set_defaults(dataset_mode='aligned') # You can rewrite default values for this model. For example, this model usually uses aligned dataset as its dataset.
+ if is_train:
+ parser.add_argument('--lambda_regression', type=float, default=1.0, help='weight for the regression loss') # You can define new arguments for this model.
+
+ return parser
+
+ def __init__(self, opt):
+ """Initialize this model class.
+
+ Parameters:
+ opt -- training/test options
+
+ A few things can be done here.
+ - (required) call the initialization function of BaseModel
+ - define loss function, visualization images, model names, and optimizers
+ """
+ BaseModel.__init__(self, opt) # call the initialization method of BaseModel
+ # specify the training losses you want to print out. The program will call base_model.get_current_losses to plot the losses to the console and save them to the disk.
+ self.loss_names = ['loss_G']
+ # specify the images you want to save and display. The program will call base_model.get_current_visuals to save and display these images.
+ self.visual_names = ['data_A', 'data_B', 'output']
+ # specify the models you want to save to the disk. The program will call base_model.save_networks and base_model.load_networks to save and load networks.
+ # you can use opt.isTrain to specify different behaviors for training and test. For example, some networks will not be used during test, and you don't need to load them.
+ self.model_names = ['G']
+ # define networks; you can use opt.isTrain to specify different behaviors for training and test.
+ self.netG = networks.define_G(opt.input_nc, opt.output_nc, opt.ngf, opt.netG, gpu_ids=self.gpu_ids)
+ if self.isTrain: # only defined during training time
+ # define your loss functions. You can use losses provided by torch.nn such as torch.nn.L1Loss.
+ # We also provide a GANLoss class "networks.GANLoss". self.criterionGAN = networks.GANLoss().to(self.device)
+ self.criterionLoss = torch.nn.L1Loss()
+ # define and initialize optimizers. You can define one optimizer for each network.
+ # If two networks are updated at the same time, you can use itertools.chain to group them. See cycle_gan_model.py for an example.
+ self.optimizer = torch.optim.Adam(self.netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
+ self.optimizers = [self.optimizer]
+
+ # Our program will automatically call to define schedulers, load networks, and print networks
+
+ def set_input(self, input):
+ """Unpack input data from the dataloader and perform necessary pre-processing steps.
+
+ Parameters:
+ input: a dictionary that contains the data itself and its metadata information.
+ """
+ AtoB = self.opt.direction == 'AtoB' # use to swap data_A and data_B
+ self.data_A = input['A' if AtoB else 'B'].to(self.device) # get image data A
+ self.data_B = input['B' if AtoB else 'A'].to(self.device) # get image data B
+ self.image_paths = input['A_paths' if AtoB else 'B_paths'] # get image paths
+
+ def forward(self):
+ """Run forward pass. This will be called by both functions and ."""
+ self.output = self.netG(self.data_A) # generate output image given the input data_A
+
+ def backward(self):
+ """Calculate losses, gradients, and update network weights; called in every training iteration"""
+ # caculate the intermediate results if necessary; here self.output has been computed during function
+ # calculate loss given the input and intermediate results
+ self.loss_G = self.criterionLoss(self.output, self.data_B) * self.opt.lambda_regression
+ self.loss_G.backward() # calculate gradients of network G w.r.t. loss_G
+
+ def optimize_parameters(self):
+ """Update network weights; it will be called in every training iteration."""
+ self.forward() # first call forward to calculate intermediate results
+ self.optimizer.zero_grad() # clear network G's existing gradients
+ self.backward() # calculate gradients for network G
+ self.optimizer.step() # update gradients for network G
diff --git a/cyclegan/models/test_model.py b/cyclegan/models/test_model.py
new file mode 100644
index 0000000..fe15f40
--- /dev/null
+++ b/cyclegan/models/test_model.py
@@ -0,0 +1,69 @@
+from .base_model import BaseModel
+from . import networks
+
+
+class TestModel(BaseModel):
+ """ This TesteModel can be used to generate CycleGAN results for only one direction.
+ This model will automatically set '--dataset_mode single', which only loads the images from one collection.
+
+ See the test instruction for more details.
+ """
+ @staticmethod
+ def modify_commandline_options(parser, is_train=True):
+ """Add new dataset-specific options, and rewrite default values for existing options.
+
+ Parameters:
+ parser -- original option parser
+ is_train (bool) -- whether training phase or test phase. You can use this flag to add training-specific or test-specific options.
+
+ Returns:
+ the modified parser.
+
+ The model can only be used during test time. It requires '--dataset_mode single'.
+ You need to specify the network using the option '--model_suffix'.
+ """
+ assert not is_train, 'TestModel cannot be used during training time'
+ parser.set_defaults(dataset_mode='single')
+ parser.add_argument('--model_suffix', type=str, default='', help='In checkpoints_dir, [epoch]_net_G[model_suffix].pth will be loaded as the generator.')
+
+ return parser
+
+ def __init__(self, opt):
+ """Initialize the pix2pix class.
+
+ Parameters:
+ opt (Option class)-- stores all the experiment flags; needs to be a subclass of BaseOptions
+ """
+ assert(not opt.isTrain)
+ BaseModel.__init__(self, opt)
+ # specify the training losses you want to print out. The training/test scripts will call
+ self.loss_names = []
+ # specify the images you want to save/display. The training/test scripts will call
+ self.visual_names = ['real', 'fake']
+ # specify the models you want to save to the disk. The training/test scripts will call and
+ self.model_names = ['G' + opt.model_suffix] # only generator is needed.
+ self.netG = networks.define_G(opt.input_nc, opt.output_nc, opt.ngf, opt.netG,
+ opt.norm, not opt.no_dropout, opt.init_type, opt.init_gain, self.gpu_ids)
+
+ # assigns the model to self.netG_[suffix] so that it can be loaded
+ # please see
+ setattr(self, 'netG' + opt.model_suffix, self.netG) # store netG in self.
+
+ def set_input(self, input):
+ """Unpack input data from the dataloader and perform necessary pre-processing steps.
+
+ Parameters:
+ input: a dictionary that contains the data itself and its metadata information.
+
+ We need to use 'single_dataset' dataset mode. It only load images from one domain.
+ """
+ self.real = input['A'].to(self.device)
+ self.image_paths = input['A_paths']
+
+ def forward(self):
+ """Run forward pass."""
+ self.fake = self.netG(self.real) # G(real)
+
+ def optimize_parameters(self):
+ """No optimization for test model."""
+ pass
diff --git a/cyclegan/options.lua b/cyclegan/options.lua
deleted file mode 100644
index 2e4e6ca..0000000
--- a/cyclegan/options.lua
+++ /dev/null
@@ -1,143 +0,0 @@
---------------------------------------------------------------------------------
--- Configure options
---------------------------------------------------------------------------------
-
-local options = {}
--- options for train
-local opt_train = {
- DATA_ROOT = '', -- path to images (should have subfolders 'train', 'val', etc)
- batchSize = 1, -- # images in batch
- loadSize = 143, -- scale images to this size
- fineSize = 128, -- then crop to this size
- ngf = 64, -- # of gen filters in first conv layer
- ndf = 64, -- # of discrim filters in first conv layer
- input_nc = 3, -- # of input image channels
- output_nc = 3, -- # of output image channels
- niter = 100, -- # of iter at starting learning rate
- niter_decay = 100, -- # of iter to linearly decay learning rate to zero
- lr = 0.0002, -- initial learning rate for adam
- beta1 = 0.5, -- momentum term of adam
- ntrain = math.huge, -- # of examples per epoch. math.huge for full dataset
- flip = 1, -- if flip the images for data argumentation
- display_id = 10, -- display window id.
- display_winsize = 128, -- display window size
- display_freq = 25, -- display the current results every display_freq iterations
- gpu = 1, -- gpu = 0 is CPU mode. gpu=X is GPU mode on GPU X
- name = '', -- name of the experiment, should generally be passed on the command line
- which_direction = 'AtoB', -- AtoB or BtoA
- phase = 'train', -- train, val, test, etc
- nThreads = 2, -- # threads for loading data
- save_epoch_freq = 1, -- save a model every save_epoch_freq epochs (does not overwrite previously saved models)
- save_latest_freq = 5000, -- save the latest model every latest_freq sgd iterations (overwrites the previous latest model)
- print_freq = 50, -- print the debug information every print_freq iterations
- save_display_freq = 2500, -- save the current display of results every save_display_freq_iterations
- continue_train = 0, -- if continue training, load the latest model: 1: true, 0: false
- serial_batches = 0, -- if 1, takes images in order to make batches, otherwise takes them randomly
- checkpoints_dir = './checkpoints', -- models are saved here
- cache_dir = './cache', -- cache files are saved here
- cudnn = 1, -- set to 0 to not use cudnn
- which_model_netD = 'basic', -- selects model to use for netD
- which_model_netG = 'resnet_6blocks', -- selects model to use for netG
- norm = 'instance', -- batch or instance normalization
- n_layers_D = 3, -- only used if which_model_netD=='n_layers'
- content_loss = 'pixel', -- content loss type: pixel, vgg
- layer_name = 'pixel', -- layer used in content loss (e.g. relu4_2)
- lambda_A = 10.0, -- weight for cycle loss (A -> B -> A)
- lambda_B = 10.0, -- weight for cycle loss (B -> A -> B)
- model = 'cycle_gan', -- which mode to run. 'cycle_gan', 'pix2pix', 'bigan', 'content_gan'
- use_lsgan = 1, -- if 1, use least square GAN, if 0, use vanilla GAN
- align_data = 0, -- if > 0, use the dataloader for where the images are aligned
- pool_size = 50, -- the size of image buffer that stores previously generated images
- resize_or_crop = 'resize_and_crop', -- resizing/cropping strategy: resize_and_crop | crop | scale_width | scale_height
- lambda_identity = 0.5, -- use identity mapping. Setting opt.lambda_identity other than 0 has an effect of scaling the weight of the identity mapping loss. For example, if the weight of the identity loss should be 10 times smaller than the weight of the reconstruction loss, please set opt.lambda_identity = 0.1
- use_optnet = 0, -- use optnet to save GPU memory during test
-}
-
--- options for test
-local opt_test = {
- DATA_ROOT = '', -- path to images (should have subfolders 'train', 'val', etc)
- loadSize = 128, -- scale images to this size
- fineSize = 128, -- then crop to this size
- flip = 0, -- horizontal mirroring data augmentation
- display = 1, -- display samples while training. 0 = false
- display_id = 200, -- display window id.
- gpu = 1, -- gpu = 0 is CPU mode. gpu=X is GPU mode on GPU X
- how_many = 'all', -- how many test images to run (set to all to run on every image found in the data/phase folder)
- phase = 'test', -- train, val, test, etc
- aspect_ratio = 1.0, -- aspect ratio of result images
- norm = 'instance', -- batchnorm or isntance norm
- name = '', -- name of experiment, selects which model to run, should generally should be passed on command line
- input_nc = 3, -- # of input image channels
- output_nc = 3, -- # of output image channels
- serial_batches = 1, -- if 1, takes images in order to make batches, otherwise takes them randomly
- cudnn = 1, -- set to 0 to not use cudnn (untested)
- checkpoints_dir = './checkpoints', -- loads models from here
- cache_dir = './cache', -- cache files are saved here
- results_dir='./results/', -- saves results here
- which_epoch = 'latest', -- which epoch to test? set to 'latest' to use latest cached model
- model = 'cycle_gan', -- which mode to run. 'cycle_gan', 'pix2pix', 'bigan', 'content_gan'; to use pretrained model, select `one_direction_test`
- align_data = 0, -- if > 0, use the dataloader for pix2pix
- which_direction = 'AtoB', -- AtoB or BtoA
- resize_or_crop = 'resize_and_crop', -- resizing/cropping strategy: resize_and_crop | crop | scale_width | scale_height
-}
-
---------------------------------------------------------------------------------
--- util functions
---------------------------------------------------------------------------------
-function options.clone(opt)
- local copy = {}
- for orig_key, orig_value in pairs(opt) do
- copy[orig_key] = orig_value
- end
- return copy
-end
-
-function options.parse_options(mode)
- if mode == 'train' then
- opt = opt_train
- opt.test = 0
- elseif mode == 'test' then
- opt = opt_test
- opt.test = 1
- else
- print("Invalid option [" .. mode .. "]")
- return nil
- end
-
- -- one-line argument parser. parses enviroment variables to override the defaults
- for k,v in pairs(opt) do opt[k] = tonumber(os.getenv(k)) or os.getenv(k) or opt[k] end
- if mode == 'test' then
- opt.nThreads = 1
- opt.continue_train = 1
- opt.batchSize = 1 -- test code only supports batchSize=1
- end
-
- -- print by keys
- keyset = {}
- for k,v in pairs(opt) do
- table.insert(keyset, k)
- end
- table.sort(keyset)
- print("------------------- Options -------------------")
- for i,k in ipairs(keyset) do
- print(('%+25s: %s'):format(k, opt[k]))
- end
- print("-----------------------------------------------")
-
- -- save opt to checkpoints
- paths.mkdir(opt.checkpoints_dir)
- paths.mkdir(paths.concat(opt.checkpoints_dir, opt.name))
- opt.visual_dir = paths.concat(opt.checkpoints_dir, opt.name, 'visuals')
- paths.mkdir(opt.visual_dir)
- -- save opt to the disk
- fd = io.open(paths.concat(opt.checkpoints_dir, opt.name, 'opt_' .. mode .. '.txt'), 'w')
- for i,k in ipairs(keyset) do
- fd:write(("%+25s: %s\n"):format(k, opt[k]))
- end
- fd:close()
-
- return opt
-end
-
-
-return options
diff --git a/cyclegan/options/__init__.py b/cyclegan/options/__init__.py
new file mode 100644
index 0000000..e7eedeb
--- /dev/null
+++ b/cyclegan/options/__init__.py
@@ -0,0 +1 @@
+"""This package options includes option modules: training options, test options, and basic options (used in both training and test)."""
diff --git a/cyclegan/options/base_options.py b/cyclegan/options/base_options.py
new file mode 100644
index 0000000..afb5d08
--- /dev/null
+++ b/cyclegan/options/base_options.py
@@ -0,0 +1,136 @@
+import argparse
+import os
+from util import util
+import torch
+import models
+import data
+
+
+class BaseOptions():
+ """This class defines options used during both training and test time.
+
+ It also implements several helper functions such as parsing, printing, and saving the options.
+ It also gathers additional options defined in functions in both dataset class and model class.
+ """
+
+ def __init__(self):
+ """Reset the class; indicates the class hasn't been initailized"""
+ self.initialized = False
+
+ def initialize(self, parser):
+ """Define the common options that are used in both training and test."""
+ # basic parameters
+ parser.add_argument('--dataroot', required=True, help='path to images (should have subfolders trainA, trainB, valA, valB, etc)')
+ parser.add_argument('--name', type=str, default='experiment_name', help='name of the experiment. It decides where to store samples and models')
+ parser.add_argument('--gpu_ids', type=str, default='0', help='gpu ids: e.g. 0 0,1,2, 0,2. use -1 for CPU')
+ parser.add_argument('--checkpoints_dir', type=str, default='./checkpoints', help='models are saved here')
+ # model parameters
+ parser.add_argument('--model', type=str, default='cycle_gan', help='chooses which model to use. [cycle_gan | pix2pix | test | colorization]')
+ parser.add_argument('--input_nc', type=int, default=3, help='# of input image channels: 3 for RGB and 1 for grayscale')
+ parser.add_argument('--output_nc', type=int, default=3, help='# of output image channels: 3 for RGB and 1 for grayscale')
+ parser.add_argument('--ngf', type=int, default=64, help='# of gen filters in the last conv layer')
+ parser.add_argument('--ndf', type=int, default=64, help='# of discrim filters in the first conv layer')
+ parser.add_argument('--netD', type=str, default='basic', help='specify discriminator architecture [basic | n_layers | pixel]. The basic model is a 70x70 PatchGAN. n_layers allows you to specify the layers in the discriminator')
+ parser.add_argument('--netG', type=str, default='resnet_9blocks', help='specify generator architecture [resnet_9blocks | resnet_6blocks | unet_256 | unet_128]')
+ parser.add_argument('--n_layers_D', type=int, default=3, help='only used if netD==n_layers')
+ parser.add_argument('--norm', type=str, default='instance', help='instance normalization or batch normalization [instance | batch | none]')
+ parser.add_argument('--init_type', type=str, default='normal', help='network initialization [normal | xavier | kaiming | orthogonal]')
+ parser.add_argument('--init_gain', type=float, default=0.02, help='scaling factor for normal, xavier and orthogonal.')
+ parser.add_argument('--no_dropout', action='store_true', help='no dropout for the generator')
+ # dataset parameters
+ parser.add_argument('--dataset_mode', type=str, default='unaligned', help='chooses how datasets are loaded. [unaligned | aligned | single | colorization]')
+ parser.add_argument('--direction', type=str, default='AtoB', help='AtoB or BtoA')
+ parser.add_argument('--serial_batches', action='store_true', help='if true, takes images in order to make batches, otherwise takes them randomly')
+ parser.add_argument('--num_threads', default=4, type=int, help='# threads for loading data')
+ parser.add_argument('--batch_size', type=int, default=1, help='input batch size')
+ parser.add_argument('--load_size', type=int, default=286, help='scale images to this size')
+ parser.add_argument('--crop_size', type=int, default=256, help='then crop to this size')
+ parser.add_argument('--max_dataset_size', type=int, default=float("inf"), help='Maximum number of samples allowed per dataset. If the dataset directory contains more than max_dataset_size, only a subset is loaded.')
+ parser.add_argument('--preprocess', type=str, default='resize_and_crop', help='scaling and cropping of images at load time [resize_and_crop | crop | scale_width | scale_width_and_crop | none]')
+ parser.add_argument('--no_flip', action='store_true', help='if specified, do not flip the images for data augmentation')
+ parser.add_argument('--display_winsize', type=int, default=256, help='display window size for both visdom and HTML')
+ # additional parameters
+ parser.add_argument('--epoch', type=str, default='latest', help='which epoch to load? set to latest to use latest cached model')
+ parser.add_argument('--load_iter', type=int, default='0', help='which iteration to load? if load_iter > 0, the code will load models by iter_[load_iter]; otherwise, the code will load models by [epoch]')
+ parser.add_argument('--verbose', action='store_true', help='if specified, print more debugging information')
+ parser.add_argument('--suffix', default='', type=str, help='customized suffix: opt.name = opt.name + suffix: e.g., {model}_{netG}_size{load_size}')
+ self.initialized = True
+ return parser
+
+ def gather_options(self):
+ """Initialize our parser with basic options(only once).
+ Add additional model-specific and dataset-specific options.
+ These options are defined in the function
+ in model and dataset classes.
+ """
+ if not self.initialized: # check if it has been initialized
+ parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
+ parser = self.initialize(parser)
+
+ # get the basic options
+ opt, _ = parser.parse_known_args()
+
+ # modify model-related parser options
+ model_name = opt.model
+ model_option_setter = models.get_option_setter(model_name)
+ parser = model_option_setter(parser, self.isTrain)
+ opt, _ = parser.parse_known_args() # parse again with new defaults
+
+ # modify dataset-related parser options
+ dataset_name = opt.dataset_mode
+ dataset_option_setter = data.get_option_setter(dataset_name)
+ parser = dataset_option_setter(parser, self.isTrain)
+
+ # save and return the parser
+ self.parser = parser
+ return parser.parse_args()
+
+ def print_options(self, opt):
+ """Print and save options
+
+ It will print both current options and default values(if different).
+ It will save options into a text file / [checkpoints_dir] / opt.txt
+ """
+ message = ''
+ message += '----------------- Options ---------------\n'
+ for k, v in sorted(vars(opt).items()):
+ comment = ''
+ default = self.parser.get_default(k)
+ if v != default:
+ comment = '\t[default: %s]' % str(default)
+ message += '{:>25}: {:<30}{}\n'.format(str(k), str(v), comment)
+ message += '----------------- End -------------------'
+ print(message)
+
+ # save to the disk
+ expr_dir = os.path.join(opt.checkpoints_dir, opt.name)
+ util.mkdirs(expr_dir)
+ file_name = os.path.join(expr_dir, '{}_opt.txt'.format(opt.phase))
+ with open(file_name, 'wt') as opt_file:
+ opt_file.write(message)
+ opt_file.write('\n')
+
+ def parse(self):
+ """Parse our options, create checkpoints directory suffix, and set up gpu device."""
+ opt = self.gather_options()
+ opt.isTrain = self.isTrain # train or test
+
+ # process opt.suffix
+ if opt.suffix:
+ suffix = ('_' + opt.suffix.format(**vars(opt))) if opt.suffix != '' else ''
+ opt.name = opt.name + suffix
+
+ self.print_options(opt)
+
+ # set gpu ids
+ str_ids = opt.gpu_ids.split(',')
+ opt.gpu_ids = []
+ for str_id in str_ids:
+ id = int(str_id)
+ if id >= 0:
+ opt.gpu_ids.append(id)
+ if len(opt.gpu_ids) > 0:
+ torch.cuda.set_device(opt.gpu_ids[0])
+
+ self.opt = opt
+ return self.opt
diff --git a/cyclegan/options/test_options.py b/cyclegan/options/test_options.py
new file mode 100644
index 0000000..c85c996
--- /dev/null
+++ b/cyclegan/options/test_options.py
@@ -0,0 +1,24 @@
+from .base_options import BaseOptions
+
+
+class TestOptions(BaseOptions):
+ """This class includes test options.
+
+ It also includes shared options defined in BaseOptions.
+ """
+
+ def initialize(self, parser):
+ parser = BaseOptions.initialize(self, parser) # define shared options
+ parser.add_argument('--ntest', type=int, default=float("inf"), help='# of test examples.')
+ parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.')
+ parser.add_argument('--aspect_ratio', type=float, default=1.0, help='aspect ratio of result images')
+ parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc')
+ # Dropout and Batchnorm has different behavioir during training and test.
+ parser.add_argument('--eval', action='store_true', help='use eval mode during test time.')
+ parser.add_argument('--num_test', type=int, default=50, help='how many test images to run')
+ # rewrite devalue values
+ parser.set_defaults(model='test')
+ # To avoid cropping, the load_size should be the same as crop_size
+ parser.set_defaults(load_size=parser.get_default('crop_size'))
+ self.isTrain = False
+ return parser
diff --git a/cyclegan/options/train_options.py b/cyclegan/options/train_options.py
new file mode 100644
index 0000000..c8d5d2a
--- /dev/null
+++ b/cyclegan/options/train_options.py
@@ -0,0 +1,40 @@
+from .base_options import BaseOptions
+
+
+class TrainOptions(BaseOptions):
+ """This class includes training options.
+
+ It also includes shared options defined in BaseOptions.
+ """
+
+ def initialize(self, parser):
+ parser = BaseOptions.initialize(self, parser)
+ # visdom and HTML visualization parameters
+ parser.add_argument('--display_freq', type=int, default=400, help='frequency of showing training results on screen')
+ parser.add_argument('--display_ncols', type=int, default=4, help='if positive, display all images in a single visdom web panel with certain number of images per row.')
+ parser.add_argument('--display_id', type=int, default=1, help='window id of the web display')
+ parser.add_argument('--display_server', type=str, default="http://localhost", help='visdom server of the web display')
+ parser.add_argument('--display_env', type=str, default='main', help='visdom display environment name (default is "main")')
+ parser.add_argument('--display_port', type=int, default=8097, help='visdom port of the web display')
+ parser.add_argument('--update_html_freq', type=int, default=1000, help='frequency of saving training results to html')
+ parser.add_argument('--print_freq', type=int, default=100, help='frequency of showing training results on console')
+ parser.add_argument('--no_html', action='store_true', help='do not save intermediate training results to [opt.checkpoints_dir]/[opt.name]/web/')
+ # network saving and loading parameters
+ parser.add_argument('--save_latest_freq', type=int, default=5000, help='frequency of saving the latest results')
+ parser.add_argument('--save_epoch_freq', type=int, default=5, help='frequency of saving checkpoints at the end of epochs')
+ parser.add_argument('--save_by_iter', action='store_true', help='whether saves model by iteration')
+ parser.add_argument('--continue_train', action='store_true', help='continue training: load the latest model')
+ parser.add_argument('--epoch_count', type=int, default=1, help='the starting epoch count, we save the model by , +, ...')
+ parser.add_argument('--phase', type=str, default='train', help='train, val, test, etc')
+ # training parameters
+ parser.add_argument('--n_epochs', type=int, default=100, help='number of epochs with the initial learning rate')
+ parser.add_argument('--n_epochs_decay', type=int, default=100, help='number of epochs to linearly decay learning rate to zero')
+ parser.add_argument('--beta1', type=float, default=0.5, help='momentum term of adam')
+ parser.add_argument('--lr', type=float, default=0.0002, help='initial learning rate for adam')
+ parser.add_argument('--gan_mode', type=str, default='lsgan', help='the type of GAN objective. [vanilla| lsgan | wgangp]. vanilla GAN loss is the cross-entropy objective used in the original GAN paper.')
+ parser.add_argument('--pool_size', type=int, default=50, help='the size of image buffer that stores previously generated images')
+ parser.add_argument('--lr_policy', type=str, default='linear', help='learning rate policy. [linear | step | plateau | cosine]')
+ parser.add_argument('--lr_decay_iters', type=int, default=50, help='multiply by a gamma every lr_decay_iters iterations')
+
+ self.isTrain = True
+ return parser
diff --git a/cyclegan/pix2pix.ipynb b/cyclegan/pix2pix.ipynb
new file mode 100644
index 0000000..bb7030d
--- /dev/null
+++ b/cyclegan/pix2pix.ipynb
@@ -0,0 +1,265 @@
+{
+ "nbformat": 4,
+ "nbformat_minor": 0,
+ "metadata": {
+ "colab": {
+ "name": "pix2pix",
+ "provenance": [],
+ "collapsed_sections": [],
+ "include_colab_link": true
+ },
+ "kernelspec": {
+ "name": "python3",
+ "display_name": "Python 3"
+ },
+ "accelerator": "GPU"
+ },
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "view-in-github",
+ "colab_type": "text"
+ },
+ "source": [
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "7wNjDKdQy35h",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Install"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "TRm-USlsHgEV",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "Pt3igws3eiVp",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "import os\n",
+ "os.chdir('pytorch-CycleGAN-and-pix2pix/')"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "z1EySlOXwwoa",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!pip install -r requirements.txt"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "8daqlgVhw29P",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Datasets\n",
+ "\n",
+ "Download one of the official datasets with:\n",
+ "\n",
+ "- `bash ./datasets/download_pix2pix_dataset.sh [cityscapes, night2day, edges2handbags, edges2shoes, facades, maps]`\n",
+ "\n",
+ "Or use your own dataset by creating the appropriate folders and adding in the images. Follow the instructions [here](https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/datasets.md#pix2pix-datasets)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "vrdOettJxaCc",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!bash ./datasets/download_pix2pix_dataset.sh facades"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "gdUz4116xhpm",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Pretrained models\n",
+ "\n",
+ "Download one of the official pretrained models with:\n",
+ "\n",
+ "- `bash ./scripts/download_pix2pix_model.sh [edges2shoes, sat2map, map2sat, facades_label2photo, and day2night]`\n",
+ "\n",
+ "Or add your own pretrained model to `./checkpoints/{NAME}_pretrained/latest_net_G.pt`"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "GC2DEP4M0OsS",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!bash ./scripts/download_pix2pix_model.sh facades_label2photo"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "yFw1kDQBx3LN",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Training\n",
+ "\n",
+ "- `python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA`\n",
+ "\n",
+ "Change the `--dataroot` and `--name` to your own dataset's path and model's name. Use `--gpu_ids 0,1,..` to train on multiple GPUs and `--batch_size` to change the batch size. Add `--direction BtoA` if you want to train a model to transfrom from class B to A."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "0sp7TCT2x9dB",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "9UkcaFZiyASl",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Testing\n",
+ "\n",
+ "- `python test.py --dataroot ./datasets/facades --direction BtoA --model pix2pix --name facades_pix2pix`\n",
+ "\n",
+ "Change the `--dataroot`, `--name`, and `--direction` to be consistent with your trained model's configuration and how you want to transform images.\n",
+ "\n",
+ "> from https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix:\n",
+ "> Note that we specified --direction BtoA as Facades dataset's A to B direction is photos to labels.\n",
+ "\n",
+ "> 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).\n",
+ "\n",
+ "> See a list of currently available models at ./scripts/download_pix2pix_model.sh"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "mey7o6j-0368",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!ls checkpoints/"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "uCsKkEq0yGh0",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "!python test.py --dataroot ./datasets/facades --direction BtoA --model pix2pix --name facades_label2photo_pretrained"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "OzSKIPUByfiN",
+ "colab_type": "text"
+ },
+ "source": [
+ "# Visualize"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "9Mgg8raPyizq",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "import matplotlib.pyplot as plt\n",
+ "\n",
+ "img = plt.imread('./results/facades_label2photo_pretrained/test_latest/images/100_fake_B.png')\n",
+ "plt.imshow(img)"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "0G3oVH9DyqLQ",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "img = plt.imread('./results/facades_label2photo_pretrained/test_latest/images/100_real_A.png')\n",
+ "plt.imshow(img)"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ },
+ {
+ "cell_type": "code",
+ "metadata": {
+ "id": "ErK5OC1j1LH4",
+ "colab_type": "code",
+ "colab": {}
+ },
+ "source": [
+ "img = plt.imread('./results/facades_label2photo_pretrained/test_latest/images/100_real_B.png')\n",
+ "plt.imshow(img)"
+ ],
+ "execution_count": 0,
+ "outputs": []
+ }
+ ]
+}
diff --git a/cyclegan/pretrained_models/download_model.sh b/cyclegan/pretrained_models/download_model.sh
deleted file mode 100644
index 7218e4d..0000000
--- a/cyclegan/pretrained_models/download_model.sh
+++ /dev/null
@@ -1,10 +0,0 @@
-FILE=$1
-
-echo "Note: available models are apple2orange, facades_photo2label, map2sat, orange2apple, style_cezanne, style_ukiyoe, summer2winter_yosemite, zebra2horse, facades_label2photo, horse2zebra,monet2photo, sat2map, style_monet,style_vangogh, winter2summer_yosemite, iphone2dslr_flower"
-
-echo "Specified [$FILE]"
-
-mkdir -p ./checkpoints/${FILE}_pretrained
-URL=https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/models/$FILE.t7
-MODEL_FILE=./checkpoints/${FILE}_pretrained/latest_net_G.t7
-wget -N $URL -O $MODEL_FILE
diff --git a/cyclegan/pretrained_models/download_vgg.sh b/cyclegan/pretrained_models/download_vgg.sh
deleted file mode 100644
index 2f89982..0000000
--- a/cyclegan/pretrained_models/download_vgg.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-URL1=https://people.eecs.berkeley.edu/~taesung_park/projects/CycleGAN/models/places_vgg.caffemodel
-MODEL_FILE1=./models/places_vgg.caffemodel
-URL2=https://people.eecs.berkeley.edu/~taesung_park/projects/CycleGAN/models/places_vgg.prototxt
-MODEL_FILE2=./models/places_vgg.prototxt
-wget -N $URL1 -O $MODEL_FILE1
-wget -N $URL2 -O $MODEL_FILE2
diff --git a/cyclegan/requirements.txt b/cyclegan/requirements.txt
new file mode 100644
index 0000000..a3561a1
--- /dev/null
+++ b/cyclegan/requirements.txt
@@ -0,0 +1,4 @@
+torch>=0.4.1
+torchvision>=0.2.1
+dominate>=2.3.1
+visdom>=0.1.8.3
diff --git a/cyclegan/scripts/conda_deps.sh b/cyclegan/scripts/conda_deps.sh
new file mode 100644
index 0000000..72df436
--- /dev/null
+++ b/cyclegan/scripts/conda_deps.sh
@@ -0,0 +1,4 @@
+set -ex
+conda install numpy pyyaml mkl mkl-include setuptools cmake cffi typing
+conda install pytorch torchvision -c pytorch # add cuda90 if CUDA 9
+conda install visdom dominate -c conda-forge # install visdom and dominate
diff --git a/cyclegan/scripts/download_cyclegan_model.sh b/cyclegan/scripts/download_cyclegan_model.sh
new file mode 100644
index 0000000..26e198a
--- /dev/null
+++ b/cyclegan/scripts/download_cyclegan_model.sh
@@ -0,0 +1,11 @@
+FILE=$1
+
+echo "Note: available models are apple2orange, orange2apple, summer2winter_yosemite, winter2summer_yosemite, horse2zebra, zebra2horse, monet2photo, style_monet, style_cezanne, style_ukiyoe, style_vangogh, sat2map, map2sat, cityscapes_photo2label, cityscapes_label2photo, facades_photo2label, facades_label2photo, iphone2dslr_flower"
+
+echo "Specified [$FILE]"
+
+mkdir -p ./checkpoints/${FILE}_pretrained
+MODEL_FILE=./checkpoints/${FILE}_pretrained/latest_net_G.pth
+URL=http://efrosgans.eecs.berkeley.edu/cyclegan/pretrained_models/$FILE.pth
+
+wget -N $URL -O $MODEL_FILE
diff --git a/cyclegan/scripts/download_pix2pix_model.sh b/cyclegan/scripts/download_pix2pix_model.sh
new file mode 100644
index 0000000..6b21232
--- /dev/null
+++ b/cyclegan/scripts/download_pix2pix_model.sh
@@ -0,0 +1,10 @@
+FILE=$1
+
+echo "Note: available models are edges2shoes, sat2map, map2sat, facades_label2photo, and day2night"
+echo "Specified [$FILE]"
+
+mkdir -p ./checkpoints/${FILE}_pretrained
+MODEL_FILE=./checkpoints/${FILE}_pretrained/latest_net_G.pth
+URL=http://efrosgans.eecs.berkeley.edu/pix2pix/models-pytorch/$FILE.pth
+
+wget -N $URL -O $MODEL_FILE
diff --git a/cyclegan/scripts/edges/PostprocessHED.m b/cyclegan/scripts/edges/PostprocessHED.m
new file mode 100755
index 0000000..78a9910
--- /dev/null
+++ b/cyclegan/scripts/edges/PostprocessHED.m
@@ -0,0 +1,77 @@
+%%% Prerequisites
+% You need to get the cpp file edgesNmsMex.cpp from https://raw.githubusercontent.com/pdollar/edges/master/private/edgesNmsMex.cpp
+% and compile it in Matlab: mex edgesNmsMex.cpp
+% You also need to download and install Piotr's Computer Vision Matlab Toolbox: https://pdollar.github.io/toolbox/
+
+%%% parameters
+% hed_mat_dir: the hed mat file directory (the output of 'batch_hed.py')
+% edge_dir: the output HED edges directory
+% image_width: resize the edge map to [image_width, image_width]
+% threshold: threshold for image binarization (default 25.0/255.0)
+% small_edge: remove small edges (default 5)
+
+function [] = PostprocessHED(hed_mat_dir, edge_dir, image_width, threshold, small_edge)
+
+if ~exist(edge_dir, 'dir')
+ mkdir(edge_dir);
+end
+fileList = dir(fullfile(hed_mat_dir, '*.mat'));
+nFiles = numel(fileList);
+fprintf('find %d mat files\n', nFiles);
+
+for n = 1 : nFiles
+ if mod(n, 1000) == 0
+ fprintf('process %d/%d images\n', n, nFiles);
+ end
+ fileName = fileList(n).name;
+ filePath = fullfile(hed_mat_dir, fileName);
+ jpgName = strrep(fileName, '.mat', '.jpg');
+ edge_path = fullfile(edge_dir, jpgName);
+
+ if ~exist(edge_path, 'file')
+ E = GetEdge(filePath);
+ E = imresize(E,[image_width,image_width]);
+ E_simple = SimpleEdge(E, threshold, small_edge);
+ E_simple = uint8(E_simple*255);
+ imwrite(E_simple, edge_path, 'Quality',100);
+ end
+end
+end
+
+
+
+
+function [E] = GetEdge(filePath)
+load(filePath);
+E = 1-edge_predict;
+end
+
+function [E4] = SimpleEdge(E, threshold, small_edge)
+if nargin <= 1
+ threshold = 25.0/255.0;
+end
+
+if nargin <= 2
+ small_edge = 5;
+end
+
+if ndims(E) == 3
+ E = E(:,:,1);
+end
+
+E1 = 1 - E;
+E2 = EdgeNMS(E1);
+E3 = double(E2>=max(eps,threshold));
+E3 = bwmorph(E3,'thin',inf);
+E4 = bwareaopen(E3, small_edge);
+E4=1-E4;
+end
+
+function [E_nms] = EdgeNMS( E )
+E=single(E);
+[Ox,Oy] = gradient2(convTri(E,4));
+[Oxx,~] = gradient2(Ox);
+[Oxy,Oyy] = gradient2(Oy);
+O = mod(atan(Oyy.*sign(-Oxy)./(Oxx+1e-5)),pi);
+E_nms = edgesNmsMex(E,O,1,5,1.01,1);
+end
diff --git a/cyclegan/scripts/edges/batch_hed.py b/cyclegan/scripts/edges/batch_hed.py
new file mode 100755
index 0000000..6de60e0
--- /dev/null
+++ b/cyclegan/scripts/edges/batch_hed.py
@@ -0,0 +1,81 @@
+# HED batch processing script; modified from https://github.com/s9xie/hed/blob/master/examples/hed/HED-tutorial.ipynb
+# Step 1: download the hed repo: https://github.com/s9xie/hed
+# Step 2: download the models and protoxt, and put them under {caffe_root}/examples/hed/
+# Step 3: put this script under {caffe_root}/examples/hed/
+# Step 4: run the following script:
+# python batch_hed.py --images_dir=/data/to/path/photos/ --hed_mat_dir=/data/to/path/hed_mat_files/
+# The code sometimes crashes after computation is done. Error looks like "Check failed: ... driver shutting down". You can just kill the job.
+# For large images, it will produce gpu memory issue. Therefore, you better resize the images before running this script.
+# Step 5: run the MATLAB post-processing script "PostprocessHED.m"
+
+
+import caffe
+import numpy as np
+from PIL import Image
+import os
+import argparse
+import sys
+import scipy.io as sio
+
+
+def parse_args():
+ parser = argparse.ArgumentParser(description='batch proccesing: photos->edges')
+ parser.add_argument('--caffe_root', dest='caffe_root', help='caffe root', default='../../', type=str)
+ parser.add_argument('--caffemodel', dest='caffemodel', help='caffemodel', default='./hed_pretrained_bsds.caffemodel', type=str)
+ parser.add_argument('--prototxt', dest='prototxt', help='caffe prototxt file', default='./deploy.prototxt', type=str)
+ parser.add_argument('--images_dir', dest='images_dir', help='directory to store input photos', type=str)
+ parser.add_argument('--hed_mat_dir', dest='hed_mat_dir', help='directory to store output hed edges in mat file', type=str)
+ parser.add_argument('--border', dest='border', help='padding border', type=int, default=128)
+ parser.add_argument('--gpu_id', dest='gpu_id', help='gpu id', type=int, default=1)
+ args = parser.parse_args()
+ return args
+
+
+args = parse_args()
+for arg in vars(args):
+ print('[%s] =' % arg, getattr(args, arg))
+# Make sure that caffe is on the python path:
+caffe_root = args.caffe_root # this file is expected to be in {caffe_root}/examples/hed/
+sys.path.insert(0, caffe_root + 'python')
+
+
+if not os.path.exists(args.hed_mat_dir):
+ print('create output directory %s' % args.hed_mat_dir)
+ os.makedirs(args.hed_mat_dir)
+
+imgList = os.listdir(args.images_dir)
+nImgs = len(imgList)
+print('#images = %d' % nImgs)
+
+caffe.set_mode_gpu()
+caffe.set_device(args.gpu_id)
+# load net
+net = caffe.Net(args.prototxt, args.caffemodel, caffe.TEST)
+# pad border
+border = args.border
+
+for i in range(nImgs):
+ if i % 500 == 0:
+ print('processing image %d/%d' % (i, nImgs))
+ im = Image.open(os.path.join(args.images_dir, imgList[i]))
+
+ in_ = np.array(im, dtype=np.float32)
+ in_ = np.pad(in_, ((border, border), (border, border), (0, 0)), 'reflect')
+
+ in_ = in_[:, :, 0:3]
+ in_ = in_[:, :, ::-1]
+ in_ -= np.array((104.00698793, 116.66876762, 122.67891434))
+ in_ = in_.transpose((2, 0, 1))
+ # remove the following two lines if testing with cpu
+
+ # shape for input (data blob is N x C x H x W), set data
+ net.blobs['data'].reshape(1, *in_.shape)
+ net.blobs['data'].data[...] = in_
+ # run net and take argmax for prediction
+ net.forward()
+ fuse = net.blobs['sigmoid-fuse'].data[0][0, :, :]
+ # get rid of the border
+ fuse = fuse[border:-border, border:-border]
+ # save hed file to the disk
+ name, ext = os.path.splitext(imgList[i])
+ sio.savemat(os.path.join(args.hed_mat_dir, name + '.mat'), {'edge_predict': fuse})
diff --git a/cyclegan/pretrained_models/places_vgg.prototxt b/cyclegan/scripts/eval_cityscapes/caffemodel/deploy.prototxt
old mode 100644
new mode 100755
similarity index 57%
rename from cyclegan/pretrained_models/places_vgg.prototxt
rename to cyclegan/scripts/eval_cityscapes/caffemodel/deploy.prototxt
index f47002b..f4d7e71
--- a/cyclegan/pretrained_models/places_vgg.prototxt
+++ b/cyclegan/scripts/eval_cityscapes/caffemodel/deploy.prototxt
@@ -1,33 +1,41 @@
-name: "VGG-Places365"
-input: "data"
-input_dim: 1
-input_dim: 3
-input_dim: 224
-input_dim: 224
+layer {
+ name: "data"
+ type: "Input"
+ top: "data"
+ input_param {
+ shape {
+ dim: 1
+ dim: 3
+ dim: 500
+ dim: 500
+ }
+ }
+}
layer {
name: "conv1_1"
type: "Convolution"
bottom: "data"
top: "conv1_1"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 64
- pad: 1
+ pad: 100
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -43,24 +51,25 @@ layer {
bottom: "conv1_1"
top: "conv1_2"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 64
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -87,24 +96,25 @@ layer {
bottom: "pool1"
top: "conv2_1"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 128
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -120,24 +130,25 @@ layer {
bottom: "conv2_1"
top: "conv2_2"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 128
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -164,24 +175,25 @@ layer {
bottom: "pool2"
top: "conv3_1"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 256
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -197,24 +209,25 @@ layer {
bottom: "conv3_1"
top: "conv3_2"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 256
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -230,24 +243,25 @@ layer {
bottom: "conv3_2"
top: "conv3_3"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 256
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -274,24 +288,25 @@ layer {
bottom: "pool3"
top: "conv4_1"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 512
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -307,24 +322,25 @@ layer {
bottom: "conv4_1"
top: "conv4_2"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 512
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -340,24 +356,25 @@ layer {
bottom: "conv4_2"
top: "conv4_3"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 512
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -384,24 +401,25 @@ layer {
bottom: "pool4"
top: "conv5_1"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 512
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -417,24 +435,25 @@ layer {
bottom: "conv5_1"
top: "conv5_2"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 512
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -450,24 +469,25 @@ layer {
bottom: "conv5_2"
top: "conv5_3"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
convolution_param {
num_output: 512
pad: 1
kernel_size: 3
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
@@ -489,105 +509,261 @@ layer {
}
}
layer {
- name: "fc6"
- type: "InnerProduct"
+ name: "fc6_cs"
+ type: "Convolution"
bottom: "pool5"
- top: "fc6"
+ top: "fc6_cs"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
- inner_product_param {
+ convolution_param {
num_output: 4096
+ pad: 0
+ kernel_size: 7
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
layer {
- name: "relu6"
+ name: "relu6_cs"
type: "ReLU"
- bottom: "fc6"
- top: "fc6"
+ bottom: "fc6_cs"
+ top: "fc6_cs"
}
layer {
- name: "drop6"
- type: "Dropout"
- bottom: "fc6"
- top: "fc6"
- dropout_param {
- dropout_ratio: 0.5
- }
-}
-layer {
- name: "fc7"
- type: "InnerProduct"
- bottom: "fc6"
- top: "fc7"
+ name: "fc7_cs"
+ type: "Convolution"
+ bottom: "fc6_cs"
+ top: "fc7_cs"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
- inner_product_param {
+ convolution_param {
num_output: 4096
+ pad: 0
+ kernel_size: 1
+ stride: 1
weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
- value: 0.0
+ value: 0
}
}
}
layer {
- name: "relu7"
+ name: "relu7_cs"
type: "ReLU"
- bottom: "fc7"
- top: "fc7"
+ bottom: "fc7_cs"
+ top: "fc7_cs"
}
layer {
- name: "drop7"
- type: "Dropout"
- bottom: "fc7"
- top: "fc7"
- dropout_param {
- dropout_ratio: 0.5
- }
-}
-layer {
- name: "fc8a"
- type: "InnerProduct"
- bottom: "fc7"
- top: "fc8a"
+ name: "score_fr"
+ type: "Convolution"
+ bottom: "fc7_cs"
+ top: "score_fr"
param {
- lr_mult: 1.0
- decay_mult: 1.0
+ lr_mult: 1
+ decay_mult: 1
}
param {
- lr_mult: 2.0
- decay_mult: 0.0
+ lr_mult: 2
+ decay_mult: 0
}
- inner_product_param {
- num_output: 365
+ convolution_param {
+ num_output: 20
+ pad: 0
+ kernel_size: 1
+ weight_filler {
+ type: "xavier"
+ }
+ bias_filler {
+ type: "constant"
+ }
}
}
layer {
- name: "prob"
- type: "Softmax"
- bottom: "fc8a"
- top: "prob"
+ name: "upscore2"
+ type: "Deconvolution"
+ bottom: "score_fr"
+ top: "upscore2"
+ param {
+ lr_mult: 1
+ }
+ convolution_param {
+ num_output: 20
+ bias_term: false
+ kernel_size: 4
+ stride: 2
+ weight_filler {
+ type: "xavier"
+ }
+ bias_filler {
+ type: "constant"
+ }
+ }
+}
+layer {
+ name: "score_pool4"
+ type: "Convolution"
+ bottom: "pool4"
+ top: "score_pool4"
+ param {
+ lr_mult: 1
+ decay_mult: 1
+ }
+ param {
+ lr_mult: 2
+ decay_mult: 0
+ }
+ convolution_param {
+ num_output: 20
+ pad: 0
+ kernel_size: 1
+ weight_filler {
+ type: "xavier"
+ }
+ bias_filler {
+ type: "constant"
+ }
+ }
+}
+layer {
+ name: "score_pool4c"
+ type: "Crop"
+ bottom: "score_pool4"
+ bottom: "upscore2"
+ top: "score_pool4c"
+ crop_param {
+ axis: 2
+ offset: 5
+ }
+}
+layer {
+ name: "fuse_pool4"
+ type: "Eltwise"
+ bottom: "upscore2"
+ bottom: "score_pool4c"
+ top: "fuse_pool4"
+ eltwise_param {
+ operation: SUM
+ }
+}
+layer {
+ name: "upscore_pool4"
+ type: "Deconvolution"
+ bottom: "fuse_pool4"
+ top: "upscore_pool4"
+ param {
+ lr_mult: 1
+ }
+ convolution_param {
+ num_output: 20
+ bias_term: false
+ kernel_size: 4
+ stride: 2
+ weight_filler {
+ type: "xavier"
+ }
+ bias_filler {
+ type: "constant"
+ }
+ }
+}
+layer {
+ name: "score_pool3"
+ type: "Convolution"
+ bottom: "pool3"
+ top: "score_pool3"
+ param {
+ lr_mult: 1
+ decay_mult: 1
+ }
+ param {
+ lr_mult: 2
+ decay_mult: 0
+ }
+ convolution_param {
+ num_output: 20
+ pad: 0
+ kernel_size: 1
+ weight_filler {
+ type: "xavier"
+ }
+ bias_filler {
+ type: "constant"
+ }
+ }
+}
+layer {
+ name: "score_pool3c"
+ type: "Crop"
+ bottom: "score_pool3"
+ bottom: "upscore_pool4"
+ top: "score_pool3c"
+ crop_param {
+ axis: 2
+ offset: 9
+ }
+}
+layer {
+ name: "fuse_pool3"
+ type: "Eltwise"
+ bottom: "upscore_pool4"
+ bottom: "score_pool3c"
+ top: "fuse_pool3"
+ eltwise_param {
+ operation: SUM
+ }
+}
+layer {
+ name: "upscore8"
+ type: "Deconvolution"
+ bottom: "fuse_pool3"
+ top: "upscore8"
+ param {
+ lr_mult: 1
+ }
+ convolution_param {
+ num_output: 20
+ bias_term: false
+ kernel_size: 16
+ stride: 8
+ weight_filler {
+ type: "xavier"
+ }
+ bias_filler {
+ type: "constant"
+ }
+ }
+}
+layer {
+ name: "score"
+ type: "Crop"
+ bottom: "upscore8"
+ bottom: "data"
+ top: "score"
+ crop_param {
+ axis: 2
+ offset: 31
+ }
}
diff --git a/cyclegan/scripts/eval_cityscapes/cityscapes.py b/cyclegan/scripts/eval_cityscapes/cityscapes.py
new file mode 100755
index 0000000..05b1471
--- /dev/null
+++ b/cyclegan/scripts/eval_cityscapes/cityscapes.py
@@ -0,0 +1,141 @@
+# The following code is modified from https://github.com/shelhamer/clockwork-fcn
+import sys
+import os
+import glob
+import numpy as np
+from PIL import Image
+
+
+class cityscapes:
+ def __init__(self, data_path):
+ # data_path something like /data2/cityscapes
+ self.dir = data_path
+ self.classes = ['road', 'sidewalk', 'building', 'wall', 'fence',
+ 'pole', 'traffic light', 'traffic sign', 'vegetation', 'terrain',
+ 'sky', 'person', 'rider', 'car', 'truck',
+ 'bus', 'train', 'motorcycle', 'bicycle']
+ self.mean = np.array((72.78044, 83.21195, 73.45286), dtype=np.float32)
+ # import cityscapes label helper and set up label mappings
+ sys.path.insert(0, '{}/scripts/helpers/'.format(self.dir))
+ labels = __import__('labels')
+ self.id2trainId = {label.id: label.trainId for label in labels.labels} # dictionary mapping from raw IDs to train IDs
+ self.trainId2color = {label.trainId: label.color for label in labels.labels} # dictionary mapping train IDs to colors as 3-tuples
+
+ def get_dset(self, split):
+ '''
+ List images as (city, id) for the specified split
+
+ TODO(shelhamer) generate splits from cityscapes itself, instead of
+ relying on these separately made text files.
+ '''
+ if split == 'train':
+ dataset = open('{}/ImageSets/segFine/train.txt'.format(self.dir)).read().splitlines()
+ else:
+ dataset = open('{}/ImageSets/segFine/val.txt'.format(self.dir)).read().splitlines()
+ return [(item.split('/')[0], item.split('/')[1]) for item in dataset]
+
+ def load_image(self, split, city, idx):
+ im = Image.open('{}/leftImg8bit_sequence/{}/{}/{}_leftImg8bit.png'.format(self.dir, split, city, idx))
+ return im
+
+ def assign_trainIds(self, label):
+ """
+ Map the given label IDs to the train IDs appropriate for training
+ Use the label mapping provided in labels.py from the cityscapes scripts
+ """
+ label = np.array(label, dtype=np.float32)
+ if sys.version_info[0] < 3:
+ for k, v in self.id2trainId.iteritems():
+ label[label == k] = v
+ else:
+ for k, v in self.id2trainId.items():
+ label[label == k] = v
+ return label
+
+ def load_label(self, split, city, idx):
+ """
+ Load label image as 1 x height x width integer array of label indices.
+ The leading singleton dimension is required by the loss.
+ """
+ label = Image.open('{}/gtFine/{}/{}/{}_gtFine_labelIds.png'.format(self.dir, split, city, idx))
+ label = self.assign_trainIds(label) # get proper labels for eval
+ label = np.array(label, dtype=np.uint8)
+ label = label[np.newaxis, ...]
+ return label
+
+ def preprocess(self, im):
+ """
+ Preprocess loaded image (by load_image) for Caffe:
+ - cast to float
+ - switch channels RGB -> BGR
+ - subtract mean
+ - transpose to channel x height x width order
+ """
+ in_ = np.array(im, dtype=np.float32)
+ in_ = in_[:, :, ::-1]
+ in_ -= self.mean
+ in_ = in_.transpose((2, 0, 1))
+ return in_
+
+ def palette(self, label):
+ '''
+ Map trainIds to colors as specified in labels.py
+ '''
+ if label.ndim == 3:
+ label = label[0]
+ color = np.empty((label.shape[0], label.shape[1], 3))
+ if sys.version_info[0] < 3:
+ for k, v in self.trainId2color.iteritems():
+ color[label == k, :] = v
+ else:
+ for k, v in self.trainId2color.items():
+ color[label == k, :] = v
+ return color
+
+ def make_boundaries(label, thickness=None):
+ """
+ Input is an image label, output is a numpy array mask encoding the boundaries of the objects
+ Extract pixels at the true boundary by dilation - erosion of label.
+ Don't just pick the void label as it is not exclusive to the boundaries.
+ """
+ assert(thickness is not None)
+ import skimage.morphology as skm
+ void = 255
+ mask = np.logical_and(label > 0, label != void)[0]
+ selem = skm.disk(thickness)
+ boundaries = np.logical_xor(skm.dilation(mask, selem),
+ skm.erosion(mask, selem))
+ return boundaries
+
+ def list_label_frames(self, split):
+ """
+ Select labeled frames from a split for evaluation
+ collected as (city, shot, idx) tuples
+ """
+ def file2idx(f):
+ """Helper to convert file path into frame ID"""
+ city, shot, frame = (os.path.basename(f).split('_')[:3])
+ return "_".join([city, shot, frame])
+ frames = []
+ cities = [os.path.basename(f) for f in glob.glob('{}/gtFine/{}/*'.format(self.dir, split))]
+ for c in cities:
+ files = sorted(glob.glob('{}/gtFine/{}/{}/*labelIds.png'.format(self.dir, split, c)))
+ frames.extend([file2idx(f) for f in files])
+ return frames
+
+ def collect_frame_sequence(self, split, idx, length):
+ """
+ Collect sequence of frames preceding (and including) a labeled frame
+ as a list of Images.
+
+ Note: 19 preceding frames are provided for each labeled frame.
+ """
+ SEQ_LEN = length
+ city, shot, frame = idx.split('_')
+ frame = int(frame)
+ frame_seq = []
+ for i in range(frame - SEQ_LEN, frame + 1):
+ frame_path = '{0}/leftImg8bit_sequence/val/{1}/{1}_{2}_{3:0>6d}_leftImg8bit.png'.format(
+ self.dir, city, shot, i)
+ frame_seq.append(Image.open(frame_path))
+ return frame_seq
diff --git a/cyclegan/scripts/eval_cityscapes/download_fcn8s.sh b/cyclegan/scripts/eval_cityscapes/download_fcn8s.sh
new file mode 100755
index 0000000..f45af15
--- /dev/null
+++ b/cyclegan/scripts/eval_cityscapes/download_fcn8s.sh
@@ -0,0 +1,3 @@
+URL=http://people.eecs.berkeley.edu/~tinghuiz/projects/pix2pix/fcn-8s-cityscapes/fcn-8s-cityscapes.caffemodel
+OUTPUT_FILE=./scripts/eval_cityscapes/caffemodel/fcn-8s-cityscapes.caffemodel
+wget -N $URL -O $OUTPUT_FILE
diff --git a/cyclegan/scripts/eval_cityscapes/evaluate.py b/cyclegan/scripts/eval_cityscapes/evaluate.py
new file mode 100755
index 0000000..500c20f
--- /dev/null
+++ b/cyclegan/scripts/eval_cityscapes/evaluate.py
@@ -0,0 +1,68 @@
+import os
+import caffe
+import argparse
+import numpy as np
+import scipy.misc
+from PIL import Image
+from util import segrun, fast_hist, get_scores
+from cityscapes import cityscapes
+
+parser = argparse.ArgumentParser()
+parser.add_argument("--cityscapes_dir", type=str, required=True, help="Path to the original cityscapes dataset")
+parser.add_argument("--result_dir", type=str, required=True, help="Path to the generated images to be evaluated")
+parser.add_argument("--output_dir", type=str, required=True, help="Where to save the evaluation results")
+parser.add_argument("--caffemodel_dir", type=str, default='./scripts/eval_cityscapes/caffemodel/', help="Where the FCN-8s caffemodel stored")
+parser.add_argument("--gpu_id", type=int, default=0, help="Which gpu id to use")
+parser.add_argument("--split", type=str, default='val', help="Data split to be evaluated")
+parser.add_argument("--save_output_images", type=int, default=0, help="Whether to save the FCN output images")
+args = parser.parse_args()
+
+
+def main():
+ if not os.path.isdir(args.output_dir):
+ os.makedirs(args.output_dir)
+ if args.save_output_images > 0:
+ output_image_dir = args.output_dir + 'image_outputs/'
+ if not os.path.isdir(output_image_dir):
+ os.makedirs(output_image_dir)
+ CS = cityscapes(args.cityscapes_dir)
+ n_cl = len(CS.classes)
+ label_frames = CS.list_label_frames(args.split)
+ caffe.set_device(args.gpu_id)
+ caffe.set_mode_gpu()
+ net = caffe.Net(args.caffemodel_dir + '/deploy.prototxt',
+ args.caffemodel_dir + 'fcn-8s-cityscapes.caffemodel',
+ caffe.TEST)
+
+ hist_perframe = np.zeros((n_cl, n_cl))
+ for i, idx in enumerate(label_frames):
+ if i % 10 == 0:
+ print('Evaluating: %d/%d' % (i, len(label_frames)))
+ city = idx.split('_')[0]
+ # idx is city_shot_frame
+ label = CS.load_label(args.split, city, idx)
+ im_file = args.result_dir + '/' + idx + '_leftImg8bit.png'
+ im = np.array(Image.open(im_file))
+ im = scipy.misc.imresize(im, (label.shape[1], label.shape[2]))
+ out = segrun(net, CS.preprocess(im))
+ hist_perframe += fast_hist(label.flatten(), out.flatten(), n_cl)
+ if args.save_output_images > 0:
+ label_im = CS.palette(label)
+ pred_im = CS.palette(out)
+ scipy.misc.imsave(output_image_dir + '/' + str(i) + '_pred.jpg', pred_im)
+ scipy.misc.imsave(output_image_dir + '/' + str(i) + '_gt.jpg', label_im)
+ scipy.misc.imsave(output_image_dir + '/' + str(i) + '_input.jpg', im)
+
+ mean_pixel_acc, mean_class_acc, mean_class_iou, per_class_acc, per_class_iou = get_scores(hist_perframe)
+ with open(args.output_dir + '/evaluation_results.txt', 'w') as f:
+ f.write('Mean pixel accuracy: %f\n' % mean_pixel_acc)
+ f.write('Mean class accuracy: %f\n' % mean_class_acc)
+ f.write('Mean class IoU: %f\n' % mean_class_iou)
+ f.write('************ Per class numbers below ************\n')
+ for i, cl in enumerate(CS.classes):
+ while len(cl) < 15:
+ cl = cl + ' '
+ f.write('%s: acc = %f, iou = %f\n' % (cl, per_class_acc[i], per_class_iou[i]))
+
+
+main()
diff --git a/cyclegan/scripts/eval_cityscapes/util.py b/cyclegan/scripts/eval_cityscapes/util.py
new file mode 100755
index 0000000..8fce27f
--- /dev/null
+++ b/cyclegan/scripts/eval_cityscapes/util.py
@@ -0,0 +1,42 @@
+# The following code is modified from https://github.com/shelhamer/clockwork-fcn
+import numpy as np
+
+
+def get_out_scoremap(net):
+ return net.blobs['score'].data[0].argmax(axis=0).astype(np.uint8)
+
+
+def feed_net(net, in_):
+ """
+ Load prepared input into net.
+ """
+ net.blobs['data'].reshape(1, *in_.shape)
+ net.blobs['data'].data[...] = in_
+
+
+def segrun(net, in_):
+ feed_net(net, in_)
+ net.forward()
+ return get_out_scoremap(net)
+
+
+def fast_hist(a, b, n):
+ k = np.where((a >= 0) & (a < n))[0]
+ bc = np.bincount(n * a[k].astype(int) + b[k], minlength=n**2)
+ if len(bc) != n**2:
+ # ignore this example if dimension mismatch
+ return 0
+ return bc.reshape(n, n)
+
+
+def get_scores(hist):
+ # Mean pixel accuracy
+ acc = np.diag(hist).sum() / (hist.sum() + 1e-12)
+
+ # Per class accuracy
+ cl_acc = np.diag(hist) / (hist.sum(1) + 1e-12)
+
+ # Per class IoU
+ iu = np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist) + 1e-12)
+
+ return acc, np.nanmean(cl_acc), np.nanmean(iu), cl_acc, iu
diff --git a/cyclegan/scripts/install_deps.sh b/cyclegan/scripts/install_deps.sh
new file mode 100644
index 0000000..801c7dd
--- /dev/null
+++ b/cyclegan/scripts/install_deps.sh
@@ -0,0 +1,3 @@
+set -ex
+pip install visdom
+pip install dominate
diff --git a/cyclegan/scripts/test_before_push.py b/cyclegan/scripts/test_before_push.py
new file mode 100644
index 0000000..ccd8788
--- /dev/null
+++ b/cyclegan/scripts/test_before_push.py
@@ -0,0 +1,51 @@
+# Simple script to make sure basic usage
+# such as training, testing, saving and loading
+# runs without errors.
+import os
+
+
+def run(command):
+ print(command)
+ exit_status = os.system(command)
+ if exit_status > 0:
+ exit(1)
+
+
+if __name__ == '__main__':
+ # download mini datasets
+ if not os.path.exists('./datasets/mini'):
+ run('bash ./datasets/download_cyclegan_dataset.sh mini')
+
+ if not os.path.exists('./datasets/mini_pix2pix'):
+ run('bash ./datasets/download_cyclegan_dataset.sh mini_pix2pix')
+
+ # pretrained cyclegan model
+ if not os.path.exists('./checkpoints/horse2zebra_pretrained/latest_net_G.pth'):
+ run('bash ./scripts/download_cyclegan_model.sh horse2zebra')
+ run('python test.py --model test --dataroot ./datasets/mini --name horse2zebra_pretrained --no_dropout --num_test 1 --no_dropout')
+
+ # pretrained pix2pix model
+ if not os.path.exists('./checkpoints/facades_label2photo_pretrained/latest_net_G.pth'):
+ run('bash ./scripts/download_pix2pix_model.sh facades_label2photo')
+ if not os.path.exists('./datasets/facades'):
+ run('bash ./datasets/download_pix2pix_dataset.sh facades')
+ run('python test.py --dataroot ./datasets/facades/ --direction BtoA --model pix2pix --name facades_label2photo_pretrained --num_test 1')
+
+ # cyclegan train/test
+ run('python train.py --model cycle_gan --name temp_cyclegan --dataroot ./datasets/mini --n_epochs 1 --n_epochs_decay 0 --save_latest_freq 10 --print_freq 1 --display_id -1')
+ run('python test.py --model test --name temp_cyclegan --dataroot ./datasets/mini --num_test 1 --model_suffix "_A" --no_dropout')
+
+ # pix2pix train/test
+ run('python train.py --model pix2pix --name temp_pix2pix --dataroot ./datasets/mini_pix2pix --n_epochs 1 --n_epochs_decay 5 --save_latest_freq 10 --display_id -1')
+ run('python test.py --model pix2pix --name temp_pix2pix --dataroot ./datasets/mini_pix2pix --num_test 1')
+
+ # template train/test
+ run('python train.py --model template --name temp2 --dataroot ./datasets/mini_pix2pix --n_epochs 1 --n_epochs_decay 0 --save_latest_freq 10 --display_id -1')
+ run('python test.py --model template --name temp2 --dataroot ./datasets/mini_pix2pix --num_test 1')
+
+ # colorization train/test (optional)
+ if not os.path.exists('./datasets/mini_colorization'):
+ run('bash ./datasets/download_cyclegan_dataset.sh mini_colorization')
+
+ run('python train.py --model colorization --name temp_color --dataroot ./datasets/mini_colorization --n_epochs 1 --n_epochs_decay 0 --save_latest_freq 5 --display_id -1')
+ run('python test.py --model colorization --name temp_color --dataroot ./datasets/mini_colorization --num_test 1')
diff --git a/cyclegan/scripts/test_colorization.sh b/cyclegan/scripts/test_colorization.sh
new file mode 100644
index 0000000..9837fd5
--- /dev/null
+++ b/cyclegan/scripts/test_colorization.sh
@@ -0,0 +1,2 @@
+set -ex
+python test.py --dataroot ./datasets/colorization --name color_pix2pix --model colorization
diff --git a/cyclegan/scripts/test_cyclegan.sh b/cyclegan/scripts/test_cyclegan.sh
new file mode 100755
index 0000000..9036bf8
--- /dev/null
+++ b/cyclegan/scripts/test_cyclegan.sh
@@ -0,0 +1,2 @@
+set -ex
+python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan --phase test --no_dropout
diff --git a/cyclegan/scripts/test_pix2pix.sh b/cyclegan/scripts/test_pix2pix.sh
new file mode 100755
index 0000000..589599b
--- /dev/null
+++ b/cyclegan/scripts/test_pix2pix.sh
@@ -0,0 +1,2 @@
+set -ex
+python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --netG unet_256 --direction BtoA --dataset_mode aligned --norm batch
diff --git a/cyclegan/scripts/test_single.sh b/cyclegan/scripts/test_single.sh
new file mode 100755
index 0000000..eada640
--- /dev/null
+++ b/cyclegan/scripts/test_single.sh
@@ -0,0 +1,2 @@
+set -ex
+python test.py --dataroot ./datasets/facades/testB/ --name facades_pix2pix --model test --netG unet_256 --direction BtoA --dataset_mode single --norm batch
diff --git a/cyclegan/scripts/train_colorization.sh b/cyclegan/scripts/train_colorization.sh
new file mode 100644
index 0000000..e6c0680
--- /dev/null
+++ b/cyclegan/scripts/train_colorization.sh
@@ -0,0 +1,2 @@
+set -ex
+python train.py --dataroot ./datasets/colorization --name color_pix2pix --model colorization
diff --git a/cyclegan/scripts/train_cyclegan.sh b/cyclegan/scripts/train_cyclegan.sh
new file mode 100755
index 0000000..567721e
--- /dev/null
+++ b/cyclegan/scripts/train_cyclegan.sh
@@ -0,0 +1,2 @@
+set -ex
+python train.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan --pool_size 50 --no_dropout
diff --git a/cyclegan/scripts/train_pix2pix.sh b/cyclegan/scripts/train_pix2pix.sh
new file mode 100755
index 0000000..0171001
--- /dev/null
+++ b/cyclegan/scripts/train_pix2pix.sh
@@ -0,0 +1,2 @@
+set -ex
+python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --netG unet_256 --direction BtoA --lambda_L1 100 --dataset_mode aligned --norm batch --pool_size 0
diff --git a/cyclegan/test.lua b/cyclegan/test.lua
deleted file mode 100644
index 8872990..0000000
--- a/cyclegan/test.lua
+++ /dev/null
@@ -1,142 +0,0 @@
--- usage: DATA_ROOT=/path/to/data/ name=expt1 which_direction=BtoA th test.lua
---
--- code derived from https://github.com/soumith/dcgan.torch and https://github.com/phillipi/pix2pix
-require 'image'
-require 'nn'
-require 'nngraph'
-require 'models.architectures'
-
-
-util = paths.dofile('util/util.lua')
-options = require 'options'
-opt = options.parse_options('test')
-
--- initialize torch GPU/CPU mode
-if opt.gpu > 0 then
- require 'cutorch'
- require 'cunn'
- cutorch.setDevice(opt.gpu)
- print ("GPU Mode")
- torch.setdefaulttensortype('torch.CudaTensor')
-else
- torch.setdefaulttensortype('torch.FloatTensor')
- print ("CPU Mode")
-end
-
--- setup visualization
-visualizer = require 'util/visualizer'
-
-function TableConcat(t1,t2)
- for i=1,#t2 do
- t1[#t1+1] = t2[i]
- end
- return t1
-end
-
-
--- load data
-local data_loader = nil
-if opt.align_data > 0 then
- require 'data.aligned_data_loader'
- data_loader = AlignedDataLoader()
-else
- require 'data.unaligned_data_loader'
- data_loader = UnalignedDataLoader()
-end
-print( "DataLoader " .. data_loader:name() .. " was created.")
-data_loader:Initialize(opt)
-
-if opt.how_many == 'all' then
- opt.how_many = data_loader:size()
-end
-
-opt.how_many = math.min(opt.how_many, data_loader:size())
-
--- set batch/instance normalization
-set_normalization(opt.norm)
-
--- load model
-opt.continue_train = 1
--- define model
-if opt.model == 'cycle_gan' then
- require 'models.cycle_gan_model'
- model = CycleGANModel()
-elseif opt.model == 'one_direction_test' then
- require 'models.one_direction_test_model'
- model = OneDirectionTestModel()
-elseif opt.model == 'pix2pix' then
- require 'models.pix2pix_model'
- model = Pix2PixModel()
-elseif opt.model == 'bigan' then
- require 'models.bigan_model'
- model = BiGANModel()
-elseif opt.model == 'content_gan' then
- require 'models.content_gan_model'
- model = ContentGANModel()
-else
- error('Please specify a correct model')
-end
-model:Initialize(opt)
-
-local pathsA = {} -- paths to images A tested on
-local pathsB = {} -- paths to images B tested on
-local web_dir = paths.concat(opt.results_dir, opt.name .. '/' .. opt.which_epoch .. '_' .. opt.phase)
-paths.mkdir(web_dir)
-local image_dir = paths.concat(web_dir, 'images')
-paths.mkdir(image_dir)
-s1 = opt.fineSize
-s2 = opt.fineSize / opt.aspect_ratio
-
-visuals = {}
-
-for n = 1, math.floor(opt.how_many) do
- print('processing batch ' .. n)
- local cur_dataA, cur_dataB, cur_pathsA, cur_pathsB = data_loader:GetNextBatch()
-
- cur_pathsA = util.basename_batch(cur_pathsA)
- cur_pathsB = util.basename_batch(cur_pathsB)
- print('pathsA', cur_pathsA)
- print('pathsB', cur_PathsB)
- model:Forward({real_A=cur_dataA, real_B=cur_dataB}, opt)
-
- visuals = model:GetCurrentVisuals(opt, opt.fineSize)
-
- for i,visual in ipairs(visuals) do
- if opt.resize_or_crop == 'scale_width' or opt.resize_or_crop == 'scale_height' then
- s1 = nil
- s2 = nil
- end
- visualizer.save_images(visual.img, paths.concat(image_dir, visual.label), {string.gsub(cur_pathsA[1],'.jpg','.png')}, s1, s2)
- end
-
-
- print('Saved images to: ', image_dir)
- pathsA = TableConcat(pathsA, cur_pathsA)
- pathsB = TableConcat(pathsB, cur_pathsB)
-end
-
-labels = {}
-for i,visual in ipairs(visuals) do
- table.insert(labels, visual.label)
-end
-
--- make webpage
-io.output(paths.concat(web_dir, 'index.html'))
-io.write('')
-io.write('| Image | ')
-for i = 1, #labels do
- io.write('' .. labels[i] .. ' | ')
-end
-io.write('
')
-
-for n = 1,math.floor(opt.how_many) do
- io.write('')
- io.write('| ' .. tostring(n) .. ' | ')
- for j = 1, #labels do
- label = labels[j]
- io.write(' .. ') | ')
- end
- io.write('
')
-end
-
-io.write('
')
diff --git a/cyclegan/test.py b/cyclegan/test.py
new file mode 100644
index 0000000..9281a99
--- /dev/null
+++ b/cyclegan/test.py
@@ -0,0 +1,69 @@
+"""General-purpose test script for image-to-image translation.
+
+Once you have trained your model with train.py, you can use this script to test the model.
+It will load a saved model from --checkpoints_dir and save the results to --results_dir.
+
+It first creates model and dataset given the option. It will hard-code some parameters.
+It then runs inference for --num_test images and save results to an HTML file.
+
+Example (You need to train models first or download pre-trained models from our website):
+ Test a CycleGAN model (both sides):
+ python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan
+
+ Test a CycleGAN model (one side only):
+ python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout
+
+ The option '--model test' is used for generating CycleGAN results 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 ' to specify the results directory.
+
+ Test a pix2pix model:
+ python test.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA
+
+See options/base_options.py and options/test_options.py for more test options.
+See training and test tips at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md
+See frequently asked questions at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/qa.md
+"""
+import os
+from options.test_options import TestOptions
+from data import create_dataset
+from models import create_model
+from util.visualizer import save_images
+from util import html
+
+
+if __name__ == '__main__':
+ opt = TestOptions().parse() # get test options
+ # hard-code some parameters for test
+ opt.num_threads = 0 # test code only supports num_threads = 1
+ opt.batch_size = 1 # test code only supports batch_size = 1
+ opt.serial_batches = True # disable data shuffling; comment this line if results on randomly chosen images are needed.
+ opt.no_flip = True # no flip; comment this line if results on flipped images are needed.
+ opt.display_id = -1 # no visdom display; the test code saves the results to a HTML file.
+ dataset = create_dataset(opt) # create a dataset given opt.dataset_mode and other options
+ model = create_model(opt) # create a model given opt.model and other options
+ model.setup(opt) # regular setup: load and print networks; create schedulers
+ # create a website
+ web_dir = os.path.join(opt.results_dir, opt.name, '{}_{}'.format(opt.phase, opt.epoch)) # define the website directory
+ if opt.load_iter > 0: # load_iter is 0 by default
+ web_dir = '{:s}_iter{:d}'.format(web_dir, opt.load_iter)
+ print('creating web directory', web_dir)
+ webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.epoch))
+ # test with eval mode. This only affects layers like batchnorm and dropout.
+ # For [pix2pix]: we use batchnorm and dropout in the original pix2pix. You can experiment it with and without eval() mode.
+ # For [CycleGAN]: It should not affect CycleGAN as CycleGAN uses instancenorm without dropout.
+ if opt.eval:
+ model.eval()
+ for i, data in enumerate(dataset):
+ if i >= opt.num_test: # only apply our model to opt.num_test images.
+ break
+ model.set_input(data) # unpack data from data loader
+ model.test() # run inference
+ visuals = model.get_current_visuals() # get image results
+ img_path = model.get_image_paths() # get image paths
+ if i % 5 == 0: # save images to an HTML file
+ print('processing (%04d)-th image... %s' % (i, img_path))
+ save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize)
+ webpage.save() # save the HTML
diff --git a/cyclegan/train.lua b/cyclegan/train.lua
deleted file mode 100644
index 6ef9974..0000000
--- a/cyclegan/train.lua
+++ /dev/null
@@ -1,178 +0,0 @@
--- usage example: DATA_ROOT=/path/to/data/ which_direction=BtoA name=expt1 th train.lua
--- code derived from https://github.com/soumith/dcgan.torch and https://github.com/phillipi/pix2pix
-
-require 'torch'
-require 'nn'
-require 'optim'
-util = paths.dofile('util/util.lua')
-content = paths.dofile('util/content_loss.lua')
-require 'image'
-require 'models.architectures'
-
--- load configuration file
-options = require 'options'
-opt = options.parse_options('train')
-
--- setup visualization
-visualizer = require 'util/visualizer'
-
--- initialize torch GPU/CPU mode
-if opt.gpu > 0 then
- require 'cutorch'
- require 'cunn'
- cutorch.setDevice(opt.gpu)
- print ("GPU Mode")
- torch.setdefaulttensortype('torch.CudaTensor')
-else
- torch.setdefaulttensortype('torch.FloatTensor')
- print ("CPU Mode")
-end
-
--- load data
-local data_loader = nil
-if opt.align_data > 0 then
- require 'data.aligned_data_loader'
- data_loader = AlignedDataLoader()
-else
- require 'data.unaligned_data_loader'
- data_loader = UnalignedDataLoader()
-end
-print( "DataLoader " .. data_loader:name() .. " was created.")
-data_loader:Initialize(opt)
-
--- set batch/instance normalization
-set_normalization(opt.norm)
-
---- timer
-local epoch_tm = torch.Timer()
-local tm = torch.Timer()
-
--- define model
-local model = nil
-local display_plot = nil
-if opt.model == 'cycle_gan' then
- assert(data_loader:name() == 'UnalignedDataLoader')
- require 'models.cycle_gan_model'
- model = CycleGANModel()
-elseif opt.model == 'pix2pix' then
- require 'models.pix2pix_model'
- assert(data_loader:name() == 'AlignedDataLoader')
- model = Pix2PixModel()
-elseif opt.model == 'bigan' then
- assert(data_loader:name() == 'UnalignedDataLoader')
- require 'models.bigan_model'
- model = BiGANModel()
-elseif opt.model == 'content_gan' then
- require 'models.content_gan_model'
- assert(data_loader:name() == 'UnalignedDataLoader')
- model = ContentGANModel()
-else
- error('Please specify a correct model')
-end
-
--- print the model name
-print('Model ' .. model:model_name() .. ' was specified.')
-model:Initialize(opt)
-
--- set up the loss plot
-require 'util/plot_util'
-plotUtil = PlotUtil()
-display_plot = model:DisplayPlot(opt)
-plotUtil:Initialize(display_plot, opt.display_id, opt.name)
-
---------------------------------------------------------------------------------
--- Helper Functions
---------------------------------------------------------------------------------
-function visualize_current_results()
- local visuals = model:GetCurrentVisuals(opt)
- for i,visual in ipairs(visuals) do
- visualizer.disp_image(visual.img, opt.display_winsize,
- opt.display_id+i, opt.name .. ' ' .. visual.label)
- end
-end
-
-function save_current_results(epoch, counter)
- local visuals = model:GetCurrentVisuals(opt)
- for i,visual in ipairs(visuals) do
- output_path = paths.concat(opt.visual_dir, 'train_epoch' .. epoch .. '_iter' .. counter .. '_' .. visual.label .. '.jpg')
- visualizer.save_results(visual.img, output_path)
- end
-end
-
-function print_current_errors(epoch, counter_in_epoch)
- print(('Epoch: [%d][%8d / %8d]\t Time: %.3f DataTime: %.3f '
- .. '%s'):
- format(epoch, ((counter_in_epoch-1) / opt.batchSize),
- math.floor(math.min(data_loader:size(), opt.ntrain) / opt.batchSize),
- tm:time().real / opt.batchSize,
- data_loader:time_elapsed_to_fetch_data() / opt.batchSize,
- model:GetCurrentErrorDescription()
- ))
-end
-
-function plot_current_errors(epoch, counter_ratio, opt)
- local errs = model:GetCurrentErrors(opt)
- local plot_vals = { epoch + counter_ratio}
- plotUtil:Display(plot_vals, errs)
-end
-
---------------------------------------------------------------------------------
--- Main Training Loop
---------------------------------------------------------------------------------
-local counter = 0
-local num_batches = math.floor(math.min(data_loader:size(), opt.ntrain) / opt.batchSize)
-print('#training iterations: ' .. opt.niter+opt.niter_decay )
-
-for epoch = 1, opt.niter+opt.niter_decay do
- epoch_tm:reset()
- for counter_in_epoch = 1, math.min(data_loader:size(), opt.ntrain), opt.batchSize do
- tm:reset()
- -- load a batch and run G on that batch
- local real_dataA, real_dataB, _, _ = data_loader:GetNextBatch()
-
- model:Forward({real_A=real_dataA, real_B=real_dataB}, opt)
- -- run forward pass
- opt.counter = counter
- -- run backward pass
- model:OptimizeParameters(opt)
- -- display on the web server
- if counter % opt.display_freq == 0 and opt.display_id > 0 then
- visualize_current_results()
- end
-
- -- logging
- if counter % opt.print_freq == 0 then
- print_current_errors(epoch, counter_in_epoch)
- plot_current_errors(epoch, counter_in_epoch/num_batches, opt)
- end
-
- -- save latest model
- if counter % opt.save_latest_freq == 0 and counter > 0 then
- print(('saving the latest model (epoch %d, iters %d)'):format(epoch, counter))
- model:Save('latest', opt)
- end
-
- -- save latest results
- if counter % opt.save_display_freq == 0 then
- save_current_results(epoch, counter)
- end
- counter = counter + 1
- end
-
- -- save model at the end of epoch
- if epoch % opt.save_epoch_freq == 0 then
- print(('saving the model (epoch %d, iters %d)'):format(epoch, counter))
- model:Save('latest', opt)
- model:Save(epoch, opt)
- end
- -- print the timing information after each epoch
- print(('End of epoch %d / %d \t Time Taken: %.3f'):
- format(epoch, opt.niter+opt.niter_decay, epoch_tm:time().real))
-
- -- update learning rate
- if epoch > opt.niter then
- model:UpdateLearningRate(opt)
- end
- -- refresh parameters
- model:RefreshParameters(opt)
-end
diff --git a/cyclegan/train.py b/cyclegan/train.py
new file mode 100644
index 0000000..9ff8b3d
--- /dev/null
+++ b/cyclegan/train.py
@@ -0,0 +1,78 @@
+"""General-purpose training script for image-to-image translation.
+
+This script works for various models (with option '--model': e.g., pix2pix, cyclegan, colorization) and
+different datasets (with option '--dataset_mode': e.g., aligned, unaligned, single, colorization).
+You need to specify the dataset ('--dataroot'), experiment name ('--name'), and model ('--model').
+
+It first creates model, dataset, and visualizer given the option.
+It then does standard network training. During the training, it also visualize/save the images, print/save the loss plot, and save models.
+The script supports continue/resume training. Use '--continue_train' to resume your previous training.
+
+Example:
+ Train a CycleGAN model:
+ python train.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan
+ Train a pix2pix model:
+ python train.py --dataroot ./datasets/facades --name facades_pix2pix --model pix2pix --direction BtoA
+
+See options/base_options.py and options/train_options.py for more training options.
+See training and test tips at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/tips.md
+See frequently asked questions at: https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/blob/master/docs/qa.md
+"""
+import time
+from options.train_options import TrainOptions
+from data import create_dataset
+from models import create_model
+from util.visualizer import Visualizer
+
+if __name__ == '__main__':
+ opt = TrainOptions().parse() # get training options
+ dataset = create_dataset(opt) # create a dataset given opt.dataset_mode and other options
+ dataset_size = len(dataset) # get the number of images in the dataset.
+ print('The number of training images = %d' % dataset_size)
+
+ model = create_model(opt) # create a model given opt.model and other options
+ model.setup(opt) # regular setup: load and print networks; create schedulers
+ visualizer = Visualizer(opt) # create a visualizer that display/save images and plots
+ total_iters = 0 # the total number of training iterations
+
+ for epoch in range(opt.epoch_count, opt.n_epochs + opt.n_epochs_decay + 1): # outer loop for different epochs; we save the model by , +
+ epoch_start_time = time.time() # timer for entire epoch
+ iter_data_time = time.time() # timer for data loading per iteration
+ epoch_iter = 0 # the number of training iterations in current epoch, reset to 0 every epoch
+ visualizer.reset() # reset the visualizer: make sure it saves the results to HTML at least once every epoch
+
+ for i, data in enumerate(dataset): # inner loop within one epoch
+ iter_start_time = time.time() # timer for computation per iteration
+ if total_iters % opt.print_freq == 0:
+ t_data = iter_start_time - iter_data_time
+
+ total_iters += opt.batch_size
+ epoch_iter += opt.batch_size
+ model.set_input(data) # unpack data from dataset and apply preprocessing
+ model.optimize_parameters() # calculate loss functions, get gradients, update network weights
+
+ if total_iters % opt.display_freq == 0: # display images on visdom and save images to a HTML file
+ save_result = total_iters % opt.update_html_freq == 0
+ model.compute_visuals()
+ visualizer.display_current_results(model.get_current_visuals(), epoch, save_result)
+
+ if total_iters % opt.print_freq == 0: # print training losses and save logging information to the disk
+ losses = model.get_current_losses()
+ t_comp = (time.time() - iter_start_time) / opt.batch_size
+ visualizer.print_current_losses(epoch, epoch_iter, losses, t_comp, t_data)
+ if opt.display_id > 0:
+ visualizer.plot_current_losses(epoch, float(epoch_iter) / dataset_size, losses)
+
+ if total_iters % opt.save_latest_freq == 0: # cache our latest model every iterations
+ print('saving the latest model (epoch %d, total_iters %d)' % (epoch, total_iters))
+ save_suffix = 'iter_%d' % total_iters if opt.save_by_iter else 'latest'
+ model.save_networks(save_suffix)
+
+ iter_data_time = time.time()
+ if epoch % opt.save_epoch_freq == 0: # cache our model every epochs
+ print('saving the model at the end of epoch %d, iters %d' % (epoch, total_iters))
+ model.save_networks('latest')
+ model.save_networks(epoch)
+
+ print('End of epoch %d / %d \t Time Taken: %d sec' % (epoch, opt.n_epochs + opt.n_epochs_decay, time.time() - epoch_start_time))
+ model.update_learning_rate() # update learning rates at the end of every epoch.
diff --git a/cyclegan/util/InstanceNormalization.lua b/cyclegan/util/InstanceNormalization.lua
deleted file mode 100644
index 595ddfe..0000000
--- a/cyclegan/util/InstanceNormalization.lua
+++ /dev/null
@@ -1,106 +0,0 @@
-require 'nn'
-
---[[
- Implements instance normalization as described in the paper
-
- Instance Normalization: The Missing Ingredient for Fast Stylization
- Dmitry Ulyanov, Andrea Vedaldi, Victor Lempitsky
- https://arxiv.org/abs/1607.08022
- This implementation is based on
- https://github.com/DmitryUlyanov/texture_nets
-]]
-
-local InstanceNormalization, parent = torch.class('nn.InstanceNormalization', 'nn.Module')
-
-function InstanceNormalization:__init(nOutput, eps, momentum, affine)
- parent.__init(self)
- self.running_mean = torch.zeros(nOutput)
- self.running_var = torch.ones(nOutput)
-
- self.eps = eps or 1e-5
- self.momentum = momentum or 0.0
- if affine ~= nil then
- assert(type(affine) == 'boolean', 'affine has to be true/false')
- self.affine = affine
- else
- self.affine = true
- end
-
- self.nOutput = nOutput
- self.prev_batch_size = -1
-
- if self.affine then
- self.weight = torch.Tensor(nOutput):uniform()
- self.bias = torch.Tensor(nOutput):zero()
- self.gradWeight = torch.Tensor(nOutput)
- self.gradBias = torch.Tensor(nOutput)
- end
-end
-
-function InstanceNormalization:updateOutput(input)
- self.output = self.output or input.new()
- assert(input:size(2) == self.nOutput)
-
- local batch_size = input:size(1)
-
- if batch_size ~= self.prev_batch_size or (self.bn and self:type() ~= self.bn:type()) then
- self.bn = nn.SpatialBatchNormalization(input:size(1)*input:size(2), self.eps, self.momentum, self.affine)
- self.bn:type(self:type())
- self.bn.running_mean:copy(self.running_mean:repeatTensor(batch_size))
- self.bn.running_var:copy(self.running_var:repeatTensor(batch_size))
-
- self.prev_batch_size = input:size(1)
- end
-
- -- Get statistics
- self.running_mean:copy(self.bn.running_mean:view(input:size(1),self.nOutput):mean(1))
- self.running_var:copy(self.bn.running_var:view(input:size(1),self.nOutput):mean(1))
-
- -- Set params for BN
- if self.affine then
- self.bn.weight:copy(self.weight:repeatTensor(batch_size))
- self.bn.bias:copy(self.bias:repeatTensor(batch_size))
- end
-
- local input_1obj = input:contiguous():view(1,input:size(1)*input:size(2),input:size(3),input:size(4))
- self.output = self.bn:forward(input_1obj):viewAs(input)
-
- return self.output
-end
-
-function InstanceNormalization:updateGradInput(input, gradOutput)
- self.gradInput = self.gradInput or gradOutput.new()
-
- assert(self.bn)
-
- local input_1obj = input:contiguous():view(1,input:size(1)*input:size(2),input:size(3),input:size(4))
- local gradOutput_1obj = gradOutput:contiguous():view(1,input:size(1)*input:size(2),input:size(3),input:size(4))
-
- if self.affine then
- self.bn.gradWeight:zero()
- self.bn.gradBias:zero()
- end
-
- self.gradInput = self.bn:backward(input_1obj, gradOutput_1obj):viewAs(input)
-
- if self.affine then
- self.gradWeight:add(self.bn.gradWeight:view(input:size(1),self.nOutput):sum(1))
- self.gradBias:add(self.bn.gradBias:view(input:size(1),self.nOutput):sum(1))
- end
- return self.gradInput
-end
-
-function InstanceNormalization:clearState()
- self.output = self.output.new()
- self.gradInput = self.gradInput.new()
-
- if self.bn then
- self.bn:clearState()
- end
-end
-
-function InstanceNormalization:evaluate()
-end
-
-function InstanceNormalization:training()
-end
diff --git a/cyclegan/util/VGG_preprocess.lua b/cyclegan/util/VGG_preprocess.lua
deleted file mode 100644
index 1e74828..0000000
--- a/cyclegan/util/VGG_preprocess.lua
+++ /dev/null
@@ -1,27 +0,0 @@
--- define nn module for VGG postprocessing
-local VGG_postprocess, parent = torch.class('nn.VGG_postprocess', 'nn.Module')
-
-function VGG_postprocess:__init()
- parent.__init(self)
-end
-
-function VGG_postprocess:updateOutput(input)
- self.output = input:add(1):mul(127.5)
- -- print(self.output:max(), self.output:min())
- if self.output:max() > 255 or self.output:min() < 0 then
- print(self.output:min(), self.output:max())
- end
- -- assert(self.output:min()>=0,"badly scaled inputs")
- -- assert(self.output:max()<=255,"badly scaled inputs")
-
- local mean_pixel = torch.FloatTensor({103.939, 116.779, 123.68})
- mean_pixel = mean_pixel:reshape(1,3,1,1)
- mean_pixel = mean_pixel:repeatTensor(input:size(1), 1, input:size(3), input:size(4)):cuda()
- self.output:add(-1, mean_pixel)
- return self.output
-end
-
-function VGG_postprocess:updateGradInput(input, gradOutput)
- self.gradInput = gradOutput:div(127.5)
- return self.gradInput
-end
diff --git a/cyclegan/util/__init__.py b/cyclegan/util/__init__.py
new file mode 100644
index 0000000..ae36f63
--- /dev/null
+++ b/cyclegan/util/__init__.py
@@ -0,0 +1 @@
+"""This package includes a miscellaneous collection of useful helper functions."""
diff --git a/cyclegan/util/content_loss.lua b/cyclegan/util/content_loss.lua
deleted file mode 100644
index d11d469..0000000
--- a/cyclegan/util/content_loss.lua
+++ /dev/null
@@ -1,87 +0,0 @@
-require 'torch'
-require 'nn'
-local content = {}
-
-function content.defineVGG(content_layer)
- local contentFunc = nn.Sequential()
- require 'loadcaffe'
- require 'util/VGG_preprocess'
- cnn = loadcaffe.load('../models/vgg.prototxt', '../models/vgg.caffemodel', 'cudnn')
- contentFunc:add(nn.SpatialUpSamplingBilinear({oheight=224, owidth=224}))
- contentFunc:add(nn.VGG_postprocess())
- for i = 1, #cnn do
- local layer = cnn:get(i):clone()
- local name = layer.name
- local layer_type = torch.type(layer)
- contentFunc:add(layer)
- if name == content_layer then
- print("Setting up content layer: ", layer.name)
- break
- end
- end
- cnn = nil
- collectgarbage()
- print(contentFunc)
- return contentFunc
-end
-
-function content.defineAlexNet(content_layer)
- local contentFunc = nn.Sequential()
- require 'loadcaffe'
- require 'util/VGG_preprocess'
- cnn = loadcaffe.load('../models/alexnet.prototxt', '../models/alexnet.caffemodel', 'cudnn')
- contentFunc:add(nn.SpatialUpSamplingBilinear({oheight=224, owidth=224}))
- contentFunc:add(nn.VGG_postprocess())
- for i = 1, #cnn do
- local layer = cnn:get(i):clone()
- local name = layer.name
- local layer_type = torch.type(layer)
- contentFunc:add(layer)
- if name == content_layer then
- print("Setting up content layer: ", layer.name)
- break
- end
- end
- cnn = nil
- collectgarbage()
- print(contentFunc)
- return contentFunc
-end
-
-
-
-function content.defineContent(content_loss, layer_name)
- -- print('content_loss_define', content_loss)
- if content_loss == 'pixel' or content_loss == 'none' then
- return nil
- elseif content_loss == 'vgg' then
- return content.defineVGG(layer_name)
- else
- print("unsupported content loss")
- return nil
- end
-end
-
-
-function content.lossUpdate(criterionContent, real_source, fake_target, contentFunc, loss_type, weight)
- if loss_type == 'none' then
- local errCont = 0.0
- local df_d_content = torch.zeros(fake_target:size())
- return errCont, df_d_content
- elseif loss_type == 'pixel' then
- local errCont = criterionContent:forward(fake_target, real_source) * weight
- local df_do_content = criterionContent:backward(fake_target, real_source)*weight
- return errCont, df_do_content
- elseif loss_type == 'vgg' then
- local f_fake = contentFunc:forward(fake_target):clone()
- local f_real = contentFunc:forward(real_source):clone()
- local errCont = criterionContent:forward(f_fake, f_real) * weight
- local df_do_tmp = criterionContent:backward(f_fake, f_real) * weight
- local df_do_content = contentFunc:updateGradInput(fake_target, df_do_tmp)--:mul(weight)
- return errCont, df_do_content
- else error("unsupported content loss")
- end
-end
-
-
-return content
diff --git a/cyclegan/util/cudnn_convert_custom.lua b/cyclegan/util/cudnn_convert_custom.lua
deleted file mode 100644
index 8b0c08d..0000000
--- a/cyclegan/util/cudnn_convert_custom.lua
+++ /dev/null
@@ -1,72 +0,0 @@
--- modified from https://github.com/NVIDIA/torch-cudnn/blob/master/convert.lua
--- removed error on nngraph
-
--- modules that can be converted to nn seamlessly
-local layer_list = {
- 'BatchNormalization',
- 'SpatialBatchNormalization',
- 'SpatialConvolution',
- 'SpatialCrossMapLRN',
- 'SpatialFullConvolution',
- 'SpatialMaxPooling',
- 'SpatialAveragePooling',
- 'ReLU',
- 'Tanh',
- 'Sigmoid',
- 'SoftMax',
- 'LogSoftMax',
- 'VolumetricBatchNormalization',
- 'VolumetricConvolution',
- 'VolumetricFullConvolution',
- 'VolumetricMaxPooling',
- 'VolumetricAveragePooling',
-}
-
--- goes over a given net and converts all layers to dst backend
--- for example: net = cudnn_convert_custom(net, cudnn)
--- same as cudnn.convert with gModule check commented out
-function cudnn_convert_custom(net, dst, exclusion_fn)
- return net:replace(function(x)
- --if torch.type(x) == 'nn.gModule' then
- -- io.stderr:write('Warning: cudnn.convert does not work with nngraph yet. Ignoring nn.gModule')
- -- return x
- --end
- local y = 0
- local src = dst == nn and cudnn or nn
- local src_prefix = src == nn and 'nn.' or 'cudnn.'
- local dst_prefix = dst == nn and 'nn.' or 'cudnn.'
-
- local function convert(v)
- local y = {}
- torch.setmetatable(y, dst_prefix..v)
- if v == 'ReLU' then y = dst.ReLU() end -- because parameters
- for k,u in pairs(x) do y[k] = u end
- if src == cudnn and x.clearDesc then x.clearDesc(y) end
- if src == cudnn and v == 'SpatialAveragePooling' then
- y.divide = true
- y.count_include_pad = v.mode == 'CUDNN_POOLING_AVERAGE_COUNT_INCLUDE_PADDING'
- end
- if src == nn and string.find(v, 'Convolution') then
- y.groups = 1
- end
- return y
- end
-
- if exclusion_fn and exclusion_fn(x) then
- return x
- end
- local t = torch.typename(x)
- if t == 'nn.SpatialConvolutionMM' then
- y = convert('SpatialConvolution')
- elseif t == 'inn.SpatialCrossResponseNormalization' then
- y = convert('SpatialCrossMapLRN')
- else
- for i,v in ipairs(layer_list) do
- if torch.typename(x) == src_prefix..v then
- y = convert(v)
- end
- end
- end
- return y == 0 and x or y
- end)
-end
diff --git a/cyclegan/util/get_data.py b/cyclegan/util/get_data.py
new file mode 100644
index 0000000..97edc3c
--- /dev/null
+++ b/cyclegan/util/get_data.py
@@ -0,0 +1,110 @@
+from __future__ import print_function
+import os
+import tarfile
+import requests
+from warnings import warn
+from zipfile import ZipFile
+from bs4 import BeautifulSoup
+from os.path import abspath, isdir, join, basename
+
+
+class GetData(object):
+ """A Python script for downloading CycleGAN or pix2pix datasets.
+
+ Parameters:
+ technique (str) -- One of: 'cyclegan' or 'pix2pix'.
+ verbose (bool) -- If True, print additional information.
+
+ Examples:
+ >>> from util.get_data import GetData
+ >>> gd = GetData(technique='cyclegan')
+ >>> new_data_path = gd.get(save_path='./datasets') # options will be displayed.
+
+ Alternatively, You can use bash scripts: 'scripts/download_pix2pix_model.sh'
+ and 'scripts/download_cyclegan_model.sh'.
+ """
+
+ def __init__(self, technique='cyclegan', verbose=True):
+ url_dict = {
+ 'pix2pix': 'http://efrosgans.eecs.berkeley.edu/pix2pix/datasets/',
+ 'cyclegan': 'https://people.eecs.berkeley.edu/~taesung_park/CycleGAN/datasets'
+ }
+ self.url = url_dict.get(technique.lower())
+ self._verbose = verbose
+
+ def _print(self, text):
+ if self._verbose:
+ print(text)
+
+ @staticmethod
+ def _get_options(r):
+ soup = BeautifulSoup(r.text, 'lxml')
+ options = [h.text for h in soup.find_all('a', href=True)
+ if h.text.endswith(('.zip', 'tar.gz'))]
+ return options
+
+ def _present_options(self):
+ r = requests.get(self.url)
+ options = self._get_options(r)
+ print('Options:\n')
+ for i, o in enumerate(options):
+ print("{0}: {1}".format(i, o))
+ choice = input("\nPlease enter the number of the "
+ "dataset above you wish to download:")
+ return options[int(choice)]
+
+ def _download_data(self, dataset_url, save_path):
+ if not isdir(save_path):
+ os.makedirs(save_path)
+
+ base = basename(dataset_url)
+ temp_save_path = join(save_path, base)
+
+ with open(temp_save_path, "wb") as f:
+ r = requests.get(dataset_url)
+ f.write(r.content)
+
+ if base.endswith('.tar.gz'):
+ obj = tarfile.open(temp_save_path)
+ elif base.endswith('.zip'):
+ obj = ZipFile(temp_save_path, 'r')
+ else:
+ raise ValueError("Unknown File Type: {0}.".format(base))
+
+ self._print("Unpacking Data...")
+ obj.extractall(save_path)
+ obj.close()
+ os.remove(temp_save_path)
+
+ def get(self, save_path, dataset=None):
+ """
+
+ Download a dataset.
+
+ Parameters:
+ save_path (str) -- A directory to save the data to.
+ dataset (str) -- (optional). A specific dataset to download.
+ Note: this must include the file extension.
+ If None, options will be presented for you
+ to choose from.
+
+ Returns:
+ save_path_full (str) -- the absolute path to the downloaded data.
+
+ """
+ if dataset is None:
+ selected_dataset = self._present_options()
+ else:
+ selected_dataset = dataset
+
+ save_path_full = join(save_path, selected_dataset.split('.')[0])
+
+ if isdir(save_path_full):
+ warn("\n'{0}' already exists. Voiding Download.".format(
+ save_path_full))
+ else:
+ self._print('Downloading Data...')
+ url = "{0}/{1}".format(self.url, selected_dataset)
+ self._download_data(url, save_path=save_path)
+
+ return abspath(save_path_full)
diff --git a/cyclegan/util/html.py b/cyclegan/util/html.py
new file mode 100644
index 0000000..cc3262a
--- /dev/null
+++ b/cyclegan/util/html.py
@@ -0,0 +1,86 @@
+import dominate
+from dominate.tags import meta, h3, table, tr, td, p, a, img, br
+import os
+
+
+class HTML:
+ """This HTML class allows us to save images and write texts into a single HTML file.
+
+ It consists of functions such as (add a text header to the HTML file),
+ (add a row of images to the HTML file), and (save the HTML to the disk).
+ It is based on Python library 'dominate', a Python library for creating and manipulating HTML documents using a DOM API.
+ """
+
+ def __init__(self, web_dir, title, refresh=0):
+ """Initialize the HTML classes
+
+ Parameters:
+ web_dir (str) -- a directory that stores the webpage. HTML file will be created at /index.html; images will be saved at 0:
+ with self.doc.head:
+ meta(http_equiv="refresh", content=str(refresh))
+
+ def get_image_dir(self):
+ """Return the directory that stores images"""
+ return self.img_dir
+
+ def add_header(self, text):
+ """Insert a header to the HTML file
+
+ Parameters:
+ text (str) -- the header text
+ """
+ with self.doc:
+ h3(text)
+
+ def add_images(self, ims, txts, links, width=400):
+ """add images to the HTML file
+
+ Parameters:
+ ims (str list) -- a list of image paths
+ txts (str list) -- a list of image names shown on the website
+ links (str list) -- a list of hyperref links; when you click an image, it will redirect you to a new page
+ """
+ self.t = table(border=1, style="table-layout: fixed;") # Insert a table
+ self.doc.add(self.t)
+ with self.t:
+ with tr():
+ for im, txt, link in zip(ims, txts, links):
+ with td(style="word-wrap: break-word;", halign="center", valign="top"):
+ with p():
+ with a(href=os.path.join('images', link)):
+ img(style="width:%dpx" % width, src=os.path.join('images', im))
+ br()
+ p(txt)
+
+ def save(self):
+ """save the current content to the HMTL file"""
+ html_file = '%s/index.html' % self.web_dir
+ f = open(html_file, 'wt')
+ f.write(self.doc.render())
+ f.close()
+
+
+if __name__ == '__main__': # we show an example usage here.
+ html = HTML('web/', 'test_html')
+ html.add_header('hello world')
+
+ ims, txts, links = [], [], []
+ for n in range(4):
+ ims.append('image_%d.png' % n)
+ txts.append('text_%d' % n)
+ links.append('image_%d.png' % n)
+ html.add_images(ims, txts, links)
+ html.save()
diff --git a/cyclegan/util/image_pool.lua b/cyclegan/util/image_pool.lua
deleted file mode 100644
index f62cd85..0000000
--- a/cyclegan/util/image_pool.lua
+++ /dev/null
@@ -1,59 +0,0 @@
-local class = require 'class'
-ImagePool= class('ImagePool')
-
-require 'torch'
-require 'image'
-
-function ImagePool:__init(pool_size)
- self.pool_size = pool_size
- if pool_size > 0 then
- self.num_imgs = 0
- self.images = {}
- end
-end
-
-function ImagePool:model_name()
- return 'ImagePool'
-end
---
--- function ImagePool:Initialize(pool_size)
--- -- torch.manualSeed(0)
--- -- assert(pool_size > 0)
--- self.pool_size = pool_size
--- if pool_size > 0 then
--- self.num_imgs = 0
--- self.images = {}
--- end
--- end
-
-function ImagePool:Query(image)
- -- print('query image')
- if self.pool_size == 0 then
- -- print('get identical image')
- return image
- end
- if self.num_imgs < self.pool_size then
- -- self.images.insert(image:clone())
- self.num_imgs = self.num_imgs + 1
- self.images[self.num_imgs] = image
- return image
- else
- local p = math.random()
- -- print('p' ,p)
- -- os.exit()
- if p > 0.5 then
- -- print('use old image')
- -- random_id = torch.Tensor(1)
- -- random_id:random(1, self.pool_size)
- local random_id = math.random(self.pool_size)
- -- print('random_id', random_id)
- local tmp = self.images[random_id]:clone()
- self.images[random_id] = image:clone()
- return tmp
- else
- return image
- end
-
- end
-
-end
diff --git a/cyclegan/util/image_pool.py b/cyclegan/util/image_pool.py
new file mode 100644
index 0000000..6d086f8
--- /dev/null
+++ b/cyclegan/util/image_pool.py
@@ -0,0 +1,54 @@
+import random
+import torch
+
+
+class ImagePool():
+ """This class implements an image buffer that stores previously generated images.
+
+ This buffer enables us to update discriminators using a history of generated images
+ rather than the ones produced by the latest generators.
+ """
+
+ def __init__(self, pool_size):
+ """Initialize the ImagePool class
+
+ Parameters:
+ pool_size (int) -- the size of image buffer, if pool_size=0, no buffer will be created
+ """
+ self.pool_size = pool_size
+ if self.pool_size > 0: # create an empty pool
+ self.num_imgs = 0
+ self.images = []
+
+ def query(self, images):
+ """Return an image from the pool.
+
+ Parameters:
+ images: the latest generated images from the generator
+
+ Returns images from the buffer.
+
+ By 50/100, the buffer will return input images.
+ By 50/100, the buffer will return images previously stored in the buffer,
+ and insert the current images to the buffer.
+ """
+ if self.pool_size == 0: # if the buffer size is 0, do nothing
+ return images
+ return_images = []
+ for image in images:
+ image = torch.unsqueeze(image.data, 0)
+ if self.num_imgs < self.pool_size: # if the buffer is not full; keep inserting current images to the buffer
+ self.num_imgs = self.num_imgs + 1
+ self.images.append(image)
+ return_images.append(image)
+ else:
+ p = random.uniform(0, 1)
+ if p > 0.5: # by 50% chance, the buffer will return a previously stored image, and insert the current image into the buffer
+ random_id = random.randint(0, self.pool_size - 1) # randint is inclusive
+ tmp = self.images[random_id].clone()
+ self.images[random_id] = image
+ return_images.append(tmp)
+ else: # by another 50% chance, the buffer will return the current image
+ return_images.append(image)
+ return_images = torch.cat(return_images, 0) # collect all the images and return
+ return return_images
diff --git a/cyclegan/util/plot_util.lua b/cyclegan/util/plot_util.lua
deleted file mode 100644
index b20bcb6..0000000
--- a/cyclegan/util/plot_util.lua
+++ /dev/null
@@ -1,44 +0,0 @@
-local class = require 'class'
-PlotUtil = class('PlotUtil')
-
-
-require 'torch'
-disp = require 'display'
-util = require 'util/util'
-require 'image'
-
-local unpack = unpack or table.unpack
-
-function PlotUtil:__init(conf)
- conf = conf or {}
-end
-
-function PlotUtil:model_name()
- return 'PlotUtil'
-end
-
-function PlotUtil:Initialize(display_plot, display_id, name)
- self.display_plot = string.split(string.gsub(display_plot, "%s+", ""), ",")
-
- self.plot_config = {
- title = name .. ' loss over time',
- labels = {'epoch', unpack(self.display_plot)},
- ylabel = 'loss',
- win = display_id,
- }
-
- self.plot_data = {}
- print('display_opt', self.display_plot)
-end
-
-
-function PlotUtil:Display(plot_vals, loss)
- for k, v in ipairs(self.display_plot) do
- if loss[v] ~= nil then
- plot_vals[#plot_vals + 1] = loss[v]
- end
- end
-
- table.insert(self.plot_data, plot_vals)
- disp.plot(self.plot_data, self.plot_config)
-end
diff --git a/cyclegan/util/util.lua b/cyclegan/util/util.lua
deleted file mode 100644
index 2b00ede..0000000
--- a/cyclegan/util/util.lua
+++ /dev/null
@@ -1,266 +0,0 @@
---
--- code derived from https://github.com/soumith/dcgan.torch
---
-
-local util = {}
-
-require 'torch'
-
-
-function util.BiasZero(net)
- net:apply(function(m) if torch.type(m):find('Convolution') then m.bias:zero() end end)
-end
-
-
-function util.checkEqual(A, B, name)
- local dif = (A:float()-B:float()):abs():mean()
- print(name, dif)
-end
-
-function util.containsValue(table, value)
- for k, v in pairs(table) do
- if v == value then return true end
- end
- return false
-end
-
-
-function util.CheckTensor(A, name)
- print(name, A:min(), A:max(), A:mean())
-end
-
-
-function util.normalize(img)
- -- rescale image to 0 .. 1
- local min = img:min()
- local max = img:max()
-
- img = torch.FloatTensor(img:size()):copy(img)
- img:add(-min):mul(1/(max-min))
- return img
-end
-
-function util.normalizeBatch(batch)
- for i = 1, batch:size(1) do
- batch[i] = util.normalize(batch[i]:squeeze())
- end
- return batch
-end
-
-function util.basename_batch(batch)
- for i = 1, #batch do
- batch[i] = paths.basename(batch[i])
- end
- return batch
-end
-
-
-
--- default preprocessing
---
--- Preprocesses an image before passing it to a net
--- Converts from RGB to BGR and rescales from [0,1] to [-1,1]
-function util.preprocess(img)
- -- RGB to BGR
- if img:size(1) == 3 then
- local perm = torch.LongTensor{3, 2, 1}
- img = img:index(1, perm)
- end
- -- [0,1] to [-1,1]
- img = img:mul(2):add(-1)
-
- -- check that input is in expected range
- assert(img:max()<=1,"badly scaled inputs")
- assert(img:min()>=-1,"badly scaled inputs")
-
- return img
-end
-
--- Undo the above preprocessing.
-function util.deprocess(img)
- -- BGR to RGB
- if img:size(1) == 3 then
- local perm = torch.LongTensor{3, 2, 1}
- img = img:index(1, perm)
- end
-
- -- [-1,1] to [0,1]
- img = img:add(1):div(2)
-
- return img
-end
-
-function util.preprocess_batch(batch)
- for i = 1, batch:size(1) do
- batch[i] = util.preprocess(batch[i]:squeeze())
- end
- return batch
-end
-
-function util.print_tensor(name, x)
- print(name, x:size(), x:min(), x:mean(), x:max())
-end
-
-function util.deprocess_batch(batch)
- for i = 1, batch:size(1) do
- batch[i] = util.deprocess(batch[i]:squeeze())
- end
- return batch
-end
-
-
-function util.scaleBatch(batch,s1,s2)
- -- print('s1', s1)
- -- print('s2', s2)
- local scaled_batch = torch.Tensor(batch:size(1),batch:size(2),s1,s2)
- for i = 1, batch:size(1) do
- scaled_batch[i] = image.scale(batch[i],s1,s2):squeeze()
- end
- return scaled_batch
-end
-
-
-
-function util.toTrivialBatch(input)
- return input:reshape(1,input:size(1),input:size(2),input:size(3))
-end
-function util.fromTrivialBatch(input)
- return input[1]
-end
-
--- input is between -1 and 1
-function util.jitter(input)
- local noise = torch.rand(input:size())/256.0
- input:add(1.0):mul(0.5*255.0/256.0):add(noise):add(-0.5):mul(2.0)
- --local scaled = (input+1.0)*0.5
- --local jittered = scaled*255.0/256.0 + torch.rand(input:size())/256.0
- --local scaled_back = (jittered-0.5)*2.0
- --return scaled_back
-end
-
-function util.scaleImage(input, loadSize)
-
- -- replicate bw images to 3 channels
- if input:size(1)==1 then
- input = torch.repeatTensor(input,3,1,1)
- end
-
- input = image.scale(input, loadSize, loadSize)
-
- return input
-end
-
-function util.getAspectRatio(path)
- local input = image.load(path, 3, 'float')
- local ar = input:size(3)/input:size(2)
- return ar
-end
-
-function util.loadImage(path, loadSize, nc)
- local input = image.load(path, 3, 'float')
- input= util.preprocess(util.scaleImage(input, loadSize))
-
- if nc == 1 then
- input = input[{{1}, {}, {}}]
- end
-
- return input
-end
-
-function file_exists(filename)
- local f = io.open(filename,"r")
- if f ~= nil then io.close(f) return true else return false end
-end
-
--- TO DO: loading code is rather hacky; clean it up and make sure it works on all types of nets / cpu/gpu configurations
-function load_helper(filename, opt)
- fileExists = file_exists(filename)
- if not fileExists then
- print('model not found! ' .. filename)
- return nil
- end
- print(('loading previously trained model (%s)'):format(filename))
- if opt.norm == 'instance' then
- print('use InstanceNormalization')
- require 'util.InstanceNormalization'
- end
-
- if opt.cudnn>0 then
- require 'cudnn'
- end
-
- local net = torch.load(filename)
- if opt.gpu > 0 then
- require 'cunn'
- net:cuda()
-
- -- calling cuda on cudnn saved nngraphs doesn't change all variables to cuda, so do it below
- if net.forwardnodes then
- for i=1,#net.forwardnodes do
- if net.forwardnodes[i].data.module then
- net.forwardnodes[i].data.module:cuda()
- end
- end
- end
-
- else
- net:float()
- end
- net:apply(function(m) if m.weight then
- m.gradWeight = m.weight:clone():zero();
- m.gradBias = m.bias:clone():zero(); end end)
- return net
-end
-
-function util.load_model(name, opt)
- -- if opt['lambda_'.. name] > 0.0 then
- -- print('not loading model '.. opt.checkpoints_dir .. opt.name ..
- -- 'latest_net_' .. name .. '.t7' .. ' because opt.lambda is not greater than zero')
- return load_helper(paths.concat(opt.checkpoints_dir, opt.name,
- 'latest_net_' .. name .. '.t7'), opt)
- -- end
-end
-
-function util.load_test_model(name, opt)
- return load_helper(paths.concat(opt.checkpoints_dir, opt.name,
- opt.which_epoch .. '_net_' .. name .. '.t7'), opt)
-end
-
-
--- load dataset from the file system
--- |name|: name of the dataset. It's currently either 'A' or 'B'
--- function util.load_dataset(name, nc, opt, nc)
--- local tensortype = torch.getdefaulttensortype()
--- torch.setdefaulttensortype('torch.FloatTensor')
---
--- local new_opt = options.clone(opt)
--- new_opt.manualSeed = torch.random(1, 10000) -- fix seed
--- new_opt.nc = nc
--- torch.manualSeed(new_opt.manualSeed)
--- local data_loader = paths.dofile('../data/data.lua')
--- new_opt.phase = new_opt.phase .. name
--- local data = data_loader.new(new_opt.nThreads, new_opt)
--- print("Dataset Size " .. name .. ": ", data:size())
---
--- torch.setdefaulttensortype(tensortype)
--- return data
--- end
-
-
-
-function util.cudnn(net)
- require 'cudnn'
- require 'util/cudnn_convert_custom'
- return cudnn_convert_custom(net, cudnn)
-end
-
-function util.save_model(net, net_name, weight)
- if weight > 0.0 then
- torch.save(paths.concat(opt.checkpoints_dir, opt.name, net_name), net:clearState())
- end
-end
-
-
-
-
-return util
diff --git a/cyclegan/util/util.py b/cyclegan/util/util.py
new file mode 100644
index 0000000..b050c13
--- /dev/null
+++ b/cyclegan/util/util.py
@@ -0,0 +1,103 @@
+"""This module contains simple helper functions """
+from __future__ import print_function
+import torch
+import numpy as np
+from PIL import Image
+import os
+
+
+def tensor2im(input_image, imtype=np.uint8):
+ """"Converts a Tensor array into a numpy image array.
+
+ Parameters:
+ input_image (tensor) -- the input image tensor array
+ imtype (type) -- the desired type of the converted numpy array
+ """
+ if not isinstance(input_image, np.ndarray):
+ if isinstance(input_image, torch.Tensor): # get the data from a variable
+ image_tensor = input_image.data
+ else:
+ return input_image
+ image_numpy = image_tensor[0].cpu().float().numpy() # convert it into a numpy array
+ if image_numpy.shape[0] == 1: # grayscale to RGB
+ image_numpy = np.tile(image_numpy, (3, 1, 1))
+ image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 # post-processing: tranpose and scaling
+ else: # if it is a numpy array, do nothing
+ image_numpy = input_image
+ return image_numpy.astype(imtype)
+
+
+def diagnose_network(net, name='network'):
+ """Calculate and print the mean of average absolute(gradients)
+
+ Parameters:
+ net (torch network) -- Torch network
+ name (str) -- the name of the network
+ """
+ mean = 0.0
+ count = 0
+ for param in net.parameters():
+ if param.grad is not None:
+ mean += torch.mean(torch.abs(param.grad.data))
+ count += 1
+ if count > 0:
+ mean = mean / count
+ print(name)
+ print(mean)
+
+
+def save_image(image_numpy, image_path, aspect_ratio=1.0):
+ """Save a numpy image to the disk
+
+ Parameters:
+ image_numpy (numpy array) -- input numpy array
+ image_path (str) -- the path of the image
+ """
+
+ image_pil = Image.fromarray(image_numpy)
+ h, w, _ = image_numpy.shape
+
+ if aspect_ratio > 1.0:
+ image_pil = image_pil.resize((h, int(w * aspect_ratio)), Image.BICUBIC)
+ if aspect_ratio < 1.0:
+ image_pil = image_pil.resize((int(h / aspect_ratio), w), Image.BICUBIC)
+ image_pil.save(image_path)
+
+
+def print_numpy(x, val=True, shp=False):
+ """Print the mean, min, max, median, std, and size of a numpy array
+
+ Parameters:
+ val (bool) -- if print the values of the numpy array
+ shp (bool) -- if print the shape of the numpy array
+ """
+ x = x.astype(np.float64)
+ if shp:
+ print('shape,', x.shape)
+ if val:
+ x = x.flatten()
+ print('mean = %3.3f, min = %3.3f, max = %3.3f, median = %3.3f, std=%3.3f' % (
+ np.mean(x), np.min(x), np.max(x), np.median(x), np.std(x)))
+
+
+def mkdirs(paths):
+ """create empty directories if they don't exist
+
+ Parameters:
+ paths (str list) -- a list of directory paths
+ """
+ if isinstance(paths, list) and not isinstance(paths, str):
+ for path in paths:
+ mkdir(path)
+ else:
+ mkdir(paths)
+
+
+def mkdir(path):
+ """create a single empty directory if it didn't exist
+
+ Parameters:
+ path (str) -- a single directory path
+ """
+ if not os.path.exists(path):
+ os.makedirs(path)
diff --git a/cyclegan/util/visualizer.lua b/cyclegan/util/visualizer.lua
deleted file mode 100644
index 2cf2ca3..0000000
--- a/cyclegan/util/visualizer.lua
+++ /dev/null
@@ -1,63 +0,0 @@
--------------------------------------------------------------
--- Various utilities for visualization through the web server
--------------------------------------------------------------
-
-local visualizer = {}
-
-require 'torch'
-disp = nil
-print(opt)
-if opt.display_id > 0 then -- [hack]: assume that opt already existed
- disp = require 'display'
-end
-util = require 'util/util'
-require 'image'
-
--- function visualizer
-function visualizer.disp_image(img_data, win_size, display_id, title)
- images = util.deprocess_batch(util.scaleBatch(img_data:float(),win_size,win_size))
- disp.image(images, {win=display_id, title=title})
-end
-
-function visualizer.save_results(img_data, output_path)
- local tensortype = torch.getdefaulttensortype()
- torch.setdefaulttensortype('torch.FloatTensor')
- local image_out = nil
- local win_size = opt.display_winsize
- images = torch.squeeze(util.deprocess_batch(util.scaleBatch(img_data:float(), win_size, win_size)))
-
- if images:dim() == 3 then
- image_out = images
- else
- for i = 1,images:size(1) do
- img = images[i]
- if image_out == nil then
- image_out = img
- else
- image_out = torch.cat(image_out, img)
- end
- end
- end
- image.save(output_path, image_out)
- torch.setdefaulttensortype(tensortype)
-end
-
-function visualizer.save_images(imgs, save_dir, impaths, s1, s2)
- local tensortype = torch.getdefaulttensortype()
- torch.setdefaulttensortype('torch.FloatTensor')
- batchSize = imgs:size(1)
- imgs_f = util.deprocess_batch(imgs):float()
- paths.mkdir(save_dir)
- for i = 1, batchSize do -- imgs_f[i]:size(2), imgs_f[i]:size(3)/opt.aspect_ratio
- if s1 ~= nil and s2 ~= nil then
- im_s = image.scale(imgs_f[i], s1, s2):float()
- else
- im_s = imgs_f[i]:float()
- end
- img_to_save = torch.FloatTensor(im_s:size()):copy(im_s)
- image.save(paths.concat(save_dir, impaths[i]), img_to_save)
- end
- torch.setdefaulttensortype(tensortype)
-end
-
-return visualizer
diff --git a/cyclegan/util/visualizer.py b/cyclegan/util/visualizer.py
new file mode 100644
index 0000000..9736b5c
--- /dev/null
+++ b/cyclegan/util/visualizer.py
@@ -0,0 +1,221 @@
+import numpy as np
+import os
+import sys
+import ntpath
+import time
+from . import util, html
+from subprocess import Popen, PIPE
+
+
+if sys.version_info[0] == 2:
+ VisdomExceptionBase = Exception
+else:
+ VisdomExceptionBase = ConnectionError
+
+
+def save_images(webpage, visuals, image_path, aspect_ratio=1.0, width=256):
+ """Save images to the disk.
+
+ Parameters:
+ webpage (the HTML class) -- the HTML webpage class that stores these imaegs (see html.py for more details)
+ visuals (OrderedDict) -- an ordered dictionary that stores (name, images (either tensor or numpy) ) pairs
+ image_path (str) -- the string is used to create image paths
+ aspect_ratio (float) -- the aspect ratio of saved images
+ width (int) -- the images will be resized to width x width
+
+ This function will save images stored in 'visuals' to the HTML file specified by 'webpage'.
+ """
+ image_dir = webpage.get_image_dir()
+ short_path = ntpath.basename(image_path[0])
+ name = os.path.splitext(short_path)[0]
+
+ webpage.add_header(name)
+ ims, txts, links = [], [], []
+
+ for label, im_data in visuals.items():
+ im = util.tensor2im(im_data)
+ image_name = '%s_%s.png' % (name, label)
+ save_path = os.path.join(image_dir, image_name)
+ util.save_image(im, save_path, aspect_ratio=aspect_ratio)
+ ims.append(image_name)
+ txts.append(label)
+ links.append(image_name)
+ webpage.add_images(ims, txts, links, width=width)
+
+
+class Visualizer():
+ """This class includes several functions that can display/save images and print/save logging information.
+
+ It uses a Python library 'visdom' for display, and a Python library 'dominate' (wrapped in 'HTML') for creating HTML files with images.
+ """
+
+ def __init__(self, opt):
+ """Initialize the Visualizer class
+
+ Parameters:
+ opt -- stores all the experiment flags; needs to be a subclass of BaseOptions
+ Step 1: Cache the training/test options
+ Step 2: connect to a visdom server
+ Step 3: create an HTML object for saveing HTML filters
+ Step 4: create a logging file to store training losses
+ """
+ self.opt = opt # cache the option
+ self.display_id = opt.display_id
+ self.use_html = opt.isTrain and not opt.no_html
+ self.win_size = opt.display_winsize
+ self.name = opt.name
+ self.port = opt.display_port
+ self.saved = False
+ if self.display_id > 0: # connect to a visdom server given and
+ import visdom
+ self.ncols = opt.display_ncols
+ self.vis = visdom.Visdom(server=opt.display_server, port=opt.display_port, env=opt.display_env)
+ if not self.vis.check_connection():
+ self.create_visdom_connections()
+
+ if self.use_html: # create an HTML object at /web/; images will be saved under /web/images/
+ self.web_dir = os.path.join(opt.checkpoints_dir, opt.name, 'web')
+ self.img_dir = os.path.join(self.web_dir, 'images')
+ print('create web directory %s...' % self.web_dir)
+ util.mkdirs([self.web_dir, self.img_dir])
+ # create a logging file to store training losses
+ self.log_name = os.path.join(opt.checkpoints_dir, opt.name, 'loss_log.txt')
+ with open(self.log_name, "a") as log_file:
+ now = time.strftime("%c")
+ log_file.write('================ Training Loss (%s) ================\n' % now)
+
+ def reset(self):
+ """Reset the self.saved status"""
+ self.saved = False
+
+ def create_visdom_connections(self):
+ """If the program could not connect to Visdom server, this function will start a new server at port < self.port > """
+ cmd = sys.executable + ' -m visdom.server -p %d &>/dev/null &' % self.port
+ print('\n\nCould not connect to Visdom server. \n Trying to start a server....')
+ print('Command: %s' % cmd)
+ Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
+
+ def display_current_results(self, visuals, epoch, save_result):
+ """Display current results on visdom; save current results to an HTML file.
+
+ Parameters:
+ visuals (OrderedDict) - - dictionary of images to display or save
+ epoch (int) - - the current epoch
+ save_result (bool) - - if save the current results to an HTML file
+ """
+ if self.display_id > 0: # show images in the browser using visdom
+ ncols = self.ncols
+ if ncols > 0: # show all the images in one visdom panel
+ ncols = min(ncols, len(visuals))
+ h, w = next(iter(visuals.values())).shape[:2]
+ table_css = """""" % (w, h) # create a table css
+ # create a table of images.
+ title = self.name
+ label_html = ''
+ label_html_row = ''
+ images = []
+ idx = 0
+ for label, image in visuals.items():
+ image_numpy = util.tensor2im(image)
+ label_html_row += '%s | ' % label
+ images.append(image_numpy.transpose([2, 0, 1]))
+ idx += 1
+ if idx % ncols == 0:
+ label_html += '%s
' % label_html_row
+ label_html_row = ''
+ white_image = np.ones_like(image_numpy.transpose([2, 0, 1])) * 255
+ while idx % ncols != 0:
+ images.append(white_image)
+ label_html_row += ' | '
+ idx += 1
+ if label_html_row != '':
+ label_html += '%s
' % label_html_row
+ try:
+ self.vis.images(images, nrow=ncols, win=self.display_id + 1,
+ padding=2, opts=dict(title=title + ' images'))
+ label_html = '' % label_html
+ self.vis.text(table_css + label_html, win=self.display_id + 2,
+ opts=dict(title=title + ' labels'))
+ except VisdomExceptionBase:
+ self.create_visdom_connections()
+
+ else: # show each image in a separate visdom panel;
+ idx = 1
+ try:
+ for label, image in visuals.items():
+ image_numpy = util.tensor2im(image)
+ self.vis.image(image_numpy.transpose([2, 0, 1]), opts=dict(title=label),
+ win=self.display_id + idx)
+ idx += 1
+ except VisdomExceptionBase:
+ self.create_visdom_connections()
+
+ if self.use_html and (save_result or not self.saved): # save images to an HTML file if they haven't been saved.
+ self.saved = True
+ # save images to the disk
+ for label, image in visuals.items():
+ image_numpy = util.tensor2im(image)
+ img_path = os.path.join(self.img_dir, 'epoch%.3d_%s.png' % (epoch, label))
+ util.save_image(image_numpy, img_path)
+
+ # update website
+ webpage = html.HTML(self.web_dir, 'Experiment name = %s' % self.name, refresh=1)
+ for n in range(epoch, 0, -1):
+ webpage.add_header('epoch [%d]' % n)
+ ims, txts, links = [], [], []
+
+ for label, image_numpy in visuals.items():
+ image_numpy = util.tensor2im(image)
+ img_path = 'epoch%.3d_%s.png' % (n, label)
+ ims.append(img_path)
+ txts.append(label)
+ links.append(img_path)
+ webpage.add_images(ims, txts, links, width=self.win_size)
+ webpage.save()
+
+ def plot_current_losses(self, epoch, counter_ratio, losses):
+ """display the current losses on visdom display: dictionary of error labels and values
+
+ Parameters:
+ epoch (int) -- current epoch
+ counter_ratio (float) -- progress (percentage) in the current epoch, between 0 to 1
+ losses (OrderedDict) -- training losses stored in the format of (name, float) pairs
+ """
+ if not hasattr(self, 'plot_data'):
+ self.plot_data = {'X': [], 'Y': [], 'legend': list(losses.keys())}
+ self.plot_data['X'].append(epoch + counter_ratio)
+ self.plot_data['Y'].append([losses[k] for k in self.plot_data['legend']])
+ try:
+ self.vis.line(
+ X=np.stack([np.array(self.plot_data['X'])] * len(self.plot_data['legend']), 1),
+ Y=np.array(self.plot_data['Y']),
+ opts={
+ 'title': self.name + ' loss over time',
+ 'legend': self.plot_data['legend'],
+ 'xlabel': 'epoch',
+ 'ylabel': 'loss'},
+ win=self.display_id)
+ except VisdomExceptionBase:
+ self.create_visdom_connections()
+
+ # losses: same format as |losses| of plot_current_losses
+ def print_current_losses(self, epoch, iters, losses, t_comp, t_data):
+ """print current losses on console; also save the losses to the disk
+
+ Parameters:
+ epoch (int) -- current epoch
+ iters (int) -- current training iteration during this epoch (reset to 0 at the end of every epoch)
+ losses (OrderedDict) -- training losses stored in the format of (name, float) pairs
+ t_comp (float) -- computational time per data point (normalized by batch_size)
+ t_data (float) -- data loading time per data point (normalized by batch_size)
+ """
+ message = '(epoch: %d, iters: %d, time: %.3f, data: %.3f) ' % (epoch, iters, t_comp, t_data)
+ for k, v in losses.items():
+ message += '%s: %.3f ' % (k, v)
+
+ print(message) # print the message
+ with open(self.log_name, "a") as log_file:
+ log_file.write('%s\n' % message) # save the message