pyiqa.matlab_utils

This folder contains pytorch implementations of matlab functions. And should produce the same results as matlab.

Note: to enable GPU acceleration, all functions take batched tensors as inputs, and return batched results.

Submodules

Package Contents

pyiqa.matlab_utils.imresize(x: torch.Tensor, scale: float | None = None, sizes: Tuple[int, int] | None = None, kernel: str | torch.Tensor = 'cubic', sigma: float = 2, rotation_degree: float = 0, padding_type: str = 'reflect', antialiasing: bool = True) torch.Tensor[source]
Parameters:
  • x (torch.Tensor)

  • scale (float)

  • sizes (tuple(int, int))

  • kernel (str, default='cubic')

  • sigma (float, default=2)

  • rotation_degree (float, default=0)

  • padding_type (str, default='reflect')

  • antialiasing (bool, default=True)

Return type:

torch.Tensor

class pyiqa.matlab_utils.ExactPadding2d(kernel, stride=1, dilation=1, mode='same')[source]

Bases: torch.nn.Module

This function calculate exact padding values for 4D tensor inputs, and support the same padding mode as tensorflow.

Parameters:
  • kernel (int or tuple) – kernel size.

  • stride (int or tuple) – stride size.

  • dilation (int or tuple) – dilation size, default with 1.

  • mode (srt) – padding mode can be (‘same’, ‘symmetric’, ‘replicate’, ‘circular’)

forward(x)[source]
pyiqa.matlab_utils.symm_pad(im: torch.Tensor, padding: Tuple[int, int, int, int])[source]

Symmetric padding same as tensorflow. Ref: https://discuss.pytorch.org/t/symmetric-padding/19866/3

pyiqa.matlab_utils.fspecial(size=None, sigma=None, channels=1, filter_type='gaussian')[source]

Function same as ‘fspecial’ in MATLAB, only support gaussian now. :param size: size of window :type size: int or tuple :param sigma: sigma of gaussian :type sigma: float :param channels: channels of output :type channels: int

pyiqa.matlab_utils.conv2d(input, weight, bias=None, stride=1, padding='same', dilation=1, groups=1)[source]

Matlab like conv2, weights needs to be flipped. :param input: (b, c, h, w) :type input: tensor :param weight: (out_ch, in_ch, kh, kw), conv weight :type weight: tensor :param bias: bias :type bias: bool or None :param stride: conv stride :type stride: int or tuple :param padding: padding mode :type padding: str :param dilation: conv dilation :type dilation: int

pyiqa.matlab_utils.imfilter(input, weight, bias=None, stride=1, padding='same', dilation=1, groups=1)[source]

imfilter same as matlab. :param input: (b, c, h, w) tensor to be filtered :type input: tensor :param weight: (out_ch, in_ch, kh, kw) filter kernel :type weight: tensor :param padding: padding mode :type padding: str :param dilation: dilation of conv :type dilation: int :param groups: groups of conv :type groups: int

pyiqa.matlab_utils.filter2(input, weight, shape='same')[source]
pyiqa.matlab_utils.dct2d(x, norm='ortho')[source]

2-dimensional Discrete Cosine Transform, Type II (a.k.a. the DCT) For the meaning of the parameter norm, see: https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.fftpack.dct.html :param x: the input signal :param norm: the normalization, None or ‘ortho’ :return: the DCT-II of the signal over the last 2 dimensions

pyiqa.matlab_utils.fitweibull(x, iters=50, eps=0.01)[source]

Simulate wblfit function in matlab.

ref: https://github.com/mlosch/python-weibullfit/blob/master/weibull/backend_pytorch.py

Fits a 2-parameter Weibull distribution to the given data using maximum-likelihood estimation. :param x (tensor): (B, N), batch of samples from an (unknown) distribution. Each value must satisfy x > 0. :param iters: Maximum number of iterations :param eps: Stopping criterion. Fit is stopped ff the change within two iterations is smaller than eps. :param use_cuda: Use gpu :return: Tuple (Shape, Scale) which can be (NaN, NaN) if a fit is impossible.

Impossible fits may be due to 0-values in x.

pyiqa.matlab_utils.nancov(x)[source]

Calculate nancov for batched tensor, rows that contains nan value will be removed.

Parameters:

x (tensor) – (B, row_num, feat_dim)

Returns:

(B, feat_dim, feat_dim)

Return type:

cov (tensor)

pyiqa.matlab_utils.nanmean(v, *args, inplace=False, **kwargs)[source]

nanmean same as matlab function: calculate mean values by removing all nan.

pyiqa.matlab_utils.im2col(x, kernel, mode='sliding')[source]

simple im2col as matlab

Parameters:
  • x (Tensor) – shape (b, c, h, w)

  • kernel (int) – kernel size

  • mode (string) –

    • sliding (default): rearranges sliding image neighborhoods of kernel size into columns with no zero-padding

    • distinct: rearranges discrete image blocks of kernel size into columns, zero pad right and bottom if necessary

Returns:

(b, h * w / kernel **2, kernel * kernel)

Return type:

flatten patch (Tensor)

pyiqa.matlab_utils.blockproc(x, kernel, fun, border_size=None, pad_partial=False, pad_method='zero', **func_args)[source]

blockproc function like matlab

Difference:
  • Partial blocks is discarded (if exist) for fast GPU process.

Parameters:
  • x (tensor) – shape (b, c, h, w)

  • kernel (int or tuple) – block size

  • func (function) – function to process each block

  • border_size (int or tuple) – border pixels to each block

  • pad_partial – pad partial blocks to make them full-sized, default False

  • pad_method – [zero, replicate, symmetric] how to pad partial block when pad_partial is set True

Returns:

concatenated results of each block

Return type:

results (tensor)

class pyiqa.matlab_utils.SCFpyr_PyTorch(height=5, nbands=4, scale_factor=2, device=None)[source]

Bases: object

This is a modified version of buildSFpyr, that constructs a complex-valued steerable pyramid using Hilbert-transform pairs of filters. Note that the imaginary parts will not be steerable. Pytorch version >= 1.8.0

build(im_batch)[source]

Decomposes a batch of images into a complex steerable pyramid. The pyramid typically has ~4 levels and 4-8 orientations.

Parameters:

im_batch (torch.Tensor) – Batch of images of shape [N,C,H,W]

Returns:

list containing torch.Tensor objects storing the pyramid

Return type:

pyramid

class pyiqa.matlab_utils.ExactPadding2d(kernel, stride=1, dilation=1, mode='same')[source]

Bases: torch.nn.Module

This function calculate exact padding values for 4D tensor inputs, and support the same padding mode as tensorflow.

Parameters:
  • kernel (int or tuple) – kernel size.

  • stride (int or tuple) – stride size.

  • dilation (int or tuple) – dilation size, default with 1.

  • mode (srt) – padding mode can be (‘same’, ‘symmetric’, ‘replicate’, ‘circular’)

forward(x)[source]
pyiqa.matlab_utils.exact_padding_2d(x, kernel, stride=1, dilation=1, mode='same')[source]
pyiqa.matlab_utils.symm_pad(im: torch.Tensor, padding: Tuple[int, int, int, int])[source]

Symmetric padding same as tensorflow. Ref: https://discuss.pytorch.org/t/symmetric-padding/19866/3