Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Junyanz Pytorch CycleGAN and pix2pix TrainOptions Parse

From Leeroopedia


Metadata
Knowledge Sources pytorch-CycleGAN-and-pix2pix
Domains Configuration, Training
Last Updated 2026-02-09 16:00 GMT

Overview

Concrete tool for assembling and parsing training experiment options provided by the pytorch-CycleGAN-and-pix2pix framework. The entry point is TrainOptions().parse(), which returns a fully populated argparse.Namespace containing every flag needed to configure data loading, model architecture, optimisation, checkpointing, and visualisation for a training run.

Description

TrainOptions is a subclass of BaseOptions (defined in options/base_options.py). The base class contributes parameters that are shared across training and testing: data paths, model architecture knobs, dataset loading behaviour, image preprocessing, and GPU selection. TrainOptions extends this set by adding training-specific arguments:

  • Visualisation and logging: --display_freq, --update_html_freq, --print_freq, --no_html
  • Checkpoint persistence: --save_latest_freq, --save_epoch_freq, --save_by_iter, --continue_train, --epoch_count, --phase
  • Optimisation: --n_epochs, --n_epochs_decay, --beta1, --lr, --lr_policy, --lr_decay_iters
  • GAN-specific: --gan_mode, --pool_size

After assembling these flags, parse() delegates to gather_options(), which dynamically injects model-specific options (e.g., --lambda_A, --lambda_B, --lambda_identity for CycleGAN; --lambda_L1 for pix2pix) and dataset-specific options by calling modify_commandline_options on the selected model and dataset classes.

Usage

Import when launching a training run to parse all CLI arguments and configure the experiment. The typical pattern is:

from options.train_options import TrainOptions
opt = TrainOptions().parse()

The returned opt namespace is then passed to model and dataset factory functions to build the complete training pipeline.

Code Reference

Source Location

Signature

TrainOptions class (inherits 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)
        # HTML visualization parameters
        parser.add_argument('--display_freq', type=int, default=400,
            help='frequency of showing training results on screen')
        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')
        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]')
        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

BaseOptions class (base of TrainOptions):

class BaseOptions:
    """This class defines options used during both training and test time."""

    def __init__(self):
        self.initialized = False

    def initialize(self, parser):
        # 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('--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]')
        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 | syncbatch]')
        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.')
        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?')
        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')
        # wandb parameters
        parser.add_argument('--use_wandb', action='store_true',
            help='if specified, then init wandb logging')
        parser.add_argument('--wandb_project_name', type=str, default='CycleGAN-and-pix2pix',
            help='specify wandb project name')
        self.initialized = True
        return parser

    def gather_options(self):
        if not self.initialized:
            parser = argparse.ArgumentParser(
                formatter_class=argparse.ArgumentDefaultsHelpFormatter)
            parser = self.initialize(parser)
        opt, _ = parser.parse_known_args()
        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()
        dataset_name = opt.dataset_mode
        dataset_option_setter = data.get_option_setter(dataset_name)
        parser = dataset_option_setter(parser, self.isTrain)
        self.parser = parser
        return parser.parse_args()

    def print_options(self, opt):
        # Print and save options to [checkpoints_dir]/opt.txt
        ...

    def parse(self):
        opt = self.gather_options()
        opt.isTrain = self.isTrain
        if opt.suffix:
            suffix = ("_" + opt.suffix.format(**vars(opt))) if opt.suffix != "" else ""
            opt.name = opt.name + suffix
        self.print_options(opt)
        self.opt = opt
        return self.opt

Model-specific options (added dynamically via modify_commandline_options):

For CycleGAN (models/cycle_gan_model.py):

parser.set_defaults(no_dropout=True)
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 pix2pix (models/pix2pix_model.py):

parser.set_defaults(norm='batch', netG='unet_256', dataset_mode='aligned')
parser.set_defaults(pool_size=0, gan_mode='vanilla')
parser.add_argument('--lambda_L1', type=float, default=100.0,
    help='weight for L1 loss')

Import

from options.train_options import TrainOptions

I/O Contract

Inputs

Name Type Description
Command-line arguments sys.argv (list of strings) The standard mechanism. When the script is invoked as python train.py --dataroot ./data --name myexp ..., argparse reads arguments from sys.argv. All flags from BaseOptions, TrainOptions, and the dynamically added model/dataset options are accepted.
Programmatic argument list list[str] (optional) If parse_args() is called with an explicit list (e.g., in a notebook or test harness), that list replaces sys.argv. Example: parser.parse_args(['--dataroot', './data', '--name', 'test']).

Outputs

Name Type Description
opt argparse.Namespace A namespace object whose attributes are the parsed and post-processed experiment flags. Includes all base, training, model-specific, and dataset-specific options. Key attributes include:
  • opt.dataroot (str) -- path to the dataset
  • opt.name (str) -- experiment name (with optional suffix applied)
  • opt.model (str) -- selected model, e.g., 'cycle_gan' or 'pix2pix'
  • opt.isTrain (bool) -- always True when produced by TrainOptions
  • opt.lr (float) -- learning rate
  • opt.n_epochs (int) -- epochs at initial learning rate
  • opt.n_epochs_decay (int) -- epochs with linear LR decay
  • opt.batch_size (int) -- batch size
  • opt.netG (str) -- generator architecture
  • opt.netD (str) -- discriminator architecture
  • opt.gan_mode (str) -- GAN loss type
  • opt.pool_size (int) -- image buffer pool size
  • Model-specific: opt.lambda_A, opt.lambda_B, opt.lambda_identity (CycleGAN) or opt.lambda_L1 (pix2pix)

Side Effects

Effect Description
Options file written to disk print_options() writes the full configuration to [checkpoints_dir]/[name]/train_opt.txt, creating the directory if it does not exist.
Options printed to console The full set of options (with non-default values annotated) is printed to standard output.

Usage Examples

Basic CycleGAN Training

python train.py \
    --dataroot ./datasets/horse2zebra \
    --name horse2zebra_cyclegan \
    --model cycle_gan \
    --n_epochs 100 \
    --n_epochs_decay 100 \
    --lr 0.0002 \
    --batch_size 1 \
    --netG resnet_9blocks \
    --netD basic \
    --gan_mode lsgan \
    --pool_size 50 \
    --lambda_A 10.0 \
    --lambda_B 10.0 \
    --lambda_identity 0.5

Basic pix2pix Training

python train.py \
    --dataroot ./datasets/facades \
    --name facades_pix2pix \
    --model pix2pix \
    --direction BtoA \
    --n_epochs 100 \
    --n_epochs_decay 100 \
    --lr 0.0002 \
    --batch_size 1 \
    --netG unet_256 \
    --netD basic \
    --gan_mode vanilla \
    --pool_size 0 \
    --lambda_L1 100.0 \
    --norm batch \
    --dataset_mode aligned

Programmatic Usage in Python

from options.train_options import TrainOptions

# Parse options from command line
opt = TrainOptions().parse()

# Access individual settings
print(opt.dataroot)       # e.g., './datasets/horse2zebra'
print(opt.model)          # e.g., 'cycle_gan'
print(opt.lr)             # e.g., 0.0002
print(opt.n_epochs)       # e.g., 100
print(opt.isTrain)        # True

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment