Tensort之uff
# This sample uses a UFF MNIST model to create a TensorRT Inference Engine
from random import randint
from PIL import Image
import numpy as np import pycuda.driver as cuda
# This import causes pycuda to automatically manage CUDA context creation and cleanup.
import pycuda.autoinit import tensorrt as trt
import time import sys, os
sys.path.insert(1, os.path.join(sys.path[0], ".."))
import common # You can set the logger severity higher to suppress messages (or lower to display more messages).
TRT_LOGGER = trt.Logger(trt.Logger.WARNING) batch_size = 128 class ModelData(object):
MODEL_FILE = os.path.join(os.path.dirname(__file__), "model2/frozen_model.uff")
INPUT_NAME ="input_1"
INPUT_SHAPE = (3, 256, 256)
OUTPUT_NAME = 'predictions/Softmax'
DTYPE = trt.float32 def build_engine(model_file):
# For more information on TRT basics, refer to the introductory samples.
with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.UffParser() as parser:
builder.max_batch_size = batch_size
builder.max_workspace_size = common.GiB(1)
# Parse the Uff Network
parser.register_input(ModelData.INPUT_NAME, ModelData.INPUT_SHAPE)
parser.register_output(ModelData.OUTPUT_NAME)
parser.parse(model_file, network)
# Build and return an engine.
return builder.build_cuda_engine(network) # Loads a test case into the provided pagelocked_buffer.
def load_normalized_test_case(data_path, pagelocked_buffer, case_num=randint(0, 9)):
# test_case_path = os.path.join(data_path, str(case_num) + ".pgm")
# Flatten the image into a 1D array, normalize, and copy to pagelocked memory.
def normalize_image(image):
# Resize, antialias and transpose the image to CHW.
c, h, w = ModelData.INPUT_SHAPE
return np.asarray(image.resize((w, h), Image.ANTIALIAS)).transpose([2, 0, 1]).astype(trt.nptype(ModelData.DTYPE))
test_case_path = "lena.jpg"
img = normalize_image(Image.open(test_case_path))
img_array = []
for i in range(batch_size):
img_array.append(img)
img_array = np.array(img_array, dtype=trt.nptype(ModelData.DTYPE))
img_array = img_array.ravel()
np.copyto(pagelocked_buffer, img_array)
return case_num def main():
# data_path = common.find_sample_data(description="Runs an MNIST network using a UFF model file", subfolder="mnist")
data_path = "/home/bjxiangboren/tools/TensorRT-5.0.2.6/data/mnist/"
model_file = ModelData.MODEL_FILE # with open("inception_batch.engine", "rb") as f, trt.Runtime(TRT_LOGGER) as runtime:
# engine = runtime.deserialize_cuda_engine(f.read())
with build_engine(model_file) as engine:
# Build an engine, allocate buffers and create a stream.
# For more information on buffer allocation, refer to the introductory samples.
with open("inception_batch.engine", "wb") as f:
f.write(engine.serialize())
inputs, outputs, bindings, stream = common.allocate_buffers(engine)
with engine.create_execution_context() as context:
case_num = load_normalized_test_case(data_path, pagelocked_buffer=inputs[0].host)
# For more information on performing inference, refer to the introductory samples.
# The common.do_inference function will return a list of outputs - we only have one in this case.
while True:
start_time = time.time()
[output] = common.do_inference(context, bindings=bindings, inputs=inputs, outputs=outputs, stream=stream, batch_size=batch_size)
end_time = time.time()
print("time dis is %s" % (end_time - start_time))
# output = output.reshape((30,1001))
# print output
# print output.shape
# print np.argmax(output, axis=1)
# pred = np.argmax(output)
# print("Test Case: " + str(case_num))
# print("Prediction: " + str(pred)) if __name__ == '__main__':
main()
1、首先将pb转为uff格式的模型
python /usr/lib/python3.5/dist-packages/uff/bin/convert_to_uff.py --input_file models/lenet5.pb
2、使用trt engine加速
这个加速还是挺明显的,但转换后的模型无法使用tfservign了,只能用tensorrt自己的engine。
参考:https://devtalk.nvidia.com/default/topic/1044466/tensorrt/uff-inference-time-large-than-pb-time-when-process-vgg-19/
https://blog.csdn.net/zong596568821xp/article/details/86077553
https://blog.csdn.net/g11d111/article/details/92061884
https://mp.weixin.qq.com/s/Ps49ZTfJprcOYrc6xo-gLg?
Tensort之uff的更多相关文章
- TensorRT学习总结
TensorRT是什么 建议先看看这篇https://zhuanlan.zhihu.com/p/35657027 深度学习 训练 部署 平常自学深度学习的时候关注的更多是训练的部分,即得到一个模型.而 ...
- TensorRT Analysis Report分析报告
TensorRT Analysis Report 一.介绍 TensorRT是一个高性能的深度学习推理(Inference)优化器,可以为深度学习应用提供低延迟.高吞吐率的部署推理.TensorRT可 ...
- Nginx反向代理,负载均衡,redis session共享,keepalived高可用
相关知识自行搜索,直接上干货... 使用的资源: nginx主服务器一台,nginx备服务器一台,使用keepalived进行宕机切换. tomcat服务器两台,由nginx进行反向代理和负载均衡,此 ...
- Android Studio开发RecyclerView遇到的各种问题以及解决(一)
以前一直在用ListView,,,最近才看RecyclerView发现好强大.RecyclerView前提是Android版本在5.0以上,本人以前用的是eclipse只支持到4.4.索性就安装一个A ...
- java web学习总结(三十) -------------------JSTL表达式
一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 二.JSTL标签库的分类 核心 ...
- javascript代码 调试方法
你的代码可能包含语法错误,逻辑错误,如果没有调试工具,这些错误比较难于发现. 通常,如果 JavaScript 出现错误,是不会有提示信息,这样你就无法找到代码错误的位置. 在程序代码中寻找错误叫做代 ...
- Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解
方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图 ...
- 多线程之互斥锁(By C++)
首先贴一段win32API实现的多线程的代码,使用CreateThread实现,如果不要传参数,就把第四个参数设为NULL #include<Windows.h> #include< ...
- MySQL数据库的安装与密码配置
MySQL是由MySQL AB公司开发,后由Oracle公司收购 MySQL是一个关系型数据库管理系统 分为社区版和企业版 ...
随机推荐
- 1.2.1 Maven到底是什么鬼
解释之前,提1个小问题. 1.1.假如你正在Eclipse下开发两个Java项目,姑且把它们称为A.B,其中A项目中的一些功能依赖于B项目中的某些类,那么如何维系这种依赖关系的呢? 很简单,这不就是跟 ...
- SQL server中的一些查询
SQL 不同于与其他编程语言的最明显特征是处理代码的顺序.在大数编程语言中,代码按编码顺序被处理,但是在SQL语言中,第一个被处理的子句是FROM子句,尽管SELECT语句第一个出现,但是几乎总是最后 ...
- Lombok 学习指南
转自:https://segmentfault.com/a/1190000020864572 一.Lombok 简介 Lombok 是一款 Java 开发插件,使得 Java 开发者可以通过其定义的一 ...
- 阶段3 2.Spring_05.基于XML的IOC的案例1_4 注解IOC案例-把自己编写的类使用注解配置
注解改造案例 复制之前的xml配置的pom.xml里面的依赖. 复制com文件 bean.xml配置文件也拷贝过来 测试类也复制过来 开始基于注解的IOC配置 右键项目,选择maven.选择更新 更新 ...
- 使用PowerShell远程连接WinServer
最近做一个项目后台,涉及到多台服务器,当程序更新的时候,由于用的是WinServer,无法像Linux使用SSH批量更新,用Windows的mstsc的远程一个一个连接又太麻烦了.查找了一下资料,发现 ...
- 如何将txt文件转换为带章节目录的mobi文件
txt文件基本没什么排版可言.所以想要把txt转换为mobi文件方便阅读. 具体步骤如下: 打开txt 用notepad++打开所需要转换的txt文件.(或者使用其他的能够支持正则表达式的编辑器). ...
- mingw32环境上静态编译 dav1d 0.4.0
2019-08-05 发布 dav1d 0.4.0 dav1d 'Cheetah', the fast and small AV1 decoder This is the fourth major r ...
- svn导出项目到myeclipse,运行报ClassNotFoundException
一开始以为是 这样的svn导出项目到myeclipse,运行报ClassNotFoundException 后来不行 又看了一下 还不行 以为是这样的MyEclipse2014报错java.lang ...
- GO——beego简单开发实例(二)
在新建项目成功之后我们可以做一个简单的动态增删查改. 1.在models文件夹下新建models.go,根据模型新建表 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...
- 英特尔® Open Image Denoise 库有助于节省时间,提高质量
在使用光线跟踪增强真实性与沉浸感时,游戏开发人员面临复杂的权衡.为了克服一系列挑战,英特尔创建了使用光线跟踪进行图像渲染的一整套解决方案,包含高性能开源滤波器.该解决方案已发布测试版,被集成至 Uni ...