在本指南中,我们将引导您了解:
这是一个ONNX模型的示例。
开放神经网络交换 (ONNX) 是一种用于表示机器学习模型的开放标准格式。ONNX 得到了一个合作伙伴社区的支持,这些合作伙伴已在许多框架和工具中实现了它。例如,如果您在 TensorFlow 或 PyTorch 中训练了一个模型,您可以轻松地将其转换为 ONNX,然后使用像 ONNX Runtime 这样的引擎/编译器在各种设备上运行它。
ONNX Model Zoo 是一个由社区成员贡献的预训练、最先进的模型的集合,这些模型以ONNX格式提供。每个模型都附带有用于模型训练和使用训练模型进行推理的Jupyter笔记本。这些笔记本是用Python编写的,并包括训练数据集的链接以及描述模型架构的原始论文的参考。
Gradio 允许用户将他们的机器学习模型作为网页应用进行演示,全部使用 Python 代码。Gradio 将一个 Python 函数包装成用户界面,并且这些演示可以在 Jupyter 笔记本、Colab 笔记本中启动,也可以嵌入到你自己的网站中,并且可以免费托管在 Hugging Face Spaces 上。
从这里开始 here
Hugging Face Spaces 是 Gradio 演示的免费托管选项。Spaces 提供 3 种 SDK 选项:Gradio、Streamlit 和静态 HTML 演示。Spaces 可以是公开的或私有的,其工作流程类似于 GitHub 仓库。目前 Hugging Face 上有超过 2000 个 Spaces。了解更多关于 Spaces 的信息 这里。
Hugging Face 模型中心也支持 ONNX 模型,并且可以通过 ONNX 标签 过滤 ONNX 模型
ONNX 模型库中有许多 Jupyter 笔记本供用户测试模型。以前,用户需要自己下载模型并在本地运行这些笔记本来进行测试。有了 Hugging Face,测试过程可以变得更加简单和用户友好。用户可以在 Hugging Face Spaces 上轻松尝试某些 ONNX 模型库模型,并通过 ONNX Runtime 运行由 Gradio 支持的快速演示,所有这些都在云端完成,无需在本地下载任何内容。请注意,ONNX 有各种运行时,例如 ONNX Runtime, MXNet。
ONNX Runtime 是一个跨平台的推理和训练机器学习加速器。它使得在 Hugging Face 上使用 ONNX Model Zoo 模型进行实时 Gradio 演示成为可能。
ONNX Runtime 推理可以加快客户体验并降低成本,支持来自深度学习框架(如 PyTorch 和 TensorFlow/Keras)以及经典机器学习库(如 scikit-learn、LightGBM、XGBoost 等)的模型。ONNX Runtime 兼容不同的硬件、驱动程序和操作系统,并通过在适用的情况下利用硬件加速器以及图优化和转换来提供最佳性能。更多信息请参见官方网站。
EfficientNet-Lite 4 是 EfficientNet-Lite 模型系列中最大且最准确的变体。它是一个仅使用整数量化的模型,在所有 EfficientNet 模型中具有最高的准确率。它在 ImageNet 上达到了 80.4% 的 top-1 准确率,同时仍然可以在 Pixel 4 CPU 上实时运行(例如 30ms/图像)。要了解更多信息,请阅读 模型卡片
在这里,我们将通过使用Gradio为EfficientNet-Lite4设置一个示例演示
首先我们导入依赖项并从onnx模型库中下载并加载efficientnet-lite4模型。然后从labels_map.txt文件中加载标签。接着我们设置预处理函数,加载模型进行推理,并设置推理函数。最后,推理函数被包装成一个gradio界面,供用户交互。请查看下面的完整代码。
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()