Image understanding, generation, and 3D reconstruction · Image generation and restoration · VIS023 · Model
Pix2Pix conditional image-to-image translation
Pix2Pix learns a mapping between aligned source and target images with conditional adversarial training and a reconstruction term.
In CodeSplash
Documented in the Academy only. There's no Set up button.
- Availability
- Explained here
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Pix2Pix learns a mapping between aligned source and target images with conditional adversarial training and a reconstruction term. It applies to paired translation tasks such as labels-to-images or edge-to-image synthesis; its paired-data requirement distinguishes it from the unpaired CycleGAN entry.
Question / hypotheses
Can a conditional adversarial model reproduce the paired target while preserving the source-target correspondence? This is a modeling/evaluation question, not a formal null-hypothesis test.
Architecture / computational structure
A conditional generator, commonly a U-Net, predicts a target image from a source image. A PatchGAN discriminator judges local source-target patch pairs. The generator balances adversarial realism against pixelwise agreement with the aligned target.
Assumptions
Pairs must be spatially and semantically aligned. Adversarial realism does not establish factual fidelity; rare targets and domain shifts can produce unsupported content. Validation images must be independent scenes/entities rather than nearby crops from training images.
Formula / computational definition
. Practical generator updates commonly use the non-saturating adversarial form; are paired images and sets reconstruction weight.
Parameters and notation
Notation: Here x is a source image, y its aligned target, G the generator, D a conditional discriminator and lambda the reconstruction weight. Patchwise discriminator outputs are averaged under the declared reduction; the toy example uses non-saturating generator updates.
Learned or estimated: Generator encoder/decoder/skip-connection weights and conditional discriminator weights. The source image is conditioning input; target alignment is part of the data, not something the basic objective estimates.
Hyperparameters and effects: Generator/discriminator capacity, PatchGAN receptive field, reconstruction weight, image normalization, augmentation and alternating optimizer schedules. Large L1 weighting can favor smoother outputs; stronger adversarial weighting can hallucinate plausible but incorrect details.
Fitting, fine-tuning, and adaptation
Fit alternating discriminator and generator updates with consistent paired augmentation. For transfer, adapt late generator layers and discriminator heads first, then cautiously unfreeze earlier encoder layers at smaller rates. Balance losses using held-out fidelity and realism, monitor source-domain retention and inspect mode collapse or discriminator domination.
How to use and calibrate
Split by source scene/object before cropping or creating degradations, and keep paired transformations spatially consistent. Use paired pixel/structural error and task-specific fidelity, plus a documented perceptual or realism evaluation. Report alignment assumptions and failure cases. For scientific images, validate measurement preservation and avoid interpreting hallucinated features as observations.
Example application
Translate paired edge maps into illustrative images or learn a controlled paired image-restoration mapping. This is a proposed use case, not a claim of validated performance on supplied real data.
Interpretation / pitfalls
Watch out
Realistic outputs can contain invented details. Evaluate fidelity to the paired target and the measurements or decisions the restored image will support. A correct component calculation does not validate the full architecture, checkpoint or deployment pipeline.
Python and R code examples
Scope: Conditional PatchGAN discriminator/generator losses plus paired-image L1 reconstruction from supplied probabilities and outputs; no generator/discriminator training. Use the shared setup. Dependencies: NumPy / base R. Validation: Python executed; R unexecuted.
Full-model route: Python: official pix2pix/CycleGAN project with pix2pix mode. R: native torch conditional GAN or bridge. The examples compute conditional patch adversarial plus L1 losses from supplied predictions, not trained image synthesis.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — Conditional PatchGAN discriminator/generator losses plus paired-image L1 reconstruction from supplied probabilities and outputs; no generator/discriminator training. Use the shared setup.
Shared setup for this chapter
import numpy as np
from itertools import permutations
def softmax(x):
x=np.asarray(x,dtype=float)
e=np.exp(x-x.max(axis=-1,keepdims=True))
return e/e.sum(axis=-1,keepdims=True)
def bilinear(a,y,x):
y=float(np.clip(y,0,a.shape[0]-1)); x=float(np.clip(x,0,a.shape[1]-1))
y0=int(np.floor(y)); x0=int(np.floor(x))
y1=min(y0+1,a.shape[0]-1); x1=min(x0+1,a.shape[1]-1)
dy=y-y0; dx=x-x0
return (1-dy)*((1-dx)*a[y0,x0]+dx*a[y0,x1])+dy*((1-dx)*a[y1,x0]+dx*a[y1,x1])
target=np.array([[.1,.8],[.4,.9]]); generated=np.array([[.2,.6],[.3,.85]])
d_real=np.array([.9,.8,.85,.7]); d_fake=np.array([.4,.3,.45,.2])
d_loss=-np.mean(np.log(d_real)+np.log1p(-d_fake))
g_adv=-np.log(d_fake).mean(); l1=np.abs(generated-target).mean(); lam=100.
g_loss=g_adv+lam*l1
assert l1>0 and g_loss>g_adv
print('D loss:',d_loss,'G adversarial:',g_adv,'paired L1:',l1,'G total:',g_loss)
Component demo, not a trained model — Conditional PatchGAN discriminator/generator losses plus paired-image L1 reconstruction from supplied probabilities and outputs; no generator/discriminator training. Use the shared setup.
Shared setup for this chapter
softmax <- function(x) {
if(is.null(dim(x))) {e<-exp(x-max(x)); return(e/sum(e))}
t(apply(x,1,function(v) {e<-exp(v-max(v)); e/sum(e)}))
}
bilinear <- function(a,y,x) {
y<-min(max(y,0),nrow(a)-1); x<-min(max(x,0),ncol(a)-1)
y0<-floor(y); x0<-floor(x); y1<-min(y0+1,nrow(a)-1); x1<-min(x0+1,ncol(a)-1)
dy<-y-y0; dx<-x-x0
(1-dy)*((1-dx)*a[y0+1,x0+1]+dx*a[y0+1,x1+1])+dy*((1-dx)*a[y1+1,x0+1]+dx*a[y1+1,x1+1])
}
target<-matrix(c(.1,.8,.4,.9),2,byrow=TRUE); generated<-matrix(c(.2,.6,.3,.85),2,byrow=TRUE)
d_real<-c(.9,.8,.85,.7); d_fake<-c(.4,.3,.45,.2)
d_loss<--mean(log(d_real)+log1p(-d_fake))
g_adv<--mean(log(d_fake)); l1<-mean(abs(generated-target)); lam<-100
g_loss<-g_adv+lam*l1
stopifnot(l1>0,g_loss>g_adv)
print(list(D_loss=d_loss,G_adversarial=g_adv,paired_L1=l1,G_total=g_loss))
Sources
- Source 1arxiv.org
- Source 2github.com
From the CodeSplash reference, entry VIS023.