自己的電腦做網(wǎng)站服務(wù)器嗎北京網(wǎng)站建設(shè)制作開(kāi)發(fā)
文章目錄
- 前言
- 一、使用 TVMC
- 二、獲得模型
- 三、將 ONNX 模型編譯到 TVM 運(yùn)行時(shí)中
- 四、TVMC 從編譯的模塊中運(yùn)行模型
- 4.1、輸入預(yù)處理
- 4.2 運(yùn)行已編譯的模塊
- 4.3 輸出后處理
前言
??在本節(jié)中,將使用 TVMC,即 TVM 命令行驅(qū)動(dòng)程序。TVMC 工具,它暴露了 TVM 的功能,如 auto-tuning、編譯、profiling 和通過(guò)命令行界面執(zhí)行模型。
在完成本節(jié)內(nèi)容后,將使用 TVMC 來(lái)完成以下任務(wù):
- 為 TVM 運(yùn)行時(shí)編譯預(yù)訓(xùn)練 ResNet-50 v2 模型。
- 通過(guò)編譯后的模型運(yùn)行真實(shí)圖像,并解釋輸出和模型的性能。
- 使用 TVM 在 CPU 上調(diào)優(yōu)模型。
- 使用 TVM 收集的調(diào)優(yōu)數(shù)據(jù)重新編譯優(yōu)化模型。
- 通過(guò)優(yōu)化后的模型運(yùn)行圖像,并比較輸出和模型的性能。
一、使用 TVMC
??TVMC 是 Python 應(yīng)用程序,是 TVM Python 軟件包的一部分。當(dāng)你使用 Python 包安裝 TVM 時(shí),你將得到 TVMC 作為命令行應(yīng)用程序,名為 tvmc。這個(gè)命令的位置將取決于你的平臺(tái)和安裝方法。
??另外,如果你在 $PYTHONPATH 上將 TVM 作為 Python 模塊,你可以通過(guò)可執(zhí)行的 python 模塊 python -m tvm.driver.tvmc 訪問(wèn)命令行驅(qū)動(dòng)功能。
??為簡(jiǎn)單起見(jiàn),將提到 TVMC 命令行使用 tvmc <options>
,但同樣的結(jié)果可以用 python -m tvm.driver.tvmc <options>
。
可以使用幫助頁(yè)面查看:
!python -m tvm.driver.tvmc --help
usage: tvmc [--config CONFIG] [-v] [--version] [-h]{micro,run,tune,compile} ...TVM compiler driveroptions:--config CONFIG configuration json file-v, --verbose increase verbosity--version print the version and exit-h, --help show this help message and exit.commands:{micro,run,tune,compile}micro select micro context.run run a compiled moduletune auto-tune a modelcompile compile a model.TVMC - TVM driver command-line interface
??tvmc 可用的 TVM 的主要功能來(lái)自子命令 compile 和 run,以及 tune。要了解某個(gè)子命令下的具體選項(xiàng),使用 tvmc <subcommand> --help
。將在本教程中逐一介紹這些命令,但首先需要下載預(yù)訓(xùn)練模型來(lái)使用。
二、獲得模型
??使用 ResNet-50 v2。ResNet-50 是卷積神經(jīng)網(wǎng)絡(luò),有 50 層深度,設(shè)計(jì)用于圖像分類。將使用的模型已經(jīng)在超過(guò)一百萬(wàn)張圖片上進(jìn)行了預(yù)訓(xùn)練,有 1000 種不同的分類。該網(wǎng)絡(luò)輸入圖像大小為 224x224。如果你有興趣探究更多關(guān)于 ResNet-50 模型的結(jié)構(gòu),建議下載 Netron,它免費(fèi)提供的 ML 模型查看器。
在本文中,將使用 ONNX 格式的模型。
!wget https://github.com/onnx/models/raw/main/vision/classification/resnet/model/resnet50-v2-7.onnx
--2022-04-26 13:07:52-- https://github.com/onnx/models/raw/main/vision/classification/resnet/model/resnet50-v2-7.onnx
Resolving github.com (github.com)... 20.205.243.166
Connecting to github.com (github.com)|20.205.243.166|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://media.githubusercontent.com/media/onnx/models/main/vision/classification/resnet/model/resnet50-v2-7.onnx [following]
--2022-04-26 13:07:53-- https://media.githubusercontent.com/media/onnx/models/main/vision/classification/resnet/model/resnet50-v2-7.onnx
Resolving media.githubusercontent.com (media.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.110.133, ...
Connecting to media.githubusercontent.com (media.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 102442450 (98M) [application/octet-stream]
Saving to: ‘resnet50-v2-7.onnx’resnet50-v2-7.onnx 100%[===================>] 97.70M 4.51MB/s in 25s 2022-04-26 13:08:27 (3.89 MB/s) - ‘resnet50-v2-7.onnx’ saved [102442450/102442450]
支持的模型格式
TVMC 支持用 Keras、ONNX、TensorFlow、TFLite 和 Torch創(chuàng)建的模型。如果你需要明確地提供你所使用的模型格式,請(qǐng)使用選項(xiàng)
--model-format
。
為 TVM 添加 ONNX 支持
TVM 依賴于你系統(tǒng)中的 ONNX python 庫(kù)。你可以使用
pip3 install --user onnx
onnxoptimizer
命令來(lái)安裝ONNX。如果你有 root 權(quán)限并且想全局安裝 ONNX,你可以去掉--user 選項(xiàng)。對(duì)
onnxoptimizer` 的依賴是可選的,僅用于 onnx>=1.9
三、將 ONNX 模型編譯到 TVM 運(yùn)行時(shí)中
??一旦下載了 ResNet-50 模型,下一步就是對(duì)其進(jìn)行編譯。為了達(dá)到這個(gè)目的,將使用 tvmc compile。從編譯過(guò)程中得到的輸出是模型的 TAR 包,它被編譯成目標(biāo)平臺(tái)的動(dòng)態(tài)庫(kù)??梢允褂?TVM 運(yùn)行時(shí)在目標(biāo)設(shè)備上運(yùn)行該模型。
# 這可能需要幾分鐘的時(shí)間,取決于你的機(jī)器
!python -m tvm.driver.tvmc compile --target "llvm" \--output resnet50-v2-7-tvm.tar \../../_models/resnet50-v2-7.onnx
One or more operators have not been tuned. Please tune your model for better performance. Use DEBUG logging level to see more details.
查看 tvmc compile 在 module 中創(chuàng)建的文件:
%%bash
mkdir model
tar -xvf resnet50-v2-7-tvm.tar -C model
mod.so
mod.json
mod.params
- mod.so 是模型,表示為 C++ 庫(kù),可以被 TVM 運(yùn)行時(shí)加載。
- mod.json 是 TVM Relay 計(jì)算圖的文本表示。
- mod.params 是包含預(yù)訓(xùn)練模型參數(shù)的文件。
指定正確的目標(biāo)(選項(xiàng) --target)可以對(duì)編譯后的模塊的性能產(chǎn)生巨大的影響,因?yàn)樗梢岳媚繕?biāo)上可用的硬件特性。
四、TVMC 從編譯的模塊中運(yùn)行模型
??TVMC 內(nèi)置了 TVM 運(yùn)行時(shí),允許你運(yùn)行編譯的 TVM 模型。為了使用 TVMC 來(lái)運(yùn)行模型并進(jìn)行預(yù)測(cè),需要兩樣?xùn)|西:
- 編譯后的模塊,剛剛生成出來(lái)。
- 對(duì)模型的有效輸入,以進(jìn)行預(yù)測(cè)。
??當(dāng)涉及到預(yù)期的張量形狀、格式和數(shù)據(jù)類型時(shí),每個(gè)模型都很特別。出于這個(gè)原因,大多數(shù)模型需要一些預(yù)處理和后處理,以確保輸入是有效的,并解釋輸出結(jié)果。TVMC 對(duì)輸入和輸出數(shù)據(jù)都采用了 NumPy 的 .npz 格式。這是得到良好支持的 NumPy 格式,可以將多個(gè)數(shù)組序列化為文件。
4.1、輸入預(yù)處理
??對(duì)于 ResNet-50 v2 模型,預(yù)期輸入是 ImageNet 格式的。下面是為 ResNet-50 v2 預(yù)處理圖像的腳本例子。你將需要安裝支持的 Python 圖像庫(kù)的版本。你可以使用 pip3 install --user pillow
來(lái)滿足腳本的這個(gè)要求
#!python ./preprocess.py
from tvm.contrib.download import download_testdata
from PIL import Image
import numpy as npimg_url = "https://s3.amazonaws.com/model-server/inputs/kitten.jpg"
img_path = download_testdata(img_url, "imagenet_cat.png", module="data")# Resize it to 224x224
resized_image = Image.open(img_path).resize((224, 224))
img_data = np.asarray(resized_image).astype("float32")# ONNX expects NCHW input, so convert the array
img_data = np.transpose(img_data, (2, 0, 1))# Normalize according to ImageNet
imagenet_mean = np.array([0.485, 0.456, 0.406])
imagenet_stddev = np.array([0.229, 0.224, 0.225])
norm_img_data = np.zeros(img_data.shape).astype("float32")
for i in range(img_data.shape[0]):norm_img_data[i, :, :] = (img_data[i, :, :] / 255 - imagenet_mean[i]) / imagenet_stddev[i]# Add batch dimension
img_data = np.expand_dims(norm_img_data, axis=0)# Save to .npz (outputs imagenet_cat.npz)
np.savez("imagenet_cat", data=img_data)
4.2 運(yùn)行已編譯的模塊
有了模型和輸入數(shù)據(jù),現(xiàn)在可以運(yùn)行 TVMC 來(lái)做預(yù)測(cè):
!python -m tvm.driver.tvmc run \--inputs imagenet_cat.npz \--output predictions.npz \resnet50-v2-7-tvm.tar
回顧一下, .tar 模型文件包括 C++ 庫(kù),對(duì) Relay 模型的描述,以及模型的參數(shù)。TVMC 包括 TVM 運(yùn)行時(shí),它可以加載模型并根據(jù)輸入進(jìn)行預(yù)測(cè)。當(dāng)運(yùn)行上述命令時(shí),TVMC 會(huì)輸出新文件,predictions.npz,其中包含 NumPy 格式的模型輸出張量。
4.3 輸出后處理
如前所述,每個(gè)模型都會(huì)有自己的特定方式來(lái)提供輸出張量。需要運(yùn)行一些后處理,利用為模型提供的查找表,將 ResNet-50 v2 的輸出渲染成人類可讀的形式。下面的腳本顯示了后處理的例子,從編譯的模塊的輸出中提取標(biāo)簽。運(yùn)行這個(gè)腳本應(yīng)該產(chǎn)生以下輸出
#!python ./postprocess.py
import os.path
import numpy as npfrom scipy.special import softmaxfrom tvm.contrib.download import download_testdata# Download a list of labels
labels_url = "https://s3.amazonaws.com/onnx-model-zoo/synset.txt"
labels_path = download_testdata(labels_url, "synset.txt", module="data")with open(labels_path, "r") as f:labels = [l.rstrip() for l in f]output_file = "predictions.npz"# Open the output and read the output tensor
if os.path.exists(output_file):with np.load(output_file) as data:scores = softmax(data["output_0"])scores = np.squeeze(scores)ranks = np.argsort(scores)[::-1]for rank in ranks[0:5]:print("class='%s' with probability=%f" % (labels[rank], scores[rank]))
class='n02123045 tabby, tabby cat' with probability=0.621104
class='n02123159 tiger cat' with probability=0.356378
class='n02124075 Egyptian cat' with probability=0.019712
class='n02129604 tiger, Panthera tigris' with probability=0.001215
class='n04040759 radiator' with probability=0.000262