• 9.2.tensorRT高级(4)封装系列-自动驾驶案例项目self-driving-深度估计


    前言

    杜老师推出的 tensorRT从零起步高性能部署 课程,之前有看过一遍,但是没有做笔记,很多东西也忘了。这次重新撸一遍,顺便记记笔记。

    本次课程学习 tensorRT 高级-自动驾驶案例项目self-driving-深度估计

    课程大纲可看下面的思维导图

    在这里插入图片描述

    1. 深度估计

    这节我们学习深度估计模型的分析,我们的目的是找到深度估计的 onnx,分析其 onnx 的大致使用逻辑,然后写出最简洁版本的 predict.py,大体可以分为以下三步:

    1. 打开深度估计的 onnx,查看其输入与输出

    2. 查看代码,找到 onnx 的预处理,分析得到预处理的逻辑

    3. 针对获得的信息,编写 predict.py,尝试写出来

    我们来观察下其 onnx 模型,如下图所示:

    在这里插入图片描述

    图1 onnx模型

    从导出的 onnx 模型我们可以知道输入的 1x3x256x512,输出存在 6 个

    我们再分析项目中的 image_processor/depth_engine.cpp 代码可以得出具体的预处理所做的工作:(详细分析请参照视频)

    1. 输入是 1x3x256x512,input.1

    2. 输出是 1x1x256x512,2499 节点

    3. normalize.mean = 0.485f,norm = 0.229f

    4. y = (x / 255.0 - mean) / norm

    5. resize 部分不搞那么复杂,直接 resize

    6. 颜色方面,需要 cvtColor → \rightarrow to RGB

    我们可以简单的写个预处理程序来验证下,代码如下:

    import onnxruntime
    import cv2
    import numpy as np
    
    session = onnxruntime.InferenceSession("workspace/ldrn_kitti_resnext101_pretrained_data_grad_256x512.onnx", provider_options=["CPUExecutionProvider"])
    
    image = cv2.imread("workspace/imgs/dashcam_00.jpg")
    image = cv2.resize(image, (512, 256))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_tensor = (image / 255.0)
    mean = [0.485, 0.456, 0.406]
    norm = [0.229, 0.224, 0.225]
    image_tensor = ((image_tensor - mean) / norm).astype(np.float32)
    image_tensor = image_tensor.transpose(2, 0, 1)[None]
    
    prob = session.run(["2499"], {"input.1": image_tensor})[0]
    
    print(prob.shape)
    
    prob = prob[0, 0] * -5 + 255
    y = int(prob.shape[0] * 0.18)
    prob = prob[y:]
    
    cv2.imwrite("depth.jpg", prob)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    输出如下图:

    在这里插入图片描述

    图2 预处理验证

    可以看到输出符合我们的预期,输出的深度估计图如下所示:

    在这里插入图片描述

    图3 深度估计图

    另外我们还可以通过 matplotlib 来可视化,代码如下所示:

    import onnxruntime
    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
    
    session = onnxruntime.InferenceSession("workspace/ldrn_kitti_resnext101_pretrained_data_grad_256x512.onnx", provider_options=["CPUExecutionProvider"])
    
    image = cv2.imread("workspace/imgs/dashcam_00.jpg")
    image = cv2.resize(image, (512, 256))
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_tensor = (image / 255.0)
    mean = [0.485, 0.456, 0.406]
    norm = [0.229, 0.224, 0.225]
    image_tensor = ((image_tensor - mean) / norm).astype(np.float32)
    image_tensor = image_tensor.transpose(2, 0, 1)[None]
    
    prob = session.run(["2499"], {"input.1": image_tensor})[0]
    
    print(prob.shape)
    
    prob = prob[0, 0]
    y = int(prob.shape[0] * 0.18)
    prob = prob[y:]
    
    plt.imsave("depth.jpg", prob, cmap='plasma_r')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    输出的深度估计图如下所示:

    在这里插入图片描述

    图4 深度估计图(matplotlib)

    总结

    本次课程学习了开源项目中的深度估计案例,主要是对深度估计模型的 onnx 进行了简单分析,并通过对项目代码的分析将预处理部分理清楚,然后通过 onnxruntime 进行了简单验证,并对最终的深度估计结果进行了可视化显示

  • 相关阅读:
    深度解读 ChatGPT基本原理
    iso9001是什么意思
    字节码进阶之java Instrumentation原理详解
    CRC8校验算法源码——C语言版
    C++入门学习(1)命名空间和输入输出
    python web开发过程
    LRU最近最少使用算法
    就推荐 4 个 yyds 的开源项目
    【面试干货】数据库乐观锁,悲观锁的区别,怎么实现
    关于Vuex的简单理解和使用
  • 原文地址:https://blog.csdn.net/qq_40672115/article/details/132655718