在 Hugging Face 使用 fastai
fastai 是一个开源深度学习库,利用 PyTorch 和 Python 提供高级组件,用于训练快速准确的神经网络,在文本、视觉和表格数据上获得最先进的输出。
在 Hub 上探索 fastai
你可以通过在模型页面左侧筛选来找到 fastai 模型。
Hub 上的所有模型都配备了以下功能:
- 自动生成的模型卡片,包含简要描述和有助于可发现性的元数据标签。
- 一个交互式小部件,你可以直接在浏览器中使用模型(用于图像分类)。
- 允许进行推理请求的推理 API(用于图像分类)。
使用现有模型
huggingface_hub 库是一个轻量级 Python 客户端,具有从 Hub 下载模型的实用函数。
pip install huggingface_hub["fastai"]
安装库后,你只需要使用 from_pretrained_fastai 方法。此方法不仅加载模型,还验证保存模型时的 fastai 版本,这对可重现性很重要。
from huggingface_hub import from_pretrained_fastai
learner = from_pretrained_fastai("espejelomar/identify-my-cat")
_,_,probs = learner.predict(img)
print(f"Probability it's a cat: {100*probs[1].item():.2f}%")
# Probability it's a cat: 100.00%
如果你想查看如何加载特定模型,可以点击 Use in fastai,你将获得一个可用的代码片段来加载它!




分享你的模型
你可以使用 push_to_hub_fastai 方法分享你的 fastai 模型。
from huggingface_hub import push_to_hub_fastai
push_to_hub_fastai(learner=learn, repo_id="espejelomar/identify-my-cat")