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 jthj^{th} image IjI^j is transformed by matrix UjU^j into IjI^{j\prime}. Indexing each pixel location by ii lets us denote the same location in every image as xijx_i^j. We stack all transformed digit images and estimate the empirical entropy of the pixels at each shared location.

H^(xi)=(N0Nlog2N0N+N1Nlog2N1N) \hat{H}(x_i) = -\left( \frac{N_0}{N} \log _2 \frac{N_0}{N} + \frac{N_1}{N} \log _2 \frac{N_1}{N} \right)

Here N0N_0 and N1N_1 are the frequencies of black (00) and white (11) 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 3×3\times 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 (x, y)(x,~y) 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, xx is a random sample from a pretrained GAN generator. The target yy is generated by further manipulating the latent code used for xx. Together they supervise an STN fθ:xyf_\theta:x\rightarrow y:

L(fθ, y)=l(fθ(x), y) \mathcal{L}(f_\theta,~y) = l(f_\theta(x),~y)

Here ll is a reconstruction loss: the goal is to turn unaligned data xx into aligned data yy. In conventional supervised learning, both xx and yy are fixed. In GAN-supervised learning, however, the target yy is also optimized end to end alongside the STN fθf_\theta. 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 GG on an unaligned input dataset. Its latent vector is written wR512w\in\mathbb{R}^{512}. The use of ww rather than zz reflects the StyleGAN-based generator used in the work. Given the trained GG, a source latent is sampled from wWw\sim\mathcal{W} and the source image is x=G(w)x=G(w).

The target image is G(c)G(c), where cc has the same dimensionality as ww and serves as the target for the STN. Although ww can be drawn randomly, we do not initially know which cc will be an appropriate target for G(w)G(w). The solution is to optimize cc jointly with the STN. Because generator GG is differentiable with respect to cc, gradient descent can do so.

Lalign(T, c)=l(T(G(w)),G(c)) \mathcal{L}_\text{align}(T,~c) = l(T(G(w)), G(c))

Here T()=fθ()T(\cdot)=f_\theta(\cdot), and ll is a distance between two images—apparently a perceptual loss in the figure. Minimizing the objective with respect to cc asks the latent cc to find a sample that every generated image xx can approach through spatial transformer TT. Thus, G(c)G(c) supplies the canonical alignment for the image set. If the initial cc generates an unreachable target, the loss automatically adjusts it; after enough iterations, cc learns to generate a useful average image.

This simple approach is plausible but works only when the dataset has limited diversity. Every input image xx is trained toward the same image G(c)G(c) 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 G(c)G(c) for every random G(w)G(w), it preserves pose and orientation across samples while taking detailed appearance from G(w)G(w).

StyleGAN constructs images from low resolution upward, applying latents to a learned constant and progressing from coarse style to fine detail. Combining target vector cc with random input latent ww at the appropriate layers makes mix(c,w)R512\text{mix}(c,w)\in\mathbb{R}^{512} retain the broad structure determined by cc while preserving details from the image generated by ww.

Lalign(T, c)=l(T(G(w)),G(mix(c, w))) \mathcal{L}_\text{align}(T,~c) = l(T(G(w)), G(\text{mix}(c,~w)))

The experiments use StyleGAN2 and exploit its style–pose disentanglement. Style mixing creates a separate target G(mix(c,w))G(\text{mix}(c,w)) for each sample: cc controls the early, coarse generator features and determines pose, while ww 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 TT receives an image and produces a sampling grid gRH×W×2g\in\mathbb{R}^{H\times W\times2}, 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 TT 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 TT, 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,

TV(x)=nyn+1yn TV(x) = \sum_n \vert y_{n+1} - y_n \vert

Definitions of total-variation distance vary. In this paper it regularizes sampling grid gg so that the grid does not deform excessively:

LTV(T)=LHuber(Δxg)+LHuber(Δyg) \mathcal{L}_\text{TV} (T) = \mathcal{L}_\text{Huber} (\Delta_x g) + \mathcal{L}_\text{Huber} (\Delta_y g)

As the equation shows, it calculates directional differences along xx and yy. An additional norm discourages gg from moving too far from the identity transformation:

LI(T)=g22 \mathcal{L}_I (T) = \parallel g \parallel^2_2

Parameterization of cc

The target latent vector cc is not optimized as a completely unconstrained input through differentiable generator GG. Instead, it is parameterized as a linear combination of the top NN principal directions in W\mathcal{W} space:

c=wˉ+i=1Nαidi c = \bar{w} + \sum_{i=1}^N \alpha_i d_i

These principal directions are the familiar axes extracted by PCA. Here wˉ\bar{w} is the empirical mean latent, did_i is the iith principal direction, and each αi\alpha_i is a learned scalar coefficient.

This parameterization matters because StyleGAN's W\mathcal{W} space is highly expressive. Optimizing cc without constraints tends to degrade target-image quality and move the result away from the manifold of natural images. Reducing the number NN of principal axes restricts the feasible directions of cc, effectively constraining it around the mean of W\mathcal{W} and mitigating this problem.

The final GANgealing objective is

L(T, c)=EwW(Lalign(T,c)+λTVLTV(T)+λILI(T)) \mathcal{L}(T,~c) = \mathbb{E}_{w \sim \mathcal{W}} (\mathcal{L}_\text{align} (T, c) + \lambda_\text{TV} \mathcal{L}_\text{TV} (T) + \lambda_I \mathcal{L}_I (T))

The experiments use λTV=1000\lambda_\text{TV}=1000 or 25002500, and λI=1\lambda_I=1.

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 cc to a clustering algorithm.

Suppose we learn KK vectors cc. Each latent constant captures a particular data mode, so learning (ck)k=1K(c_k)_{k=1}^K models several modes in a dataset. As before, each cc is a linear combination of the top NN principal directions in W\mathcal{W}, with a separate set of learned α\alpha coefficients for every ckc_k. The paper similarly uses KK distinct spatial transformers TkT_k, one per mode. The resulting model learns dense correspondence among all images within each cluster.

The remaining question is how to guide each pair (ck,Tk)(c_k,T_k) toward a particular mode. The method uses the hard-assignment step

LalignK(T,c)=minkLalign(Tk,ck) \mathcal{L}_\text{align}^K (T, c) = \min_k \mathcal{L}_\text{align} (T_k, c_k)

so that each unaligned image is assigned to and aligned under one mode. The familiar unimodal case corresponds to K=1K=1.

At test time, a generated image G(w)G(w) can be assigned to

k=argminkLalign(Tk, ck)k^\ast = \arg \min_k \mathcal{L}_\text{align}(T_k,~c_k)

and warped with the corresponding transformer TkT_{k^\ast}. Real images are harder: calculating Lalign\mathcal{L}_\text{align} requires not only the input image, but the latent ww that generated it.

Clustering over KK modes optimizes multiple modalities, and each cc is mixed and optimized with the input latent ww, 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 ww whose generated sample resembles image xx. 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 kk^\ast is already available, so pairs (G(w),k)(G(w),k^\ast) 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.