当前位置: 首页 > web >正文

Timm 加载本地 huggingface 模型

最近使用 Timm 自动加载在线 hf-hub 模型时,由于服务器存在网络限制 huggingface 无法正常连接,导致无法加载模型以及权重。解决办法就是本地电脑下载,再上传到服务器。

以下载 huggingface.co/MahmoodLab/UNI 为例。

import timm
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform
from huggingface_hub import loginlogin()  # login with your User Access Token, found at https://huggingface.co/settings/tokens# pretrained=True needed to load UNI weights (and download weights for the first time)
# init_values need to be passed in to successfully load LayerScale parameters (e.g. - block.0.ls1.gamma)
model = timm.create_model("hf-hub:MahmoodLab/uni", pretrained=True, init_values=1e-5, dynamic_img_size=True)
transform = create_transform(**resolve_data_config(model.pretrained_cfg, model=model))
model.eval()

本地下载

# 在可联网的机器运行,确保模型缓存
from huggingface_hub import snapshot_download# 指定存储路径
download_path = "D:/Research/pre_training_models"models = ["MahmoodLab/uni"
]
for repo in models:snapshot_download(repo_id=repo),
cache_dir=download_path

下载时可以存在一些获取 hf token 和对应模型库的邮箱认证等问题,可以自行 AI 获取解决步骤。

上传到服务器指定缓存目录

将文件复制到你的服务器 [用户名]/.cache/huggingface/hub 中,尝试复制到其他的路径发现 timm.create_model 无法正确识别,尽量还是放在该目录下。

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')def load_model(model_name, device):if model_name == 'UNI':model = timm.create_model("hf-hub:MahmoodLab/uni", pretrained=True, init_values=1e-5, dynamic_img_size=True) # PMID:38504018else:raise NotImplementedError(f'Model {model_name} not implemented !')return model.to(device).eval()uni_model = load_model('UNI', device)
uni_transform = create_transform(**resolve_data_config(uni_model.pretrained_cfg, model=uni_model))
http://www.xdnf.cn/news/965.html

相关文章:

  • requestAnimationFrame是什么?【前端】
  • Unreal如何实现一个Highlight高亮效果
  • 【PyQt5】@QtCore.pyqtSlot()的作用
  • 深度学习-全连接神经网络-2
  • Fluent 内置双向流固耦合FSI 液舱晃荡仿真计算
  • Java Lambda表达式指南
  • 4月21日复盘
  • 飞控系统的鲁棒性模块详解!
  • GPU软硬件架构协同设计解析
  • 蓝牙WiFi模组rtl8821cs在Android14调
  • 推荐系统/业务,相关知识/概念2
  • 【LaTeX】图片大小调整和并排放置
  • 一文详解卷积神经网络中的卷积层和池化层原理 !!
  • 大模型相关 XSS等 漏洞事件深度剖析
  • 进程和线程(2)
  • 跨境电商行业新周期下的渠道突围策略
  • [python] set
  • JavaScript day5
  • 一文了解相位阵列天线中的真时延
  • 深度学习:人工智能的核心驱动力
  • C语言——数组
  • Sentinel源码—7.参数限流和注解的实现二
  • 字符串拼接问题的最佳解决方案
  • Shell常用功能详细使用
  • STM32 栈地址起始位置不正确
  • 【GIT】为什么要变基下拉,变基下拉失败,提示没有暂存如何解决?
  • 华为OD机试2025A卷七日集训第6期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
  • Transformer系列(三):Transformer架构
  • Spring Boot
  • Batch Size