BigGAN-PyTorch sampling
Use this skill when the task is to load a trained BigGAN-PyTorch generator,
produce random or conditional/interpolated images, export a TensorFlow-compatible
NPZ, or compare the repository's PyTorch Inception metrics. Work from the
repository root so that sample.py, utils.py, inception_utils.py, and the
model module can be imported.
This is a CUDA sampling path: sample.py constructs G with .cuda(), uses
CUDA distributions for z and y, and the metric wrapper also moves Inception
to CUDA. Confirm the CUDA, PyTorch, torchvision, and checkpoint environment
before launching a large run. Read all three bundled references for exact flag
semantics and recovery procedures.
Operating procedure
- Resolve the checkpoint before choosing outputs. Identify the exact
weights_root/<experiment_name>/ directory and the suffix to load. An empty
suffix means G.pth and state_dict.pth; best0 and copy0 mean
G_best0.pth/state_dict_best0.pth and
G_copy0.pth/state_dict_copy0.pth, respectively. A usable normal-G run
needs the generator and state dict; a --use_ema run additionally needs
G_ema[_suffix].pth. --load_weights selects a file suffix, not a
directory.
- Make the architecture agree with the saved config. Prefer an explicit
--experiment_name NAME --load_weights SUFFIX --config_from_name when the
checkpoint has a saved state dict. The bootstrap state-dict load happens
before update_config_roots, so if the checkpoint is outside the default
weights/, pass its actual --weights_root explicitly as well as
--base_root if desired. Without --config_from_name, supply the training
architecture flags yourself; strict=False in sample.py is not a remedy
for incompatible tensor shapes.
- Select normal or EMA parameters deliberately. Omit
--use_ema to load
G[_suffix].pth. Use --use_ema only when the checkpoint was saved with an
EMA generator and the matching G_ema[_suffix].pth exists. --ema describes
the training/configuration and contributes to auto-generated experiment
names; --use_ema controls which generator state is loaded for evaluation.
- Fix the normalization/statistics mode before comparing samples.
--G_eval_mode calls G.eval() and uses running BatchNorm estimates. With
the custom myBN implementation, --accumulate_stats runs
--num_standing_accumulations random forward passes in training mode and
then switches to eval. Keep G_eval_mode, mybn, accumulation count, EMA
choice, and z_var fixed for fair comparisons. The BigGAN recipe uses
--mybn --accumulate_stats --num_standing_accumulations 32 when standing
statistics are wanted; the provided main sampling recipe instead uses
--use_ema --G_eval_mode.
- Set noise and batch size explicitly.
--z_var controls the var field
of the custom normal Distribution. In this code that field is passed as
the second argument to Tensor.normal_, i.e. as the normal scale despite
the historical “variance” name. G_batch_size is set to
max(--G_batch_size, --batch_size), so lower both flags when reducing GPU
memory. --seed seeds PyTorch CUDA and NumPy RNGs but does not make every
multi-GPU or cuDNN operation deterministic.
- Choose one or more output actions and check their prerequisites.
--sample_random writes a normalized random_samples.jpg;
--sample_sheets writes class-conditional sheets;
--sample_interps writes Z/Y interpolation sheets;
--sample_npz --sample_num_npz N writes samples.npz with x uint8 CHW
images and y integer labels; and metric flags compute repository-specific
scores. Create the samples root first: the script does not call
utils.prepare_root, and NPZ/random output assumes the experiment directory
already exists. See references/workflows.md for exact layout and commands.
- Treat metric names as implementation-specific. The built-in path uses
torchvision Inception, ImageNet normalization, 299x299 resizing, and
<base-dataset>_inception_moments.npz. Its IS/FID are explicitly unofficial
and not numerically interchangeable with the legacy TensorFlow metric. To
obtain the TensorFlow-style IS, first produce the NPZ and then run the
repository's TensorFlow 1.3-or-earlier evaluator. Record the implementation,
weights (normal/EMA), mode, z_var, image count, and moments used with every
score.
Standard command shape
For a checkpoint with a saved config, use a command of this form (replace the
paths and name; do not invent a name from a changed sampling batch size):
mkdir -p /path/to/samples /path/to/samples/EXPERIMENT
python sample.py \
--experiment_name EXPERIMENT --weights_root /path/to/weights \
--load_weights best0 --config_from_name \
--batch_size 32 --G_batch_size 32 --seed 0 \
--use_ema --G_eval_mode --sample_random
Add only the required output flags. A truncation sweep, for example, adds
--sample_inception_metrics --sample_trunc_curves 0.05_0.05_1.0; this is a
metric run, not a cheap image smoke test. Do not start with 50,000 images or
multiple sheets when first validating a checkpoint.
Required recovery behavior
- For missing weights, inspect the experiment directory and repair the
--weights_root, --experiment_name, or --load_weights selection. A
missing state_dict[_suffix].pth prevents --config_from_name; recover the
saved config or provide every architecture argument explicitly.
- For an EMA load failure, remove
--use_ema only if a normal-G evaluation is
acceptable; otherwise locate a checkpoint containing the matching
G_ema[_suffix].pth. Never label a normal-G result as an EMA result.
- For missing Inception moments, generate the dataset moments using the data
preparation/moments script, put the file where
inception_utils.py will
search, or restrict the run to checkpoint/image loading after applying the
lazy-metrics source fix described in references/troubleshooting.md.
--no_fid is not a missing-moments workaround because moments are loaded
before the metric flags are consulted.
- For incompatible configs, load the state dict's saved config, use
--config_from_name, and keep only documented runtime overrides. A shape
mismatch means the checkpoint and model are genuinely incompatible; do not
paper over it with strict=False.
- For CUDA OOM, reduce both
--batch_size and --G_batch_size, disable
--parallel only if it reduces overhead on the available topology, and
reduce image counts or run outputs separately. NPZ sampling accumulates all
generated arrays in host memory before writing, so large --sample_num_npz
values can also exhaust RAM.
- For
sample.py --help crashing with ValueError: unsupported format character '#', the --logstyle help text in utils.py contains literal
%#.#f/%#.#e strings. Escape the percent signs as %%#.#f and
%%#.#e (or remove the formatting examples), then retry help. Until that
source-only fix is made, use known flags directly rather than treating the
help crash as a checkpoint failure.
For the complete flag contract, file names, metric caveats, multi-GPU behavior,
and failure matrix, read references/api-reference.md,
references/workflows.md, and references/troubleshooting.md.
1---2name: sampling3description: "Operate BigGAN-PyTorch sampling from compatible checkpoints,4 including EMA and batch-normalization statistics, image sheets, NPZ export,5 truncation curves, and repository-specific Inception evaluation."6disable-model-invocation: true7metadata:8 source: https://skillmd.com/skills/vectorspacelab/sampling9 disco-role: operating10license: MIT11---1213# BigGAN-PyTorch sampling1415Use this skill when the task is to load a trained BigGAN-PyTorch generator,16produce random or conditional/interpolated images, export a TensorFlow-compatible17NPZ, or compare the repository's PyTorch Inception metrics. Work from the18repository root so that `sample.py`, `utils.py`, `inception_utils.py`, and the19model module can be imported.2021This is a CUDA sampling path: `sample.py` constructs `G` with `.cuda()`, uses22CUDA distributions for `z` and `y`, and the metric wrapper also moves Inception23to CUDA. Confirm the CUDA, PyTorch, torchvision, and checkpoint environment24before launching a large run. Read all three bundled references for exact flag25semantics and recovery procedures.2627## Operating procedure28291. **Resolve the checkpoint before choosing outputs.** Identify the exact30 `weights_root/<experiment_name>/` directory and the suffix to load. An empty31 suffix means `G.pth` and `state_dict.pth`; `best0` and `copy0` mean32 `G_best0.pth`/`state_dict_best0.pth` and33 `G_copy0.pth`/`state_dict_copy0.pth`, respectively. A usable normal-G run34 needs the generator and state dict; a `--use_ema` run additionally needs35 `G_ema[_suffix].pth`. `--load_weights` selects a file suffix, not a36 directory.372. **Make the architecture agree with the saved config.** Prefer an explicit38 `--experiment_name NAME --load_weights SUFFIX --config_from_name` when the39 checkpoint has a saved state dict. The bootstrap state-dict load happens40 before `update_config_roots`, so if the checkpoint is outside the default41 `weights/`, pass its actual `--weights_root` explicitly as well as42 `--base_root` if desired. Without `--config_from_name`, supply the training43 architecture flags yourself; `strict=False` in `sample.py` is not a remedy44 for incompatible tensor shapes.453. **Select normal or EMA parameters deliberately.** Omit `--use_ema` to load46 `G[_suffix].pth`. Use `--use_ema` only when the checkpoint was saved with an47 EMA generator and the matching `G_ema[_suffix].pth` exists. `--ema` describes48 the training/configuration and contributes to auto-generated experiment49 names; `--use_ema` controls which generator state is loaded for evaluation.504. **Fix the normalization/statistics mode before comparing samples.**51 `--G_eval_mode` calls `G.eval()` and uses running BatchNorm estimates. With52 the custom `myBN` implementation, `--accumulate_stats` runs53 `--num_standing_accumulations` random forward passes in training mode and54 then switches to eval. Keep `G_eval_mode`, `mybn`, accumulation count, EMA55 choice, and `z_var` fixed for fair comparisons. The BigGAN recipe uses56 `--mybn --accumulate_stats --num_standing_accumulations 32` when standing57 statistics are wanted; the provided main sampling recipe instead uses58 `--use_ema --G_eval_mode`.595. **Set noise and batch size explicitly.** `--z_var` controls the `var` field60 of the custom normal `Distribution`. In this code that field is passed as61 the second argument to `Tensor.normal_`, i.e. as the normal scale despite62 the historical “variance” name. `G_batch_size` is set to63 `max(--G_batch_size, --batch_size)`, so lower both flags when reducing GPU64 memory. `--seed` seeds PyTorch CUDA and NumPy RNGs but does not make every65 multi-GPU or cuDNN operation deterministic.666. **Choose one or more output actions and check their prerequisites.**67 `--sample_random` writes a normalized `random_samples.jpg`;68 `--sample_sheets` writes class-conditional sheets;69 `--sample_interps` writes Z/Y interpolation sheets;70 `--sample_npz --sample_num_npz N` writes `samples.npz` with `x` uint8 CHW71 images and `y` integer labels; and metric flags compute repository-specific72 scores. Create the samples root first: the script does not call73 `utils.prepare_root`, and NPZ/random output assumes the experiment directory74 already exists. See `references/workflows.md` for exact layout and commands.757. **Treat metric names as implementation-specific.** The built-in path uses76 torchvision Inception, ImageNet normalization, 299x299 resizing, and77 `<base-dataset>_inception_moments.npz`. Its IS/FID are explicitly unofficial78 and not numerically interchangeable with the legacy TensorFlow metric. To79 obtain the TensorFlow-style IS, first produce the NPZ and then run the80 repository's TensorFlow 1.3-or-earlier evaluator. Record the implementation,81 weights (normal/EMA), mode, `z_var`, image count, and moments used with every82 score.8384## Standard command shape8586For a checkpoint with a saved config, use a command of this form (replace the87paths and name; do not invent a name from a changed sampling batch size):8889```bash90mkdir -p /path/to/samples /path/to/samples/EXPERIMENT91python sample.py \92 --experiment_name EXPERIMENT --weights_root /path/to/weights \93 --load_weights best0 --config_from_name \94 --batch_size 32 --G_batch_size 32 --seed 0 \95 --use_ema --G_eval_mode --sample_random96```9798Add only the required output flags. A truncation sweep, for example, adds99`--sample_inception_metrics --sample_trunc_curves 0.05_0.05_1.0`; this is a100metric run, not a cheap image smoke test. Do not start with 50,000 images or101multiple sheets when first validating a checkpoint.102103## Required recovery behavior104105- For missing weights, inspect the experiment directory and repair the106 `--weights_root`, `--experiment_name`, or `--load_weights` selection. A107 missing `state_dict[_suffix].pth` prevents `--config_from_name`; recover the108 saved config or provide every architecture argument explicitly.109- For an EMA load failure, remove `--use_ema` only if a normal-G evaluation is110 acceptable; otherwise locate a checkpoint containing the matching111 `G_ema[_suffix].pth`. Never label a normal-G result as an EMA result.112- For missing Inception moments, generate the dataset moments using the data113 preparation/moments script, put the file where `inception_utils.py` will114 search, or restrict the run to checkpoint/image loading after applying the115 lazy-metrics source fix described in `references/troubleshooting.md`.116 `--no_fid` is not a missing-moments workaround because moments are loaded117 before the metric flags are consulted.118- For incompatible configs, load the state dict's saved config, use119 `--config_from_name`, and keep only documented runtime overrides. A shape120 mismatch means the checkpoint and model are genuinely incompatible; do not121 paper over it with `strict=False`.122- For CUDA OOM, reduce both `--batch_size` and `--G_batch_size`, disable123 `--parallel` only if it reduces overhead on the available topology, and124 reduce image counts or run outputs separately. NPZ sampling accumulates all125 generated arrays in host memory before writing, so large `--sample_num_npz`126 values can also exhaust RAM.127- For `sample.py --help` crashing with `ValueError: unsupported format128 character '#'`, the `--logstyle` help text in `utils.py` contains literal129 `%#.#f`/`%#.#e` strings. Escape the percent signs as `%%#.#f` and130 `%%#.#e` (or remove the formatting examples), then retry help. Until that131 source-only fix is made, use known flags directly rather than treating the132 help crash as a checkpoint failure.133134For the complete flag contract, file names, metric caveats, multi-GPU behavior,135and failure matrix, read `references/api-reference.md`,136`references/workflows.md`, and `references/troubleshooting.md`.