CycleGAN的代码组成

train.py 是通用训练脚本。它为许多模型(可选的模型比如:pix2pix, cyclegan, colorization)和不同的数据集服务(可选的数据集模式包括:aligned, unaligned, single, colorization)。

test.py 是通用测试脚本。如果用train.py训练好了自己的模型,可以用测试脚本对模型进行测试。脚本会从 checkpoints_dir 中加载保存好的模型,并将结果保存在 results_dir 处。

data 目录包括了所有与数据加载和预处理相关的模块。如果想要添加名为 dummy 的自定义数据集类,则需要创建 dummy_dataset.py 文件,并定义继承 Base Dataset 的子类(subclass)Dummy Dataset。需要实现四个功能:1)__init__(初始化类,首先要调用BaseDataset.__init__(self, opt)),2)__len__(返回数据集大小),3)__getitem__(获取数据点), 4)modify_commandline_options(添加特定于数据集的选项,设置默认选项)。可以通过指定--dataset_mode去使用数据集类(dataset class)。下面详细解释每个文件。

        __init__.py 实现了data包和训练及测试脚本文件的接口,train.py 和 test.py 从 data import create_dataset 和 dataset = create_dataset(opt) 调用数据,用给定的选项 opt. 创建数据集。

        base_dataset.py 为数据集实现了理论上的基类 (ABC)。它还包括常见的转换函数(比如  get_transform,__scale_width),这些函数可以在之后的子类中使用。

        image_folder.py 实现一个 image folder 类。我们修改了官方 PyTorch 的 image folder 代码以便这个类可以从当前目录及其子目录中加载图像。

        template_dataset.py 提供了一个包含详细文档的数据集模板。如果计划实现自己的数据集可以检查此文件。

        aligned_dataset.py 包含一个可以加载图像对的数据集类。它假设一个单一图像目录 /path/to/data/train,这个目录包括 {A,B} 形式的图像对。在测试时,需要准备目录 /path/to/data/test作为测试数据。

        unaligned_dataset.py 包含一个可以加载未对齐/未配对数据集的数据集类。它假设两个目录分别承载来自域A /path/to/data/trainA 和域B /path/to/data/trainB 的训练图像。可以用数据集标志 --dataroot /path/to/data 训练模型。同样,需要在测试期间准备两个目录 /path/to/data/testA 和 /path/to/data/testB。

        single_dataset.py 包含一个 dataset 类,它可以通过指定路径 --dataroot /path/to/data 来加载一组单个图像。它可以用可选的测试模型来生成单边 CycleGAN 的结果。

        colorization_dataset.py 实现了一个 dataset 类,可以加载一组 RGB 的自然图像,并且在Lab color space 中将 RGB 格式转换为 (L, ab) 对。它是基于 Pix2Pix 的着色模型所要求的 (--model colorization)。

