question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HFValidationError when loading the model

See original GitHub issue

Hi there! I am trying to load a model I have stored at Google Drive for inferencing:

# Load SetFit model
tuned_model = SetFitModel.from_pretrained("/content/drive/My Drive/models/tuned-model")
# Run inference
tuned_model(["i didnt feel humiliated", "i feel romantic too", "im grabbing a minute to post i feel greedy wrong"])

But I get the following error:

HFValidationError: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '/content/drive/My Drive/models /tuned-model'. Use repo_type argument if needed.

it works however when I load it from the same script in which I have saved it, by:

# Save trained model to disk
trainer.model.save_pretrained("/content/drive/My Drive/models/tuned-model")

What can be the problem? Can’t just I save/load from pretrained to Google Drive?

Many thanks in advance for your support and terrific work.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Mouhanedg56commented, Oct 19, 2022

I saved a trained model on local path. I can’t see anything wrong when loading the model using from_pretrained with correct path.

This error appears when you try to load a model from a nonexistent local path which have more than 1 backslash \ with local_files_only=True.

  • if we pass a nonexistent path:
    • if path is in the form ‘repo_name’ or ‘namespace/repo_name’: ModelHubMixin.from_pretrained will throw FileNotFoundError.
    • else: ModelHubMixin.from_pretrained will throw a HFValidationError because the path does not exist locally and is not in the expected hub form.
  • if we pass an existing local folder with no body: SentenceTransformer will throw an OSError: /path/to/your/model does not appear to have a file named config.json
  • if we pass an existing folder with a body and head: no issue in this case
  • if we pass an existing folder with a body with no head: SetFitModel._from_pretrained throws HFValidationError not catched by try/except. Since the expected behaviour in SetFit _from_pretrained classmethod is to initialise classification head with random weights when the MODEL_HEAD_NAME is not found, this can be considered as a bug.

@pdhall99 's suggestion looks fixing the issue and follows the same logic in the ModelHubMixin.from_pretrained:

  • if folder exists:
    • if model head file exists => model_head_file = os.path.join(model_id, MODEL_HEAD_NAME) else None => initialise classification head with random weights
  • else: look for the model head in the hub using hf_hub_download.
1reaction
pdhall99commented, Oct 18, 2022

I get the same error. When I try to load a locally saved model:

from setfit import SetFitModel

model = SetFitModel.from_pretrained("/path/to/model-directory", local_files_only=True)

I get

HFValidationError: Repo id must be in the form 'repo_name' or 'namespace/repo_name': '/path/to/model-directory'. Use `repo_type` argument if needed.

I think this could be solved by changing these lines from

        if os.path.isdir(model_id) and MODEL_HEAD_NAME in os.listdir(model_id):
            model_head_file = os.path.join(model_id, MODEL_HEAD_NAME)

to something like

        if os.path.isdir(model_id):
            if MODEL_HEAD_NAME in os.listdir(model_id):
                model_head_file = os.path.join(model_id, MODEL_HEAD_NAME)
            else:
                model_head_file = None
Read more comments on GitHub >

github_iconTop Results From Across the Web

Raise a validation error in a model's save method in Django
It's fine to put validation in your model by using validators or writing a clean() method. All I was saying is that the...
Read more >
error when loading config for pretraining · Issue #7289 - GitHub
I'm working on pretraining a tok2vec model in spacy v3, and I set up my config file with python -m spacy init fill-config...
Read more >
Model validation error and test error - Cross Validated
No. First, there is always residual variance, so even if the model with the lowest validation error has the lowest expected prediction error ......
Read more >
Validators - Django documentation
A validator is a callable that takes a value and raises a ValidationError if it doesn't meet some criteria. Validators can be useful...
Read more >
Backbone.js model.validationError() - Javatpoint
The Backbone.js validationError model returns values by using validate() method. It displays validation error in two cases :.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found