direct.functionals package

Contents

direct.functionals package#

Submodules#

direct.functionals.challenges module#

Direct metrics for the FastMRI and Calgary-Campinas challenges.

direct.functionals.challenges.fastmri_ssim(gt, target)[source]#

Compute Structural Similarity Index Measure (SSIM) compatible with the FastMRI challenge.

Parameters:
  • gt – Ground truth tensor.

  • target – Target/prediction tensor.

Returns:

SSIM value.

direct.functionals.challenges.fastmri_psnr(gt, pred)[source]#

Compute Peak Signal to Noise Ratio metric (PSNR) compatible with the FastMRI challenge.

Parameters:
  • gt – Ground truth tensor.

  • pred – Prediction tensor.

Returns:

PSNR value.

direct.functionals.challenges.fastmri_nmse(gt, pred)[source]#

Compute Normalized Mean Square Error metric (NMSE) compatible with the FastMRI challenge.

Parameters:
  • gt – Ground truth tensor.

  • pred – Prediction tensor.

Returns:

NMSE value.

direct.functionals.challenges.calgary_campinas_ssim(gt, pred)[source]#
direct.functionals.challenges.calgary_campinas_psnr(gt, pred)[source]#
direct.functionals.challenges.calgary_campinas_vif(gt, pred)[source]#

direct.functionals.grad module#

class direct.functionals.grad.SobelGradL1Loss(reduction='mean', normalized_grad=True)[source]#

Bases: SobelGradLoss

Computes the sum of the l1-loss between the gradient of input and target:

It returns

\[||u_x - v_x ||_1 + ||u_y - v_y||_1\]

where \(u\) and \(v\) denote the input and target images. The gradients w.r.t. to \(x\) and \(y\) directions are computed using the Sobel operators.

__init__(reduction='mean', normalized_grad=True)[source]#

Inits SobelGradL1Loss.

Parameters:
  • reduction (str) – Loss reduction. Can be "mean" or "sum". Default: "mean".

  • normalized_grad (bool) – Whether the computed gradients are normalized. Default: True.

class direct.functionals.grad.SobelGradL2Loss(reduction='mean', normalized_grad=True)[source]#

Bases: SobelGradLoss

Computes the sum of the l2-loss between the gradient of input and target:

It returns

\[||u_x - v_x ||_2^2 + ||u_y - v_y||_2^2\]

where \(u\) and \(v\) denote the input and target images. The gradients w.r.t. to \(x\) and \(y\) directions are computed using the Sobel operators.

__init__(reduction='mean', normalized_grad=True)[source]#

Inits SobelGradL2Loss.

Parameters:
  • reduction (str) – Loss reduction. Can be "mean" or "sum". Default: "mean".

  • normalized_grad (bool) – Whether the computed gradients are normalized. Default: True.

direct.functionals.hfen module#

direct.nn.functionals.hfen module.