models 目录包含了与目标函数、优化和网络体系结构相关的模块。要添加名为 dummy 的自定义数据集类,则需要创建 dummy_model.py 文件,并定义一个继承 Base Model 的子类(subclass)Dummy Model。需要实现以下功能:1)__init__(初始化类,首先要调用BaseModel.__init__(self, opt)),2)set__input(从数据集中解包数据并进行预处理),3)forward(生成中间结果), 4)optimize_parameters(计算损失、梯度,更新网络权重)。5)也可以添加modify_commandline_options (通过添加特定于模型的选项来设置默认选项)。可以通过指定标志 --model dummy 来使用模型类。下面详细解释每个文件。  

        __init__.py 实现了 model 包和训练及测试脚本文件的接口,train.py 和 test.py用 from models import create_model 和 model = create_model(opt) ,用给定的选项 opt.创建模型。用 model.setup(opt) 初始化模型。

        base_model.py 为模型实现了理论上的基类 (ABC) 。它还包括常用的辅助功能(比如: setup, test, update_learning_rate, save_networks,load_networks),这些功能之后会在子类中用到。

        template_model.py 提供带有详细文档的模型模板。如果想要实现自己的模型,可以检查此文件。

        pix2pix_model.py 实现了 Pix2Pix 模型,用于学习成对图像的从输入到输出的映射。模型训练需要 --dataset_mode aligned 数据集。默认情况下,它使用 --netG unet256 网络,--netD basic 判别器(PatchGAN)和 --gan_model vanillagan 损失(标准交叉熵目标)。

        colorization_model.py 实现了 Pix2PixModel 的子类,用于图像着色(黑白图像到彩色图像)。模型训练需要 -dataset_model colorization 数据集。它训练一个 Pix2Pix 模型,在 Lab color space 中从 L 通道映射到 ab 通道。默认情况下,colorization 数据集将自动设置 --input_nc 1 和 --output_nc 2。

        cycle_gan_model.py 实现了 cycle gan 模型,用于学习未成对图像到图像转换。模型训练需要 --dataset_mode unaligned 数据集。默认情况下,它使用 --netG resnet_9blocks 生成器、--netD basic 判别器和最小二乘 Gans 目标( --gan_model lsgan)。

        networks.py 模块实现网络架构(生成器和鉴别器),以及规范化层、初始化方法、优化调度程序(即学习率策略)和 GAN 目标函数(vanilla、lsgan、wgangp)。

        test_model.py 实现用于生成单边Cyclegan结果的模型。这个模型将自动设置--dataset_mode single,它只从一个集合中加载图像。(--model cycle_gan可进行双向测试)

options 目录包括选项模块:训练选项、测试选项和基本选项(用于训练和测试)。TrainOptions 和 TestOptions 是 BaseOptions 的两个子类。它们将重新调用在 BaseOptions 中定义的选项。

        __init__.py 使 Python 将 options 目录视为被包含的包(containing package)。

        base_options.py 包括训练和测试中使用的选项。它还实现一些辅助功能,例如解析、打印和保存选项。它还收集在数据集类和模型类中的 modify_commandline_options 函数中定义的其他选项。

        train_options.py 仅包括在训练期间使用的选项。

        test_options.py 仅包括在测试期间使用的选项。

util 目录包括了各种有用的辅助函数的集合

        __init__.py 使 Python 将 util 目录视为被包含的包。

        get_data.py 提供了一个 Python 脚本用于下载 CycleGAN 和 pix2pix 数据集。比如:download_pix2pix_model.sh 或 download_cyclegan_model.sh.

        html.py 用于将图像保存到一个单独的 HTML 文件中。它包括一些函数比如:add_header(在HTML文件中添加文本头),add_images (向HTML文件添加一行图像),save (将HTML保存到磁盘)。它基于 Python 的库 dominate,这是一个用 DOM API 创建和操作 HTML 文档的python 库。

        image_pool.py 是存储以前生成图像的图像缓冲区。这个缓冲区使我们能够使用之前生成的图像来更新鉴别器,而不是使用最新的生成器生成的图像。论文对这一最初的想法进行了讨论。缓冲区的大小由 --pool_size控制。

        visualizer.py 可以显示/保存图像和打印/保存日志信息。它使用 Python 库 visdom 进行显示,使用 Python 库 dominate(包装在 HTML 中)创建带有图像的 HTML 文件。

        util.py 包含辅助函数,如 tensor2im(将张量数组转换为 numpy 图像数组)、diagnose_network(计算并打印梯度平均绝对值的平均值)、mkdirs(创建多个目录)。

## 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).

CycleGAN的代码组成 - 简书 (jianshu.com)https://www.jianshu.com/p/c77d69f6aa18相关解释:

从零开始的CycleGAN学习笔记 代码分析_datasets mode unaligned_Joe9800的博客-CSDN博客https://blog.csdn.net/Joe9800/article/details/103224383

猜你喜欢

转载自blog.csdn.net/qq_46703208/article/details/130464967