Skip to content

Feature/classic mil - #34

Merged
ejdam87 merged 274 commits into
masterfrom
feature/classic-mil
Aug 4, 2026
Merged

Feature/classic mil#34
ejdam87 merged 274 commits into
masterfrom
feature/classic-mil

Conversation

@ejdam87

@ejdam87 ejdam87 commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

This PR adds MIL with SL supervision

Summary by CodeRabbit

  • New Features

    • Added classic multiple-instance learning workflows for PGP and Virchow2.
    • Added slide-label-only training, validation, testing, and prediction support.
    • Added configurable experiments for training, prediction, MIL evaluation, threshold estimation, and test suites.
    • Added support for slide-level and tile-level metrics with configurable thresholds.
    • Added dataset sampling and batching options for slide-level workflows.
  • Improvements

    • Increased default prediction batch size and worker capacity for improved throughput.
    • Standardized experiment configurations across classic and hybrid workflows.

@ejdam87
ejdam87 requested review from a team, Adames4 and vejtek August 3, 2026 11:06
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 777131cd-414f-4bb0-baf2-cd65e5d7c73c

📥 Commits

Reviewing files that changed from the base of the PR and between 612d5d5 and 6feff65.

📒 Files selected for processing (5)
  • prostate_cancer/attention_mil_model.py
  • prostate_cancer/classic_mil_model.py
  • prostate_cancer/datamodule/datasets/bag_of_embeddings_dataset.py
  • prostate_cancer/mil_model_base.py
  • prostate_cancer/typing.py
🚧 Files skipped from review as they are similar to previous changes (4)
  • prostate_cancer/typing.py
  • prostate_cancer/classic_mil_model.py
  • prostate_cancer/datamodule/datasets/bag_of_embeddings_dataset.py
  • prostate_cancer/mil_model_base.py

📝 Walkthrough

Walkthrough

The change introduces slide-label-only bag datasets and datamodules, extracts shared MIL behavior into ProstateCancerMILBase, adds ProstateCancerClassicMIL, and expands Hydra configurations for classic and hybrid training, prediction, and testing workflows.

Changes

Classic MIL workflows

Layer / File(s) Summary
Bag dataset and sample contracts
prostate_cancer/typing.py, prostate_cancer/datamodule/datasets/*
Bag datasets now support unlabeled, slide-labeled, and fully labeled samples through shared loading logic and explicit sample aliases.
Label-aware datamodule wiring
prostate_cancer/datamodule/*, configs/datamodule/*
The datamodule now selects slide-only or full-label collation for train and validation, while test loading uses full labels. Classic and hybrid datamodule configurations are available.
Shared and classic MIL models
prostate_cancer/mil_model_base.py, prostate_cancer/attention_mil_model.py, prostate_cancer/classic_mil_model.py, configs/model/*
Shared attention, metric, test, prediction, and optimizer behavior moves to ProstateCancerMILBase. ProstateCancerClassicMIL trains and validates with slide labels only.
Classic training configuration
configs/experiment/train/bag_of_embeddings/*
Training configurations now compose classic datamodules and PGP or Virchow2 classic MIL models. Hybrid training configurations inherit dedicated hybrid bases.
Prediction configuration composition
configs/experiment/predict/bag_of_embeddings/*
Prediction configurations add classic and hybrid bases, PGP and Virchow2 model workflows, threshold experiments, and MMCI dataset variants.
Test and threshold configuration composition
configs/experiment/test/bag_of_embeddings/*
Test configurations add classic and hybrid PGP and Virchow2 workflows, tile-threshold experiments, and MMCI test-suite variants.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Dataset as SLLabeledBagOfEmbeddingsDataset
  participant DataModule as ClassicBagOfTilesDataModule
  participant Model as ProstateCancerClassicMIL
  participant Base as ProstateCancerMILBase
  Dataset->>DataModule: provide slide-labeled bags
  DataModule->>Model: collate slide labels and embeddings
  Model->>Base: compute attention and slide prediction
  Base-->>Model: return slide logits
  Model-->>DataModule: log slide loss and metrics
Loading

Possibly related PRs

Suggested reviewers: adames4, vejtek

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title identifies the addition of Classic MIL, which matches the pull request's main objective of adding slide-level MIL supervision.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/classic-mil

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
prostate_cancer/datamodule/datasets/bag_of_embeddings_dataset.py (1)

32-32: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Place Generic[T] as the last base class.

Ruff reports PYI059 because Generic[T] is not last. Reorder from class BagOfEmbeddingsDataset(Dataset[T], Generic[T], ABC): to class BagOfEmbeddingsDataset(Dataset[T], ABC, Generic[T]):.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@prostate_cancer/datamodule/datasets/bag_of_embeddings_dataset.py` at line 32,
Update the base-class order in BagOfEmbeddingsDataset so Generic[T] is the last
base class, preserving Dataset[T] and ABC before it.

Source: Linters/SAST tools

prostate_cancer/mil_model_base.py (1)

97-133: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Guard against NaN when a bag has zero valid tiles.

mask at Line 104 can be all-zero if every tile in a bag is treated as padding. In that case, raw_attn.masked_fill(~mask.bool(), float("-inf")) at Line 110 sets every attention logit to -inf, and torch.softmax over an all--inf row produces NaN (0/0). That NaN then propagates into sl_pred_raw and the downstream loss/metrics for the whole batch.

The class docstring states a bag is "all embeddings from a single slide," so this is unlikely in practice today, but there is no explicit safeguard if an upstream data issue ever produces an empty bag. Consider asserting mask.sum(dim=1) > 0 per bag, or clamping the softmax denominator.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@prostate_cancer/mil_model_base.py` around lines 97 - 133, Update forward to
explicitly handle bags with zero valid tiles before softmax, using a per-bag
validity check such as mask.sum(dim=1) > 0 and the project’s established
failure-handling convention. Ensure attention_weights never comes from an
all--inf row, preventing NaN propagation into sl_pred_raw and downstream
outputs.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@prostate_cancer/mil_model_base.py`:
- Around line 135-149: Apply sigmoid once to the slide-level and tile-level
outputs before passing them to binary metric update calls, and reuse those
probabilities for the returned values where applicable. Update test_step in
prostate_cancer/mil_model_base.py#L135-L149, both affected metric-update sites
in prostate_cancer/attention_mil_model.py#L59-L60 and `#L99-L100`, and both sites
in prostate_cancer/classic_mil_model.py#L25 and `#L40`; preserve masking and
existing metric/logging behavior.

In `@prostate_cancer/typing.py`:
- Line 46: Update the comment describing the UnlabeledTileSampleBatch alias to
state “Images | Metadata” instead of “Images | labels | Metadata,” matching its
Tensor and TileMetadataBatch contents.

---

Nitpick comments:
In `@prostate_cancer/datamodule/datasets/bag_of_embeddings_dataset.py`:
- Line 32: Update the base-class order in BagOfEmbeddingsDataset so Generic[T]
is the last base class, preserving Dataset[T] and ABC before it.

In `@prostate_cancer/mil_model_base.py`:
- Around line 97-133: Update forward to explicitly handle bags with zero valid
tiles before softmax, using a per-bag validity check such as mask.sum(dim=1) > 0
and the project’s established failure-handling convention. Ensure
attention_weights never comes from an all--inf row, preventing NaN propagation
into sl_pred_raw and downstream outputs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2ed322df-24aa-4869-8280-3d5402420d27

📥 Commits

Reviewing files that changed from the base of the PR and between 83e6131 and 612d5d5.

📒 Files selected for processing (64)
  • configs/datamodule/classic_bag_of_embeddings_datamodule.yaml
  • configs/datamodule/datasets/embeddings/bag_of_embeddings_sl_labeled.yaml
  • configs/experiment/predict/bag_of_embeddings/base.yaml
  • configs/experiment/predict/bag_of_embeddings/classic_base.yaml
  • configs/experiment/predict/bag_of_embeddings/hybrid_base.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/classic_base.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/classic_slide_threshold.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/hybrid_base.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/hybrid_slide_threshold.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/mil_predictions/classic_base.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/mil_predictions/classic_mmci_sl_group_1.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/mil_predictions/classic_mmci_tl_val.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/mil_predictions/hybrid_base.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/mil_predictions/hybrid_mmci_sl_group_1.yaml
  • configs/experiment/predict/bag_of_embeddings/pgp/mil_predictions/hybrid_mmci_tl_val.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/classic_base.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/classic_slide_threshold.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/hybrid_base.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/hybrid_slide_threshold.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/mil_predictions/classic_base.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/mil_predictions/classic_mmci_sl_group_1.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/mil_predictions/classic_mmci_tl_val.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/mil_predictions/hybrid_base.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/mil_predictions/hybrid_mmci_sl_group_1.yaml
  • configs/experiment/predict/bag_of_embeddings/virchow2/mil_predictions/hybrid_mmci_tl_val.yaml
  • configs/experiment/test/bag_of_embeddings/base.yaml
  • configs/experiment/test/bag_of_embeddings/classic_base.yaml
  • configs/experiment/test/bag_of_embeddings/hybrid_base.yaml
  • configs/experiment/test/bag_of_embeddings/pgp/classic_base.yaml
  • configs/experiment/test/bag_of_embeddings/pgp/classic_tile_threshold.yaml
  • configs/experiment/test/bag_of_embeddings/pgp/hybrid_base.yaml
  • configs/experiment/test/bag_of_embeddings/pgp/hybrid_tile_threshold.yaml
  • configs/experiment/test/bag_of_embeddings/pgp/test_suite/classic_base.yaml
  • configs/experiment/test/bag_of_embeddings/pgp/test_suite/classic_mmci_tl_test.yaml
  • configs/experiment/test/bag_of_embeddings/pgp/test_suite/hybrid_base.yaml
  • configs/experiment/test/bag_of_embeddings/pgp/test_suite/hybrid_mmci_tl_test.yaml
  • configs/experiment/test/bag_of_embeddings/virchow2/classic_base.yaml
  • configs/experiment/test/bag_of_embeddings/virchow2/classic_tile_threshold.yaml
  • configs/experiment/test/bag_of_embeddings/virchow2/hybrid_base.yaml
  • configs/experiment/test/bag_of_embeddings/virchow2/hybrid_tile_threshold.yaml
  • configs/experiment/test/bag_of_embeddings/virchow2/test_suite/classic_base.yaml
  • configs/experiment/test/bag_of_embeddings/virchow2/test_suite/classic_mmci_tl_test.yaml
  • configs/experiment/test/bag_of_embeddings/virchow2/test_suite/hybrid_base.yaml
  • configs/experiment/test/bag_of_embeddings/virchow2/test_suite/hybrid_mmci_tl_test.yaml
  • configs/experiment/train/bag_of_embeddings/base.yaml
  • configs/experiment/train/bag_of_embeddings/classic_base.yaml
  • configs/experiment/train/bag_of_embeddings/classic_pgp.yaml
  • configs/experiment/train/bag_of_embeddings/classic_virchow2.yaml
  • configs/experiment/train/bag_of_embeddings/hybrid_base.yaml
  • configs/experiment/train/bag_of_embeddings/hybrid_pgp.yaml
  • configs/experiment/train/bag_of_embeddings/hybrid_virchow2.yaml
  • configs/model/checkpoints/mmci_tl/pgp_classic_mil.yaml
  • configs/model/checkpoints/mmci_tl/virchow2_classic_mil.yaml
  • configs/model/pgp_classic_mil_model.yaml
  • configs/model/virchow2_classic_mil_model.yaml
  • prostate_cancer/attention_mil_model.py
  • prostate_cancer/classic_mil_model.py
  • prostate_cancer/datamodule/__init__.py
  • prostate_cancer/datamodule/bag_of_tiles_data_module.py
  • prostate_cancer/datamodule/datasets/__init__.py
  • prostate_cancer/datamodule/datasets/bag_of_embeddings_dataset.py
  • prostate_cancer/datamodule/samplers/bag_of_tiles_stratified_weighted_random_sampler.py
  • prostate_cancer/mil_model_base.py
  • prostate_cancer/typing.py
💤 Files with no reviewable changes (2)
  • configs/experiment/test/bag_of_embeddings/base.yaml
  • configs/experiment/train/bag_of_embeddings/base.yaml

Comment thread prostate_cancer/mil_model_base.py Outdated
Comment thread prostate_cancer/typing.py Outdated
@ejdam87
ejdam87 merged commit 8c9bcf0 into master Aug 4, 2026
3 checks passed
@ejdam87
ejdam87 deleted the feature/classic-mil branch August 4, 2026 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants