ai papers
GAN-Supervised Alignment: GANgealing
Junyoung Park · 2023-01-01 · 12 min
Paper Overview
While surveying work on generative models, I came across an interesting idea called GAN-supervised learning. Earlier reviews covered several papers at once, but this article focuses on a single paper. The name might sound as though the work simply trains a generative network. Instead, GAN-supervised learning means using images produced by a GAN as supervision while optimizing another model. To understand the idea, let us first look briefly at classical congealing.
What Is Congealing?
Consider the familiar MNIST handwritten-digit dataset, simplified to contain only zeros and ones. Even when the same person writes the same digit, the result varies in pixel position, tilt, width, height, and many other ways. In other words, the distribution associated with a single label is spread across many forms.
In the figure, the image is transformed by matrix into . Indexing each pixel location by lets us denote the same location in every image as . We stack all transformed digit images and estimate the empirical entropy of the pixels at each shared location.
Here and are the frequencies of black () and white () at the same pixel location across the images. Dividing each by the total number of images maps it to a probability. The objective minimizes this entropy so that the transformations make all images resemble the same canonical form as closely as possible.
Returning to the Abstract
Extending that framework leads to the paper's central question: if we can generate a consistent target mode for an unaligned dataset, could that mode provide supervision for training a spatial transformer network (STN)?
The method improved precise correspondence—the agreement between aligned regions—by roughly over the previous state of the art. The resulting preprocessing can support augmented reality, image editing, and a variety of downstream GAN tasks.
Why GANgealing?
This section summarizes the paper's introduction. The title asks why GANgealing is necessary because the classical method used for MNIST does not readily extend to complex RGB images.
Visual alignment means finding correspondence or registration across still images and video—for example, locating the same object or matching positions between different images. It underlies computer-vision problems such as optical flow, 3D matching and depth estimation, medical imaging, tracking, and augmented reality.
Recent research has made pairwise alignment from image A to image B easier, but aligning an entire dataset has received less attention. Problems that need a generalized reference frame—automatically annotating a whole dataset or finding keypoints in every image, for instance—remain difficult.
Other work has shown that generative models trained on already aligned images, such as FFHQ, AFHQ, and CelebA-HQ after face detection and cropping, can produce higher-quality samples. This creates a need for more global keypoint reasoning and alignment.
GANgealing expands the congealing idea beyond simple modalities such as binary digits. At a high level, it begins with a pretrained GAN generator learned from an unaligned dataset. Random generated samples and a target sample supervise an STN, while the STN parameters and the target sample are optimized together. The STN is trained independently of the GAN itself.
Related Work
Training with GANs
GAN-based generative models are used throughout computer vision, from classification and segmentation to representation learning and 3D graphics. This paper aims to exploit the pretrained joint distribution of a deep generative network. Unlike the familiar pipeline that first generates images with a GAN and then trains a discriminative network on them, GANgealing learns the discriminative model and optimizes its GAN-generated supervision together.
To avoid ambiguity: the method does not train the generative network. It optimizes the “GAN-generated image” itself. Generated images serve as supervision without pixel-level augmentation or postprocessing based on domain knowledge.
Joint Image-Set Alignment
The idea of an image average has been used to visualize joint alignment within a set sharing the same semantic content. Classical congealing, on which this paper builds, performs unsupervised joint alignment by minimizing entropy across images. It works well for structured data such as binary digits but not for more diverse data.
Later approaches simplified the problem by projecting high-dimensional datasets into low-rank subspaces or factorizing images into color, appearance, and shape features. These methods assume that every image can be represented by one mode—another subspace onto which the dataset is projected.
Other work combined joint visual alignment and clustering with user-provided data. Rather than relying on unsupervised alignment, it used bounding-box supervision and clustered multiple modes within an object category. Automated variants exist, but tend to be restricted to particular domains. Some approaches trained a network to predict a warp for each image, enabling clustering and alignment of large-scale collections, yet remained limited to simple color transformations. For complex datasets, those constraints undermine the underlying assumptions.
Spatial Transformer Networks
The main framework trained with GAN supervision is the spatial transformer network, a well-known example of deep-learning-based data processing. An STN learns parameters for a geometric transformation. It predicts a small set of warp parameters, builds a sampling grid over an input image, and defines sampling and warping as differentiable operations so the entire process can be trained.
STN modules have performed well in discriminative tasks and have been used for robust filter learning, view synthesis, and 3D representation learning. Those methods generally use an STN as a constraint that makes some other task easier. This paper differs by using a GAN to train the STN itself.
GAN-Based Supervised Learning
We can view the supervision pair as the usual source–target relationship in supervised learning. Because the model being trained is an STN, the source image is unaligned—the casually positioned cat in the figure—while the target image is aligned, like the cat on the right. A GAN generator provides the images used to construct this aligned dataset.
First, is a random sample from a pretrained GAN generator. The target is generated by further manipulating the latent code used for . Together they supervise an STN :
Here is a reconstruction loss: the goal is to turn unaligned data into aligned data . In conventional supervised learning, both and are fixed. In GAN-supervised learning, however, the target is also optimized end to end alongside the STN . After training, the STN is evaluated on real inputs rather than GAN-generated images.
Dense Visual Alignment
How can congealing be formulated so that GAN supervision becomes possible? This brings us to the algorithm called GANgealing.
GANgealing begins by training a latent-variable generative model on an unaligned input dataset. Its latent vector is written . The use of rather than reflects the StyleGAN-based generator used in the work. Given the trained , a source latent is sampled from and the source image is .
The target image is , where has the same dimensionality as and serves as the target for the STN. Although can be drawn randomly, we do not initially know which will be an appropriate target for . The solution is to optimize jointly with the STN. Because generator is differentiable with respect to , gradient descent can do so.
Here , and is a distance between two images—apparently a perceptual loss in the figure. Minimizing the objective with respect to asks the latent to find a sample that every generated image can approach through spatial transformer . Thus, supplies the canonical alignment for the image set. If the initial generates an unreachable target, the loss automatically adjusts it; after enough iterations, learns to generate a useful average image.
This simple approach is plausible but works only when the dataset has limited diversity. Every input image is trained toward the same image generated from one constant latent, which imposes a hard optimization limit.
Anyone who has experimented with GAN inversion will know that optimizing from a sample close to the input generally produces better images than optimizing a completely random latent. GANgealing applies the same idea. Rather than use the identical target for every random , it preserves pose and orientation across samples while taking detailed appearance from .
StyleGAN constructs images from low resolution upward, applying latents to a learned constant and progressing from coarse style to fine detail. Combining target vector with random input latent at the appropriate layers makes retain the broad structure determined by while preserving details from the image generated by .
The experiments use StyleGAN2 and exploit its style–pose disentanglement. Style mixing creates a separate target for each sample: controls the early, coarse generator features and determines pose, while enters later layers to determine texture. The paper also ablates the choice of mixing point.
STN Parameterization
Recall the STN architecture. The spatial-transformer function receives an image and produces a sampling grid , whose parameterization depends on the intended transformation. GANgealing tests transformations for rotation, scaling, and horizontal and vertical translation, as well as unconstrained deformation. The final combines an unconstrained STN with a similarity STN for rotation, scale, and translation. It can even learn transformations such as a horizontal flip.
For the unconstrained , a total-variation (TV) regularizer is effective. Total variation is commonly defined through the supremum of the sum or average of input gradients; in a simplified discrete form,
Definitions of total-variation distance vary. In this paper it regularizes sampling grid so that the grid does not deform excessively:
As the equation shows, it calculates directional differences along and . An additional norm discourages from moving too far from the identity transformation:
Parameterization of
The target latent vector is not optimized as a completely unconstrained input through differentiable generator . Instead, it is parameterized as a linear combination of the top principal directions in space:
These principal directions are the familiar axes extracted by PCA. Here is the empirical mean latent, is the th principal direction, and each is a learned scalar coefficient.
This parameterization matters because StyleGAN's space is highly expressive. Optimizing without constraints tends to degrade target-image quality and move the result away from the manifold of natural images. Reducing the number of principal axes restricts the feasible directions of , effectively constraining it around the mean of and mitigating this problem.
The final GANgealing objective is
The experiments use or , and .
Joint Alignment and Clustering
The description so far works well for several multimodal datasets, including LSUN Bicycles and Cats. Some datasets, such as LSUN Horses, contain poses too diverse to represent with a single mode. To handle them, GANgealing can be extended from one target latent to a clustering algorithm.
Suppose we learn vectors . Each latent constant captures a particular data mode, so learning models several modes in a dataset. As before, each is a linear combination of the top principal directions in , with a separate set of learned coefficients for every . The paper similarly uses distinct spatial transformers , one per mode. The resulting model learns dense correspondence among all images within each cluster.
The remaining question is how to guide each pair toward a particular mode. The method uses the hard-assignment step
so that each unaligned image is assigned to and aligned under one mode. The familiar unimodal case corresponds to .
At test time, a generated image can be assigned to
and warped with the corresponding transformer . Real images are harder: calculating requires not only the input image, but the latent that generated it.
Clustering over modes optimizes multiple modalities, and each is mixed and optimized with the input latent , so applying the same process to a real image would require recovering a latent variable. GAN inversion is the intuitive solution, as discussed in my earlier post on GAN-based image manipulation: find a latent whose generated sample resembles image . Accurate GAN inversion is nevertheless slow and particularly challenging for non-face datasets, since StyleGAN is commonly trained on FFHQ faces.
The authors therefore train a network that predicts the cluster assignment of an input image as a classification result. For generated images, the target cluster is already available, so pairs provide straightforward supervision. This permits assignment of an arbitrary image without GAN inversion. The classifier reuses the spatial transformer's weights but replaces its warping head with a classification head. Both the spatial transformer and classifier, although optimized on fake samples, generalize well to real images.
Closing Thoughts
That concludes the explanation of GANgealing; I will stop without covering the experiments and results in detail. In reading recent papers, I have become more interested in the insights and mathematical background used to develop an idea—the related work and preliminaries—than in cataloging every experiment and number.
The paper's main contribution is not a method for training a GAN. It uses a GAN to train an STN and thereby align unaligned real images effectively. I found the subject exceptionally interesting, and it seems broadly useful.