Skip to content

ENH: Add BubbleSAM detection method#3

Open
nidhinthomas-ai wants to merge 36 commits intolanl:mainfrom
nidhinthomas-ai:nidhin_bubblesam_detection
Open

ENH: Add BubbleSAM detection method#3
nidhinthomas-ai wants to merge 36 commits intolanl:mainfrom
nidhinthomas-ai:nidhin_bubblesam_detection

Conversation

@nidhinthomas-ai
Copy link

@nidhinthomas-ai nidhinthomas-ai commented Sep 25, 2025

This merge request introduces a new library module, neat_ml/bubblesam, which wraps the Segment Anything‑2 (SAM‑2) model for detecting circular “bubble” features in microscopy images. It also adds an accompanying test suite with deterministic baseline image comparisons.

What’s inside

  • BubbleSAM Blob Detection
    • neat_ml/bubblesam/SAM.py: Wrapper around SAM‑2 providing:
      • model construction via build_sam2(...) using a YAML config + checkpoint.
      • setup_cuda() helper to enable bfloat16/TF32 on Ampere‑class GPUs for speed.
      • generate_masks(...) to run SAM2AutomaticMaskGenerator on an RGB image and return masks sorted by area.
    • neat_ml/bubblesam/bubblesam.py End‑to‑end bubble detection utilities built on SAMModel
      • analyze_and_filter_masks(...) computes shape metrics (area, circularity, axes)
      • plot_filtered_masks(...) draws blue contours and green bounding boxes for retained regions.
      • process_image(...) runs the full SAM‑2 -> filter -> (optional) debug overlay/contours flow and persists artifacts/summary.
      • run_bubblesam(...) batch‑orchestrator returning one‑row‑per‑image summaries with median_radii_SAM and num_blobs_SAM.
  • Running bubblesam detection
    Instructions for running bubblesam detection have been added to the README.md file

@adamwitmer adamwitmer mentioned this pull request Oct 10, 2025
5 tasks
@tylerjereddy tylerjereddy added the enhancement New feature or request label Oct 15, 2025
@adamwitmer
Copy link
Collaborator

adamwitmer commented Nov 10, 2025

Initial TODO items for reviewing this branch:

  • read/understand diff/PR (read line-by-line, probing for weaknesses)
  • check for unnecessary complexity; areas for improvement/simplification
  • rebase branch against main (and push backup branch)
  • fix issues with git lfs (i.e. image storage with zenodo https://github.com/lanl/ldrd_neat_ml_images)
  • fix github CI
  • run test-suite and check test coverage
  • run branch according to README.md instructions
  • copy remaining review comments from gitlab
  • perform detailed code review
  • address all review comments
  • triple check diff
    • 1st check
    • 2nd check
    • 3rd check

@adamwitmer
Copy link
Collaborator

There are several files in this repo that are also included in PR #2, specifically detection.py, preprocessing.py and their testing modules, as well as references to OpenCV in the README.md. Depending on how dependent the BubbleSAM functions/tests are on these modules, they should either be removed here or rebased after merging PR #2 (removing here being preferable to avoid tedious conflicts)

@tylerjereddy
Copy link
Collaborator

Ok, I've provided a pretty detailed review here, good luck! This was presented for review pretty far past the original December 2025 deadline, and I probably can't provide reviews this detailed super regularly, so I do recommend presenting things for review roughly on time moving forward.

* parameterize tests over cpu/gpu
* modify yaml input to pass default model args
* fix docstrings
* remove vendored sam2 module
* remove mock test params
* modify CI to use new sam2 module imports
* modify workflow to use updated sam2.1 model/weights
* remove unnecessary tests
* fix test assertions
@tylerjereddy
Copy link
Collaborator

I'd say make sure you have tested instructions for running on Chicoma here before pinging me for another round of review.

README.md Outdated
```bash
module load cudatoolkit/24.7_12.5
```

Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's get proper instructions for running on Chicoma--you've had a few months to reproduce the paper results there now, so I think that's fair to ask for. And you need to run there again for Mihee soon on block copolymer images.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was able to run the workflow incantation described in #3 (comment) (with the full mask_settings) on Chicoma using the allocation for this LDRD. I allocated an interactive node with the following command:

salloc -p gpu -C gpu40 -N 1 -n 1 -t 1:00:00 --account=w25_fly-aug_g

and ran the workflow on a single GPU, which gave the following outputs:

[BubbleSAM]: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [25:30<00:00, 1530.90s/it]
                                      image_filepath  median_radii_SAM  num_blobs_SAM
0  /users/awitmer/.cache/test_images/images_raw.tiff          5.808687          11848

I also ran the same workflow incantation on a back-end node with -C gpu80 and points_per_batch: 256, which gave the following outputs:

[BubbleSAM]: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [24:51<00:00, 1491.52s/it]
                                      image_filepath  median_radii_SAM  num_blobs_SAM
0  /users/awitmer/.cache/test_images/images_raw.tiff          5.808687          11847

Both the iteration times and detection results are nearly identical between the two workflow runs, and the skimage post-processing steps still seem to be taking significant time on chicoma as well.

I pushed a commit to update the README with instructions on how to run the workflow on chicoma including download SAM2 checkpoints on the front-end node before running the workflow, and loading the cuda module with module load cudatoolkit/12.6.0

* fix docs
* modify handling of device setting
* modify tests to handle device settings appropriately
* add in-line comments clarifying design choices
* modify handling of model_cfg in yaml file
* add ValueError check for model_cfg input
* add test for ValueError raise
* update docs to reflect changes
* update bubblesam yaml file to reflect change in detection_cfg
* update instructions for running workflow on chicoma
* remove leftover settings from vendoring sam2
* update README.md with flavored markdowns
* add/fix inline comments and docstrings
* add cuda enabled image post-processing with `cucim`
* add instructions for installing cucim on cuda enabled systems
* modify contour finding to use opencv
* fix test to enforce assertions for checking sam2.1 detections
* update mypy types
* fix parquet data handling/saving/loading
* add test to check that parquet logic behaves as expected
* modify pyproject to install fastparquet instead of pyarrow
* revert unnecessary change
* update README for clarity
* remove unnecessary None type return in test signature
@adamwitmer
Copy link
Collaborator

@tylerjereddy I believe I have addressed all remaining review comments, and responded to comments requiring further clarification. Specifically, I added instructions for running the workflow on chicoma, as well as for using gpu accelerated skimage processing with cucim (which I tested on gp160, where the full test suite passes as well), and updated test_sam_internal_api to ensure desired behavior from the updated sam2.1 weights.

I also modified the workflow to use user defined settings from the yaml input file for providing mask_settings, area and circularity thresholds, sam2 weight parameter settings, and an option for performing detection on cpu or gpu. I updated issue #13 with a reference to the discussion at #3 (comment), detailing slow iteration times for bubblesam detection and some considerations for how to improve the speed of the workflow in relation to the task of detecting extremely small bubbles.

I re-read the diff again multiple times to check for additional improvements (I found several leftover -> None return types in test function signatures), and addressed those changes in individual commits. That being said, I think that this PR is ready for re-review, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants