CombinedLoss computes a weighted combination of SSIM, MSE, and MAE losses.
This class allows for the combination of three different loss functions: Structural Similarity Index (SSIM), Mean Squared Error (MSE), and Mean Absolute Error (MAE). The weights for MSE and MAE can be adjusted, and the weight for SSIM is automatically calculated as the remaining weight.
CombinedLoss reference paper: Shah, Z. H., Müller, M., Hammer, B., Huser, T., & Schenck, W. (2022, July). Impact of different loss functions on denoising of microscopic images. In 2022 International Joint Conference on Neural Networks (IJCNN) (pp. 1-10). IEEE.
Type
Default
Details
spatial_dims
int
2
Number of spatial dimensions (2 for 2D images, 3 for 3D images)
Multi-Scale Structural Similarity (MSSSIM) with Gaussian-weighted L1 Loss.
Reference paper: Zhao, H., Gallo, O., Frosio, I., & Kautz, J. (2016). Loss functions for image restoration with neural networks. IEEE Transactions on computational imaging, 3(1), 47-57.
Type
Default
Details
spatial_dims
int
2
Number of spatial dimensions.
alpha
float
0.025
Weighting factor between MS-SSIM and L1 loss.
window_size
int
8
Size of the Gaussian filter for SSIM.
sigma
float
1.5
Standard deviation of the Gaussian filter.
reduction
str
mean
Specifies the reduction to apply to the output (‘mean’, ‘sum’, or ‘none’).
levels
int
3
Number of scales to use for MS-SSIM.
weights
NoneType
None
Weights to apply to each scale. If None, default values are used.
Multi-Scale Structural Similarity (MSSSIM) with Gaussian-weighted L2 Loss.
Reference paper: Zhao, H., Gallo, O., Frosio, I., & Kautz, J. (2016). Loss functions for image restoration with neural networks. IEEE Transactions on computational imaging, 3(1), 47-57.
Type
Default
Details
spatial_dims
int
2
Number of spatial dimensions.
alpha
float
0.1
Weighting factor between MS-SSIM and L2 loss.
window_size
int
11
Size of the Gaussian window for SSIM.
sigma
float
1.5
Standard deviation of the Gaussian.
reduction
str
mean
Specifies the reduction to apply to the output (‘mean’, ‘sum’, or ‘none’).
levels
int
3
Number of scales to use for MS-SSIM.
weights
NoneType
None
Weights to apply to each scale. If None, default values are used.
msssim_l2_loss = MSSSIML2Loss()output = torch.rand(10, 3, 64, 64).cuda() # Example output with even dimensionstarget = torch.rand(10, 3, 64, 64).cuda() # Example target with even dimensionsloss = msssim_l2_loss(output, target)print(loss)
Same as nn.CrossEntropyLoss, but flattens input and target for 3D inputs.
DiceLoss
DiceLoss (smooth=1)
DiceLoss computes the Sørensen–Dice coefficient loss, which is often used for evaluating the performance of image segmentation algorithms.
The Dice coefficient is a measure of overlap between two samples. It ranges from 0 (no overlap) to 1 (perfect overlap). The Dice loss is computed as 1 - Dice coefficient, so it ranges from 1 (no overlap) to 0 (perfect overlap).
Attributes: smooth (float): A smoothing factor to avoid division by zero and ensure numerical stability.
Methods: forward(inputs, targets): Computes the Dice loss between the predicted probabilities (inputs) and the ground truth (targets).
Type
Default
Details
smooth
int
1
Smoothing factor to avoid division by zero
# inputs and targets must be equally dimensional tensorsfrom torch import randn, randint