Views
No views yet
1from transformers import AutoModel
2from huggingface_hub import hf_hub_download
3import shutil
4import os
5import torch
6import sys
7
8
9# helpfer function to download huggingface repo and use model
10def download(repo_id, path, HF_TOKEN=None):
11 os.makedirs(path, exist_ok=True)
12 files_path = os.path.join(path, 'files.txt')
13 if not os.path.exists(files_path):
14 hf_hub_download(repo_id, 'files.txt', token=HF_TOKEN, local_dir=path, local_dir_use_symlinks=False)
15 with open(os.path.join(path, 'files.txt'), 'r') as f:
16 files = f.read().split('\n')
17 for file in [f for f in files if f] + ['config.json', 'wrapper.py', 'model.safetensors']:
18 full_path = os.path.join(path, file)
19 if not os.path.exists(full_path):
20 hf_hub_download(repo_id, file, token=HF_TOKEN, local_dir=path, local_dir_use_symlinks=False)
21
22
23# helpfer function to download huggingface repo and use model
24def load_model_from_local_path(path, HF_TOKEN=None):
25 cwd = os.getcwd()
26 os.chdir(path)
27 sys.path.insert(0, path)
28 model = AutoModel.from_pretrained(path, trust_remote_code=True, token=HF_TOKEN)
29 os.chdir(cwd)
30 sys.path.pop(0)
31 return model
32
33
34# helpfer function to download huggingface repo and use model
35def load_model_by_repo_id(repo_id, save_path, HF_TOKEN=None, force_download=False):
36 if force_download:
37 if os.path.exists(save_path):
38 shutil.rmtree(save_path)
39 download(repo_id, save_path, HF_TOKEN)
40 return load_model_from_local_path(save_path, HF_TOKEN)
41
42
43if __name__ == '__main__':
44
45 HF_TOKEN = 'YOUR_HUGGINGFACE_TOKEN'
46 path = os.path.expanduser('~/.cvlface_cache/minchul/cvlface_adaface_ir101_ms1mv3')
47 repo_id = 'minchul/cvlface_adaface_ir101_ms1mv3'
48 model = load_model_by_repo_id(repo_id, path, HF_TOKEN)
49
50 # input is a rgb image normalized.
51 from torchvision.transforms import Compose, ToTensor, Normalize
52 from PIL import Image
53 img = Image.open('path/to/image.jpg')
54 trans = Compose([ToTensor(), Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
55 input = trans(img).unsqueeze(0) # torch.randn(1, 3, 112, 112)
56 out = model(input)