Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.When “on average right” looks wrong
Ask a network to sharpen a blurry photo. Many sharp images could have produced that blur: the edge could be here or one pixel over. Squared error rewards predicting the average of all those possibilities, and the average of many sharp edges is a soft blur. Two families of losses fight this. Perceptual losses compare images by the features a vision network sees, not by raw pixels. Adversarial losses train a second network to spot fakes, and reward outputs it cannot tell from real images.
| MSE-optimal (mean) | expected pixel MSE 0.0441 | sharpness 0.059 |
| MAE-optimal (median) | expected pixel MSE 0.0662 | sharpness 1.000 |
| a realistic sample | expected pixel MSE 0.0744 | sharpness 1.000 |
Super-resolution of satellite imagery, image enhancement for low-light inspection, synthetic training data for perception, and simulation-to-real translation all use perceptual and adversarial losses. They also hallucinate plausible detail that is not in the data, which matters greatly when images feed measurements or safety decisions.
- 1. Why do MSE-trained image models blur?
- 2. A perceptual loss compares images…
- 3. In an adversarial loss, the discriminator…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Perceptual (feature) loss: Σ_l w_l·mean((φ_l(ŷ) − φ_l(y))²), where φ_l are feature maps from layers of a fixed network.
- Style loss: compare Gram matrices G = FFᵀ/n of the feature maps (channel correlations). Gram matrices ignore positions, so they capture texture rather than layout.
- GAN game: min_G max_D E[log D(x)] + E[log(1 − D(G(z)))].
- Discriminator loss: −log D(x) − log(1 − D(G(z))). At D = ½ on both it is 2 log 2 ≈ 1.386.
- Minimax generator loss: log(1 − D(G(z))). Its gradient is tiny when D(G(z)) ≈ 0, which is early in training.
- Non-saturating generator loss: −log D(G(z)). Its gradient is large then: −1/D = −100 at D = 0.01, against about −1.01 for minimax.
- Hinge GAN: D minimises relu(1 − s_real) + relu(1 + s_fake); G minimises −s_fake.
- Optimal discriminator: D*(x) = p_data(x)/(p_data(x) + p_g(x)). At p_data = 0.6, p_g = 0.2: 0.75.
- 1. Gram matrices capture…
- 2. The non-saturating generator loss is…
- 3. D* where the data and generator densities are equal is…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.The losses in code
Exercise 1.
- Perceptual and Gram losses are computed on feature maps. The Gram matrix is invariant to reordering positions.
- The discriminator loss is 2 log 2 when D outputs ½.
- When D(G(z)) = 0.01, the minimax generator’s gradient with respect to D is −1.01, while the non-saturating one’s is −100. This is why practical GANs use the latter.
- With the optimal discriminator, the GAN value equals 2·JS(p_data, p_g) − log 4, reaching −log 4 exactly when the generator matches the data.
Practice.
- Perceptual losses use a pretrained network (VGG-16/19 layers relu2_2 to relu4_3 are common) with frozen weights. Normalise inputs as that network expects.
- LPIPS (Zhang et al., 2018) calibrates feature distances to human judgements and is a standard evaluation metric.
- Adversarial training is unstable. Common stabilisers are spectral normalisation, gradient penalties, the hinge loss, two time-scale learning rates and an added pixel or perceptual term (as in SRGAN and pix2pix).
loss_G = F.l1_loss(out, tgt) + 0.1 * F.mse_loss(vgg(out), vgg(tgt)) + 1e-3 * F.softplus(-D(out)).mean()- 1. LPIPS is…
- 2. SRGAN combines an adversarial loss with…
- 3. A common GAN stabiliser is…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why pixel losses blur. For an input with several plausible outputs, the MSE-optimal prediction is the conditional mean E[y | x], which averages them. The MAE-optimal prediction is the pixelwise median: sharper, but pixelwise medians can still mix structure across pixels. Neither produces a sample from p(y | x).
Perceptual losses. Feature distances are more tolerant of small shifts and more sensitive to structure than pixel distances, which improves texture in style transfer and super-resolution (Johnson, Alahi and Fei-Fei, 2016). But a feature-space MSE is still a mean-seeking objective. Under true ambiguity it also averages, in feature space. Exercise 2 shows a random-feature version blurring just like pixel MSE.
Adversarial losses.
- Goodfellow et al. (2014) showed that with the optimal discriminator the generator minimises 2·JS(p_data, p_g) − log 4, a divergence between whole distributions.
- A generator trained this way is rewarded for producing outputs that lie on the data distribution, so it picks a plausible mode instead of an average.
- Conditional GANs (pix2pix, SRGAN) add this to a pixel or perceptual loss, so outputs are both faithful and realistic.
The cost: training instability, mode collapse, and hallucinated detail, plausible but not true, a real danger when outputs are used as measurements.
- 1. With the optimal discriminator, GAN training minimises…
- 2. A feature-space MSE under true ambiguity…
- 3. The main danger of adversarial super-resolution for measurement is…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Do perceptual losses remove blur?
History.
- I. Goodfellow et al. (2014) introduced GANs and the non-saturating loss.
- L. Gatys, A. Ecker and M. Bethge (2015) introduced Gram-matrix style losses.
- J. Johnson, A. Alahi and L. Fei-Fei (2016) introduced perceptual losses for fast style transfer and super-resolution.
- C. Ledig et al. (2017) combined content and adversarial losses in SRGAN.
- P. Isola et al. (2017) introduced pix2pix.
- R. Zhang et al. (2018) introduced the LPIPS metric.
Exercise 2. A 64-pixel output must match a sharp step edge whose position is uniformly uncertain within ±spread pixels. A single output signal is optimised by gradient descent to minimise the average loss against 200 sampled targets under pixel MSE, pixel MAE, or a perceptual loss using 16 fixed random ReLU convolution filters as the feature network.
| edge spread | loss | expected pixel MSE | sharpness (largest jump; 1 = sharp) | feature distance |
|---|---|---|---|---|
| ±0 | all three | 0.000 | 1.00 | 0.000 |
| ±4 | pixel MSE | 0.0242 | 0.150 | 0.0928 |
| ±4 | pixel MAE | 0.0370 | 0.991 | 0.1350 |
| ±4 | perceptual | 0.0243 | 0.144 | 0.0927 |
| ±8 | pixel MSE | 0.0445 | 0.090 | 0.1743 |
| ±8 | pixel MAE | 0.0627 | 0.723 | 0.2438 |
| ±8 | perceptual | 0.0445 | 0.087 | 0.1742 |
- Pixel MSE produced the predicted blur. Its output was a ramp whose largest step was 0.15 or less, while having the lowest pixel error.
- The random-feature perceptual loss blurred just as much. Its output was almost identical to MSE’s. In a native probe, wider filters (15 and 31 pixels) and more filters did not change this: a feature-space mean is still a mean.
- Pixel MAE kept the edge sharp at the median position, at the cost of a higher pixel MSE, the expected trade-off.
- Producing a realistic sharp sample needs an objective that prefers realistic outputs over averages, which is what adversarial losses provide. That was not simulated here. Pretrained perceptual networks help with texture and small shifts, but this experiment shows that feature matching alone does not resolve genuine ambiguity.
Perceptual and adversarial losses in AI, robotics and aerospace
- Remote sensing: GAN and perceptual super-resolution sharpen satellite imagery for display, but the results must not be used for measurement without validation, because detail can be invented.
- Synthetic data: GANs and image translation turn simulator renders into realistic camera images for training perception (sim-to-real).
- Image enhancement: low-light and dehazing networks for inspection drones combine pixel, perceptual and adversarial terms.
References
- I. Goodfellow et al. (2014). “Generative adversarial nets.” Advances in NeurIPS 27.
- L. A. Gatys, A. S. Ecker, M. Bethge (2016). “Image style transfer using convolutional neural networks.” Proc. CVPR.
- J. Johnson, A. Alahi, L. Fei-Fei (2016). “Perceptual losses for real-time style transfer and super-resolution.” Proc. ECCV.
- C. Ledig et al. (2017). “Photo-realistic single image super-resolution using a generative adversarial network.” Proc. CVPR.
- R. Zhang, P. Isola, A. A. Efros, E. Shechtman, O. Wang (2018). “The unreasonable effectiveness of deep features as a perceptual metric.” Proc. CVPR.