From 5549ce90a1f2585b1bb2c013b0ee5c37299b68d0 Mon Sep 17 00:00:00 2001 From: Yushun Xiang Date: Tue, 21 May 2024 22:01:31 +0800 Subject: [PATCH 1/2] load CLIP model and tokenizer from local path --- src/open_clip/factory.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/open_clip/factory.py b/src/open_clip/factory.py index cf62d5a1c..08076de13 100644 --- a/src/open_clip/factory.py +++ b/src/open_clip/factory.py @@ -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 @@ -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, @@ -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'] From 87df73eb2b3fd2277288a4fa3a310140e1979fe3 Mon Sep 17 00:00:00 2001 From: Yushun Xiang Date: Mon, 19 Aug 2024 15:36:44 +0800 Subject: [PATCH 2/2] change .gitignore exclude src_copy dir --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 960651015..2e4599d17 100644 --- a/.gitignore +++ b/.gitignore @@ -150,4 +150,5 @@ src/debug core.* # Allow -!src/evaluation/misc/results_dbs/* \ No newline at end of file +!src/evaluation/misc/results_dbs/* +src_copy \ No newline at end of file