Project-MONAI / MONAI

AI Toolkit for Healthcare Imaging
https://monai.io/
Apache License 2.0
5.5k stars 1.01k forks source link

Add integration with HF Hub #7833

Open qubvel opened 2 weeks ago

qubvel commented 2 weeks ago

Description

Hi! Amazing repo!

I made a draft with PytorchModelHubMixin for a few models in the repo. This mixin adds a few methods to make it possible to save/load models and share them with the HF hub.

Here is a short example:

from monai.networks.nets import AHNet

net = AHNet()

# save to a local folder
net.save_pretrained("qubvel-hf/ahnet")

# push to HF hub
net.push_to_hub("qubvel-hf/ahnet")

# load form local folder or HF hub
net = AHNet.from_pretrained("qubvel-hf/ahnet")

I believe the repo and community may benefit from sharing pretrained models built with this framework! Let me know if it aligns with your roadmap, I will be happy to help you with implementation or to complete the PR!

Types of changes

qubvel commented 1 week ago

Hey! I would appreciate any comments on this đŸ¤— ping @ericspod @KumoLiu for visibility

ericspod commented 1 week ago

Hi @qubvel Thanks again. Please double check places like this in setup.cfg have the updated information as your code mentions needed safetensors. Is it possible to perhaps subclass these networks as well? I am hesitant to modify existing definitions as a general rule even though things like the model weights are unchanged. For example instead of changing AHNet we would have class AHNetHub(AHNet, MonaiHubMixin): pass. I'd like some feedback from others on this as well.

qubvel commented 1 week ago

@ericspod thanks for the review, I will address the comments! At the moment it's a raw draft to know how you feel about this integration in general. Any comments on how to make it smooth are appreciated!

I'm not sure I got the point here, can you provide more details?

Is it possible to perhaps subclass these networks as well? I am hesitant to modify existing definitions as a general rule even though things like the model weights are unchanged.