Skip to content

Enable automatic segmentation state caching for SAM2#1295

Merged
anwai98 merged 15 commits into
devfrom
add-auto-state-caching
Jul 19, 2026
Merged

Enable automatic segmentation state caching for SAM2#1295
anwai98 merged 15 commits into
devfrom
add-auto-state-caching

Conversation

@anwai98

@anwai98 anwai98 commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

WIP. Need to check how this works across tools!

@anwai98 anwai98 self-assigned this Jul 15, 2026
@anwai98
anwai98 marked this pull request as ready for review July 16, 2026 00:52
@anwai98

anwai98 commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator Author

Okay I tried it on a Mac, looks pretty good. What do you think? @constantinpape

@constantinpape constantinpape left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would change / clarify a few things:

  • Is the loading now also lazy / on-demand for tiled / 3d data?
  • I would try to save the state in zarr, as part of the embedding data structure. Also add a respective attribute to the zarr so we can record the state of this.
  • Rename amg_state to something more general.

Comment thread micro_sam/sam_annotator/_state.py Outdated
Comment thread micro_sam/sam_annotator/_state.py Outdated
Comment thread micro_sam/sam_annotator/annotator.py Outdated
Comment thread micro_sam/precompute_state.py Outdated
Comment thread micro_sam/precompute_state.py Outdated
@anwai98

anwai98 commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator Author

I'll answer the high-level questions here:

Is the loading now also lazy / on-demand for tiled / 3d data?

Yes, that's right -- it's lazy on-demand loading. I updated it a bit to compat with the catched zarr dispatcher now!

I would try to save the state in zarr, as part of the embedding data structure. Also add a respective attribute to the zarr so we can record the state of this.

Took care of it. Here's a high-level file structure inside the cached zarr:

embeddings.zarr/
    -> features/
        - ... (contains primary image embedding stuff -- an array / group when tiled)
    -> high_res_feats / pos_enc / fpn
        - ... sibling HieraViT/SAM2 features -- still in the embeddings family
    -> automatic_segmentation_state/
        - ais/
            - ... (contains segmentation decoder outputs as a chunked array)
        - amg/
            - ... (whole state pickeld to a byte array)

Rename amg_state to something more general.

This one's a bit of a thinker. I am thinking of going with a general alias, eg. autoseg_state? (let's talk quickly over lunch)

@anwai98
anwai98 requested a review from constantinpape July 16, 2026 21:04
@anwai98

anwai98 commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator Author

Okay, this is ready for the next round of review @constantinpape

Comment thread micro_sam/sam_annotator/_state.py Outdated
Comment thread micro_sam/v2/instance_segmentation.py
Comment thread micro_sam/precompute_state.py
@anwai98
anwai98 merged commit 8592e16 into dev Jul 19, 2026
4 checks passed
@anwai98
anwai98 deleted the add-auto-state-caching branch July 19, 2026 13:39
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.

2 participants