Hub 文档
在 Hugging Face 上使用 timm
并获得增强的文档体验
开始使用
在 Hugging Face 上使用 timm
timm
,也称为 pytorch-image-models,是一个开源的、最先进的 PyTorch 图像模型、预训练权重和实用脚本的集合,用于训练、推理和验证。
本文档重点介绍 Hugging Face Hub 中 timm
的功能,而不是 timm
库本身。有关 timm
库的详细信息,请访问 其文档。
您可以使用模型页面左侧的过滤器在 Hub 上找到许多 timm
模型。
Hub 上的所有模型都具有以下几个有用的功能
- 自动生成的模型卡片,模型作者可以使用有关其模型的信息进行完善。
- 元数据标签帮助用户发现相关的
timm
模型。 - 一个交互式小部件,您可以使用它直接在浏览器中试用模型。
- 一个推理 API,允许用户发出推理请求。
使用 Hub 中的现有模型
只要您安装了 timm
,就可以用单行代码加载 Hugging Face Hub 中的任何 timm
模型!从 Hub 中选择模型后,将模型 ID 前缀为 hf-hub:
传递给 timm
的 create_model
方法,即可下载并实例化该模型。
import timm
# Loading https://huggingface.co/timm/eca_nfnet_l0
model = timm.create_model("hf-hub:timm/eca_nfnet_l0", pretrained=True)
如果您想了解如何加载特定模型,可以点击在 timm 中使用,您将获得一个可用的代码片段来加载它!




推理
下面的代码片段展示了如何在从 Hub 加载的 timm
模型上执行推理
import timm
import torch
from PIL import Image
from timm.data import resolve_data_config
from timm.data.transforms_factory import create_transform
# Load from Hub 🔥
model = timm.create_model(
'hf-hub:nateraw/resnet50-oxford-iiit-pet',
pretrained=True
)
# Set model to eval mode for inference
model.eval()
# Create Transform
transform = create_transform(**resolve_data_config(model.pretrained_cfg, model=model))
# Get the labels from the model config
labels = model.pretrained_cfg['label_names']
top_k = min(len(labels), 5)
# Use your own image file here...
image = Image.open('boxer.jpg').convert('RGB')
# Process PIL image with transforms and add a batch dimension
x = transform(image).unsqueeze(0)
# Pass inputs to model forward function to get outputs
out = model(x)
# Apply softmax to get predicted probabilities for each class
probabilities = torch.nn.functional.softmax(out[0], dim=0)
# Grab the values and indices of top 5 predicted classes
values, indices = torch.topk(probabilities, top_k)
# Prepare a nice dict of top k predictions
predictions = [
{"label": labels[i], "score": v.item()}
for i, v in zip(indices, values)
]
print(predictions)
这应该会给您留下一个预测列表,如下所示
[
{'label': 'american_pit_bull_terrier', 'score': 0.9999998807907104},
{'label': 'staffordshire_bull_terrier', 'score': 1.0000000149011612e-07},
{'label': 'miniature_pinscher', 'score': 1.0000000149011612e-07},
{'label': 'chihuahua', 'score': 1.0000000149011612e-07},
{'label': 'beagle', 'score': 1.0000000149011612e-07}
]
分享您的模型
您可以将您的 timm
模型直接分享到 Hugging Face Hub。 这会将您的模型的新版本发布到 Hugging Face Hub,如果您的模型仓库尚不存在,则会为您创建一个模型仓库。
在推送模型之前,请确保您已登录 Hugging Face
python -m pip install huggingface_hub huggingface-cli login
或者,如果您更喜欢从 Jupyter 或 Colaboratory 笔记本电脑工作,一旦您安装了 huggingface_hub
,您可以使用以下命令登录
from huggingface_hub import notebook_login
notebook_login()
然后,使用 push_to_hf_hub
方法推送您的模型
import timm
# Build or load a model, e.g. timm's pretrained resnet18
model = timm.create_model('resnet18', pretrained=True, num_classes=4)
###########################
# [Fine tune your model...]
###########################
# Push it to the 🤗 Hub
timm.models.hub.push_to_hf_hub(
model,
'resnet18-random-classifier',
model_config={'labels': ['a', 'b', 'c', 'd']}
)
# Load your model from the Hub
model_reloaded = timm.create_model(
'hf-hub:<your-username>/resnet18-random-classifier',
pretrained=True
)
推理小部件和 API
Hub 上的所有 timm
模型都自动配备了一个推理小部件,如下所示的是 nateraw/timm-resnet50-beans 的小部件。 此外,timm
模型可通过 推理 API 获得,您可以使用 cURL、Python 的 requests
库或您首选的网络请求方法通过 HTTP 访问该 API。


curl https://api-inference.huggingface.co/models/nateraw/timm-resnet50-beans \
-X POST \
--data-binary '@beans.jpeg' \
-H "Authorization: Bearer {$HF_API_TOKEN}"
# [{"label":"angular_leaf_spot","score":0.9845947027206421},{"label":"bean_rust","score":0.01368315052241087},{"label":"healthy","score":0.001722085871733725}]
其他资源
- timm (pytorch-image-models) GitHub 仓库。
- timm 文档。
- timmdocs 上的其他文档,由 Aman Arora 提供。
- PyTorch 图像模型 (timm) 入门:实践者指南,作者:Chris Hughes。