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.grad module#
- class direct.functionals.grad.SobelGradL1Loss(reduction='mean', normalized_grad=True)[source]#
Bases:
SobelGradLossComputes 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:
SobelGradLossComputes 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:
ModuleHigh 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.
- class direct.functionals.hfen.HFENL1Loss(reduction='mean', kernel_size=15, sigma=2.5, norm=False)[source]#
Bases:
HFENLossHigh 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:
HFENLossHigh 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:
ModuleComputes the Normalized Mean Absolute Error (NMAE), i.e.:
\[\frac{||u - v||_1}{||u||_1},\]where \(u\) and \(v\) denote the target and the input.
direct.functionals.nmse module#
- class direct.functionals.nmse.NMSELoss(reduction='mean')[source]#
Bases:
ModuleComputes 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.
- class direct.functionals.nmse.NRMSELoss(reduction='mean')[source]#
Bases:
ModuleComputes 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.
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:
ModulePeak signal-to-noise ratio loss function PyTorch implementation.
- Parameters:
reduction (
str) – Batch reduction. Default:"mean".
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
reductionis"mean", the function returns the mean SNR value. Ifreductionis"sum", the function returns the sum of SNR values. Ifreductionis"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.
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:
ModuleSSIM 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
[1]
- class direct.functionals.ssim.SSIM3DLoss(win_size=7, k1=0.01, k2=0.03)[source]#
Bases:
ModuleSSIM 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:
HFENLossHigh 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:
HFENLossHigh 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:
ModuleHigh 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
[1] S. Ravishankar and Y. Bresler, “MR Image Reconstruction From Highly Undersampled k-Space Data by Dictionary Learning,” in IEEE Transactions on Medical Imaging, vol. 30, no. 5, pp. 1028-1041, May 2011, doi: 10.1109/TMI.2010.2090538.
[2] - __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.
- class direct.functionals.NMAELoss(reduction='mean')[source]#
Bases:
ModuleComputes the Normalized Mean Absolute Error (NMAE), i.e.:
\[\frac{||u - v||_1}{||u||_1},\]where \(u\) and \(v\) denote the target and the input.
- class direct.functionals.NMSELoss(reduction='mean')[source]#
Bases:
ModuleComputes 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.
- class direct.functionals.NRMSELoss(reduction='mean')[source]#
Bases:
ModuleComputes 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.
- class direct.functionals.PSNRLoss(reduction='mean')[source]#
Bases:
ModulePeak signal-to-noise ratio loss function PyTorch implementation.
- Parameters:
reduction (
str) – Batch reduction. Default:"mean".
- class direct.functionals.SNRLoss(reduction='mean')[source]#
Bases:
ModuleSNR loss function PyTorch implementation.
- class direct.functionals.SSIM3DLoss(win_size=7, k1=0.01, k2=0.03)[source]#
Bases:
ModuleSSIM 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:
ModuleSSIM 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
[1]
- class direct.functionals.SobelGradL1Loss(reduction='mean', normalized_grad=True)[source]#
Bases:
SobelGradLossComputes 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:
SobelGradLossComputes 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.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
reductionis"mean", the function returns the mean SNR value. Ifreductionis"sum", the function returns the sum of SNR values. Ifreductionis"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.