direct.functionals.hfen.hfen_l1(inp, target, reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Calculates HFENL1 loss between inp and target.

Parameters:
  • inp (Tensor) – Input tensor.

  • target (Tensor) – Target tensor.

  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

Return type:

Tensor

Returns:

HFENL1 loss value.

direct.functionals.hfen.hfen_l2(inp, target, reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Calculates HFENL2 loss between inp and target.

Parameters:
  • inp (Tensor) – Input tensor.

  • target (Tensor) – Target tensor.

  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

Return type:

Tensor

Returns:

HFENL2 loss value.

class direct.functionals.hfen.HFENLoss(criterion, reduction='mean', kernel_size=5, sigma=2.5, norm=False)[source]#

Bases: Module

High Frequency Error Norm (HFEN) Loss as defined in _[1].

Calculates:

\[|| \text{LoG}(x_\text{rec}) - \text{LoG}(x_\text{tar}) ||_C\]

Where C can be any norm, LoG is the Laplacian of Gaussian filter, and \(x_\text{rec}), \text{LoG}(x_\text{tar}\) are the reconstructed inp and target images. If normalized it scales it by \(|| \text{LoG}(x_\text{tar}) ||_C\).

Code was borrowed and adapted from _[2] (not licensed).

References

__init__(criterion, reduction='mean', kernel_size=5, sigma=2.5, norm=False)[source]#

Inits HFENLoss.

Parameters:
  • criterion (Module) – Loss function to calculate the difference between log1 and log2.

  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

forward(inp, target)[source]#

Forward pass of the HFENLoss.

Parameters:
  • inp (Tensor) – Input tensor.

  • target (Tensor) – Target tensor.

Return type:

Tensor

Returns:

HFEN loss value.

class direct.functionals.hfen.HFENL1Loss(reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Bases: HFENLoss

High Frequency Error Norm (HFEN) Loss using L1Loss criterion.

Calculates:

\[|| \text{LoG}(x_\text{rec}) - \text{LoG}(x_\text{tar}) ||_1\]

Where LoG is the Laplacian of Gaussian filter, and \(x_\text{rec}), \text{LoG}(x_\text{tar}\) are the reconstructed inp and target images. If normalized it scales it by \(|| \text{LoG}(x_\text{tar}) ||_1\).

__init__(reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Inits HFENL1Loss.

Parameters:
  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

class direct.functionals.hfen.HFENL2Loss(reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Bases: HFENLoss

High Frequency Error Norm (HFEN) Loss using L2Loss criterion.

Calculates:

\[|| \text{LoG}(x_\text{rec}) - \text{LoG}(x_\text{tar}) ||_2\]

Where LoG is the Laplacian of Gaussian filter, and \(x_\text{rec}), \text{LoG}(x_\text{tar}\) are the reconstructed inp and target images. If normalized it scales it by \(|| \text{LoG}(x_\text{tar}) ||_2\).

__init__(reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Inits HFENL2Loss.

Parameters:
  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

direct.functionals.nmae module#

class direct.functionals.nmae.NMAELoss(reduction='mean')[source]#

Bases: Module

Computes the Normalized Mean Absolute Error (NMAE), i.e.:

\[\frac{||u - v||_1}{||u||_1},\]

where \(u\) and \(v\) denote the target and the input.

__init__(reduction='mean')[source]#

Inits NMAELoss.

Parameters:

reduction – Specifies the reduction to apply to the output. Can be "none", "mean" or "sum". Note that "mean" or "sum" will yield the same output. Default: "mean".

forward(input, target)[source]#

Forward method of NMAELoss.

Parameters:
  • input (Tensor) – Tensor of shape (*), where * means any number of dimensions.

  • target (Tensor) – Tensor of same shape as the input.

Returns:

NMAE loss value.

direct.functionals.nmse module#

class direct.functionals.nmse.NMSELoss(reduction='mean')[source]#

Bases: Module

Computes the Normalized Mean Squared Error (NMSE), i.e.:

\[\frac{||u - v||_2^2}{||u||_2^2},\]

where \(u\) and \(v\) denote the target and the input.

__init__(reduction='mean')[source]#

Inits NMSELoss.

Parameters:

reduction – Specifies the reduction to apply to the output. Can be "none", "mean" or "sum". Note that "mean" or "sum" will yield the same output. Default: "mean".

forward(input, target)[source]#

Forward method of NMSELoss.

Parameters:
  • input (Tensor) – Tensor of shape (*), where * means any number of dimensions.

  • target (Tensor) – Tensor of same shape as the input.

Returns:

NMSE loss value.

class direct.functionals.nmse.NRMSELoss(reduction='mean')[source]#

Bases: Module

Computes the Normalized Root Mean Squared Error (NRMSE), i.e.:

\[\frac{||u - v||_2}{||u||_2},\]

where \(u\) and \(v\) denote the target and the input.

__init__(reduction='mean')[source]#

Inits NRMSELoss.

Parameters:

reduction – Specifies the reduction to apply to the output. Can be "none", "mean" or "sum". Note that "mean" or "sum" will yield the same output. Default: "mean".

forward(input, target)[source]#

Forward method of NRMSELoss.

Parameters:
  • input (Tensor) – Tensor of shape (*), where * means any number of dimensions.

  • target (Tensor) – Tensor of same shape as the input.

Returns:

NRMSE loss value.

direct.functionals.psnr module#

Peak signal-to-noise ratio (pSNR) metric for the direct package.

direct.functionals.psnr.batch_psnr(input_data, target_data, reduction='mean')[source]#

This function is a torch implementation of skimage.metrics.compare_psnr.

Parameters:
  • input_data (Tensor) – Input tensor.

  • target_data (Tensor) – Target tensor.

  • reduction (str) – Reduction method. Can be "mean", "sum" or "none". Default: "mean".

Return type:

Tensor

Returns:

PSNR value.

class direct.functionals.psnr.PSNRLoss(reduction='mean')[source]#

Bases: Module

Peak signal-to-noise ratio loss function PyTorch implementation.

Parameters:

reduction (str) – Batch reduction. Default: "mean".

__init__(reduction='mean')[source]#

Inits PSNRLoss.

Parameters:

reduction (str) – Batch reduction. Default: "mean".

forward(input_data, target_data)[source]#

Performs forward pass of PSNRLoss.

Parameters:
  • input_data (Tensor) – Input 2D data.

  • target_data (Tensor) – Target 2D data.

Return type:

Tensor

Returns:

PSNR loss value.

direct.functionals.snr module#

Signal-to-noise ratio (SNR) metric for the direct package.

direct.functionals.snr.snr_metric(input_data, target_data, reduction='mean')[source]#

This function is a torch implementation of SNR metric for batches.

\[SNR = 10 \cdot \log_{10}\left(\frac{\text{square_error}}{\text{square_error_noise}}\right)\]

where:

  • \(\text{square_error}\) is the sum of squared values of the clean (target) data.

  • \(\text{square_error_noise}\) is the sum of squared differences between the input data and the clean (target) data.

If reduction is "mean", the function returns the mean SNR value. If reduction is "sum", the function returns the sum of SNR values. If reduction is "none", the function returns a tensor of SNR values for each batch.

Parameters:
  • input_data (Tensor) – Input tensor.

  • target_data (Tensor) – Target tensor.

  • reduction (str) – Reduction method. Can be "mean", "sum" or "none". Default: "mean".

Return type:

Tensor

Returns:

SNR metric value.

class direct.functionals.snr.SNRLoss(reduction='mean')[source]#

Bases: Module

SNR loss function PyTorch implementation.

__init__(reduction='mean')[source]#

Inits SNRLoss.

Parameters:

reduction (str) – Batch reduction. Default: "mean".

forward(input_data, target_data)[source]#

Performs forward pass of SNRLoss.

Parameters:
  • input_data (Tensor) – Input 2D data.

  • target_data (Tensor) – Target 2D data.

Return type:

Tensor

Returns:

SNR loss value.

direct.functionals.ssim module#

This module contains SSIM loss functions for the direct package.

class direct.functionals.ssim.SSIMLoss(win_size=7, k1=0.01, k2=0.03)[source]#

Bases: Module

SSIM loss module as implemented in [1]_.

Parameters:
  • win_size – Window size for SSIM calculation. Default: 7.

  • k1 – k1 parameter for SSIM calculation. Default: 0.01.

  • k2 – k2 parameter for SSIM calculation. Default: 0.03.

References

__init__(win_size=7, k1=0.01, k2=0.03)[source]#

Inits SSIMLoss.

Parameters:
  • win_size – Window size for SSIM calculation. Default: 7.

  • k1 – k1 parameter for SSIM calculation. Default: 0.01.

  • k2 – k2 parameter for SSIM calculation. Default: 0.03.

forward(input_data, target_data, data_range)[source]#

Forward pass of SSIMLoss.

Parameters:
  • input_data (Tensor) – 2D Input data.

  • target_data (Tensor) – 2D Target data.

  • data_range (Tensor) – Data range.

Return type:

Tensor

Returns:

SSIM loss value.

class direct.functionals.ssim.SSIM3DLoss(win_size=7, k1=0.01, k2=0.03)[source]#

Bases: Module

SSIM loss module for 3D data.

Parameters:
  • win_size – Window size for SSIM calculation. Default: 7.

  • k1 – k1 parameter for SSIM calculation. Default: 0.01.

  • k2 – k2 parameter for SSIM calculation. Default: 0.03.

__init__(win_size=7, k1=0.01, k2=0.03)[source]#

Inits SSIM3DLoss.

Parameters:
  • win_size – Window size for SSIM calculation. Default: 7.

  • k1 – k1 parameter for SSIM calculation. Default: 0.01.

  • k2 – k2 parameter for SSIM calculation. Default: 0.03.

forward(input_data, target_data, data_range)[source]#

Forward pass of SSIM3DLoss.

Parameters:
  • input_data (Tensor) – 3D Input data.

  • target_data (Tensor) – 3D Target data.

  • data_range (Tensor) – Data range.

Return type:

Tensor

Returns:

SSIM 3D loss value.

Module contents#

direct.nn.functionals module.

This module contains functionals for the direct package as well as the loss functions needed for training models.

class direct.functionals.HFENL1Loss(reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Bases: HFENLoss

High Frequency Error Norm (HFEN) Loss using L1Loss criterion.

Calculates:

\[|| \text{LoG}(x_\text{rec}) - \text{LoG}(x_\text{tar}) ||_1\]

Where LoG is the Laplacian of Gaussian filter, and \(x_\text{rec}), \text{LoG}(x_\text{tar}\) are the reconstructed inp and target images. If normalized it scales it by \(|| \text{LoG}(x_\text{tar}) ||_1\).

__init__(reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Inits HFENL1Loss.

Parameters:
  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

class direct.functionals.HFENL2Loss(reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Bases: HFENLoss

High Frequency Error Norm (HFEN) Loss using L2Loss criterion.

Calculates:

\[|| \text{LoG}(x_\text{rec}) - \text{LoG}(x_\text{tar}) ||_2\]

Where LoG is the Laplacian of Gaussian filter, and \(x_\text{rec}), \text{LoG}(x_\text{tar}\) are the reconstructed inp and target images. If normalized it scales it by \(|| \text{LoG}(x_\text{tar}) ||_2\).

__init__(reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Inits HFENL2Loss.

Parameters:
  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

class direct.functionals.HFENLoss(criterion, reduction='mean', kernel_size=5, sigma=2.5, norm=False)[source]#

Bases: Module

High Frequency Error Norm (HFEN) Loss as defined in _[1].

Calculates:

\[|| \text{LoG}(x_\text{rec}) - \text{LoG}(x_\text{tar}) ||_C\]

Where C can be any norm, LoG is the Laplacian of Gaussian filter, and \(x_\text{rec}), \text{LoG}(x_\text{tar}\) are the reconstructed inp and target images. If normalized it scales it by \(|| \text{LoG}(x_\text{tar}) ||_C\).

Code was borrowed and adapted from _[2] (not licensed).

References

__init__(criterion, reduction='mean', kernel_size=5, sigma=2.5, norm=False)[source]#

Inits HFENLoss.

Parameters:
  • criterion (Module) – Loss function to calculate the difference between log1 and log2.

  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

forward(inp, target)[source]#

Forward pass of the HFENLoss.

Parameters:
  • inp (Tensor) – Input tensor.

  • target (Tensor) – Target tensor.

Return type:

Tensor

Returns:

HFEN loss value.

class direct.functionals.NMAELoss(reduction='mean')[source]#

Bases: Module

Computes the Normalized Mean Absolute Error (NMAE), i.e.:

\[\frac{||u - v||_1}{||u||_1},\]

where \(u\) and \(v\) denote the target and the input.

__init__(reduction='mean')[source]#

Inits NMAELoss.

Parameters:

reduction – Specifies the reduction to apply to the output. Can be "none", "mean" or "sum". Note that "mean" or "sum" will yield the same output. Default: "mean".

forward(input, target)[source]#

Forward method of NMAELoss.

Parameters:
  • input (Tensor) – Tensor of shape (*), where * means any number of dimensions.

  • target (Tensor) – Tensor of same shape as the input.

Returns:

NMAE loss value.

class direct.functionals.NMSELoss(reduction='mean')[source]#

Bases: Module

Computes the Normalized Mean Squared Error (NMSE), i.e.:

\[\frac{||u - v||_2^2}{||u||_2^2},\]

where \(u\) and \(v\) denote the target and the input.

__init__(reduction='mean')[source]#

Inits NMSELoss.

Parameters:

reduction – Specifies the reduction to apply to the output. Can be "none", "mean" or "sum". Note that "mean" or "sum" will yield the same output. Default: "mean".

forward(input, target)[source]#

Forward method of NMSELoss.

Parameters:
  • input (Tensor) – Tensor of shape (*), where * means any number of dimensions.

  • target (Tensor) – Tensor of same shape as the input.

Returns:

NMSE loss value.

class direct.functionals.NRMSELoss(reduction='mean')[source]#

Bases: Module

Computes the Normalized Root Mean Squared Error (NRMSE), i.e.:

\[\frac{||u - v||_2}{||u||_2},\]

where \(u\) and \(v\) denote the target and the input.

__init__(reduction='mean')[source]#

Inits NRMSELoss.

Parameters:

reduction – Specifies the reduction to apply to the output. Can be "none", "mean" or "sum". Note that "mean" or "sum" will yield the same output. Default: "mean".

forward(input, target)[source]#

Forward method of NRMSELoss.

Parameters:
  • input (Tensor) – Tensor of shape (*), where * means any number of dimensions.

  • target (Tensor) – Tensor of same shape as the input.

Returns:

NRMSE loss value.

class direct.functionals.PSNRLoss(reduction='mean')[source]#

Bases: Module

Peak signal-to-noise ratio loss function PyTorch implementation.

Parameters:

reduction (str) – Batch reduction. Default: "mean".

__init__(reduction='mean')[source]#

Inits PSNRLoss.

Parameters:

reduction (str) – Batch reduction. Default: "mean".

forward(input_data, target_data)[source]#

Performs forward pass of PSNRLoss.

Parameters:
  • input_data (Tensor) – Input 2D data.

  • target_data (Tensor) – Target 2D data.

Return type:

Tensor

Returns:

PSNR loss value.

class direct.functionals.SNRLoss(reduction='mean')[source]#

Bases: Module

SNR loss function PyTorch implementation.

__init__(reduction='mean')[source]#

Inits SNRLoss.

Parameters:

reduction (str) – Batch reduction. Default: "mean".

forward(input_data, target_data)[source]#

Performs forward pass of SNRLoss.

Parameters:
  • input_data (Tensor) – Input 2D data.

  • target_data (Tensor) – Target 2D data.

Return type:

Tensor

Returns:

SNR loss value.

class direct.functionals.SSIM3DLoss(win_size=7, k1=0.01, k2=0.03)[source]#

Bases: Module

SSIM loss module for 3D data.

Parameters:
  • win_size – Window size for SSIM calculation. Default: 7.

  • k1 – k1 parameter for SSIM calculation. Default: 0.01.

  • k2 – k2 parameter for SSIM calculation. Default: 0.03.

__init__(win_size=7, k1=0.01, k2=0.03)[source]#

Inits SSIM3DLoss.

Parameters:
  • win_size – Window size for SSIM calculation. Default: 7.

  • k1 – k1 parameter for SSIM calculation. Default: 0.01.

  • k2 – k2 parameter for SSIM calculation. Default: 0.03.

forward(input_data, target_data, data_range)[source]#

Forward pass of SSIM3DLoss.

Parameters:
  • input_data (Tensor) – 3D Input data.

  • target_data (Tensor) – 3D Target data.

  • data_range (Tensor) – Data range.

Return type:

Tensor

Returns:

SSIM 3D loss value.

class direct.functionals.SSIMLoss(win_size=7, k1=0.01, k2=0.03)[source]#

Bases: Module

SSIM loss module as implemented in [1]_.

Parameters:
  • win_size – Window size for SSIM calculation. Default: 7.

  • k1 – k1 parameter for SSIM calculation. Default: 0.01.

  • k2 – k2 parameter for SSIM calculation. Default: 0.03.

References

__init__(win_size=7, k1=0.01, k2=0.03)[source]#

Inits SSIMLoss.

Parameters:
  • win_size – Window size for SSIM calculation. Default: 7.

  • k1 – k1 parameter for SSIM calculation. Default: 0.01.

  • k2 – k2 parameter for SSIM calculation. Default: 0.03.

forward(input_data, target_data, data_range)[source]#

Forward pass of SSIMLoss.

Parameters:
  • input_data (Tensor) – 2D Input data.

  • target_data (Tensor) – 2D Target data.

  • data_range (Tensor) – Data range.

Return type:

Tensor

Returns:

SSIM loss value.

class direct.functionals.SobelGradL1Loss(reduction='mean', normalized_grad=True)[source]#

Bases: SobelGradLoss

Computes the sum of the l1-loss between the gradient of input and target:

It returns

\[||u_x - v_x ||_1 + ||u_y - v_y||_1\]

where \(u\) and \(v\) denote the input and target images. The gradients w.r.t. to \(x\) and \(y\) directions are computed using the Sobel operators.

__init__(reduction='mean', normalized_grad=True)[source]#

Inits SobelGradL1Loss.

Parameters:
  • reduction (str) – Loss reduction. Can be "mean" or "sum". Default: "mean".

  • normalized_grad (bool) – Whether the computed gradients are normalized. Default: True.

class direct.functionals.SobelGradL2Loss(reduction='mean', normalized_grad=True)[source]#

Bases: SobelGradLoss

Computes the sum of the l2-loss between the gradient of input and target:

It returns

\[||u_x - v_x ||_2^2 + ||u_y - v_y||_2^2\]

where \(u\) and \(v\) denote the input and target images. The gradients w.r.t. to \(x\) and \(y\) directions are computed using the Sobel operators.

__init__(reduction='mean', normalized_grad=True)[source]#

Inits SobelGradL2Loss.

Parameters:
  • reduction (str) – Loss reduction. Can be "mean" or "sum". Default: "mean".

  • normalized_grad (bool) – Whether the computed gradients are normalized. Default: True.

direct.functionals.batch_psnr(input_data, target_data, reduction='mean')[source]#

This function is a torch implementation of skimage.metrics.compare_psnr.

Parameters:
  • input_data (Tensor) – Input tensor.

  • target_data (Tensor) – Target tensor.

  • reduction (str) – Reduction method. Can be "mean", "sum" or "none". Default: "mean".

Return type:

Tensor

Returns:

PSNR value.

direct.functionals.calgary_campinas_psnr(gt, pred)[source]#
direct.functionals.calgary_campinas_ssim(gt, pred)[source]#
direct.functionals.calgary_campinas_vif(gt, pred)[source]#
direct.functionals.fastmri_nmse(gt, pred)[source]#

Compute Normalized Mean Square Error metric (NMSE) compatible with the FastMRI challenge.

Parameters:
  • gt – Ground truth tensor.

  • pred – Prediction tensor.

Returns:

NMSE value.

direct.functionals.fastmri_psnr(gt, pred)[source]#

Compute Peak Signal to Noise Ratio metric (PSNR) compatible with the FastMRI challenge.

Parameters:
  • gt – Ground truth tensor.

  • pred – Prediction tensor.

Returns:

PSNR value.

direct.functionals.fastmri_ssim(gt, target)[source]#

Compute Structural Similarity Index Measure (SSIM) compatible with the FastMRI challenge.

Parameters:
  • gt – Ground truth tensor.

  • target – Target/prediction tensor.

Returns:

SSIM value.

direct.functionals.hfen_l1(inp, target, reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Calculates HFENL1 loss between inp and target.

Parameters:
  • inp (Tensor) – Input tensor.

  • target (Tensor) – Target tensor.

  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

Return type:

Tensor

Returns:

HFENL1 loss value.

direct.functionals.hfen_l2(inp, target, reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#

Calculates HFENL2 loss between inp and target.

Parameters:
  • inp (Tensor) – Input tensor.

  • target (Tensor) – Target tensor.

  • reduction (str) – Criterion reduction. Default: "mean".

  • kernel_size (int | list[int]) – Size of the LoG filter kernel. Default: 15.

  • sigma (float | list[float]) – Standard deviation of the LoG filter kernel. Default: 2.5.

  • norm (bool) – Whether to normalize the loss. Default: False.

Return type:

Tensor

Returns:

HFENL2 loss value.

direct.functionals.snr_metric(input_data, target_data, reduction='mean')[source]#

This function is a torch implementation of SNR metric for batches.

\[SNR = 10 \cdot \log_{10}\left(\frac{\text{square_error}}{\text{square_error_noise}}\right)\]

where:

  • \(\text{square_error}\) is the sum of squared values of the clean (target) data.

  • \(\text{square_error_noise}\) is the sum of squared differences between the input data and the clean (target) data.

If reduction is "mean", the function returns the mean SNR value. If reduction is "sum", the function returns the sum of SNR values. If reduction is "none", the function returns a tensor of SNR values for each batch.

Parameters:
  • input_data (Tensor) – Input tensor.

  • target_data (Tensor) – Target tensor.

  • reduction (str) – Reduction method. Can be "mean", "sum" or "none". Default: "mean".

Return type:

Tensor

Returns:

SNR metric value.