ImageSegmentationMetrics Module¶
Provides metrics for evaluating image segmentation results and algorithms.
- HMB.ImageSegmentationMetrics.ComputeIoU(preds, targets, smooth=1.0, iouType='binary', weight=None)[source]¶
Compute the Intersection over Union (IoU) metric.
\[IoU = \frac{|Prediction \cap Ground\ Truth| + smooth}{|Prediction \cup Ground\ Truth| + smooth}\]- where:
\(|Prediction \cap Ground\ Truth|\) is the intersection of the predicted and ground truth tensors.
\(|Prediction \cup Ground\ Truth|\) is the union of the predicted and ground truth tensors.
\(smooth\) is a small constant to avoid division by zero.
Note
- The iouType parameter determines how the IoU is computed:
binary: Threshold predictions at 0.5 to obtain binary masks.
soft: Use raw predictions for soft IoU.
weighted: Use class weights for weighted IoU (requires weight parameter).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
smooth (float, optional) – Smoothing factor to avoid division by zero.
iouType (str, optional) – Type of IoU to compute (“binary”, “soft”, or “weighted”).
weight (numpy.ndarray, optional) – Class weights for weighted IoU. Required if iouType is “weighted”.
- Returns:
IoU value.
- Return type:
- Raises:
ValueError – If iouType is not one of “binary”, “soft”, or “weighted”.
ValueError – If weight is not provided when iouType is “weighted”.
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) iou = ism.ComputeIoU(preds, targets, iouType="binary") print(f"IoU: {iou}") iouSoft = ism.ComputeIoU(preds, targets, iouType="soft") print(f"Soft IoU: {iouSoft}") weight = np.array([0.7, 0.3]) # Example weights for two classes. iouWeighted = ism.ComputeIoU(preds, targets, iouType="weighted", weight=weight) print(f"Weighted IoU: {iouWeighted}")
- HMB.ImageSegmentationMetrics.ComputeDice(preds, targets, smooth=1.0)[source]¶
Compute the Dice coefficient.
\[Dice = \frac{2 \times |Prediction \cap Ground\ Truth| + smooth}{|Prediction| + |Ground\ Truth| + smooth}\]- where:
\(|Prediction \cap Ground\ Truth|\) is the intersection of the predicted and ground truth tensors.
\(|Prediction|\) is the sum of the predicted tensor.
\(|Ground\ Truth|\) is the sum of the ground truth tensor.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
smooth (float, optional) – Smoothing factor to avoid division by zero. Default is 1.0.
- Returns:
Dice coefficient value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) dice = ism.ComputeDice(preds, targets) print(f"Dice: {dice}")
- HMB.ImageSegmentationMetrics.ComputePixelAccuracy(preds, targets)[source]¶
Compute the pixel accuracy metric.
\[Pixel\ Accuracy = \frac{Number\ of\ Correct\ Pixels}{Total\ Number\ of\ Pixels}\]- where:
Number of Correct Pixels is the sum of pixels where predictions match targets.
Total Number of Pixels is the product of the dimensions of the predicted tensor.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
Pixel accuracy value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) acc = ism.ComputePixelAccuracy(preds, targets) print(f"Pixel Accuracy: {acc}")
- HMB.ImageSegmentationMetrics.ComputePrecision(preds, targets)[source]¶
Compute the precision metric.
\[Precision = \frac{TP}{TP + FP}\]- where:
\(TP\) is the number of true positives (predicted positive and actually positive).
\(FP\) is the number of false positives (predicted positive but actually negative).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
Precision value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) precision = ism.ComputePrecision(preds, targets) print(f"Precision: {precision}")
- HMB.ImageSegmentationMetrics.ComputeRecall(preds, targets)[source]¶
Compute the recall metric.
\[Recall = \frac{TP}{TP + FN}\]- where:
\(TP\) is the number of true positives (predicted positive and actually positive).
\(FN\) is the number of false negatives (predicted negative but actually positive).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
Recall value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) recall = ism.ComputeRecall(preds, targets) print(f"Recall: {recall}")
- HMB.ImageSegmentationMetrics.ComputeSpecificity(preds, targets)[source]¶
Compute the specificity metric.
\[Specificity = \frac{TN}{TN + FP}\]- where:
\(TN\) is the number of true negatives (predicted negative and actually negative).
\(FP\) is the number of false positives (predicted positive but actually negative).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
Specificity value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) specificity = ism.ComputeSpecificity(preds, targets) print(f"Specificity: {specificity}")
- HMB.ImageSegmentationMetrics.ComputeFPR(preds, targets)[source]¶
Compute the false positive rate (FPR).
\[FPR = \frac{FP}{FP + TN}\]- where:
\(FP\) is the number of false positives (predicted positive but actually negative).
\(TN\) is the number of true negatives (predicted negative and actually negative).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
FPR value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) fpr = ism.ComputeFPR(preds, targets) print(f"FPR: {fpr}")
- HMB.ImageSegmentationMetrics.ComputeFNR(preds, targets)[source]¶
Compute the false negative rate (FNR).
\[FNR = \frac{FN}{FN + TP}\]- where:
\(FN\) is the number of false negatives (predicted negative but actually positive).
\(TP\) is the number of true positives (predicted positive and actually positive).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
FNR value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) fnr = ism.ComputeFNR(preds, targets) print(f"FNR: {fnr}")
- HMB.ImageSegmentationMetrics.ComputeF1Score(preds, targets)[source]¶
Compute the F1 score.
\[F1 = \frac{2 \times Precision \times Recall}{Precision + Recall}\]- where:
Precision is the ratio of true positives to the sum of true positives and false positives.
Recall is the ratio of true positives to the sum of true positives and false negatives.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
F1 score value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) f1 = ism.ComputeF1Score(preds, targets) print(f"F1 Score: {f1}")
- HMB.ImageSegmentationMetrics.ComputeMeanAveragePrecision(preds, targets)[source]¶
Compute the mean average precision (mAP) for binary masks.
\[mAP = \frac{1}{N} \times \sum_{i=1}^{N} Precision_i\]- where:
\(Precision_i\) is the precision for the i-th image in the batch.
\(N\) is the total number of images in the batch.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
mAP value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(2, 1, 256, 256) targets = np.random.randint(0, 2, size=(2, 1, 256, 256)) mapScore = ism.ComputeMeanAveragePrecision(preds, targets) print(f"mAP: {mapScore}")
- HMB.ImageSegmentationMetrics.ComputeHausdorffDistance(preds, targets)[source]¶
Compute the Hausdorff distance between predicted and ground truth masks.
\[H(A, B) = \max\{\sup_{a \in A} \inf_{b \in B} d(a, b), \sup_{b \in B} \inf_{a \in A} d(a, b)\}\]- where:
\(A\) is the set of points in the predicted mask.
\(B\) is the set of points in the ground truth mask.
\(d(a, b)\) is the Euclidean distance between points a and b.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
Hausdorff distance value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) hd = ism.ComputeHausdorffDistance(preds, targets) print(f"Hausdorff Distance: {hd}")
- HMB.ImageSegmentationMetrics.ComputeBoundaryF1Score(preds, targets, dilationRatio=0.02, eps=1e-07)[source]¶
Compute the Boundary F1 Score (BF Score).
\[BF = \frac{2 \times Precision_{boundary} \times Recall_{boundary}}{Precision_{boundary} + Recall_{boundary}}\]- where:
\(Precision_{boundary}\) is the precision of the predicted boundary.
\(Recall_{boundary}\) is the recall of the predicted boundary.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
dilationRatio (float, optional) – Ratio to determine the dilation size based on image dimensions. Default is 0.02.
eps (float, optional) – Small constant to avoid division by zero. Default is 1e-7.
- Returns:
Boundary F1 Score value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) bfScore = ism.ComputeBoundaryF1Score(preds, targets) print(f"Boundary F1 Score: {bfScore}")
- HMB.ImageSegmentationMetrics.ComputeMatthewsCorrelationCoefficient(preds, targets)[source]¶
Compute the Matthews Correlation Coefficient (MCC).
\[MCC = \frac{TP \times TN - FP \times FN}{\sqrt{(TP + FP) \times (TP + FN) \times (TN + FP) \times (TN + FN)}}\]- where
\(TP\) is the number of true positives (predicted positive and actually positive).
\(TN\) is the number of true negatives (predicted negative and actually negative).
\(FP\) is the number of false positives (predicted positive but actually negative).
\(FN\) is the number of false negatives (predicted negative but actually positive).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
MCC value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) mcc = ism.ComputeMatthewsCorrelationCoefficient(preds, targets) print(f"Matthews Correlation Coefficient: {mcc}")
- HMB.ImageSegmentationMetrics.ComputeCohensKappa(preds, targets)[source]¶
Compute Cohen’s Kappa metric.
\[\kappa = \frac{p_o - p_e}{1 - p_e}\]- where:
\(p_o\) is the observed agreement between predictions and targets.
\(p_e\) is the expected agreement by chance.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
Cohen’s Kappa value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) kappa = ism.ComputeCohensKappa(preds, targets) print(f"Cohen's Kappa: {kappa}")
- HMB.ImageSegmentationMetrics.ComputeBalancedAccuracy(preds, targets)[source]¶
Compute the balanced accuracy metric.
\[Balanced\ Accuracy = \frac{Recall + Specificity}{2}\]- where:
\(Recall\) is the true positive rate.
\(Specificity\) is the true negative rate.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
Balanced accuracy value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) balancedAcc = ism.ComputeBalancedAccuracy(preds, targets) print(f"Balanced Accuracy: {balancedAcc}")
- HMB.ImageSegmentationMetrics.ComputeMeanSurfaceDistance(preds, targets)[source]¶
Compute the Mean Surface Distance (MSD) between predicted and ground truth masks.
\[MSD = \frac{1}{|S_P|} \times \sum_{p \in S_P} \min_{q \in S_T} d(p, q)\]- where:
\(S_P\) is the set of points on the predicted mask boundary.
\(S_T\) is the set of points on the ground truth mask boundary
\(d(p, q)\) is the Euclidean distance between points p and q.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
MSD value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) msd = ism.ComputeMeanSurfaceDistance(preds, targets) print(f"Mean Surface Distance: {msd}")
- HMB.ImageSegmentationMetrics.ComputeAverageSymmetricSurfaceDistance(preds, targets)[source]¶
Compute the Average Symmetric Surface Distance (ASSD) between predicted and ground truth masks.
\[ASSD = \frac{MSD(P, T) + MSD(T, P)}{2}\]- where:
\(MSD(P, T)\) is the Mean Surface Distance from prediction to target.
\(MSD(T, P)\) is the Mean Surface Distance from target to prediction.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
ASSD value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) assd = ism.ComputeAverageSymmetricSurfaceDistance(preds, targets) print(f"Average Symmetric Surface Distance: {assd}")
- HMB.ImageSegmentationMetrics.ComputeVolumetricOverlapError(preds, targets, smooth=1.0)[source]¶
Compute the Volumetric Overlap Error (VOE).
\[VOE = 1 - IoU\]- where:
\(IoU\) is the Intersection over Union value.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
smooth (float, optional) – Smoothing factor to avoid division by zero. Default is 1.0.
- Returns:
VOE value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) voe = ism.ComputeVolumetricOverlapError(preds, targets) print(f"Volumetric Overlap Error: {voe}")
- HMB.ImageSegmentationMetrics.ComputeGlobalConsistencyError(preds, targets)[source]¶
Compute the Global Consistency Error (GCE).
\[GCE = \frac{1}{N} \times \sum_{i=1}^{N} \min(E(S_1, S_2, p_i), E(S_2, S_1, p_i))\]- where:
\(E(S_1, S_2, p_i)\) is the error for pixel p_i in the predicted mask compared to the ground truth mask.
\(N\) is the total number of pixels in the mask.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
GCE value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) gce = ism.ComputeGlobalConsistencyError(preds, targets) print(f"Global Consistency Error: {gce}")
- HMB.ImageSegmentationMetrics.ComputeTversky(preds, targets, alpha=0.5)[source]¶
Compute the Tversky index metric.
\[Tversky = \frac{TP}{TP + \alpha \times FP + (1-\alpha) \times FN}\]- where:
\(TP\) is the number of true positives (predicted positive and actually positive).
\(FP\) is the number of false positives (predicted positive but actually negative).
\(FN\) is the number of false negatives (predicted negative but actually positive).
\(\alpha\) is the weight for false positives (default 0.5).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
alpha (float, optional) – Weight for false positives. Default is 0.5.
- Returns:
Tversky index value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) tversky = ism.ComputeTversky(preds, targets, alpha=0.7) print(f"Tversky Index: {tversky}")
- HMB.ImageSegmentationMetrics.ComputeFocalTverskyLoss(preds, targets, alpha=0.5, gamma=1.33333)[source]¶
Compute the Focal Tversky index metric.
\[FocalTversky = (1 - Tversky)^{1/\gamma}\]- where:
\(Tversky\) is the Tversky index (see ComputeTversky).
\(\gamma\) is the focusing parameter (default 4/3).
\(\alpha\) is the weight for false positives (default 0.5).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
alpha (float, optional) – Weight for false positives. Default is 0.5.
gamma (float, optional) – Focusing parameter. Default is 4/3.
- Returns:
Focal Tversky index value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) focalTversky = ism.ComputeFocalTverskyLoss(preds, targets, alpha=0.7, gamma=1.33) print(f"Focal Tversky Index: {focalTversky}")
- HMB.ImageSegmentationMetrics.ComputeFocalLoss(preds, targets, beta=0.5, gamma=2.0, eps=1e-07)[source]¶
Compute the Focal Loss for binary segmentation.
\[FL = -\beta \times (1 - p)^{\gamma} \times y \times \log(p) - (1 - \beta) \times p^{\gamma} \times (1 - y) \times \log(1 - p)\]- where:
\(p\) is the predicted probability.
\(y\) is the ground truth label.
\(\beta\) balances positive/negative examples.
\(\gamma\) focuses on hard examples.
\(\epsilon\) is a small constant to avoid log(0).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
beta (float, optional) – Balancing factor for positive/negative examples. Default is 0.5.
gamma (float, optional) – Focusing parameter. Default is 2.0.
eps (float, optional) – Small constant to avoid log(0). Default is 1e-7.
- Returns:
Focal loss value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) loss = ism.ComputeFocalLoss(preds, targets) print(f"Focal Loss: {loss}")
- HMB.ImageSegmentationMetrics.ComputeComboLoss(preds, targets, alpha=0.5, beta=0.5, smooth=1.0, eps=1e-07)[source]¶
Compute the Combo Loss, combining weighted cross-entropy and Dice loss.
\[ComboLoss = \beta \times CE + (1 - \beta) \times -\log(Dice)\]- where:
\(CE\) is the weighted cross-entropy.
\(Dice\) is the Dice coefficient.
\(\alpha\) weights positive/negative classes in CE.
\(\beta\) balances CE and Dice.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
alpha (float, optional) – Weight for positive class in CE. Default is 0.5.
beta (float, optional) – Balance between CE and Dice. Default is 0.5.
smooth (float, optional) – Smoothing factor for Dice. Default is 1.0.
eps (float, optional) – Small constant to avoid log(0). Default is 1e-7.
- Returns:
Combo loss value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) loss = ism.ComputeComboLoss(preds, targets) print(f"Combo Loss: {loss}")
- HMB.ImageSegmentationMetrics.ComputeTanimotoLoss(preds, targets)[source]¶
Compute the Tanimoto Loss for binary segmentation.
\[Tanimoto = 1 - \frac{\sum p t}{\sum p^2 + \sum t^2 - \sum p t}\]- where:
\(p\) is the predicted mask.
\(t\) is the ground truth mask.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
Tanimoto loss value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) loss = ism.ComputeTanimotoLoss(preds, targets) print(f"Tanimoto Loss: {loss}")
- HMB.ImageSegmentationMetrics.ComputeMSELoss(preds, targets)[source]¶
Compute the Mean Squared Error (MSE) loss.
\[MSE = \frac{1}{N} \times \sum_{i=1}^N (p_i - t_i)^2\]- where:
\(p_i\) is the predicted value.
\(t_i\) is the ground truth value.
\(N\) is the number of elements.
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
MSE loss value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.rand(1, 1, 256, 256) loss = ism.ComputeMSELoss(preds, targets) print(f"MSE Loss: {loss}")
- HMB.ImageSegmentationMetrics.ComputeBCELoss(preds, targets, smooth=1e-07)[source]¶
Compute the Binary Cross-Entropy (BCE) loss.
\[BCE = -\frac{1}{N} \times \sum_{i=1}^N [t_i \times \log(p_i) + (1 - t_i) \times \log(1 - p_i)]\]- where:
\(p_i\) is the predicted probability.
\(t_i\) is the ground truth label.
\(N\) is the number of elements.
\(smooth\) is a small constant to avoid log(0).
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
smooth (float, optional) – Small constant to avoid log(0). Default is 1e-7.
- Returns:
BCE loss value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) loss = ism.ComputeBCELoss(preds, targets) print(f"BCE Loss: {loss}")
- HMB.ImageSegmentationMetrics.ComputeHMBLoss(preds, targets)[source]¶
Compute the HMB Loss, a weighted combination of multiple loss functions for segmentation.
The HMB Loss (H-Loss), as suggested by Hossam (the author), introduces a weighted sum of various loss functions: Dice, IoU, MSE, BCE, Tversky, and Tanimoto losses [1]. This idea is presented in a research article that can be accessed from: https://doi.org/10.1109/ACCESS.2024.3483661
- The HMB Loss combines:
MSE Loss (distance-based)
Dice Loss (region-based)
IoU Loss (region-based)
Tversky Loss (region-based)
BCE Loss (distribution-based)
Tanimoto Loss (distribution-based)
- Parameters:
preds (numpy.ndarray) – Predicted tensor (logits).
targets (numpy.ndarray) – Ground truth tensor (binary mask).
- Returns:
Weighted average loss value.
- Return type:
Examples
import numpy as np import HMB.ImageSegmentationMetrics as ism preds = np.random.rand(1, 1, 256, 256) targets = np.random.randint(0, 2, size=(1, 1, 256, 256)) loss = ism.ComputeHMBLoss(preds, targets) print(f"HMB Loss: {loss}")
References