Skip to content

Commit 2215995

Browse files
committed
update comments for datasets, options, and utils
1 parent 5c8b122 commit 2215995

14 files changed

+110
-55
lines changed

data/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
-- <__getitem__> (get a data point)
88
-- (optionally) <modify_commandline_options> (add dataset-specific options and set default options).
99
Now you can use the dataset class by specifying flag '--dataset_mode dummy'.
10-
See our template dataset class 'template_dataset.py' for an example.
10+
See our template dataset class 'template_dataset.py' for more details.
1111
"""
1212
import importlib
1313
import torch.utils.data
1414
from data.base_dataset import BaseDataset
1515

1616

1717
def find_dataset_using_name(dataset_name):
18-
"""Import the module "data/[datasetname]_dataset.py" given the option --dataset_mode [datasetname].
18+
"""Import the module "data/[datasetname]_dataset.py" given the option '--dataset_mode [datasetname].
1919
2020
In the file, the class called DatasetNameDataset() will
2121
be instantiated. It has to be a subclass of BaseDataset,
@@ -64,8 +64,8 @@ class CustomDatasetDataLoader():
6464
def __init__(self, opt):
6565
"""Initialize this class
6666
67-
It first create a dataset instance given the name [dataset_mode]
68-
It then create a multi-threaded data loader.
67+
Step 1: create a dataset instance given the name [dataset_mode]
68+
Step 2: create a multi-threaded data loader.
6969
"""
7070
self.opt = opt
7171
dataset_class = find_dataset_using_name(opt.dataset_mode)

data/aligned_dataset.py

Lines changed: 5 additions & 5 deletions
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 -- stores all the experiment flags; needs to be a subclass of BaseOptions
20+
opt (Option class) -- 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
@@ -37,10 +37,10 @@ def __getitem__(self, index):
3737
index - - a random integer for data indexing
3838
3939
Returns a dictionary that contains A, B, A_paths and B_paths
40-
A(tensor) - - an image in the input domain
41-
B(tensor) - - its corresponding image in the target domain
42-
A_paths(str) - - image paths
43-
B_paths(str) - - image paths
40+
A (tensor) - - an image in the input domain
41+
B (tensor) - - its corresponding image in the target domain
42+
A_paths (str) - - image paths
43+
B_paths (str) - - image paths (same as A_paths)
4444
"""
4545
# read a image given a random integer index
4646
AB_path = self.AB_paths[index]

data/base_dataset.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,15 @@ def __getitem__(self, index):
6161
def get_transform(opt, grayscale=False, convert=True, crop=True, flip=True):
6262
"""Create a torchvision transformation function
6363
64-
The type of transformation is defined by option(e.g., [preprocess], [load_size], [crop_size])
64+
The type of transformation is defined by option (e.g., [opt.preprocess], [opt.load_size], [opt.crop_size])
6565
and can be overwritten by arguments such as [convert], [crop], and [flip]
66+
67+
Parameters:
68+
opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
69+
grayscale (bool) -- if convert input RGB image to a grayscale image
70+
convert (bool) -- if convert an image to a tensor array betwen [-1, 1]
71+
crop (bool) -- if apply cropping
72+
flip (bool) -- if apply horizontal flippling
6673
"""
6774
transform_list = []
6875
if grayscale:
@@ -94,7 +101,12 @@ def get_transform(opt, grayscale=False, convert=True, crop=True, flip=True):
94101

95102

96103
def __adjust(img):
97-
"""Modify the width and height to be multiple of 4
104+
"""Modify the width and height to be multiple of 4.
105+
106+
Parameters:
107+
img (PIL image) -- input image
108+
109+
Returns a modified image whose width and height are mulitple of 4.
98110
99111
the size needs to be a multiple of 4,
100112
because going through generator network may change img size
@@ -118,6 +130,12 @@ def __adjust(img):
118130
def __scale_width(img, target_width):
119131
"""Resize images so that the width of the output image is the same as a target width
120132
133+
Parameters:
134+
img (PIL image) -- input image
135+
target_width (int) -- target image width
136+
137+
Returns a modified image whose width matches the target image width;
138+
121139
the size needs to be a multiple of 4,
122140
because going through generator network may change img size
123141
and eventually cause size mismatch error

data/colorization_dataset.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,45 @@
88

99

1010
class ColorizationDataset(BaseDataset):
11-
"""This dataset class can load a set of nature images in RGB, and convert RGB format into (L, ab) pairs in Lab color space.
11+
"""This dataset class can load a set of natural images in RGB, and convert RGB format into (L, ab) pairs in Lab color space.
1212
1313
This dataset is required by pix2pix-based colorization model ('--model colorization')
1414
"""
1515
@staticmethod
1616
def modify_commandline_options(parser, is_train):
17+
"""Add new dataset-specific options, and rewrite default values for existing options.
18+
19+
By default, the number of channels for input image is 1 (L) and
20+
the nubmer of channels for output image is 2 (ab). The direction is from A to B
21+
"""
1722
parser.set_defaults(input_nc=1, output_nc=2, direction='AtoB')
1823
return parser
1924

