input: dynamic input is missing dimensions in profile

举报
风吹稻花香 发表于 2022/07/21 23:12:03 2022/07/21
【摘要】 input: dynamic input is missing dimensions in profile onnx2trt代码报错: import numpy as npimport tensorrt as trtimport osimport pycuda.driver as cudaimport argparse d...

input: dynamic input is missing dimensions in profile

onnx2trt代码报错:


  
  1. import numpy as np
  2. import tensorrt as trt
  3. import os
  4. import pycuda.driver as cuda
  5. import argparse
  6. def GiB(val):
  7. return val * 1 << 30
  8. def ONNX_build_engine(onnx_file_path, write_engine=True):
  9. # :return: engine
  10. G_LOGGER = trt.Logger(trt.Logger.WARNING)
  11. # 1、动态输入第一点必须要写的
  12. explicit_batch = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
  13. batch_size = 8 # trt推理时最大支持的batchsize
  14. with trt.Builder(G_LOGGER) as builder, builder.create_network(explicit_batch) as network, trt.OnnxParser(network,
  15. G_LOGGER) as parser:
  16. builder.max_batch_size = batch_size
  17. config = builder.create_builder_config()
  18. config.max_workspace_size = GiB(2)
  19. config.set_flag(trt.BuilderFlag.FP16)
  20. print('Loading ONNX file from path {}...'.format(onnx_file_path))
  21. with open(onnx_file_path, 'rb') as model:
  22. print('Beginning ONNX file parsing')
  23. parser.parse(model.read())
  24. print('Completed parsing of ONNX file')
  25. print('Building an engine from file {}; this may take a while...'.format(onnx_file_path))
  26. # 重点
  27. profile = builder.create_optimization_profile() # 动态输入时候需要 分别为最小输入、常规输入、最大输入
  28. # 有几个输入就要写几个profile.set_shape 名字和转onnx的时候要对应
  29. # tensorrt6以后的版本是支持动态输入的,需要给每个动态输入绑定一个profile,用于指定最小值,常规值和最大值,如果超出这个范围会报异常。
  30. profile.set_shape("input", (1, 3, 128, 128), (4, 3, 128, 128), (16, 3, 128, 128))
  31. config.add_optimization_profile(profile)
  32. engine = builder.build_engine(network, config)
  33. print("Completed creating Engine")
  34. # 保存engine文件
  35. if write_engine:
  36. engine_file_path = 'efficientnet_b1.trt'
  37. with open(engine_file_path, "wb") as f:
  38. f.write(engine.serialize())
  39. return engine
  40. onnx_file_path = r'skipnet_0712.onnx'
  41. onnx_file_path = r'model2.onnx'
  42. onnx_file_path = r'skip_simp2.onnx'
  43. # onnx_file_path = r'mobileone_0713.onnx'
  44. write_engine = True
  45. engine = ONNX_build_engine(onnx_file_path, write_engine)

原错误代码:

profile.set_shape("inputs", (1, 3, 240, 240), (8, 3, 240, 240), (16, 3, 480, 480))

改之后代码:

profile.set_shape("inputs", (1, 3, 128, 128), (8, 3, 128, 128), (16, 3, 128, 128))

文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/125903947

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。