TVM yolov3优化代码修改(编译运行OK)
yolov3_quantize_sample.py






附https://github.com/makihiro/tvm_yolov3_sample代码:
yolov3_quantize_sample.py
import nnvm
import nnvm.frontend.darknet
import nnvm.testing.yolo_detection
import nnvm.testing.darknet
import matplotlib.pyplot as plt
import numpy as np
import tvm
from tvm import rpc
import sys
import cv2
import time

from ctypes import *
from tvm.contrib import util
from tvm.contrib.download import download
from nnvm.testing.darknet import __darknetffi__# Model name
MODEL_NAME = 'yolov3'######################################################################
# Download required files
# -----------------------
# Download cfg and weights file if first time.
CFG_NAME = MODEL_NAME + '.cfg'
WEIGHTS_NAME = MODEL_NAME + '.weights'
REPO_URL = 'https://github.com/siju-samuel/darknet/blob/master/'
CFG_URL = REPO_URL + 'cfg/' + CFG_NAME + '?raw=true'
WEIGHTS_URL = 'https://pjreddie.com/media/files/' + WEIGHTS_NAMEdownload(CFG_URL, CFG_NAME)
download(WEIGHTS_URL, WEIGHTS_NAME)# Download and Load darknet library
if sys.platform in ['linux', 'linux2']:DARKNET_LIB = 'libdarknet2.0.so'DARKNET_URL = REPO_URL + 'lib/' + DARKNET_LIB + '?raw=true'
elif sys.platform == 'darwin':DARKNET_LIB = 'libdarknet_mac2.0.so'DARKNET_URL = REPO_URL + 'lib_osx/' + DARKNET_LIB + '?raw=true'
else:err = "Darknet lib is not supported on {} platform".format(sys.platform)raise NotImplementedError(err)download(DARKNET_URL, DARKNET_LIB)DARKNET_LIB = __darknetffi__.dlopen('./' + DARKNET_LIB)
cfg = "./" + str(CFG_NAME)
weights = "./" + str(WEIGHTS_NAME)
net = DARKNET_LIB.load_network(cfg.encode('utf-8'), weights.encode('utf-8'), 0)
dtype = 'float32'
batch_size = 1print("Converting darknet to nnvm symbols...")
sym, params = nnvm.frontend.darknet.from_darknet(net, dtype)######################################################################
# Compile the model on NNVM
# -------------------------
# compile the model
local = Trueif local:target = 'llvm'ctx = tvm.cpu(0)
else:target = 'cuda'ctx = tvm.gpu(0)data = np.empty([batch_size, net.c, net.h, net.w], dtype)
shape = {'data': data.shape}dtype_dict = {}# convert nnvm to relay
print("convert nnvm symbols into relay function...")
from nnvm.to_relay import to_relay
func, params = to_relay(sym, shape, 'float32', params=params)
# optimization
print("optimize relay graph...")
with tvm.relay.build_config(opt_level=2):func = tvm.relay.optimize(func, target, params)
# quantize
print("apply quantization...")
from tvm.relay import quantize
with quantize.qconfig():func = quantize.quantize(func, params)# Relay build
print("Compiling the model...")
print(func.astext(show_meta_data=False))
with tvm.relay.build_config(opt_level=3):graph, lib, params = tvm.relay.build(func, target=target, params=params)# Save the model
tmp = util.tempdir()
lib_fname = tmp.relpath('model.tar')
lib.export_library(lib_fname)# NNVM
# with nnvm.compiler.build_config(opt_level=2):
#     graph, lib, params = nnvm.compiler.build(sym, target, shape, dtype_dict, params)[neth, netw] = shape['data'][2:]  # Current image shape is 608x608
######################################################################
# Execute on TVM Runtime
# ----------------------
# The process is no different from other examples.
from tvm.contrib import graph_runtimeif local:remote = rpc.LocalSession()ctx = remote.cpu(0)
else:# The following is my environment, change this to the IP address of your target devicehost = 'localhost'port = 9090remote = rpc.connect(host, port)ctx = remote.gpu(0)# upload the library to remote device and load it
remote.upload(lib_fname)
rlib = remote.load_module('model.tar')# create the remote runtime module
m = graph_runtime.create(graph, rlib, ctx)
m.set_input(**params)
thresh = 0.5
nms_thresh = 0.45
coco_name = 'coco.names'
coco_url = 'https://github.com/siju-samuel/darknet/blob/master/data/' + coco_name + '?raw=true'
font_name = 'arial.ttf'
font_url = 'https://github.com/siju-samuel/darknet/blob/master/data/' + font_name + '?raw=true'
download(coco_url, coco_name)
download(font_url, font_name)with open(coco_name) as f:content = f.readlines()names = [x.strip() for x in content]# test image demo
test_image = 'dog.jpg'
print("Loading the test image...")
img_url = 'https://github.com/siju-samuel/darknet/blob/master/data/' + \test_image + '?raw=true'
download(img_url, test_image)data = nnvm.testing.darknet.load_image(test_image, netw, neth)
# set inputs
m.set_input('data', tvm.nd.array(data.astype(dtype)))
# execute
print("Running the test image...")m.run()
# get outputs
tvm_out = []
for i in range(3):layer_out = {}layer_out['type'] = 'Yolo'# Get the yolo layer attributes (n, out_c, out_h, out_w, classes, total)layer_attr = m.get_output(i*4+3).asnumpy()layer_out['biases'] = m.get_output(i*4+2).asnumpy()layer_out['mask'] = m.get_output(i*4+1).asnumpy()out_shape = (layer_attr[0], layer_attr[1] // layer_attr[0],layer_attr[2], layer_attr[3])layer_out['output'] = m.get_output(i*4).asnumpy().reshape(out_shape)layer_out['classes'] = layer_attr[4]tvm_out.append(layer_out)img = nnvm.testing.darknet.load_image_color(test_image)
_, im_h, im_w = img.shape
dets = nnvm.testing.yolo_detection.fill_network_boxes((netw, neth), (im_w, im_h), thresh,1, tvm_out)
last_layer = net.layers[net.n - 1]
nnvm.testing.yolo_detection.do_nms_sort(dets, last_layer.classes, nms_thresh)
nnvm.testing.yolo_detection.draw_detections(img, dets, thresh, names, last_layer.classes)plt.imshow(img.transpose(1, 2, 0))
plt.show()# video demo
video_demo = False
if video_demo:#vcap = cv2.VideoCapture("video.mp4")vcap = cv2.VideoCapture(0)n_frames = 0seconds = 0.0fps = 0.0while True:# Start timestart = time.time()# Capture frame-by-framen_frames = n_frames + 1ret, frame = vcap.read()img = np.array(frame)img = img.transpose((2, 0, 1))img = np.divide(img, 255.0)img = np.flip(img, 0)data = nnvm.testing.darknet._letterbox_image(img, netw, neth)# set inputsm.set_input('data', tvm.nd.array(data.astype(dtype)))# executeprint("Running the test image...")m.run()# get outputstvm_out = []#tvm_output_list = []# for i in range(0, 3):#     tvm_output = m.get_output(i)#     tvm_output_list.append(tvm_output.asnumpy())#print(tvm_output_list)#print(m.get_num_outputs())#layer_attr = [m.get_output(i).asnumpy() for i in range(m.get_num_outputs())]for i in range(3):layer_out = {}layer_out['type'] = 'Yolo'# Get the yolo layer attributes (n, out_c, out_h, out_w, classes, total)layer_attr = m.get_output(i*4+3).asnumpy()layer_out['biases'] = m.get_output(i*4+2).asnumpy()layer_out['mask'] = m.get_output(i*4+1).asnumpy()out_shape = (layer_attr[0], layer_attr[1] // layer_attr[0],layer_attr[2], layer_attr[3])layer_out['output'] = m.get_output(i*4).asnumpy().reshape(out_shape)layer_out['classes'] = layer_attr[4]tvm_out.append(layer_out)_, im_h, im_w = img.shapedets = nnvm.testing.yolo_detection.fill_network_boxes((netw, neth), (im_w, im_h), thresh,1, tvm_out)last_layer = net.layers[net.n - 1]nnvm.testing.yolo_detection.do_nms_sort(dets, last_layer.classes, nms_thresh)nnvm.testing.yolo_detection.draw_detections(img, dets, thresh, names, last_layer.classes)# End timeend = time.time()# Time elapsedseconds = (end - start)# Calculate frames per secondfps = (fps + (1 / seconds)) / 2print(fps)cv2.putText(img, str(fps), (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)cv2.imshow('Video', img.transpose(1, 2, 0))#cv2.waitKey(3)# Press Q to stop!if cv2.waitKey(1) & 0xFF == ord('q'):breakcv2.destroyAllWindows()

