yuefanhao / SuperPoint-SuperGlue-TensorRT

SuperPoint and SuperGlue with TensorRT. Deploy with C++.
Apache License 2.0
233 stars 44 forks source link

convert onnx to engine in python doesn't work #6

Closed song-GAO-1992 closed 1 year ago

song-GAO-1992 commented 1 year ago

Hi, i rewrite the superglue serialization part referring your code in python. Although the engine finally generated, the inference result is wrong. I have no idea how to fix it. Could you please give a hand. Tks. my environment: cuda: 11.1 tensorRT: 8.4.3.1 below is my code:


import tensorrt as trt
import cuda.ccudart as cudart
import cuda.cuda as cuda

onnx_path = 'superglue_indoor_folded.onnx'

logger = trt.Logger(trt.Logger.WARNING) # create a trt logger

builder = trt.Builder(logger)   # create a builder

network = builder.create_network(1 << 
        int(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)) # create a network

config = builder.create_builder_config()    # build a config
# set up configs
config.set_memory_pool_limit(trt.MemoryPoolType.WORKSPACE, 1 << 30) 
# config.set_flag(trt.BuilderFlag.FP16)
config.profiling_verbosity=trt.ProfilingVerbosity.DETAILED
print(config.profiling_verbosity)

# create optimization profile()
profile = builder.create_optimization_profile()
profile.set_shape("keypoints_0",(1,1,2),(1,512,2),(1,1024,2))
profile.set_shape("keypoints_1",(1,1,2),(1,512,2),(1,1024,2))
profile.set_shape("scores_0",(1,1),(1,512),(1,1024))
profile.set_shape("scores_1",(1,1),(1,512),(1,1024))
profile.set_shape("descriptors_0",(1,256,1),(1,256,512),(1,256,1024))
profile.set_shape("descriptors_1",(1,256,1),(1,256,512),(1,256,1024))
config.add_optimization_profile(profile)    # add profile to config

# parse onnx to trt
parser = trt.OnnxParser(network, logger)
success = parser.parse_from_file(onnx_path)
print("Parse onnx to engine success")
for idx in range(parser.num_errors):
    print(parser.get_error(idx))
if not success:
    pass

err, stream = cuda.cuStreamCreate(0)
assert(err == cuda.CUresult.CUDA_SUCCESS)
config.profile_stream = stream

# serialize the network
plan = builder.build_serialized_network(network,config)

runtime = trt.Runtime(logger)
engine = runtime.deserialize_cuda_engine(plan)
engine = engine.serialize()

if engine is None:
    print("[error] serialize engine failed!")

with open("superglue_indoor_folded.engine",'wb') as f:
    f.write(engine)
    print("output serialized engine.")
song-GAO-1992 commented 1 year ago

solved! Change a higher version tensorrt can solve this problem.

WJJLBJ commented 3 months ago

my tensorrt version is 8.0.1 and i also get wrong matching result . May I ask you do you get correct result only by changing a higher version of tensorrt? thanks

song-GAO-1992 commented 3 months ago

@WJJLBJ it's have been a while. I think you can upgrade your tensorRT and give it a shot.