Implementation:Junyanz Pytorch CycleGAN and pix2pix HTML Gallery
| Knowledge Sources | pytorch-CycleGAN-and-pix2pix |
|---|---|
| Domains | Image-to-Image Translation, Visualization, GAN Evaluation |
| Last Updated | 2026-02-09 |
Overview
The HTML class in util/html.py provides a programmatic interface for constructing browsable HTML image galleries using the dominate library for DOM manipulation. It generates an index.html file containing a table of images organized by headers.
Description
The HTML class wraps the dominate library to build an HTML document with a structured table layout. On initialization, it creates the output directory structure (including an images/ subdirectory), sets up the HTML document with a title and optional auto-refresh meta tag, and prepares a table element for image rows.
The class provides three main operations:
- add_header(text) -- Inserts an h3 element as a row spanning the full table width
- add_images(ims, txts, links, width) -- Adds a row of images, each in its own td cell with a caption and hyperlink
- save() -- Writes the completed DOM to index.html in the web directory
Usage
Used by the Visualizer class during training and by save_images during testing to compile image outputs into a gallery page.
Code Reference
Source Location
| File | Lines |
|---|---|
| util/html.py | L6-71 |
Signature
class HTML:
def __init__(self, web_dir, title, refresh=0):
"""Initialize the HTML class.
Parameters:
web_dir (str) -- a directory that stores the webpage
title (str) -- the webpage title
refresh (int) -- how often the website refreshes itself; 0 = no refresh
"""
def get_image_dir(self):
"""Return the image directory."""
def add_header(self, text):
"""Insert a header to the HTML file.
Parameters:
text (str) -- the header text
"""
def add_images(self, ims, txts, links, width=400):
"""Add images to the HTML file.
Parameters:
ims (list) -- a list of image paths
txts (list) -- a list of image names/captions
links (list) -- a list of hyperref links
width (int) -- the width of displayed images
"""
def save(self):
"""Save the current content to the HTML file (index.html)."""
Import
from util import html
webpage = html.HTML(web_dir, title, refresh=0)
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| web_dir | str | Directory path where the HTML file and images/ subdirectory will be created |
| title | str | Title displayed in the HTML page header |
| refresh | int | Auto-refresh interval in seconds; 0 disables auto-refresh |
| ims | list of str | Image file paths (relative to images/ directory) |
| txts | list of str | Caption text for each image |
| links | list of str | Hyperlink targets for each image |
| width | int | Display width of images in pixels (default: 400) |
| Output | Type | Description |
|---|---|---|
| index.html | file | The generated HTML gallery page written to web_dir/index.html |
| images/ | directory | Subdirectory under web_dir where image files are expected to reside |
Usage Examples
from util import html
# Create a gallery page
web_dir = './results/experiment_name/test_latest'
webpage = html.HTML(web_dir, 'Experiment: horse2zebra test results')
# Add a header for the current epoch
webpage.add_header('Epoch 200')
# Add a row of images
ims = ['real_A.png', 'fake_B.png', 'real_B.png']
txts = ['Input (Horse)', 'Generated (Zebra)', 'Ground Truth (Zebra)']
links = ['real_A.png', 'fake_B.png', 'real_B.png']
webpage.add_images(ims, txts, links, width=256)
# Save the gallery
webpage.save()