参考链接:
https://github.com/makihiro/tvm_yolov3_sample

TVM yolov3优化代码修改(编译运行OK)相关推荐

  1. 如何用vscode实现c语言代码快速编译运行(适合初学者)

    目录 1.下载mingw64 2.配置环境变量 3.打开Vscode安装code runner扩展 本人刚开始学习C语言,发现Dev-c++纵然编译运行方便,但其代码联想功不够智能,界面也非常单调.在 ...

  2. vs qt 在linux运行,QT安装以及使用(QT支持linux和windows,也支持C/C++代码的编译运行,比vs简洁多)...

    Windows:linux 0. QT Version qt-win-opensource-4.7.4-mingw qt-creator-win-opensource-2.4.1 1. 系统 Wind ...

  3. Windows系统使用VScode开发golang程序,远程连接Ubuntu系统编辑代码并编译运行

    文章目录 Windows系统配置Linux golang开发环境 VMware安装 VMware安装Ubuntu无桌面版 Ubuntu系统下配置golang开发环境 配置golang环境 配置git流 ...

  4. Linux环境中Visual Studio Code的配置使用----编译运行C/C++(良心教程)

    之前的博文分享了下载安装[VS code]的详细教程, 有需要速戳–>Linux环境中Visual Studio Code 安装配置及其卸载(详细教程) 本篇博文分享本人初次使用[VS code ...

  5. QQ音乐Android端120万行代码,编译耗时是怎样优化的,凭借这份《数据结构与算法》核心文档

    在本文的后续内容中,将介绍几个重点模块的实现. 5. 核心原理 代码编译 (1)获取改动文件并进行编译 首先需要考虑的问题是,如何识别出用户改动了哪些文件? 我们的做法是,在每次编译成功后,收集所有工 ...

  6. C语言编译运行代码的过程

    源程序是指未经编译的,按照一定的程序设计语言规范书写的,人类可读的文本文件,源程序就是所写好的代码.可执行程序,即常说的.exe程序,可以执行程序,完成计算机功能.在C语言中,.c文件就是所谓的源文件 ...

  7. deepin终端编译c程序_C/C++知识点之Ubuntu / Debian / Deepin等 Sublime Text 3 配置C++环境(一键编译运行,格式化代码)...

    本文主要向大家介绍了 C/C++知识点之Ubuntu / Debian / Deepin等 Sublime Text 3 配置C++环境(一键编译&运行,格式化代码),通过具体的内容向大家展示 ...

  8. 一套代码编译出ios和android,Hippy: Hippy 是一个新生的跨端开发框架,目标是使开发者可以只写一套代码就直接运行于三个平台(iOS、Android 和 Web)...

    Hippy 跨端开发框架 介绍 Hippy 是一个新生的跨端开发框架,目标是使开发者可以只写一套代码就直接运行于三个平台(iOS.Android 和 Web).Hippy 的设计是面向传统 Web 开 ...

  9. 家用电器用户行为分析与事件识别代码详解+修改后运行无误的代码

    运行环境: ubuntu16.04 64位 pycharm python3.5.2 相关软件列表: cycler (0.10.0) graphviz (0.7.1) h5py (2.7.0) Kera ...

最新文章

  1. 关于校验规则(Validation Rule)和业务规则(Bussiness Rule)的思考
  2. A Common Framework for Interactive Texture Transfer(CVPR 2018)学习笔记
  3. python安装pygame模块_windows下 python 如何安装pygame模块
  4. axios+vue实现动态渲染员工数据+数据是对象
  5. 针对于lvs分发mysql的监控
  6. html5中本地存储概念是什么?
  7. (day 53 - 动态规划 ) 剑指 Offer 63. 股票的最大利润
  8. anaconda3.6.5安装pyhive
  9. 打垮你的永远不是压力,而是选择的能力!
  10. 微信小程序后端系统CMS开发笔记--04
  11. 最新【2021.1.28】今日头条_signature 分析
  12. Kernel:里的某某某;xxx
  13. 【金融财经】金融市场一周简报(2017-09-15)
  14. MPI_Bcast与MPI_Comm_split配合,实现行广播或列广播
  15. VBS:基本输出函数
  16. APP:校园网登录app—中小南—源码简析
  17. 《白帽子讲Web安全 》 随手记(一)
  18. php 搜索引擎包含哪些技术,浅谈三大搜索引擎爬虫性感 B-G-B
  19. IDEA的使用:4.IntelliJ IDEA的激活
  20. 学习Swift:经验丰富的开发人员指南

热门文章

  1. 2022-2028年中国汽车用胶管行业市场深度分析及投资前景趋势报告
  2. Linux shell 学习笔记(11)— 理解输入和输出(标准输入、输出、错误以及临时重定向和永久重定向)
  3. javascript的知识总结
  4. java局部变量全局变量,实例变量的理解
  5. super(Student,self).__init__()初始化的是什么东西?
  6. 高并发下的接口幂等性解决方案
  7. LeetCode简单题之将所有数字用字符替换
  8. 新材料,比钢硬一倍,但重量只有钢1/6
  9. AI芯片结构目标图形处理
  10. 5G和AI机器人平台为工业4.0和无人机提供服务