Skip to content

Commit

Permalink
load CLIP model and tokenizer from local path
Browse files Browse the repository at this point in the history
  • Loading branch information
YushunXiang committed May 21, 2024
1 parent 2e8de83 commit 5549ce9
Showing 1 changed file with 10 additions and 3 deletions.
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

0 comments on commit 5549ce9

Please sign in to comment.