相关空间:https: //huggingface.co/spaces/onnx/EfficientNet-Lite4标签:ONNX,空间由 Gradio 和 the
在本指南中,我们将引导你完成:
In this Guide, we'll walk you through:
ONNX、ONNX 模型动物园、Gradio 和 Hugging Face Spaces 的介绍
Introduction of ONNX, ONNX model zoo, Gradio, and Hugging Face Spaces
如何为 EfficientNet-Lite4 设置 Gradio 演示
How to setup a Gradio demo for EfficientNet-Lite4
如何在 Hugging Face 上为 ONNX 组织贡献你自己的 Gradio 演示
How to contribute your own Gradio demos for the ONNX organization on Hugging Face
下面是 ONNX 模型的示例:尝试下面的 EfficientNet-Lite4 演示。
Here's an example of an ONNX model: try out the EfficientNet-Lite4 demo below.
Open Neural Network Exchange ( ONNX ) 是一种用于表示机器学习模型的开放标准格式。 ONNX 得到合作伙伴社区的支持,他们在许多框架和工具中实施了它。 例如,如果你在 TensorFlow 或 PyTorch 中训练了一个模型,你可以轻松地将其转换为 ONNX,然后使用 ONNX Runtime 等引擎/编译器从那里在各种设备上运行它。
Open Neural Network Exchange (ONNX) is an open standard format for representing machine learning models. ONNX is supported by a community of partners who have implemented it in many frameworks and tools. For example, if you have trained a model in TensorFlow or PyTorch, you can convert it to ONNX easily, and from there run it on a variety of devices using an engine/compiler like ONNX Runtime.
ONNX Model Zoo是社区成员贡献的 ONNX 格式的预训练、最先进模型的集合。 每个模型都附有 Jupyter 笔记本,用于模型训练和对训练模型的运行推理。 这些笔记本是用 Python 编写的,包括指向训练数据集的链接以及对描述模型架构的原始论文的引用。
The ONNX Model Zoo is a collection of pre-trained, state-of-the-art models in the ONNX format contributed by community members. Accompanying each model are Jupyter notebooks for model training and running inference with the trained model. The notebooks are written in Python and include links to the training dataset as well as references to the original paper that describes the model architecture.
Gradio 允许用户将他们的机器学习模型演示为 web 应用程序,全部使用 python 代码。 Gradio 将 python 函数包装到用户界面中,演示可以在 jupyter notebooks、colab notebooks 中启动,也可以嵌入你自己的网站并免费托管在 Hugging Face Spaces 上。
Gradio lets users demo their machine learning models as a web app all in python code. Gradio wraps a python function into a user interface and the demos can be launched inside jupyter notebooks, colab notebooks, as well as embedded in your own website and hosted on Hugging Face Spaces for free.
从这里开始
Get started here
Hugging Face Spaces 是 Gradio 演示的免费托管选项。 Spaces 带有 3 个 SDK 选项:Gradio、Streamlit 和静态 HTML 演示。 空间可以是公共的也可以是私有的,工作流程类似于 github repos。 Hugging Face 目前有超过 2000+个空间。 在此处了解有关空间的更多信息。
Hugging Face Spaces is a free hosting option for Gradio demos. Spaces comes with 3 SDK options: Gradio, Streamlit and Static HTML demos. Spaces can be public or private and the workflow is similar to github repos. There are over 2000+ spaces currently on Hugging Face. Learn more about spaces here.
Hugging Face Model Hub 也支持 ONNX 模型,可以通过ONNX 标签过滤 ONNX 模型
Hugging Face Model Hub also supports ONNX models and ONNX models can be filtered through the ONNX tag
ONNX Model Zoo 中有大量的 Jupyter notebook 供用户测试模型。 以前,用户需要自己下载模型并在本地运行这些笔记本进行测试。 使用 Hugging Face,测试过程可以变得更加简单和更加人性化。 用户可以轻松地在 Hugging Face Spaces 上尝试某些 ONNX Model Zoo 模型,并运行一个由 Gradio 支持的带有 ONNX Runtime 的快速演示,所有这些都在云上进行,无需在本地下载任何内容。 请注意,ONNX 有多种运行时,例如ONNX Runtime 、 MXNet 。
There are a lot of Jupyter notebooks in the ONNX Model Zoo for users to test models. Previously, users needed to download the models themselves and run those notebooks locally for testing. With Hugging Face, the testing process can be much simpler and more user-friendly. Users can easily try certain ONNX Model Zoo model on Hugging Face Spaces and run a quick demo powered by Gradio with ONNX Runtime, all on cloud without downloading anything locally. Note, there are various runtimes for ONNX, e.g., ONNX Runtime, MXNet.
ONNX Runtime 是一个跨平台推理和训练机器学习加速器。 它使在 Hugging Face 上使用 ONNX Model Zoo 模型进行现场 Gradio 演示成为可能。
ONNX Runtime is a cross-platform inference and training machine-learning accelerator. It makes live Gradio demos with ONNX Model Zoo model on Hugging Face possible.
ONNX Runtime 推理可以实现更快的客户体验和更低的成本,支持来自 PyTorch 和 TensorFlow/Keras 等深度学习框架以及 scikit-learn、LightGBM、XGBoost 等经典机器学习库的模型。ONNX Runtime 兼容不同的硬件、驱动程序和操作系统,并通过在适用的情况下利用硬件加速器以及图形优化和转换来提供最佳性能。 更多信息请查看官方网站。
ONNX Runtime inference can enable faster customer experiences and lower costs, supporting models from deep learning frameworks such as PyTorch and TensorFlow/Keras as well as classical machine learning libraries such as scikit-learn, LightGBM, XGBoost, etc. ONNX Runtime is compatible with different hardware, drivers, and operating systems, and provides optimal performance by leveraging hardware accelerators where applicable alongside graph optimizations and transforms. For more information please see the official website.
EfficientNet-Lite 4 是 EfficientNet-Lite 模型集中最大的变体和最准确的模型。 它是一个纯整数量化模型,在所有 EfficientNet 模型中精度最高。 它实现了 80.4% 的 ImageNet top-1 精度,同时仍在 Pixel 4 CPU 上实时运行(例如 30 毫秒/图像)。 要了解更多信息,请阅读模型卡
EfficientNet-Lite 4 is the largest variant and most accurate of the set of EfficientNet-Lite models. It is an integer-only quantized model that produces the highest accuracy of all of the EfficientNet models. It achieves 80.4% ImageNet top-1 accuracy, while still running in real-time (e.g. 30ms/image) on a Pixel 4 CPU. To learn more read the model card
在这里,我们将逐步介绍如何使用 Gradio 为 EfficientNet-Lite4 设置示例演示
Here we walk through setting up a example demo for EfficientNet-Lite4 using Gradio
首先,我们导入依赖项并从 onnx 模型动物园下载并加载 efficientnet-lite4 模型。 然后从 labels_map.txt 文件加载标签。 然后我们设置我们的预处理函数,加载模型进行推理,并设置推理函数。 最后,推理函数被包装到一个 gradio 界面中,供用户与之交互。 请参阅下面的完整代码。
First we import our dependencies and download and load the efficientnet-lite4 model from the onnx model zoo. Then load the labels from the labels_map.txt file. We then setup our preprocessing functions, load the model for inference, and setup the inference function. Finally, the inference function is wrapped into a gradio interface for a user to interact with. See the full code below.
import numpy as np
import math
import matplotlib.pyplot as plt
import cv2
import json
import gradio as gr
from huggingface_hub import hf_hub_download
from onnx import hub
import onnxruntime as ort
# loads ONNX model from ONNX Model Zoo
model = hub.load("efficientnet-lite4")
# loads the labels text file
labels = json.load(open("labels_map.txt", "r"))
# sets image file dimensions to 224x224 by resizing and cropping image from center
def pre_process_edgetpu(img, dims):
output_height, output_width, _ = dims
img = resize_with_aspectratio(img, output_height, output_width, inter_pol=cv2.INTER_LINEAR)
img = center_crop(img, output_height, output_width)
img = np.asarray(img, dtype='float32')
# converts jpg pixel value from [0 - 255] to float array [-1.0 - 1.0]
img -= [127.0, 127.0, 127.0]
img /= [128.0, 128.0, 128.0]
return img
# resizes the image with a proportional scale
def resize_with_aspectratio(img, out_height, out_width, scale=87.5, inter_pol=cv2.INTER_LINEAR):
height, width, _ = img.shape
new_height = int(100. * out_height / scale)
new_width = int(100. * out_width / scale)
if height > width:
w = new_width
h = int(new_height * height / width)
else:
h = new_height
w = int(new_width * width / height)
img = cv2.resize(img, (w, h), interpolation=inter_pol)
return img
# crops the image around the center based on given height and width
def center_crop(img, out_height, out_width):
height, width, _ = img.shape
left = int((width - out_width) / 2)
right = int((width + out_width) / 2)
top = int((height - out_height) / 2)
bottom = int((height + out_height) / 2)
img = img[top:bottom, left:right]
return img
sess = ort.InferenceSession(model)
def inference(img):
img = cv2.imread(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = pre_process_edgetpu(img, (224, 224, 3))
img_batch = np.expand_dims(img, axis=0)
results = sess.run(["Softmax:0"], {"images:0": img_batch})[0]
result = reversed(results[0].argsort()[-5:])
resultdic = {}
for r in result:
resultdic[labels[str(r)]] = float(results[0][r])
return resultdic
title = "EfficientNet-Lite4"
description = "EfficientNet-Lite 4 is the largest variant and most accurate of the set of EfficientNet-Lite model. It is an integer-only quantized model that produces the highest accuracy of all of the EfficientNet models. It achieves 80.4% ImageNet top-1 accuracy, while still running in real-time (e.g. 30ms/image) on a Pixel 4 CPU."
examples = [['catonnx.jpg']]
gr.Interface(inference, gr.Image(type="filepath"), "label", title=title, description=description, examples=examples).launch()
将模型添加到onnx 模型动物园
Add model to the onnx model zoo
在此处创建 Hugging Face 帐户。
Create an account on Hugging Face here.
查看剩余要添加到 ONNX 组织的模型列表,请参阅模型列表表
See list of models left to add to ONNX organization, please refer to the table with the Models list
在你的用户名下添加 Gradio Demo,请参阅此博客文章以在 Hugging Face 上设置 Gradio Demo。
Add Gradio Demo under your username, see this blog post for setting up Gradio Demo on Hugging Face.
在此处请求加入 ONNX 组织。
Request to join ONNX Organization here.
一旦批准从你的用户名转移模型到 ONNX 组织
Once approved transfer model from your username to ONNX organization
在模型表中为模型添加徽章,请参见模型列表中的示例
Add a badge for model in model table, see examples in Models list