docs/overview.md
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 and frequently asked questions.
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 and training/test tips for more details.
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 and training/test tips for more details.
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 for an example. Below we explain each file in details.
train.py and test.py call from data import create_dataset and dataset = create_dataset(opt) to create a dataset given the option opt.get_transform, __scale_width), which can be later used in subclasses./path/to/data/train, which contains image pairs in the form of {A,B}. See here on how to prepare aligned datasets. During test time, you need to prepare a directory /path/to/data/test as test data./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.--dataroot /path/to/data. It can be used for generating CycleGAN results only for one side with the model option -model test.--model colorization).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 for an example. Below we explain each file in details.
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.setup, test, update_learning_rate, save_networks, load_networks), which can be later used in subclasses.--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 (standard cross-entropy objective).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 color space. By default, the colorization dataset will automatically set --input_nc 1 and --output_nc 2.--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).vanilla, lsgan, wgangp).--dataset_mode single, which only loads the images from one set. See the test instruction for more details.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.
options as containing packages,modify_commandline_options functions in both dataset class and model class.util directory includes a miscellaneous collection of useful helper functions.
util as containing packages,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.--pool_size.dominate (wrapped in HTML) for creating HTML files with images.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).