1、編譯需要打開的選項:
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),觀山湖企業(yè)網(wǎng)站建設(shè),觀山湖品牌網(wǎng)站建設(shè),網(wǎng)站定制,觀山湖網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,觀山湖網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。
set(USE_SORT ON)
參考資料:
discuss.tvm.ai/t/solved-cant-run-tutorials-ssd-model-on-my-own-cpu/2005
2、編譯gpu模型:
編譯時,打開編譯cuda選項:tvm_option(USE_CUDA "Build with CUDA" ON)
在jetson nano上編譯GPU版本時,需要將cuda加入到環(huán)境變量里面去:
export CUBA_HOME=/usr/local/cuda-10.0:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64:$LD_LIBRARY_PATH
export PATH=/usr/local/cuda-10.0/bin:$PATH
將target = tvm.target.create("llvm -mcpu=haswell")替換為:target = "cuda"
參考資料:
github.com/
3、Andorid編譯
cp make/config.mk
APP_ABI = armeabi-v7a
./make_standalone_toolchain.py –arch arm --api 23 --install-dir /opt/android-toolchain-armv7 -mfloat-abi=soft
參考資料:
discuss.tvm.ai
4、LLVM 在windows上編譯
1)下載LLVM源碼
首先下載LLVM源碼,下載地址為:
github.com/MirrorYuChen/llvm-project/tree/release/6.x
這里對應(yīng)LLVM版本為6.x,后面需要用LLD工具,這個源碼里面就自帶有,然后在LLVM文件夾下面新建一個build文件夾,并在此文件夾路徑下打開cmd窗口,輸入如下命令:
cmake -G "Visual Studio 15 2017 Win64" .. -Thost=x64 -DLLVM_ENABLE_PROJECTS=lld
打開生成的llvm.sln項目,切換到release x64模式編譯,大約需要1小時時間編譯完成,并運行install。
Jetson nano:
>> git clone https://github.com/llvm/llvm-project llvm-project
>> cd llvm-project
>> mkdir build
>> cd build
>> cmake -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS=lld -DCMAKE_INSTALL_PREFIX=/usr/local ../../llvm-project/llvm
>> make -j3 && make install
2)下載tvm源碼
git clone --recursive https://github.com/dmlc/tvm/
在tvm項目路徑下新建build子文件夾,并在當前路徑下新建一個bash.sh文件,文件內(nèi)容為:
cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CONFIGURATION_TYPES="Release" .. \
-DLLVM_DIR=D:\softW\LLVM\lib\cmake\llvm
后面LLVM路徑對應(yīng)到剛install生成的LLVM路徑,打開生成的tvm.sln項目,編譯運行。
3)安裝
先新建一個conda環(huán)境變量:
conda create -n tf python==3.5
激活環(huán)境:
activate tf
分別安裝tensorflow和mxnet
pip install tensorflow
pip install mxnet
分別進入tvm、topi、nnvm文件夾下,運行下面命令進行安裝
python setup.py install
安裝完成之后,可以進入tvm的tutorials子文件夾下,運行相關(guān)例程。
這里是一個ssd運行例程:
測試代碼為:
#!/usr/bin/python3
import os
import tvm
import numpy as np
import time
from tvm.contrib.download import download
from tvm.contrib import graph_runtime
current_milli_time = lambda: int(round(time.time() * 1000))
test_image = "dog.jpg"
dshape = (1, 3, 512, 512)
#dshape = (1, 3, 608, 608)
dtype = "float32"
image_url = "/upload/otherpic49/113691.jpg"
download(image_url, test_image)
# Preprocess image
import cv2
test_image_path = test_image
image = cv2.imread(test_image_path)
img_data = cv2.resize(image, (dshape[2], dshape[3]))
img_data = img_data[:, :, (2, 1, 0)].astype(np.float32)
img_data -= np.array([123, 117, 104])
img_data = np.transpose(np.array(img_data), (2, 0, 1))
ctx = tvm.cpu()
target="llvm"
#base = "deploy_ssd_resnet50_512/{}/".format(target)
#base = "deploy_ssd_inceptionv3_512/{}/".format(target)
#base = "deploy_ssd_mobilenet_512/{}/".format(target)
#base = "deploy_ssd_mobilenet_608/{}/".format(target)
#base = "cpu-model/"
base = "./"
path_lib = base + "model.so"
path_graph = base + "model.json"
path_param = base + "model.params"
graph = open(path_graph).read()
params = bytearray(open(path_param, "rb").read())
lib = tvm.module.load(path_lib)
class_names = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair",
"cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant",
"sheep", "sofa", "train", "tvmonitor"]
######################################################################
# Create TVM runtime and do inference
# Build TVM runtime
m = graph_runtime.create(graph, lib, ctx)
m.load_params(params)
input_data = tvm.nd.array(img_data.astype(dtype))
# dryrun
m.run(data = input_data)
# execute
t1 = current_milli_time()
m.run(data = input_data)
# get outputs
tvm_output = m.get_output(0)
t2 = current_milli_time()
print(base)
print("time: {} ms".format(t2 - t1))
out = tvm_output.asnumpy()[0]
i = 0無錫×××醫(yī)院 https://yyk.familydoctor.com.cn/20612/
for det in out:
cid = int(det[0])
if cid < 0:
continue
score = det[1]
if score < 0.5:
continue
i += 1
print(i, class_names[cid], det)
######################################################################
# Display result
def display(img, out, thresh=0.5):
import random
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['figure.figsize'] = (10, 10)
pens = dict()
plt.clf()
plt.imshow(img)
for det in out:
cid = int(det[0])
if cid < 0:
continue
score = det[1]
if score < thresh:
continue
if cid not in pens:
pens[cid] = (random.random(), random.random(), random.random())
scales = [img.shape[1], img.shape[0]] * 2
xmin, ymin, xmax, ymax = [int(p * s) for p, s in zip(det[2:6].tolist(), scales)]
rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False,
edgecolor=pens[cid], linewidth=3)
plt.gca().add_patch(rect)
text = class_names[cid]
plt.gca().text(xmin, ymin-2, '{:s} {:.3f}'.format(text, score),
bbox=dict(facecolor=pens[cid], alpha=0.5),
fontsize=12, color='white')
plt.show()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
display(image, tvm_output.asnumpy()[0], thresh=0.45)
代碼來自于github