• Jetson-inference -Coding Your Own Image Recognition Program (Python)学习笔记


    有多种类型的深度学习网络可用,包括识别、检测/定位和语义分割。我们在本教程中重点介绍的第一个深度学习功能是图像识别,使用在大型数据集上训练的分类网络来识别场景和对象。 

    镜像操作

    docker pull dustynv/jetson-inference:r32.6.1

    传输文件到docker容器,这个你网不好的情况下也不会在容器内添加代理的话可以使用的到。

    首先需要知道docker容器的container_id,可以使用docker ps命令来查看你要操作的docker容器的container_id

    Docker容器向宿主机传送文件

    docker cp container_id: <本地保存文件的路径>

     宿主机向Docker容器传送文件

    docker cp 本地文件的路径 container_id:

     

    1. git clone --recursive https://github.com/dusty-nv/jetson-inference
    2. cd jetson-inference
    3. docker/run.sh

    首先,让我们尝试使用imagenet程序在一些示例图像上测试imagenet识别。它加载一个或多个图像,使用TensorRT和imageNet类进行推理,然后覆盖分类结果并保存输出图像。该项目附带示例图像供您使用,位于图像/目录下。

    构建项目后,确保您的终端位于aarch64/bin目录中:

    $ cd jetson-inference/build/aarch64/bin
    1. # C++
    2. $ ./imagenet images/orange_0.jpg images/test/output_0.jpg # (default network is googlenet)
    3. # Python
    4. $ ./imagenet.py images/orange_0.jpg images/test/output_0.jpg # (default network is googlenet)
    1. # C++
    2. $ ./imagenet images/strawberry_0.jpg images/test/output_1.jpg
    3. # Python
    4. $ ./imagenet.py images/strawberry_0.jpg images/test/output_1.jpg

     除了加载单个图像外,还可以加载目录或图像序列或视频文件。有关详细信息,请参阅“相机流和多媒体”页或启动带有该标志的应用程序。--help

    下载其他分类模型

    默认情况下,该项目将在构建步骤中下载 GoogleNet 和 ResNet-18 网络。

    如果您选择下载其他预先训练的模型,也可以使用它们:

    网络CLI 参数网络类型枚举
    亚历克斯网alexnetALEXNET
    谷歌网googlenetGOOGLENET
    谷歌网-12googlenet-12GOOGLENET_12
    ResNet-18resnet-18RESNET_18
    ResNet-50resnet-50RESNET_50
    ResNet-101resnet-101RESNET_101
    ResNet-152resnet-152RESNET_152
    VGG-16系列vgg-16VGG-16
    VGG-19vgg-19VGG-19
    盗梦空间-v4inception-v4INCEPTION_V4

    注意:要下载其他网络,请运行模型下载器工具
                 $ cd jetson-inference/tools
                 $ ./download-models.sh

    通常,更复杂的网络可以具有更高的分类准确性,并增加运行时间。

     接下来,让我们使用C++或Python变体,使用imagenet程序对示例图像进行分类。如果您使用的是Docker容器,建议将分类输出图像保存到images/test-mounted目录。然后,您可以在jetson推断/数据/图像/测试目录中的主机设备上轻松查看这些图像(有关更多信息,请参阅装入的数据卷)。

    处理视频

    摄像机流和多媒体”页面显示程序可以处理的不同类型的流。imagenet

    https://github.com/dusty-nv/jetson-inference/blob/master/docs/aux-streaming.md

    下面是在磁盘中的视频上运行它的示例:

    1. # Download test video (thanks to jell.yfish.us)
    2. $ wget https://nvidia.box.com/shared/static/tlswont1jnyu3ix2tbf7utaekpzcx4rc.mkv -O jellyfish.mkv
    3. # C++
    4. $ ./imagenet --network=resnet-18 jellyfish.mkv images/test/jellyfish_resnet18.mkv
    5. # Python
    6. $ ./imagenet.py --network=resnet-18 jellyfish.mkv images/test/jellyfish_resnet18.mkv

    1. # run these commands outside of container
    2. $ cd ~/
    3. $ mkdir my-recognition-python
    4. $ cd my-recognition-python
    5. $ touch my-recognition.py
    6. $ chmod +x my-recognition.py
    7. $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/black_bear.jpg
    8. $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/brown_bear.jpg
    9. $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/polar_bear.jpg

    1. #!/usr/bin/python3
    2. import jetson.inference
    3. import jetson.utils
    4. import argparse
    5. # parse the command line
    6. parser = argparse.ArgumentParser()
    7. parser.add_argument("filename", type=str, help="filename of the image to process")
    8. parser.add_argument("--network", type=str, default="googlenet", help="model to use, can be: googlenet, resnet-18, ect.")
    9. args = parser.parse_args()
    10. # load an image (into shared CPU/GPU memory)
    11. img = jetson.utils.loadImage(args.filename)
    12. # load the recognition network
    13. net = jetson.inference.imageNet(args.network)
    14. # classify the image
    15. class_idx, confidence = net.Classify(img)
    16. # find the object description
    17. class_desc = net.GetClassDesc(class_idx)
    18. # print out the result
    19. print("image is recognized as '{:s}' (class #{:d}) with {:f}% confidence".format(class_desc, class_idx, confidence * 100))
    $ docker/run.sh --volume ~/my-recognition-python:/my-recognition-python   # mounted inside the container to /my-recognition-python 

    1. #include
    2. #include
    3. int main( int argc, char** argv )
    4. {
    5. // a command line argument containing the image filename is expected,
    6. // so make sure we have at least 2 args (the first arg is the program)
    7. if( argc < 2 )
    8. {
    9. printf("my-recognition: expected image filename as argument\n");
    10. printf("example usage: ./my-recognition my_image.jpg\n");
    11. return 0;
    12. }
    13. // retrieve the image filename from the array of command line args
    14. const char* imgFilename = argv[1];
    15. // these variables will store the image data pointer and dimensions
    16. uchar3* imgPtr = NULL; // shared CPU/GPU pointer to image
    17. int imgWidth = 0; // width of the image (in pixels)
    18. int imgHeight = 0; // height of the image (in pixels)
    19. // load the image from disk as uchar3 RGB (24 bits per pixel)
    20. if( !loadImage(imgFilename, &imgPtr, &imgWidth, &imgHeight) )
    21. {
    22. printf("failed to load image '%s'\n", imgFilename);
    23. return 0;
    24. }
    25. // load the GoogleNet image recognition network with TensorRT
    26. // you can use imageNet::RESNET_18 to load ResNet-18 model instead
    27. imageNet* net = imageNet::Create(imageNet::GOOGLENET);
    28. // check to make sure that the network model loaded properly
    29. if( !net )
    30. {
    31. printf("failed to load image recognition network\n");
    32. return 0;
    33. }
    34. // this variable will store the confidence of the classification (between 0 and 1)
    35. float confidence = 0.0;
    36. // classify the image, return the object class index (or -1 on error)
    37. const int classIndex = net->Classify(imgPtr, imgWidth, imgHeight, &confidence);
    38. // make sure a valid classification result was returned
    39. if( classIndex >= 0 )
    40. {
    41. // retrieve the name/description of the object class index
    42. const char* classDescription = net->GetClassDesc(classIndex);
    43. // print out the classification results
    44. printf("image is recognized as '%s' (class #%i) with %f%% confidence\n",
    45. classDescription, classIndex, confidence * 100.0f);
    46. }
    47. else
    48. {
    49. // if Classify() returned < 0, an error occurred
    50. printf("failed to classify image\n");
    51. }
    52. // free the network's resources before shutting down
    53. delete net;
    54. return 0;
    55. }
    1. $ mkdir ~/my-recognition
    2. $ cd ~/my-recognition
    3. $ touch my-recognition.cpp
    4. $ touch CMakeLists.txt
    5. $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/black_bear.jpg
    6. $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/brown_bear.jpg
    7. $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/polar_bear.jpg
    1. $ cd ~/my-recognition
    2. $ cmake .
    3. $ make

    1. $ ./my-recognition polar_bear.jpg
    2. image is recognized as 'ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus' (class #296) with 99.999878% confidence

    使用检测网定位对象

    前面的识别示例输出表示整个输入图像的类概率。接下来,我们将重点介绍对象检测,并通过提取各种对象的边界框来查找它们在帧中的位置。与图像分类不同,对象检测网络能够每帧检测许多不同的对象。

    detectNet 对象接受图像作为输入,并输出检测到的边界框的坐标列表及其类和置信度值。detectNet可以从PythonC++中使用。有关可供下载的各种预训练检测模型,请参见下文。使用的默认模型是在MS COCO数据集上训练的91级SSD-Mobilenet-v2模型,该模型使用TensorRT在Jetson上实现实时推理性能。

    作为使用该类的示例,我们为C++和Python提供了示例程序:detectNet

    这些示例能够检测图像、视频和相机源中的对象。有关支持的各种类型的输入/输出流的详细信息,请参阅相机流和多媒体页面。

    从图像中检测对象

    首先,让我们尝试使用该程序在静态图像中定位对象。除了输入/输出路径之外,还有一些其他命令行选项:detectnet

    • 更改正在使用的检测模型的可选标志(默认值为 SSD-Mobilenet-v2)。--network
    • 可选标志,可以是 、 和 的逗号分隔组合--overlayboxlineslabelsconfnone
      • 默认值为显示框、标签和置信度值--overlay=box,labels,conf
      • 该选项绘制填充的边界框,而仅绘制未填充的轮廓boxlines
    • 可选值,用于设置叠加期间使用的 Alpha 混合值(默认值为 )。--alpha120
    • 设置检测最小阈值的可选值(默认值为 )。--threshold0.5

    如果您使用的是 Docker 容器,建议将输出映像保存到已装载的目录中。然后,可以从主机设备轻松查看这些映像(有关详细信息,请参阅已装载的数据卷)。images/testjetson-inference/data/images/test

    以下是使用默认 SSD-Mobilenet-v2 模型在映像中检测行人的一些示例:

    1. # C++
    2. $ ./detectnet --network=ssd-mobilenet-v2 images/peds_0.jpg images/test/output.jpg # --network flag is optional
    3. # Python
    4. $ ./detectnet.py --network=ssd-mobilenet-v2 images/peds_0.jpg images/test/output.jpg # --network flag is optional

  • 相关阅读:
    docker怎样安装redis
    分享|破世界纪录的OceanBase,如今入选了国际顶会VLDB 2022
    计算机的错误计算(五十七)
    JDK,JRE和JVM三者间关系
    Leetcode.2172 数组的最大与和
    深度学习推荐系统(五)Deep&Crossing模型及其在Criteo数据集上的应用
    flume拦截器
    【融云出海白皮书免费看】-巴西成增量潜力「应许之地」
    Rust--流程控制
    Flask基础:环境搭建+配置+URL与试图之间的映射+重定向+数据库连接
  • 原文地址:https://blog.csdn.net/weixin_46151178/article/details/125760998