有多种类型的深度学习网络可用,包括识别、检测/定位和语义分割。我们在本教程中重点介绍的第一个深度学习功能是图像识别,使用在大型数据集上训练的分类网络来识别场景和对象。
镜像操作
docker pull dustynv/jetson-inference:r32.6.1
传输文件到docker容器,这个你网不好的情况下也不会在容器内添加代理的话可以使用的到。
首先需要知道docker容器的container_id,可以使用docker ps命令来查看你要操作的docker容器的container_id
docker cp container_id: <本地保存文件的路径>
宿主机向Docker容器传送文件
docker cp 本地文件的路径 container_id:
- git clone --recursive https://github.com/dusty-nv/jetson-inference
- cd jetson-inference
- docker/run.sh
首先,让我们尝试使用imagenet程序在一些示例图像上测试imagenet识别。它加载一个或多个图像,使用TensorRT和imageNet类进行推理,然后覆盖分类结果并保存输出图像。该项目附带示例图像供您使用,位于图像/目录下。
构建项目后,确保您的终端位于aarch64/bin目录中:
$ cd jetson-inference/build/aarch64/bin
- # C++
- $ ./imagenet images/orange_0.jpg images/test/output_0.jpg # (default network is googlenet)
-
- # Python
- $ ./imagenet.py images/orange_0.jpg images/test/output_0.jpg # (default network is googlenet)
- # C++
- $ ./imagenet images/strawberry_0.jpg images/test/output_1.jpg
-
- # Python
- $ ./imagenet.py images/strawberry_0.jpg images/test/output_1.jpg
除了加载单个图像外,还可以加载目录或图像序列或视频文件。有关详细信息,请参阅“相机流和多媒体”页或启动带有该标志的应用程序。--help
下载其他分类模型
默认情况下,该项目将在构建步骤中下载 GoogleNet 和 ResNet-18 网络。
如果您选择下载其他预先训练的模型,也可以使用它们:
网络 CLI 参数 网络类型枚举 亚历克斯网 alexnetALEXNET谷歌网 googlenetGOOGLENET谷歌网-12 googlenet-12GOOGLENET_12ResNet-18 resnet-18RESNET_18ResNet-50 resnet-50RESNET_50ResNet-101 resnet-101RESNET_101ResNet-152 resnet-152RESNET_152VGG-16系列 vgg-16VGG-16VGG-19 vgg-19VGG-19盗梦空间-v4 inception-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
下面是在磁盘中的视频上运行它的示例:
- # Download test video (thanks to jell.yfish.us)
- $ wget https://nvidia.box.com/shared/static/tlswont1jnyu3ix2tbf7utaekpzcx4rc.mkv -O jellyfish.mkv
-
- # C++
- $ ./imagenet --network=resnet-18 jellyfish.mkv images/test/jellyfish_resnet18.mkv
-
- # Python
- $ ./imagenet.py --network=resnet-18 jellyfish.mkv images/test/jellyfish_resnet18.mkv

