piqa.gmsd#
Gradient Magnitude Similarity Deviation (GMSD) and Multi-Scale Gradient Magnitude Similarity Deviation (MS-GMSD)
This module implements the GMSD and MS-GMSD in PyTorch.
Original
https://www4.comp.polyu.edu.hk/~cslzhang/IQA/GMSD/GMSD.htm
References
Functions#
Classes#
Descriptions#
- piqa.gmsd.gmsd(x, y, kernel, value_range=1.0, c=0.00261437908496732, alpha=0.0)#
Returns the GMSD between \(x\) and \(y\), without color space conversion and downsampling.
\(\text{GMSD}(x, y)\) is the standard deviation of the Gradient Magnitude Similarity (GMS).
\[\begin{split}\text{GMS}(x, y) &= \frac{(2 - \alpha) \text{GM}(x) \text{GM}(y) + C} {\text{GM}(x)^2 + \text{GM}(y)^2 - \alpha \text{GM}(x) \text{GM}(y) + C} \\ \text{GM}(z) &= \left\| \nabla z \right\|_2\end{split}\]where \(\nabla z\) is the result of a gradient convolution over \(z\).
- Parameters:
Note
For the remaining arguments, refer to Xue et al. (2013).
- Returns:
The GMSD vector, \((N,)\).
- Return type:
Example
>>> x = torch.rand(5, 1, 256, 256) >>> y = torch.rand(5, 1, 256, 256) >>> kernel = gradient_kernel(prewitt_kernel()) >>> l = gmsd(x, y, kernel) >>> l.shape torch.Size([5])
- piqa.gmsd.ms_gmsd(x, y, kernel, weights, value_range=1.0, c=0.00261437908496732, alpha=0.5)#
Returns the MS-GMSD between \(x\) and \(y\), without color space conversion.
\[\text{MS-GMSD}(x, y) = \sqrt{\sum^{M}_{i = 1} w_i \text{GMSD}(x^i, y^i)^2}\]where \(x^i\) and \(y^i\) are obtained by downsampling the initial tensors by a factor \(2^{i - 1}\).
- Parameters:
Note
For the remaining arguments, refer to Zhang et al. (2017).
- Returns:
The MS-GMSD vector, \((N,)\).
- Return type:
Example
>>> x = torch.rand(5, 1, 256, 256) >>> y = torch.rand(5, 1, 256, 256) >>> kernel = gradient_kernel(prewitt_kernel()) >>> weights = torch.rand(4) >>> l = ms_gmsd(x, y, kernel, weights) >>> l.shape torch.Size([5])
- class piqa.gmsd.GMSD(downsample=True, kernel=None, reduction='mean', **kwargs)#
Measures the GMSD between an input and a target.
Before applying
gmsd
, the input and target are converted from RBG to Y, the luminance color space, and downsampled by a factor 2.- Parameters:
Example
>>> criterion = GMSD() >>> x = torch.rand(5, 3, 256, 256, requires_grad=True) >>> y = torch.rand(5, 3, 256, 256) >>> l = criterion(x, y) >>> l.shape torch.Size([]) >>> l.backward()
- class piqa.gmsd.MS_GMSD(kernel=None, weights=None, reduction='mean', **kwargs)#
Measures the MS-GMSD between an input and a target.
Before applying
ms_gmsd
, the input and target are converted from RBG to Y, the luminance color space.- Parameters:
kernel (Tensor) – A gradient kernel, \((2, 1, K, K)\). If
None
, use the Prewitt kernel instead.weights (Tensor) – The weights of the scales, \((M,)\). If
None
, use theMS_GMSD.WEIGHTS
instead.reduction (str) – Specifies the reduction to apply to the output:
'none'
,'mean'
or'sum'
.kwargs – Keyword arguments passed to
ms_gmsd
.
Example
>>> criterion = MS_GMSD() >>> x = torch.rand(5, 3, 256, 256, requires_grad=True) >>> y = torch.rand(5, 3, 256, 256) >>> l = criterion(x, y) >>> l.shape torch.Size([]) >>> l.backward()