Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable loading custom models in the QuPath extension #8

Closed
constantinpape opened this issue Sep 4, 2023 · 11 comments
Closed

Enable loading custom models in the QuPath extension #8

constantinpape opened this issue Sep 4, 2023 · 11 comments

Comments

@constantinpape
Copy link

Hi @ksugar,
we would like to implement loading of custom SAM models (e.g. from micro_sam) in the QuPath plugin.
This should be easy on the server side, and I think my PR in ksugar/samapi#11 already adds everything that's necessary there.

But I am not so sure what needs to be changed here. From a quick check I think I would need to add the option for a custom URL here:

private final ObjectProperty<SAMModel> model = PathPrefs.createPersistentPreference(
"ext.SAM.model", SAMModel.VIT_L, SAMModel.class);

Is that correct, or is there some other relevant place in the code? Also, how should the default (no custom URL) be represented? In python it's just None.

Thanks!

@petebankhead
Copy link
Contributor

That sounds great! It looks like the use of an enum for SAMModel would need to change. The way we did it in the WSInfer extension is to use a String or alternative non-enum class instead, and populate the combobox by querying the server for the list of available models on the server.

  • Current SAM approach here
  • Alternative WSInfer approach here

Then hopefully adding more models becomes easy, and doesn't require any change to the extension.

@ksugar
Copy link
Owner

ksugar commented Sep 5, 2023

Hi @constantinpape, thank you for the PR!
I checked your PR and found that it is a good idea to handle all models (incl. currently available models) with a single interface with just the checkpoint_url argument.

On the client side (i.e., QuPath), in addition to the preset model options (vit_h, vit_l, vit_b and vit_m), the user will have the option to add a custom model by specifying the URL with a nickname. Once it is added, it will remain until the user explicitly deletes it. QuPath's PathPrefs will manage a list of custom urls.

I will work on it later today.

@petebankhead
Copy link
Contributor

QuPath's PathPrefs will manage a list of custom urls.

I'm not sure PathPrefs will be very good at that by default. The trouble is that Java's Preferences class doesn't support arrays/lists. It should still work if you don't mind squeezing everything into a single string, although this won't support extremely long lists (the maximum string length is 8192 characters).

My guess is that the client supporting a list of models returned by the server would be easier to develop and manage, so that any extra URLs are handled when starting up the server.

@ksugar
Copy link
Owner

ksugar commented Sep 5, 2023

Hi @petebankhead, thank you for the feedback.

I was expecting that PathPrefs.createPersistentJsonPreference() can handle arrays/lists of String but it seems that it is not the case.

Following your suggestion, I will implement an endpoint for getting a list of the available models on the server side.

@petebankhead
Copy link
Contributor

I forgot about PathPrefs.createPersistentJsonPreference() :)
Probably it should work (although I guess maybe doesn't).

@constantinpape
Copy link
Author

Thanks for the quick replies @ksugar and @petebankhead! From what I understand you will take care of this for now @ksugar :). Let me know if you need any more info, or once it's ready to be tested!

@ksugar
Copy link
Owner

ksugar commented Sep 14, 2023

Hi @constantinpape and @petebankhead, I have implemented the function for loading custom model weights from URL.

Please try qupath-extension-sam-0.4.0-SNAPSHOT.jar with the updated version of samapi from the dev branch.

python -m pip install -U git+https://github.com/ksugar/samapi.git@dev

Since this version, it is important to launch the server with --workers 2 (or more) to enable cancellation of a download of a weights file.

export PYTORCH_ENABLE_MPS_FALLBACK=1 # Required for running on Apple silicon
uvicorn samapi.main:app --workers 2

The relevant documentation is found here.
https://github.com/ksugar/qupath-extension-sam/tree/dev#register-sam-weights-from-url

@constantinpape
Copy link
Author

Thanks @ksugar ! I will try it out as soon as possible and let you know what I find.

@constantinpape
Copy link
Author

I tested it now @ksugar and it all works as expected!

@ksugar
Copy link
Owner

ksugar commented Sep 15, 2023

Thank you @constantinpape for testing it! I will release it soon.
I'm thinking that it would be helpful to have a catalog for weights in README so that the users can find them easily.
How do you think?

Type Name (customizable) URL Citation
vit_h vit_h_lm https://zenodo.org/record/8250299/files/vit_h_lm.pth?download=1 Archit, A. et al. Segment Anything for Microscopy. bioRxiv 2023. doi:10.1101/2023.08.21.554208

https://github.com/computational-cell-analytics/micro-sam
vit_b vit_b_lm https://zenodo.org/record/8250281/files/vit_b_lm.pth?download=1
vit_h vit_h_em https://zenodo.org/record/8250291/files/vit_h_em.pth?download=1
vit_b vit_b_em https://zenodo.org/record/8250260/files/vit_b_em.pth?download=1

This was referenced Sep 15, 2023
@constantinpape
Copy link
Author

Thank you @constantinpape for testing it! I will release it soon. I'm thinking that it would be helpful to have a catalog for weights in README so that the users can find them easily. How do you think?
Type Name (customizable) URL Citation
vit_h vit_h_lm https://zenodo.org/record/8250299/files/vit_h_lm.pth?download=1 Archit, A. et al. Segment Anything for Microscopy. bioRxiv 2023. doi:10.1101/2023.08.21.554208

https://github.com/computational-cell-analytics/micro-sam
vit_b vit_b_lm https://zenodo.org/record/8250281/files/vit_b_lm.pth?download=1
vit_h vit_h_em https://zenodo.org/record/8250291/files/vit_h_em.pth?download=1
vit_b vit_b_em https://zenodo.org/record/8250260/files/vit_b_em.pth?download=1

Yes, that would be great!

@ksugar ksugar closed this as completed Dec 14, 2023
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

No branches or pull requests

3 participants