溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

tvm學習筆記之編譯安裝

發布時間:2020-07-20 15:15:33 來源:網絡 閱讀:1270 作者:nineteens 欄目:編程語言

  1、編譯需要打開的選項:

  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加入到環境變量里面去:

  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

  這里對應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文件,文件內容為:

  cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Release \

  -DCMAKE_CONFIGURATION_TYPES="Release" .. \

  -DLLVM_DIR=D:\softW\LLVM\lib\cmake\llvm

  后面LLVM路徑對應到剛install生成的LLVM路徑,打開生成的tvm.sln項目,編譯運行。

  3)安裝

  先新建一個conda環境變量:

  conda create -n tf python==3.5

  激活環境:

  activate tf

  分別安裝tensorflow和mxnet

  pip install tensorflow

  pip install mxnet

  分別進入tvm、topi、nnvm文件夾下,運行下面命令進行安裝

  python setup.py install

  安裝完成之后,可以進入tvm的tutorials子文件夾下,運行相關例程。

  這里是一個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 = "https://cache.yisu.com/upload/information/20200310/57/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無錫×××醫院 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


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

tvm
AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女