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

load OpenCLIP model and tokenizer from local path #883

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,5 @@ src/debug
core.*

# Allow
!src/evaluation/misc/results_dbs/*
!src/evaluation/misc/results_dbs/*
src_copy
13 changes: 10 additions & 3 deletions src/open_clip/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ def get_model_config(model_name):


def _get_hf_config(model_id, cache_dir=None):
config_path = download_pretrained_from_hf(model_id, filename='open_clip_config.json', cache_dir=cache_dir)
if cache_dir is None:
config_path = download_pretrained_from_hf(model_id, filename='open_clip_config.json', cache_dir=cache_dir)
else:
config_path = os.path.join(cache_dir, model_id, 'open_clip_config.json')
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
return config
Expand All @@ -83,12 +86,13 @@ def _get_hf_config(model_id, cache_dir=None):
def get_tokenizer(
model_name: str = '',
context_length: Optional[int] = None,
cache_dir: Optional[str] = None,
**kwargs,
):
if model_name.startswith(HF_HUB_PREFIX):
model_name = model_name[len(HF_HUB_PREFIX):]
try:
config = _get_hf_config(model_name)['model_cfg']
config = _get_hf_config(model_name, cache_dir=cache_dir)['model_cfg']
except Exception:
tokenizer = HFTokenizer(
model_name,
Expand Down Expand Up @@ -185,7 +189,10 @@ def create_model(
has_hf_hub_prefix = model_name.startswith(HF_HUB_PREFIX)
if has_hf_hub_prefix:
model_id = model_name[len(HF_HUB_PREFIX):]
checkpoint_path = download_pretrained_from_hf(model_id, cache_dir=cache_dir)
if cache_dir is None:
checkpoint_path = download_pretrained_from_hf(model_id, cache_dir=cache_dir)
else:
checkpoint_path = os.path.join(cache_dir, model_id, "open_clip_pytorch_model.bin")
config = _get_hf_config(model_id, cache_dir)
preprocess_cfg = merge_preprocess_dict(preprocess_cfg, config['preprocess_cfg'])
model_cfg = config['model_cfg']
Expand Down