Implementation:Junyanz Pytorch CycleGAN and pix2pix Save Images
| Knowledge Sources | pytorch-CycleGAN-and-pix2pix |
|---|---|
| Domains | Image-to-Image Translation, Visualization, Tensor Processing |
| Last Updated | 2026-02-09 |
Overview
The save_images function in util/visualizer.py converts model output tensors to image files and adds them as rows in an HTML gallery. It relies on tensor2im and save_image from util/util.py for tensor conversion and file writing.
Description
The save_images function receives a dictionary of visual outputs from the model (keyed by name, e.g., real_A, fake_B), the HTML webpage object, the image path, and display parameters. For each visual:
- Converts the tensor to a NumPy image using tensor2im
- Constructs a save path based on the image name and visual label
- Optionally resizes the image to match the desired aspect_ratio
- Saves the image to disk using save_image
- Collects all image paths, labels, and links for the gallery row
After processing all visuals, it calls webpage.add_images to insert the row into the HTML gallery.
Usage
Called in the test loop of test.py for each processed sample. The function is the primary interface between the model inference pipeline and the result visualization system.
Code Reference
Source Location
| File | Lines |
|---|---|
| util/visualizer.py | L12-37 |
| util/util.py | (tensor2im, save_image helper functions) |
Signature
def save_images(webpage, visuals, image_path, aspect_ratio=1.0, width=256):
"""Save images to disk and add them to an HTML page.
Parameters:
webpage (html.HTML) -- the HTML webpage object
visuals (OrderedDict) -- an ordered dict of images to display/save;
keys are names, values are tensors
image_path (str) -- the file path of the current image sample
aspect_ratio (float) -- the aspect ratio of saved images (default: 1.0)
width (int) -- the display width of images on the HTML page (default: 256)
"""
Helper functions (from util/util.py):
def tensor2im(input_image, imtype=np.uint8):
"""Convert a Tensor array into a numpy image array.
Parameters:
input_image (tensor) -- the input image tensor
imtype (type) -- the desired numpy dtype (default: np.uint8)
Returns:
image_numpy (numpy) -- the converted numpy image array
"""
def save_image(image_numpy, image_path, aspect_ratio=1.0):
"""Save a numpy image to disk.
Parameters:
image_numpy (numpy) -- input numpy image array
image_path (str) -- the path of the image file
aspect_ratio (float) -- aspect ratio for resizing
"""
Import
from util.visualizer import save_images
from util import html
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| webpage | html.HTML | The HTML gallery object to add image rows to |
| visuals | OrderedDict | Dictionary mapping label names (str) to image tensors (torch.Tensor in [-1, 1]) |
| image_path | str | File path of the current test sample (used to derive output filenames) |
| aspect_ratio | float | Aspect ratio for resizing saved images (default: 1.0, no resizing) |
| width | int | Display width in pixels for images on the HTML page (default: 256) |
| Output | Type | Description |
|---|---|---|
| Image files | .png files | Saved to webpage.get_image_dir(), one per visual in the dict |
| HTML row | side effect | A new row of images added to the webpage object via add_images |
Usage Examples
from util.visualizer import save_images
from util import html
from collections import OrderedDict
# Set up the webpage
web_dir = os.path.join(opt.results_dir, opt.name,
'{}_{}'.format(opt.phase, opt.epoch))
webpage = html.HTML(web_dir,
'Experiment = %s, Phase = %s, Epoch = %s' %
(opt.name, opt.phase, opt.epoch))
# In the test loop
for i, data in enumerate(dataset):
model.set_input(data)
model.test()
visuals = model.get_current_visuals() # OrderedDict of tensors
img_path = model.get_image_paths() # list of file paths
save_images(webpage, visuals, img_path,
aspect_ratio=opt.aspect_ratio, width=opt.display_winsize)
# After the loop, save the gallery
webpage.save()
# Direct use of helper functions
from util.util import tensor2im, save_image
import numpy as np
# Convert a model output tensor to a displayable image
fake_B_tensor = model.fake_B # shape: (1, 3, 256, 256), range: [-1, 1]
fake_B_image = tensor2im(fake_B_tensor) # shape: (256, 256, 3), dtype: uint8
# Save to disk
save_image(fake_B_image, './output/fake_B.png')