2025
def __init__(self, opt):
26+
"""Initialize this dataset class.
27+
28+
Parameters:
29+
opt (Option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions
30+
"""
2131
BaseDataset.__init__(self, opt)
22-
self.dir_A = os.path.join(opt.dataroot)
23-
self.A_paths = sorted(make_dataset(self.dir_A, opt.max_dataset_size))
32+
self.dir = os.path.join(opt.dataroot)
33+
self.AB_paths = sorted(make_dataset(self.dir, opt.max_dataset_size))
2434
assert(opt.input_nc == 1 and opt.output_nc == 2 and opt.direction == 'AtoB')
2535
self.transform = get_transform(opt, convert=False)
2636

2737
def __getitem__(self, index):
28-
path = self.A_paths[index]
38+
"""Return a data point and its metadata information.
39+
40+
Parameters:
41+
index - - a random integer for data indexing
42+
43+
Returns a dictionary that contains A, B, A_paths and B_paths
44+
A (tensor) - - the L channel of an image
45+
B (tensor) - - the ab channels of the same image
46+
A_paths (str) - - image paths
47+
B_paths (str) - - image paths (same as A_paths)
48+
"""
49+
path = self.AB_paths[index]
2950
im = Image.open(path).convert('RGB')
3051
im = self.transform(im)
3152
im = np.array(im)
@@ -36,4 +57,5 @@ def __getitem__(self, index):
3657
return {'A': A, 'B': B, 'A_paths': path, 'B_paths': path}
3758

3859
def __len__(self):
39-
return len(self.A_paths)
60+
"""Return the total number of images in the dataset."""
61+
return len(self.AB_paths)

data/template_dataset.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Dataset class template
22
3-
This module provides a templete for users to implement custom datasets.
3+
This module provides a template for users to implement custom datasets.
44
You can specify '--dataset_mode template' to use this dataset.
55
The class name should be consistent with both the filename and its dataset_mode option.
66
The filename should be <dataset_mode>_dataset.py
@@ -47,7 +47,7 @@ def __init__(self, opt):
4747
# save the option and dataset root
4848
BaseDataset.__init__(self, opt)
4949
# get the image paths of your dataset;
50-
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
50+
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
5151
# define the default transform function. You can use <base_dataset.get_transform>; You can also define your custom transform function
5252
self.transform = get_transform(opt)
5353

@@ -58,11 +58,11 @@ def __getitem__(self, index):
5858
index -- a random integer for data indexing
5959
6060
Returns:
61-
a dictionary of data with their names. It ususally contains the data itself and its metadata information.
61+
a dictionary of data with their names. It usually contains the data itself and its metadata information.
6262
6363
Step 1: get a random image path: e.g., path = self.image_paths[index]
6464
Step 2: load your data from the disk: e.g., image = Image.open(path).convert('RGB').
65-
Step 3: convert your data to a PyTorch tensor. You can use function such as self.transform. e.g., data = self.transform(image)
65+
Step 3: convert your data to a PyTorch tensor. You can use helpder functions such as self.transform. e.g., data = self.transform(image)
6666
Step 4: return a data point as a dictionary.
6767
"""
6868
path = 'temp' # needs to be a string

docs/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ To help users better understand and use our codebase, we briefly overview the fu
3939
[util](../util) directory includes a miscellaneous collection of useful helper functions.
4040
* [\_\_init\_\_.py](../util/__init__.py) is required to make Python treat the directory `util` as containing packages,
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).
42-
* [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.
42+
* [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.
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`.
4444
* [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.
4545
* [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/base_options.py

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

88

99
class BaseOptions():
10-
"""This class defines options that are used during both training and test time.
10+
"""This class defines options used during both training and test time.
1111
1212
It also implements several helper functions such as parsing, printing, and saving the options.
1313
It also gathers additional options defined in <modify_commandline_options> functions in both dataset class and model class.
@@ -59,11 +59,11 @@ def initialize(self, parser):
5959

6060
def gather_options(self):
6161
"""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
62+
Add additional model-specific and dataset-specific options.
63+
These options are difined in the <modify_commandline_options> function
6464
in model and dataset classes.
6565
"""
66-
if not self.initialized: # check if it has been initalized
66+
if not self.initialized: # check if it has been initialized
6767
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
6868
parser = self.initialize(parser)
6969

options/test_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class TestOptions(BaseOptions):
5-
"""This class includes options that are only used during test time.
5+
"""This class includes test options.
66
77
It also includes shared options defined in BaseOptions.
88
"""

options/train_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class TrainOptions(BaseOptions):
5-
"""This class includes options that are only used during training time.
5+
"""This class includes training options.
66
77
It also includes shared options defined in BaseOptions.
88
"""

util/get_data.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,15 @@ def get(self, save_path, dataset=None):
8181
8282
Download a dataset.
8383
84-
Args:
85-
save_path : str
86-
A directory to save the data to.
87-
dataset : str, optional
88-
A specific dataset to download.
89-
Note: this must include the file extension.
90-
If None, options will be presented for you
91-
to choose from.
84+
Parameters:
85+
save_path (str) -- A directory to save the data to.
86+
dataset (str) -- (optional). A specific dataset to download.
87+
Note: this must include the file extension.
88+
If None, options will be presented for you
89+
to choose from.
9290
9391
Returns:
94-
save_path_full : str
95-
The absolute path to the downloaded data.
92+
save_path_full (str) -- the absolute path to the downloaded data.
9693
9794
"""
9895
if dataset is None:

0 commit comments

Comments
 (0)