Use TensorRT to accelerate inference speed in Nvidia Jetson AGX Xavier

Confirm that you have installed the environment needed:
My env: torch2.0.0 torchvision0.15.1 tensorrt8.5.2 CUDA:11.4 onnxruntime_gpu 1.12.1
First,prepare your model and datasets:

device = torch.device('cuda')
model = ResNet18().eval().cuda()
check_point = torch.load('models/model-epoch109.pt', map_location=device)
model.load_state_dict(check_point)
print('ok')
test_dataset = torchvision.datasets.CIFAR10(
    root='./data',
    train=False,
    download=True,
    transform=transforms.Compose([
        transforms.ToTensor(),
    ])
)
batch=100
testloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch, shuffle=False, num_workers=2)

Next, convert your pytorch model to ONNX model:

def to_onnx(model):
    dummy_input = torch.randn(1,3,32,32)   # input sample
    input_names=['input']
    output_names=['output']
    dynamic_axes = {'input':{0:"batch_size"},'output':{0:"batch_size"}}
    torch.onnx.export(model, dummy_input, 'res18Z.onnx', export_params=True, opset_version=11, do_constant_folding=True,input_names=input_names,output_names=output_names,dynamic_axes=dynamic_axes)

Then, use trtexec to convert the onnx model to TensorRT engine file:
you should jump to the path of trtexec:

cd /usr/src/tensorrt/bin

Then execute the following command:

./trtexec --onnx=your_onnx_file_path/res18Z.onnx --saveEngine=your_save_path/res18Z.trt  --workspace=4096 --shapes=input:100x3x32x32

ATTENTION: the --shape must follow your input strictly(eg. B,C,H,W),and shouldn’t be ignored.
Finally,we just use the trt file to run tensorrt file:

def load_engine(trt_runtime, eng_path):
    with open(eng_path, 'rb') as f:
        eng_data = f.read()
    return trt_runtime.deserialize_cuda_engine(eng_data)

def test_trt():
    TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
    trt_runtime = trt.Runtime(TRT_LOGGER)
    engine = load_engine(trt_runtime, 'res18Z.trt')
    context = engine.create_execution_context()
    # allocate buffer of in and output
    input_shape = (batch, 3, 32, 32)
    output_shape = (batch, 10)
    ins = int(np.prod(input_shape) * np.dtype(np.float32).itemsize)
    outs = int(np.prod(output_shape) * np.dtype(np.float32).itemsize)
    print(ins, outs)
    d_input = cuda.mem_alloc(ins)
    d_output = cuda.mem_alloc(outs)
    acc = 0
    for images, labels in tqdm(testloader):
        input_data = images.numpy()
        labels = labels.numpy()
        # print(input_data.shape)
        input_data = np.ascontiguousarray(input_data)
        output_data = np.empty(output_shape, dtype=np.float32)
        cuda.memcpy_htod(d_input, input_data)  # to gpu
        # print(input_data)
        context.execute_v2(bindings=[int(d_input), int(d_output)])
        # print(output_data)
        cuda.memcpy_dtoh(output_data, d_output)  # to cpu
        # print(output_data)
        pred = output_data.argmax(axis=1)
        # print(pred)
        acc += sum(pred==labels)   #batch_size should be divided by the total loader_len
        # print(acc)

    print(acc / 10000)

That’s all.The biggest problem is still environment config. Oh, If you ask me why I wrote it in English, because I edit the blog on the Jetson AGX, There is no Chinese input method.

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: ClassSR是一种通过数据特征来加速超分辨率网络的通用框架。该框架可以根据不同的数据特征,自动选择合适的网络结构和超分辨率算法,从而提高超分辨率的效率和精度。同时,ClassSR还可以通过数据增强和模型蒸馏等技术,进一步提升超分辨率的性能。总之,ClassSR是一种非常实用的超分辨率技术,可以广泛应用于图像和视频处理领域。 ### 回答2: classsr: a general framework to accelerate super-resolution networks by data characteristic 是一个用于加速超分辨率网络的通用框架,通过对数据特征进行分析和处理,提高了超分辨率网络的训练效率和超分辨率图像的质量。 在超分辨率图像的生成中,一般采用深度学习的方法。但是传统的超分辨率网络存在训练速度慢、参数量多、生成的图像模糊等问题。classsr 认为这些问题是因为原始数据的特征并没有在训练过程中得到充分利用所导致的。 为了解决这些问题,classsr 提出了一种新的训练框架,它能够利用数据特征来加速训练过程和提高超分辨率图像的质量。具体来说,classsr 框架主要包含以下两个部分: 1. 特征提取的方法:classsr 采用了自适应卷积核技术,可以根据原始数据的纹理和结构特征,动态生成不同大小和形状的卷积核,从而提取更加准确的特征信息。 2. 数据特征的建模:classsr 通过分析数据的结构和特点,建立了一种数据特征模型,可以自动学习数据的统计特性。然后,利用这些数据特征来指导网络的训练,使得网络更快更准确地收敛。 总体来说,classsr 框架可以有效提高超分辨率网络的训练效率和超分辨率图像的质量。未来,该框架还能够为其他图像处理任务提供新的思路和方法。 ### 回答3: classsr是一种通用的框架,用于通过数据特性加速超分辨率网络。这种框架旨在提高超分辨率网络的训练速度和效果,并为图像和视频处理领域的任务提供更好的解决方案。 classsr的设计基于三个核心概念:数据特性,特征捕捉和解码器设计。首先,该框架通过对数据进行分析,确定了数据的特性。然后,特征捕捉模块通过特征检测和分类来提取输入图像的特征。最后,解码器根据提供的特征对图像进行重建。 classsr可以加速超分辨率网络的训练速度。该框架使用了轻量级的网络结构和高效的损失函数,使得训练速度比传统的超分辨率网络更快。同时,classsr还可以提高超分辨率网络的效果。该框架可以通过对数据特征的分析来优化网络结构,提高网络的性能和稳定性。 除此以外,classsr还可以为图像和视频处理领域的任务提供更好的解决方案。 classsr可以处理各种不同类型的图像和视频,并为各种应用场景(例如图像增强、视频压缩等)提供专门的解决方案。 综上所述,classsr是一种通用的框架,可以加速超分辨率网络的训练速度并提高网络的效果。该框架还可以为图像和视频处理领域的任务提供更好的解决方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值