Formula

Total Variation Loss in CNN-Based Style Transfer

In neural style transfer, the learned synthesized image often accumulates high-frequency noise, manifesting as particularly bright or dark pixels. To mitigate this, a noise reduction method known as total variation denoising is applied. Denoting xi,jx_{i, j} as the pixel value at coordinate (i,j)(i, j), the total variation loss is defined as:

i,jxi,jxi+1,j+xi,jxi,j+1\sum_{i, j} \left|x_{i, j} - x_{i+1, j}\right| + \left|x_{i, j} - x_{i, j+1}\right|

Reducing this total variation loss encourages the values of neighboring pixels on the synthesized image to become closer, thereby promoting a smoother and visually cleaner final output.

# PyTorch def tv_loss(Y_hat): return 0.5 * (torch.abs(Y_hat[:, :, 1:, :] - Y_hat[:, :, :-1, :]).mean() + torch.abs(Y_hat[:, :, :, 1:] - Y_hat[:, :, :, :-1]).mean())

0

1

Updated 2026-05-21

Contributors are:

Who are from:

Tags

D2L

Dive into Deep Learning @ D2L