1. 其他教程
  2. Gradio 和 Hugging Face 上的 ONNX

Hugging Face 上的 Gradio 和 ONNX

简介

在本指南中,我们将带您了解

  • ONNX、ONNX 模型库、Gradio 和 Hugging Face Spaces 简介
  • 如何为 EfficientNet-Lite4 设置 Gradio 演示
  • 如何为 Hugging Face 上的 ONNX 组织贡献您自己的 Gradio 演示

这是一个 ONNX 模型的示例

什么是 ONNX 模型库?

开放神经网络交换(ONNX)是一种用于表示机器学习模型的开放标准格式。ONNX 得到了实施它的许多框架和工具的合作伙伴社区的支持。例如,如果您在 TensorFlow 或 PyTorch 中训练了一个模型,您可以轻松地将其转换为 ONNX,然后使用 ONNX Runtime 等引擎/编译器在各种设备上运行它。

ONNX 模型库是社区成员贡献的 ONNX 格式的预训练、最先进模型的集合。每个模型都附有 Jupyter notebook,用于模型训练和使用训练好的模型运行推理。这些 notebook 用 Python 编写,包含指向训练数据集的链接以及描述模型架构的原始论文的参考文献。

什么是 Hugging Face Spaces 和 Gradio?

Gradio

Gradio 允许用户将他们的机器学习模型演示为一个完全用 Python 代码编写的 Web 应用程序。Gradio 将一个 Python 函数封装成一个用户界面,演示可以在 jupyter notebook、colab notebook 中启动,也可以嵌入到您自己的网站上并免费托管在 Hugging Face Spaces 上。

这里开始

Hugging Face Spaces

Hugging Face Spaces 是 Gradio 演示的免费托管选项。Spaces 提供 3 个 SDK 选项:Gradio、Streamlit 和静态 HTML 演示。Spaces 可以是公共的或私有的,工作流程类似于 github 存储库。Hugging Face 上目前有 2000 多个 Spaces。在此处了解有关 Spaces 的更多信息这里

Hugging Face Models

Hugging Face 模型中心也支持 ONNX 模型,可以通过 ONNX 标签过滤 ONNX 模型。

Hugging Face 如何帮助 ONNX 模型库?

ONNX 模型库中有很多 Jupyter notebook 供用户测试模型。以前,用户需要自己下载模型并在本地运行这些 notebook 进行测试。有了 Hugging Face,测试过程可以更简单、更用户友好。用户可以轻松地在 Hugging Face Spaces 上试用特定的 ONNX 模型库模型,并运行由 Gradio 和 ONNX Runtime 提供支持的快速演示,所有这些都在云端进行,无需在本地下载任何内容。请注意,ONNX 有多种运行时,例如 ONNX RuntimeMXNet

ONNX Runtime 的作用是什么?

ONNX Runtime 是一个跨平台的推理和训练机器学习加速器。它使得在 Hugging Face 上使用 ONNX 模型库模型进行 Gradio 实时演示成为可能。

ONNX Runtime 推理可以提供更快的客户体验和更低的成本,支持来自 PyTorch 和 TensorFlow/Keras 等深度学习框架以及 scikit-learn、LightGBM、XGBoost 等经典机器学习库的模型。ONNX Runtime 与不同的硬件、驱动程序和操作系统兼容,并通过利用硬件加速器以及图优化和转换来提供最佳性能。欲了解更多信息,请参阅官方网站

为 EfficientNet-Lite4 设置 Gradio 演示

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()

如何使用 ONNX 模型在 HF spaces 上贡献 Gradio 演示

  • 将模型添加到 onnx 模型库
  • 在此处创建 Hugging Face 帐户这里
  • 查看要添加到 ONNX 组织的模型列表,请参阅包含模型列表的表格
  • 在您的用户名下添加 Gradio 演示,请参阅这篇关于在 Hugging Face 上设置 Gradio 演示的博客文章
  • 在此处申请加入 ONNX 组织这里
  • 一旦获得批准,将模型从您的用户名转移到 ONNX 组织
  • 在模型表中添加一个徽章,请参阅模型列表中的示例。
gradio