-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[FEATURE] Load model config AND weights from local folder (support locally cloned HF hub models) #2338
Comments
CC @qubvel @LysandreJik re transformers integration |
This will be very useful! However, I think requiring users to manually download the model in advance (e.g., using HF's CLI) adds unnecessary complexity, such as introducing an extra dependency (HF CLI) and adding another step to the workflow (running the command with the exact same model name that will be in the code, leading to possible duplication). This could be avoided if This approach would offer flexibility without requiring a "pre-download" step. For users who prefer to pre-download, the HF CLI could remain an optional tool to populate the cache directory, accommodating both workflows. Regarding |
@turicas the huggingface-cli is installed in the python env (and will be in the path) when you pip install huggingface_hub, which is required by timm to access the weights via the API, so dep is there, but understand the extra step/lookup isn't desirable. So, I could punch a pretrained_cfg_overlay was not really supposed to be a user fancing 'thing', it was supposed to be more of a developer testing/power user thing that could be used to override and key-value in the pretrained-cfg for various purposes, it happened to be useful here and filled a need for some to punt this feature here down the road (needed some more thought). I use it a lot in my model staging process, scripts to help with publishing, etc. |
Oh, my bad - I thought it would require an extra library, sorry.
Nice! I tested and it worked perfectly. Thanks for this. I've also created a little PR with an example of |
@turicas thanks for confirming, I'll merge that soon, and your example. Will probably be closer to the end of the month before it makes it into another pypi release though. |
Is your feature request related to a problem? Please describe.
Right now to load pretrained models w/ associated pretained config (preprocess + head details)
pretrained=True
must be used withtimm.list_pretrained()
)OR
hf-hub:repo_name
For option 1 above, the pretrained_cfg is loaded from builtins in the library. The builtin config can specify a weight location at a url or a specific HF hub repo + filename.
For option 2, the pretrained_cfg is loaded from the specified repo in the
config.json
file, and the weight is loaded from that same repo.In any case, if it is desired to using an existing pretrained_cfg by either mechanism above, the
create_model
factory will accept apretrained_cfg_overlay
argument, it should be a dict, and each key-value of that dict will override the values in the originally sourced config. This allows one to load weights locally by passing afile=
key that will override any url or hf_hub entry, but cannot change how/where the config is sourced from.Example:
Describe the solution you'd like
It should be possible to call
timm.create_model('...', pretrained=True)
and specify a local folder from which both the config and weights will be sourced, as withtransformers.AutoModel.from_pretrained()
The hardest part of adding this is figuring out how to integrate passing a folder into the create_model API. The pretrained_cfg_overlay was always a bit clunky/kludgy ... ideally this would be cleaner.
Based on existing use the two options that initially stand out to me
timm.create_model('/blah/blah/my-resnet50')
hf-hub
with something likelocal:
, 'folder:
...timm.create_model('local:/blah/blah/my-resnet50')
I'm liking 2 because it's a bit safer and more explicit, parallels hf-hub use. May be some considerations re timm wrapper in
transformers
Additional context
This is useful generally but would be particularly useful with new timm model wrapper in transformers to maintain full API compability. Users expect to be able to clone a model repo from the hub and point to a local folder with those files when calling
from_pretrained()
.The text was updated successfully, but these errors were encountered: