Skip to content

Commit 3bdcc65

Browse files
committed
add comments: utils and options
1 parent 8cc270b commit 3bdcc65

17 files changed

+214
-75
lines changed

data/aligned_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self, opt):
1717
"""Initialize this dataset class.
1818
1919
Parameters:
20-
opt -- options (needs to be a subclass of BaseOptions)
20+
opt -- stores all the experiment flags; needs to be a subclass of BaseOptions
2121
"""
2222
BaseDataset.__init__(self, opt)
2323
self.dir_AB = os.path.join(opt.dataroot, opt.phase) # get the image directory

data/base_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, opt):
2222
"""Initialize the class; save the options in the class
2323
2424
Parameters:
25-
opt -- options (needs to be a subclass of BaseOptions)
25+
opt -- stores all the experiment flags; needs to be a subclass of BaseOptions
2626
"""
2727
self.opt = opt
2828
self.root = opt.dataroot

data/single_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, opt):
1313
"""Initialize this dataset class.
1414
1515
Parameters:
16-
opt -- options (needs to be a subclass of BaseOptions)
16+
opt -- stores all the experiment flags; needs to be a subclass of BaseOptions
1717
"""
1818
BaseDataset.__init__(self, opt)
1919
self.A_paths = sorted(make_dataset(opt.dataroot, opt.max_dataset_size))

data/template_dataset.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def __init__(self, opt):
3737
"""Initialize this dataset class.
3838
3939
Parameters:
40-
opt -- training/test options
40+
opt -- stores all the experiment flags; needs to be a subclass of BaseOptions
41+
4142
A few things can be done here.
4243
- save the options (have been done in BaseDataset)
4344
- get image paths and meta information of the dataset.

data/unaligned_dataset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(self, opt):
2020
"""Initialize this dataset class.
2121
2222
Parameters:
23-
opt -- options (needs to be a subclass of BaseOptions)
23+
opt -- stores all the experiment flags; needs to be a subclass of BaseOptions
2424
"""
2525
BaseDataset.__init__(self, opt)
2626
self.dir_A = os.path.join(opt.dataroot, opt.phase + 'A') # create a path '/path/to/data/trainA'

docs/overview.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ To help users better understand and use our codebase, we briefly overview the fu
2929
* [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`).
3030
* [test_model.py](../models/test_model.py) implements a model that can be used to generate CycleGAN results for only one direction. This option 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.
3131

32-
[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`.
32+
[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`.
3333
* [\_\_init\_\_.py](../options/__init__.py) is required to make Python treat the directory `options` as containing packages,
3434
* [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.
3535
* [train_options.py](../options/train_options.py) includes options that are only used during training time.
@@ -41,5 +41,5 @@ To help users better understand and use our codebase, we briefly overview the fu
4141
* [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).
4242
* [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 an elegant DOM API.
4343
* [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`.
44-
* [visualizer.py](../util/visualizer.py) includes several functions to display and save images as well as print and save logging information. It is based on Python library `visdom` display.
45-
* [util.py](../util/util.py) consists of simple helper functions such as `tensor2im` (convert a tensor array to a numpy image array), `diagnose_network` (print the mean and stddev of weights for each layer), and `mkdirs` (create multiple directories).
44+
* [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.
45+
* [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).

options/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""This package options includes option modules: training options, test options, and basic options (used in both training and test)."""

options/base_options.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,18 @@
77

88

99
class BaseOptions():
10+
"""This class defines options that are used during both training and test time.
11+
12+
It also implements several helper functions such as parsing, printing, and saving the options.
13+
It also gathers additional options defined in <modify_commandline_options> functions in both dataset class and model class.
14+
"""
15+
1016
def __init__(self):
17+
"""Reset the class; indicates the class hasn't been initailized"""
1118
self.initialized = False
1219

1320
def initialize(self, parser):
21+
"""Define the common options that are used in both training and test."""
1422
# basic parameters
1523
parser.add_argument('--dataroot', required=True, help='path to images (should have subfolders trainA, trainB, valA, valB, etc)')
1624
parser.add_argument('--name', type=str, default='experiment_name', help='name of the experiment. It decides where to store samples and models')
@@ -50,10 +58,13 @@ def initialize(self, parser):
5058
return parser
5159

