Gradio 月活跃用户突破 100 万!

阅读更多
Gradio logo
  1. 其他教程
  2. Gradio 和 Hugging Face 上的 ONNX

相关 Spaces

Gradio 和 Hugging Face 上的 ONNX

简介

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

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

这是一个 ONNX 模型的示例

什么是 ONNX 模型动物园?

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

ONNX 模型动物园是由社区成员贡献的 ONNX 格式的预训练、最先进模型的集合。每个模型都附带有 Jupyter 笔记本,用于模型训练和使用训练模型运行推理。这些笔记本是用 Python 编写的,并包含指向训练数据集的链接以及对描述模型架构的原始论文的引用。

什么是 Hugging Face Spaces 和 Gradio?

Gradio

Gradio 让用户可以通过 Python 代码将他们的机器学习模型演示为 Web 应用程序。Gradio 将 Python 函数包装到用户界面中,并且演示可以在 Jupyter 笔记本、Colab 笔记本中启动,以及嵌入到您自己的网站中并在 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 模型

Hugging Face Model Hub 也支持 ONNX 模型,并且可以通过 ONNX 标签筛选 ONNX 模型

Hugging Face 是如何帮助 ONNX 模型动物园的?

ONNX 模型动物园中有很多 Jupyter Notebook,供用户测试模型。 以前,用户需要自行下载模型并在本地运行这些 notebook 进行测试。 借助 Hugging Face,测试过程可以变得更加简单和用户友好。 用户可以轻松地在 Hugging Face Spaces 上尝试某些 ONNX 模型动物园模型,并运行由 Gradio 和 ONNX Runtime 驱动的快速演示,所有操作都在云端完成,无需在本地下载任何内容。 请注意,ONNX 有多种运行时,例如 ONNX Runtime, MXNet

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 组织
  • 在模型表格中为模型添加徽章,请参阅模型列表中的示例