Timm 加载本地模型 Huggingface 下载

Timm 加载本地模型 Huggingface 下载

方法一:

huggingface_hub.utils._errors.LocalEntryNotFoundError: An error happened while trying to locate the file on the Hub and we cannot find the requested files in the local cache. Please check your connection and try again or make sure your Internet connection is on.
在使用到huggingface时,下载连接不稳定导致,ConnectError。

解决方式
1)在python代码中加入
os.environ[“HF_ENDPOINT”] = “https://hf-mirror.com”

2)在命令行中设置环境变量
HF_ENDPOINT=https://hf-mirror.com python xxx.py
参考链接:https://github.com/Stability-AI/generative-models/issues/215#issuecomment-1960803222

忘记在哪里看的了–

方法二:

1)获取模型权重下载链接

net = timm.create_model(name, pretrained=True)

print(timm.create_model(name).default_cfg)

输出:

{‘url’: ‘https://storage.googleapis.com/vit_models/augreg/Ti_16-i21k-300ep-lr_0.001-aug_none-wd_0.03-do_0.0-sd_0.0–imagenet2012-steps_20k-lr_0.03-res_224.npz’, ‘hf_hub_id’: ‘timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k’, ‘architecture’: ‘vit_tiny_patch16_224’, ‘tag’: ‘augreg_in21k_ft_in1k’, ‘custom_load’: True, ‘input_size’: (3, 224, 224), ‘fixed_input_size’: True, ‘interpolation’: ‘bicubic’, ‘crop_pct’: 0.9, ‘crop_mode’: ‘center’, ‘mean’: (0.5, 0.5, 0.5), ‘std’: (0.5, 0.5, 0.5), ‘num_classes’: 1000, ‘pool_size’: None, ‘first_conv’: ‘patch_embed.proj’, ‘classifier’: ‘head’}

取 ‘hf_hub_id’ 键对应的值: ‘timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k’

加上 https://huggingface.co/ 得到 https://huggingface.co/timm/vit_tiny_patch16_224.augreg_in21k_ft_in1k,即为模型下载链接。

  1. 下载 pytorch_model.bin 文件,改名后,直接放到工程中,然后在代码中增加一个参数pretrained_cfg_overlay,指定此权重文件。重新执行,此时不再需要连接huggingface.co。

net=timm.create_model(name,pretrained=True, pretrained_cfg_overlay=dict(file=“…/pretrained/vit_tiny_patch16_224.bin”))

参考链接

你可能感兴趣的:(python)