- # run these commands outside of container
- $ cd ~/
- $ mkdir my-recognition-python
- $ cd my-recognition-python
- $ touch my-recognition.py
- $ chmod +x my-recognition.py
- $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/black_bear.jpg
- $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/brown_bear.jpg
- $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/polar_bear.jpg
- #!/usr/bin/python3
-
- import jetson.inference
- import jetson.utils
-
- import argparse
-
- # parse the command line
- parser = argparse.ArgumentParser()
- parser.add_argument("filename", type=str, help="filename of the image to process")
- parser.add_argument("--network", type=str, default="googlenet", help="model to use, can be: googlenet, resnet-18, ect.")
- args = parser.parse_args()
-
- # load an image (into shared CPU/GPU memory)
- img = jetson.utils.loadImage(args.filename)
-
- # load the recognition network
- net = jetson.inference.imageNet(args.network)
-
- # classify the image
- class_idx, confidence = net.Classify(img)
-
- # find the object description
- class_desc = net.GetClassDesc(class_idx)
-
- # print out the result
- 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
- #include
- #include
-
- int main( int argc, char** argv )
- {
- // a command line argument containing the image filename is expected,
- // so make sure we have at least 2 args (the first arg is the program)
- if( argc < 2 )
- {
- printf("my-recognition: expected image filename as argument\n");
- printf("example usage: ./my-recognition my_image.jpg\n");
- return 0;
- }
-
- // retrieve the image filename from the array of command line args
- const char* imgFilename = argv[1];
-
- // these variables will store the image data pointer and dimensions
- uchar3* imgPtr = NULL; // shared CPU/GPU pointer to image
- int imgWidth = 0; // width of the image (in pixels)
- int imgHeight = 0; // height of the image (in pixels)
-
- // load the image from disk as uchar3 RGB (24 bits per pixel)
- if( !loadImage(imgFilename, &imgPtr, &imgWidth, &imgHeight) )
- {
- printf("failed to load image '%s'\n", imgFilename);
- return 0;
- }
-
- // load the GoogleNet image recognition network with TensorRT
- // you can use imageNet::RESNET_18 to load ResNet-18 model instead
- imageNet* net = imageNet::Create(imageNet::GOOGLENET);
-
- // check to make sure that the network model loaded properly
- if( !net )
- {
- printf("failed to load image recognition network\n");
- return 0;
- }
-
- // this variable will store the confidence of the classification (between 0 and 1)
- float confidence = 0.0;
-
- // classify the image, return the object class index (or -1 on error)
- const int classIndex = net->Classify(imgPtr, imgWidth, imgHeight, &confidence);
-
- // make sure a valid classification result was returned
- if( classIndex >= 0 )
- {
- // retrieve the name/description of the object class index
- const char* classDescription = net->GetClassDesc(classIndex);
-
- // print out the classification results
- printf("image is recognized as '%s' (class #%i) with %f%% confidence\n",
- classDescription, classIndex, confidence * 100.0f);
- }
- else
- {
- // if Classify() returned < 0, an error occurred
- printf("failed to classify image\n");
- }
-
- // free the network's resources before shutting down
- delete net;
- return 0;
- }
- $ mkdir ~/my-recognition
- $ cd ~/my-recognition
- $ touch my-recognition.cpp
- $ touch CMakeLists.txt
- $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/black_bear.jpg
- $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/brown_bear.jpg
- $ wget https://github.com/dusty-nv/jetson-inference/raw/master/data/images/polar_bear.jpg
- $ cd ~/my-recognition
- $ cmake .
- $ make
- $ ./my-recognition polar_bear.jpg
- image is recognized as 'ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus' (class #296) with 99.999878% confidence
前面的识别示例输出表示整个输入图像的类概率。接下来,我们将重点介绍对象检测,并通过提取各种对象的边界框来查找它们在帧中的位置。与图像分类不同,对象检测网络能够每帧检测许多不同的对象。
detectNet 对象接受图像作为输入,并输出检测到的边界框的坐标列表及其类和置信度值。detectNet可以从Python和C++中使用。有关可供下载的各种预训练检测模型,请参见下文。使用的默认模型是在MS COCO数据集上训练的91级SSD-Mobilenet-v2模型,该模型使用TensorRT在Jetson上实现实时推理性能。
作为使用该类的示例,我们为C++和Python提供了示例程序:detectNet
这些示例能够检测图像、视频和相机源中的对象。有关支持的各种类型的输入/输出流的详细信息,请参阅相机流和多媒体页面。
首先,让我们尝试使用该程序在静态图像中定位对象。除了输入/输出路径之外,还有一些其他命令行选项:detectnet
--network--overlayboxlineslabelsconfnone
--overlay=box,labels,confboxlines--alpha120--threshold0.5如果您使用的是 Docker 容器,建议将输出映像保存到已装载的目录中。然后,可以从主机设备轻松查看这些映像(有关详细信息,请参阅已装载的数据卷)。images/testjetson-inference/data/images/test
以下是使用默认 SSD-Mobilenet-v2 模型在映像中检测行人的一些示例:
- # C++
- $ ./detectnet --network=ssd-mobilenet-v2 images/peds_0.jpg images/test/output.jpg # --network flag is optional
-
- # Python
- $ ./detectnet.py --network=ssd-mobilenet-v2 images/peds_0.jpg images/test/output.jpg # --network flag is optional