Implementation:Junyanz Pytorch CycleGAN and pix2pix TestOptions Parse
| Knowledge Sources | pytorch-CycleGAN-and-pix2pix |
|---|---|
| Domains | Image-to-Image Translation, Configuration Management, Inference Pipeline |
| Last Updated | 2026-02-09 |
Overview
The TestOptions class in options/test_options.py extends BaseOptions to add inference-specific command-line arguments and override default values for testing. BaseOptions in options/base_options.py provides the shared argument parser and parsing logic.
Description
TestOptions inherits from BaseOptions and overrides the initialize() method to register additional command-line arguments specific to inference. It also modifies default values for existing arguments to better suit the testing workflow.
BaseOptions handles:
- Creating an argparse.ArgumentParser with shared arguments
- The parse() method that processes command-line arguments, sets up GPU devices, and saves options to disk
- print_options() for displaying the current configuration
Usage
Instantiated at the top of test.py to configure the inference pipeline. The parse() method returns an options namespace object that is passed to model and dataset constructors.
Code Reference
Source Location
| File | Lines |
|---|---|
| options/test_options.py | L1-23 |
| options/base_options.py | L9-127 |
Signature
class TestOptions(BaseOptions):
"""This class includes test options.
It also includes shared options defined in BaseOptions.
"""
def initialize(self, parser):
"""Define test-specific parameters.
Parameters added:
--results_dir (str) -- saves results here (default: ./results/)
--aspect_ratio (float) -- aspect ratio of result images (default: 1.0)
--phase (str) -- train, val, test, etc. (default: test)
--eval -- use eval mode during test time
--num_test (int) -- how many test images to run (default: 50)
Default overrides:
--model -> 'test' (single-direction generator only)
--load_size -> crop_size (no random cropping during test)
"""
class BaseOptions():
"""This class defines options used during both training and test time."""
def __init__(self, cmd_line=None):
"""Reset the class; used to initialize options."""
def initialize(self, parser):
"""Define shared options for training and testing."""
def gather_options(self):
"""Initialize parser with basic options, then add model/dataset options."""
def print_options(self, opt):
"""Print and save options to a text file."""
def parse(self):
"""Parse options, create checkpoints dir, and set up GPU devices.
Returns:
opt (Namespace) -- parsed options namespace
"""
Import
from options.test_options import TestOptions
opt = TestOptions().parse()
I/O Contract
| Argument | Type | Default | Description |
|---|---|---|---|
| --results_dir | str | ./results/ | Directory to save test results |
| --aspect_ratio | float | 1.0 | Aspect ratio for output images |
| --phase | str | test | Phase identifier (determines data subdirectory) |
| --eval | flag | False | Enable evaluation mode (model.eval()) |
| --num_test | int | 50 | Maximum number of test images to process |
| Argument | Type | Default | Description |
|---|---|---|---|
| --dataroot | str | (required) | Path to dataset root |
| --name | str | experiment_name | Experiment name (determines checkpoint/results subdirectory) |
| --model | str | test (overridden) | Model type: cycle_gan, pix2pix, test, colorization |
| --input_nc | int | 3 | Number of input image channels |
| --output_nc | int | 3 | Number of output image channels |
| --ngf | int | 64 | Number of generator filters |
| --netG | str | resnet_9blocks | Generator architecture name |
| --norm | str | instance | Normalization layer type |
| --no_dropout | flag | False | Disable dropout in generator |
| --load_size | int | crop_size (overridden) | Scale images to this size |
| --crop_size | int | 256 | Crop images to this size |
| --gpu_ids | str | 0 | Comma-separated GPU IDs (e.g., 0,1,2; -1 for CPU) |
| Output | Type | Description |
|---|---|---|
| opt | argparse.Namespace | Parsed options object with all configuration values as attributes |
Usage Examples
from options.test_options import TestOptions
# Parse test options from command line
opt = TestOptions().parse()
# Access parsed values
print(opt.dataroot) # Dataset path
print(opt.name) # Experiment name
print(opt.results_dir) # Results output directory
print(opt.num_test) # Number of test images
print(opt.eval) # Whether to use eval mode
# Example command line invocation
python test.py --dataroot ./datasets/horse2zebra/testA \
--name horse2zebra_pretrained \
--model test \
--no_dropout \
--num_test 100 \
--eval \
--results_dir ./my_results/
Related Pages
- Principle:Junyanz_Pytorch_CycleGAN_and_pix2pix_Test_Options_Configuration
- Implementation:Junyanz_Pytorch_CycleGAN_and_pix2pix_Download_Pretrained_Model
- Implementation:Junyanz_Pytorch_CycleGAN_and_pix2pix_Save_Images
- Environment:Junyanz_Pytorch_CycleGAN_and_pix2pix_Python_PyTorch_Runtime
- Heuristic:Junyanz_Pytorch_CycleGAN_and_pix2pix_Test_Train_Option_Consistency