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