Documentation Index
Fetch the complete documentation index at: https://mintlify.com/tommyngx/MammoMix/llms.txt
Use this file to discover all available pages before exploring further.
mocae.py implements the Mixture of Calibrated Experts (MoCaE) ensemble strategy for mammography detection. Three dataset-specific YOLOS models each produce predictions; scores are calibrated by a RandomForestRegressor, then merged via Soft NMS and Score Voting.
soft_nms
Soft Non-Maximum Suppression — iteratively selects the highest-scoring box and decays the scores of nearby overlapping boxes instead of removing them outright. Produces a sparser set of boxes than greedy NMS while retaining weak detections.
Parameters
Detection boxes in
(x1, y1, x2, y2) format. Shape (N, 4).Confidence scores for each box. Shape
(N,).Gaussian decay parameter σ. Controls how sharply scores drop as IoU increases. Smaller values produce stronger suppression. Only used when
method="gaussian".IoU threshold for the linear decay method. Boxes with IoU above this value are linearly down-weighted. Only used when
method="linear".Boxes whose decayed score falls at or below this threshold are removed from consideration in subsequent iterations.
Decay strategy.
"gaussian" applies exp(-(iou² / σ)); "linear" applies 1 - iou for boxes above iou_nms.Returns
Surviving boxes after suppression. Shape
(M, 4) where M ≤ N.Decayed confidence scores corresponding to
keep_boxes. Shape (M,).score_voting
Refines bounding-box coordinates and scores using neighbourhood agreement. Each box is updated as a weighted average of all other boxes, where weights are a product of calibrated confidence and IoU-derived similarity. Self-influence is excluded.
Parameters
Boxes in
(x1, y1, x2, y2) format. Shape (N, 4). Returns the inputs unchanged if N == 0.Calibrated confidence scores. Shape
(N,).Score Voting sigma parameter. Controls how steeply the IoU-based weight falls off. Smaller values mean only very high-IoU neighbours contribute significantly.
Returns
Weighted-average box coordinates. Shape
(N, 4). Division by zero is avoided by adding 1e-8 to the denominator.Neighbourhood-agreement scores computed as a weighted average of neighbour confidence values. Shape
(N,).combine_predictions
Runs the full MoCaE ensemble pipeline on the test split of a single dataset. For each batch, all expert models produce scored predictions; scores are calibrated; predictions are pooled across experts then refined with Soft NMS and Score Voting; and cumulative mAP is tracked with MeanAveragePrecision.
Parameters
Mapping from dataset name (e.g.
"CSAW") to its corresponding AutoImageProcessor. The processor for dataset_name is used when constructing the test dataset; all processors are iterated when decoding each expert’s predictions.List of expert models in the same order as
calibrators. Each model is called with pixel_values and must return an output with logits and pred_boxes.List of fitted
sklearn.ensemble.RandomForestRegressor calibrators, one per model. Each calibrator predicts calibrated IoU-scores from a feature vector of [image_embedding (512-d), confidence (1-d)].Name of the dataset to evaluate on (e.g.
"CSAW", "DMID", or "DDSM"). Used to load the test split from splits_dir.Root directory containing per-dataset split
.txt files. Forwarded to BreastCancerDataset.Number of images per inference batch.
Gaussian sigma passed to
soft_nms. Also reused as sigma_sv in the subsequent score_voting call.IoU threshold passed to
soft_nms for the linear decay method.Minimum score threshold for
soft_nms.NMS decay method passed to
soft_nms. "gaussian" or "linear".Returns
Output of
torchmetrics.detection.MeanAveragePrecision.compute(). Contains keys such as "map", "map_50", "map_75", "map_small", "map_medium", "map_large".build_calibrate_dataset
Populates three mutable lists — image_embeddings, confidences, and ious — by running a single expert model over a dataset split. Image features are extracted with a frozen ResNet-18 backbone. IoU values are computed against ground-truth boxes for in-domain data and set to 0 for out-of-domain data.
This function mutates the lists passed in. Call it in a loop over all datasets to build a combined calibration corpus with
combine_datasets.Parameters
Expert configuration dict with keys
"model" (the expert nn.Module), "image_processor" (AutoImageProcessor), and "dataset_name" (string). The "dataset_name" key is compared with dataset_name to decide whether IoU labels are real (in-domain) or zeroed (out-of-domain).Mutable list to append 512-dimensional ResNet-18 feature vectors (as NumPy arrays) to, one per predicted cancer box.
Mutable list to append scalar confidence scores (
float) to, one per predicted cancer box.Mutable list to append IoU labels (
float) to. For in-domain predictions, values are the max IoU against ground-truth boxes; for out-of-domain predictions, values are 0.Name of the dataset split to load (e.g.
"CSAW"). Used to construct a BreastCancerDataset.Batch size for the internal
DataLoader.Dataset split to iterate over. Typically
"train" or "val".combine_datasets
Aggregates calibration data across all datasets for a single expert by calling build_calibrate_dataset once per dataset name, then concatenating the embeddings with the confidence scores to form the final feature matrix.
Parameters
Expert configuration dict forwarded to
build_calibrate_dataset. See the config parameter of that function for required keys.Ordered list of dataset names to iterate over (e.g.
["CSAW", "DMID", "DDSM"]).Dataset split to use when collecting calibration data.
Returns
Feature matrix of shape
(num_samples, 513) where the first 512 columns are ResNet-18 image embeddings and the last column is the raw confidence score. Used as X when fitting a RandomForestRegressor.Target IoU values of length
num_samples. Used as y when fitting the calibrator.