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

Classes

ExactPadding2d

This function calculate exact padding values for 4D tensor inputs,

SCFpyr_PyTorch

This is a modified version of buildSFpyr, that constructs a

ExactPadding2d

This function calculate exact padding values for 4D tensor inputs,

Functions

imresize(→ torch.Tensor)

Args:

symm_pad(im, padding)

Symmetric padding same as tensorflow.

fspecial([size, sigma, channels, filter_type])

Function same as 'fspecial' in MATLAB, only support gaussian now.

conv2d(input, weight[, bias, stride, padding, ...])

Matlab like conv2, weights needs to be flipped.

imfilter(input, weight[, bias, stride, padding, ...])

imfilter same as matlab.

filter2(input, weight[, shape])

dct2d(x[, norm])

2-dimentional Discrete Cosine Transform, Type II (a.k.a. the DCT)

fitweibull(x[, iters, eps])

Simulate wblfit function in matlab.

nancov(x)

Calculate nancov for batched tensor, rows that contains nan value

nanmean(v, *args[, inplace])

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

im2col(x, kernel[, mode])

simple im2col as matlab

blockproc(x, kernel, fun[, border_size, pad_partial, ...])

blockproc function like matlab

exact_padding_2d(x, kernel[, stride, dilation, mode])

symm_pad(im, padding)

Symmetric padding same as tensorflow.

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]
Args:

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:

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.

Args:

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. Args:

size (int or tuple): size of window sigma (float): sigma of gaussian channels (int): channels of output

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. Args:

input (tensor): (b, c, h, w) weight (tensor): (out_ch, in_ch, kh, kw), conv weight bias (bool or None): bias stride (int or tuple): conv stride padding (str): padding mode dilation (int): conv dilation

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

imfilter same as matlab. Args:

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

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

2-dimentional 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.

Args:

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

Return:

cov (tensor): (B, feat_dim, feat_dim)

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

Args:

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

Return:

flatten patch (Tensor): (b, h * w / kernel **2, kernel * kernel)

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.

Args:

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

Return:

results (tensor): concatenated results of each block

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.

Args:

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

Returns:

pyramid: list containing torch.Tensor objects storing the 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.

Args:

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