5260
def gather_options(self):
53-
# initialize parser with basic options
54-
if not self.initialized:
55-
parser = argparse.ArgumentParser(
56-
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
61+
"""Initialize our parser with basic options(only once).
62+
Add additional model - specific and dataset - specific options.
63+
These options are difined in the < modify_commandline_options > function
64+
in model and dataset classes.
65+
"""
66+
if not self.initialized: # check if it has been initalized
67+
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
5768
parser = self.initialize(parser)
5869

5970
# get the basic options
@@ -63,18 +74,23 @@ def gather_options(self):
6374
model_name = opt.model
6475
model_option_setter = models.get_option_setter(model_name)
6576
parser = model_option_setter(parser, self.isTrain)
66-
opt, _ = parser.parse_known_args() # parse again with the new defaults
77+
opt, _ = parser.parse_known_args() # parse again with new defaults
6778

6879
# modify dataset-related parser options
6980
dataset_name = opt.dataset_mode
7081
dataset_option_setter = data.get_option_setter(dataset_name)
7182
parser = dataset_option_setter(parser, self.isTrain)
7283

84+
# save and return the parser
7385
self.parser = parser
74-
7586
return parser.parse_args()
7687

7788
def print_options(self, opt):
89+
"""Print and save options
90+
91+
It will print both current options and default values(if different).
92+
It will save options into a text file / [checkpoints_dir] / opt.txt
93+
"""
7894
message = ''
7995
message += '----------------- Options ---------------\n'
8096
for k, v in sorted(vars(opt).items()):
@@ -95,7 +111,7 @@ def print_options(self, opt):
95111
opt_file.write('\n')
96112

97113
def parse(self):
98-
114+
"""Parse our options, create checkpoints directory suffix, and set up gpu device."""
99115
opt = self.gather_options()
100116
opt.isTrain = self.isTrain # train or test
101117

options/test_options.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22

33

44
class TestOptions(BaseOptions):
5+
"""This class includes options that are only used during test time.
6+
7+
It also includes shared options defined in BaseOptions.
8+
"""
9+
510
def initialize(self, parser):
6-
parser = BaseOptions.initialize(self, parser)
11+
parser = BaseOptions.initialize(self, parser) # define shared options
712
parser.add_argument('--ntest', type=int, default=float("inf"), help='# of test examples.')
813
parser.add_argument('--results_dir', type=str, default='./results/', help='saves results here.')
914
parser.add_argument('--aspect_ratio', type=float, default=1.0, help='aspect ratio of result images')
1015
parser.add_argument('--phase', type=str, default='test', help='train, val, test, etc')
11-
# Dropout and Batchnorm has different behavioir during training and test.
16+
# Dropout and Batchnorm has different behavioir during training and test.
1217
parser.add_argument('--eval', action='store_true', help='use eval mode during test time.')
1318
parser.add_argument('--num_test', type=int, default=50, help='how many test images to run')
14-
19+
# rewrite devalue values
1520
parser.set_defaults(model='test')
1621
# To avoid cropping, the load_size should be the same as crop_size
1722
parser.set_defaults(load_size=parser.get_default('crop_size'))

options/train_options.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33

44
class TrainOptions(BaseOptions):
5+
"""This class includes options that are only used during training time.
6+
7+
It also includes shared options defined in BaseOptions.
8+
"""
9+
510
def initialize(self, parser):
611
parser = BaseOptions.initialize(self, parser)
12+
# visdom and HTML visualization parameters
713
parser.add_argument('--display_freq', type=int, default=400, help='frequency of showing training results on screen')
814
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.')
915
parser.add_argument('--display_id', type=int, default=1, help='window id of the web display')
@@ -12,19 +18,21 @@ def initialize(self, parser):
1218
parser.add_argument('--display_port', type=int, default=8097, help='visdom port of the web display')
1319
parser.add_argument('--update_html_freq', type=int, default=1000, help='frequency of saving training results to html')
1420
parser.add_argument('--print_freq', type=int, default=100, help='frequency of showing training results on console')
21+
parser.add_argument('--no_html', action='store_true', help='do not save intermediate training results to [opt.checkpoints_dir]/[opt.name]/web/')
22+
# network saving and loading parameters
1523
parser.add_argument('--save_latest_freq', type=int, default=5000, help='frequency of saving the latest results')
1624
parser.add_argument('--save_epoch_freq', type=int, default=5, help='frequency of saving checkpoints at the end of epochs')
1725
parser.add_argument('--save_by_iter', action='store_true', help='whether saves model by iteration')
1826
parser.add_argument('--continue_train', action='store_true', help='continue training: load the latest model')
1927
parser.add_argument('--epoch_count', type=int, default=1, help='the starting epoch count, we save the model by <epoch_count>, <epoch_count>+<save_latest_freq>, ...')
2028
parser.add_argument('--phase', type=str, default='train', help='train, val, test, etc')
29+
# training parameters
2130
parser.add_argument('--niter', type=int, default=100, help='# of iter at starting learning rate')
2231
parser.add_argument('--niter_decay', type=int, default=100, help='# of iter to linearly decay learning rate to zero')
2332
parser.add_argument('--beta1', type=float, default=0.5, help='momentum term of adam')
2433
parser.add_argument('--lr', type=float, default=0.0002, help='initial learning rate for adam')
2534
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.')
2635
parser.add_argument('--pool_size', type=int, default=50, help='the size of image buffer that stores previously generated images')
27-
parser.add_argument('--no_html', action='store_true', help='do not save intermediate training results to [opt.checkpoints_dir]/[opt.name]/web/')
2836
parser.add_argument('--lr_policy', type=str, default='lambda', help='learning rate policy. [lambda | step | plateau | cosine]')
2937
parser.add_argument('--lr_decay_iters', type=int, default=50, help='multiply by a gamma every lr_decay_iters iterations')
3038

0 commit comments

Comments
 (0)