piqa.tv#

Total Variation (TV)

This module implements the TV in PyTorch.

Wikipedia

https://wikipedia.org/wiki/Total_variation

Functions#

tv

Returns the TV of \(x\).

Classes#

TV

Measures the TV of an input.

Descriptions#

piqa.tv.tv(x, norm='L1')#

Returns the TV of \(x\).

With 'L1',

\[\text{TV}(x) = \sum_{i, j} \left| x_{i+1, j} - x_{i, j} \right| + \left| x_{i, j+1} - x_{i, j} \right|\]

Alternatively, with 'L2',

\[\text{TV}(x) = \left( \sum_{c, i, j} (x_{c, i+1, j} - x_{c, i, j})^2 + (x_{c, i, j+1} - x_{c, i, j})^2 \right)^{\frac{1}{2}}\]
Parameters:
  • x (Tensor) – An input tensor, \((*, C, H, W)\).

  • norm (str) – Specifies the norm funcion to apply: 'L1', 'L2' or 'L2_squared'.

Returns:

The TV tensor, \((*,)\).

Return type:

Tensor

Example

>>> x = torch.rand(5, 3, 256, 256)
>>> l = tv(x)
>>> l.shape
torch.Size([5])
class piqa.tv.TV(reduction='mean', **kwargs)#

Measures the TV of an input.

Parameters:
  • reduction (str) – Specifies the reduction to apply to the output: 'none', 'mean' or 'sum'.

  • kwargs – Keyword arguments passed to tv.

Example

>>> criterion = TV()
>>> x = torch.rand(5, 3, 256, 256, requires_grad=True)
>>> l = criterion(x)
>>> l.shape
torch.Size([])
>>> l.backward()
forward(x)#
Parameters:

x (Tensor) – An input tensor, \((N, C, H, W)\).

Returns:

The TV vector, \((N,)\) or \(()\) depending on reduction.

Return type:

Tensor