一、准备工作

1、首先你得给你的TX2 刷好机,而且最好刷最新版本Jetpack4.4.1(因为本人刷过3.3的版本,运行代码时会出现很多的问题)
2、教程原地址

二、正式的学习

1、该教程做了哪些

(1)此仓库使用NVIDIA TensorRT将神经网络有效地部署到嵌入式Jetson平台上.
(2)在Jetson上运行推理和转移学习,包括收集自己的数据集和训练自己的模型。它涵盖了图像分类,对象检测和分割。

2、从源头开始构建项目

(1)总体的执行过程

$ sudo apt-get update//更新安装程序列表
$ sudo apt-get install git cmake libpython3-dev python3-numpy//安装python3依赖库和cmake
$ git clone --recursive https://github.com/dusty-nv/jetson-inference//克隆该项目
$ cd jetson-inference//进入到该目录
$ git submodule update --init//更新submodule
$ mkdir build
$ cd build
$ cmake ../
$ make -j$(nproc)
$ sudo make install
$ sudo ldconfig

(2)有些指令为什么要执行?

1、为什么需要cmke、make、makefile

在前面我们通过sudo apt install cmake安装了一个cmake的工具,那么它到底是什么呢?
①是什么?

CMake是一个跨平台的编译(Build)工具,可以用简单的语句来描述所有平台的编译过程。

②为什么需要它?它不能构建最终的软件,但是可以产生输出各种各样的makefile或者project文件。

假如我们有一个深度学习框架的部分工程列表,里面有超过40个互相调用的工程共同组成,一些用于生成库文件,一些用于实现逻辑功能。他们之间的调用关系复杂而严格,如果我想在这样复杂的框架下进行二次开发,显然只拥有它的源码是远远不够的,还需要清楚的明白这几十个项目之间的复杂关系,在没有原作者的帮助下进行这项工作几乎是不可能的。即使是原作者给出了相关的结构文档,对新手来说建立工程的过程依旧是漫长而艰辛的,因此CMake的作用就凸显出来了。原作者只需要生成一份CMakeLists.txt文档,框架的使用者们只需要在下载源码的同时下载作者提供的CMakeLists.txt,就可以利用CMake,在”原作者的帮助下“进行工程的搭建。

③输出了makefile文件后有什么用?

makefile定义了一系列的规则来指定哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至于进行更复杂的功能操作,因为makefile就像一个Shell脚本一样,其中也可以执行操作系统的命令。makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率。make是一个命令工具,是一个解释makefile中指令的命令工具。

④为什么需要makefile?

有效地描述这些文件之间的依赖关系以及处理命令,当个别文件改动后仅执行必要的处理,而不必重复整个编译过程,可以大大提高软件开发的效率。

⑤最终完整的实现逻辑
通过cmake编译cmakelist.txt生成了makefile文件,然后通过make工具就可以解释makefile最终生成可执行文件。
执行make就可以实现自动编译,执行make clean 就可以清楚编译产生的文件,本人博客的makefile专栏有简单的makefile编写,链接如下
2、在刚刚的构建步骤中为什么执行cmake …/?
看下图既可知道。

此命令将启动CMakePreBuild.sh在Jetson上安装某些必备软件包,该脚本还从Web服务下载经过预训练的网络。

3、make的相关命令的作用
make,仅编译;
make install,编译并安装(比如安装到/usr/bin目录下,然后可以直接使用。因为/usr/bin只有管理员才能向里面添加文件,所以通常要加sudo)
make就是make all,编译用的,具体编译了那些文件要看你的Makefile
make install就是把编译出来的二进制文件,库,配置文件等等放到相应目录下
make clean清除编译结果
具体的东西都在Makefile里面,只不过大部分应用程序的Makefile都是由configure脚本自动生成的,所以Makefile内容都差不多。

上面的构建过程中用到了sudo make install ,更新代码,请记住再次运行它。

4、sudo ldconfig
ldconfig是一个动态链接库管理命令,为了让动态链接库为系统所共享,还需运行动态链接库的管理命令–ldconfig。 ldconfig 命令的用途,主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜索出可共享的动态 链接库(格式如前介绍,lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件.缓存文件默认为 /etc/ld.so.cache,此文件保存已排好序的动态链接库名字列表.

linux下的共享库机制采用了类似于高速缓存的机制,将库信息保存在/etc/ld.so.cache里边。
程序连接的时候首先从这个文件里边查找,然后再到ld.so.conf的路径里边去详细找。
这就是为什么修改了ld.so.conf要重新运行一下ldconfig的原因
补充一点,ldconfig在/sbin里面。
可参考这篇博主的文章

3、使用ImageNet程序

(1)处理图像

使用TensorRT和imageNet类执行推理,然后叠加分类结果并保存输出图像

首次运行每种模型时,TensorRT将花费几分钟来优化网络。然后将优化的网络文件缓存到磁盘,因此使用该模型的将来运行将加载得更快。

c++的imagenet程序

class imageNet : public tensorNet
{public:/*** Network choice enumeration.*/enum NetworkType{CUSTOM,        /**< Custom model provided by the user */ALEXNET,       /**< AlexNet trained on 1000-class ILSVRC12 */GOOGLENET, /**< GoogleNet trained 1000-class ILSVRC12 */GOOGLENET_12,   /**< GoogleNet trained on 12-class subset of ImageNet ILSVRC12 from the tutorial */RESNET_18,    /**< ResNet-18 trained on 1000-class ILSVRC15 */RESNET_50,   /**< ResNet-50 trained on 1000-class ILSVRC15 */RESNET_101,  /**< ResNet-101 trained on 1000-class ILSVRC15 */RESNET_152, /**< ResNet-50 trained on 1000-class ILSVRC15 */VGG_16,      /**< VGG-16 trained on 1000-class ILSVRC14 */VGG_19,     /**< VGG-19 trained on 1000-class ILSVRC14 */INCEPTION_V4,   /**< Inception-v4 trained on 1000-class ILSVRC12 */};/*** Load a new network instance*/static imageNet* Create( NetworkType networkType=GOOGLENET, uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE, precisionType precision=TYPE_FASTEST,deviceType device=DEVICE_GPU, bool allowGPUFallback=true );/*** Load a new network instance* @param prototxt_path File path to the deployable network prototxt* @param model_path File path to the caffemodel* @param mean_binary File path to the mean value binary proto (can be NULL)* @param class_labels File path to list of class name labels* @param input Name of the input layer blob.* @param output Name of the output layer blob.* @param maxBatchSize The maximum batch size that the network will support and be optimized for.*/static imageNet* Create( const char* prototxt_path, const char* model_path, const char* mean_binary, const char* class_labels, const char* input=IMAGENET_DEFAULT_INPUT, const char* output=IMAGENET_DEFAULT_OUTPUT, uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE, precisionType precision=TYPE_FASTEST,deviceType device=DEVICE_GPU, bool allowGPUFallback=true );/*** Determine the maximum likelihood image class.* This function performs pre-processing to the image (apply mean-value subtraction and NCHW format), @see PreProcess() * @param rgba float4 input image in CUDA device memory.* @param width width of the input image in pixels.* @param height height of the input image in pixels.* @param confidence optional pointer to float filled with confidence value.* @returns Index of the maximum class, or -1 on error.*/int Classify( float* rgba, uint32_t width, uint32_t height, float* confidence=NULL );/*** Retrieve the number of image recognition classes (typically 1000)*/inline uint32_t GetNumClasses() const                            { return mOutputClasses; }/*** Retrieve the description of a particular class.*/inline const char* GetClassDesc( uint32_t index ) const          { return mClassDesc[index].c_str(); }
};
cd ~/jetson-inference/build/aarch64/bin

# C++
./imagenet images/orange_0.jpg images/test/output_0.jpg     # (default network is googlenet)实质上imagenet是一个二进制文件

下面有可能会报错,因为没有截好图,所以直接给出解决方法。
原因:模型文件有可能被损坏了,我们可以把他们删除掉,然后重新下载后,再返回build目录下进行编译,然后再重新执行命令。具体步骤:

cd ~/jetson-inference/data/networks
rm googlenet.prototxt bvlc_googlenet.caffemodel
wget https://raw.githubusercontent.com/BVLC/caffe/master/models/bvlc_googlenet/deploy.prototxt -O googlenet.prototxt
wget http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel -O bvlc_googlenet.caffemodel
cd ~/jetson-inference/build
cmake ../
cd ~/jetson-inference/build/aarch64/bin

最后再重新执行

./imagenet images/orange_0.jpg images/test/output_0.jpg

首次运行每种模型时,TensorRT将花费几分钟来优化网络。然后将优化的网络文件缓存到磁盘,因此将来使用该模型的时候加载得更快。运行效果如下:
然后我们可以自己拍一张照片,看看它的准确率。

显然这个概率就小很多了。

因为在构建构成中,实际上是下载了GoogleNet和ResNet-18网络模型。你可以通过将–network命令行上的标志设置为相应的CLI参数之一来指定要加载的模型。默认情况下,如果–network未指定可选标志,则会加载GoogleNet 。下面我们用ResNet-18来分类。

因为我们不能连接上Box.com,所以我们可以通过这个网址下载,然后解压。

cd ~/jetson-inference/data/networks


然后我们再重新执行下面的命令

./imagenet --network=resnet-18 images/jellyfish.jpg images/test/output_jellyfish.jpg

效果如下:

我们同样用这个预训练好的模型来去识别笔,然后再看看效果。

(2)处理视频
imagenet程序可以处理的视频流。

4、构建自己的图像分类程序

在上面我们使用了imagenet的应用程序,下面我们自己编写一个c++的图像识别程序。具体的构建步骤如下:

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


1、编写c++程序


#include <jetson-inference/imageNet.h>
#include <jetson-utils/loadImage.h>
//上面这两个头文件存放在/usr/local/include中,在sudo make install步骤构建,如果没有执行该命令,则找不到该头文件int main( int argc, char** argv )
{//需要包含图像文件名的命令行参数,//因此请确保至少有2个args(第一个arg是程序)if( argc < 2 ){printf("my-recognition:  expected image filename as argument\n");printf("example usage:   ./my-recognition my_image.jpg\n");return 0;}// 从 argv数组中获取文件名const char* imgFilename = argv[1];// these variables will store the image data pointer and dimensionsuchar3* imgPtr = NULL;   // 用cpu/gpu的共享内存指针指向图像,加载的图像将存储映射到CPU和GPU的共享内存中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 also use imageNet::RESNET_18 to load ResNet-18 model insteadimageNet* net = imageNet::Create(imageNet::GOOGLENET);// check to make sure that the network model loaded properlyif( !net ){printf("failed to load image recognition network\n");return 0;}// this variable will store the confidence of the classification (between 0 and 1),事实上就是输出概率值,该值的范围是0-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 indexconst char* classDescription = net->GetClassDesc(classIndex);// print out the classification resultsprintf("image is recognized as '%s' (class #%i) with %f%% confidence\n", classDescription, classIndex, confidence * 100.0f);}else{// if Classify() returned < 0, an error occurredprintf("failed to classify image\n");}// free the network's resources before shutting downdelete net;// this is the end of the example!return 0;
}

2、编写CMakeLists.txt

# require CMake 2.8 or greater
cmake_minimum_required(VERSION 2.8)
# declare my-recognition project
project(my-recognition)# import jetson-inference and jetson-utils packages.
# note that if you didn't do "sudo make install"
# while building jetson-inference, this will error.
find_package(jetson-utils)
find_package(jetson-inference)
# CUDA is required
find_package(CUDA)# compile the my-recognition program
cuda_add_executable(my-recognition my-recognition.cpp)# link my-recognition to jetson-inference library
target_link_libraries(my-recognition jetson-inference)

3、编译项目

cd ~/my-recognition
cmake .
make

4、结果

./my-recognition polar_bear.jpg

image is recognized as ‘ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus’ (class #296) with 100.000000% confidence

./my-recognition brown_bear.jpg

image is recognized as ‘brown bear, bruin, Ursus arctos’ (class #294) with 99.951172% confidence

./my-recognition black_bear.jpg

image is recognized as ‘American black bear, black bear, Ursus americanus, Euarctos americanus’ (class #295) with 98.974609% confidence

④用我们自己的图像,也即是前面有两支笔的那个图像

./my-recognition 1.jpg

image is recognized as ‘ballpoint, ballpoint pen, ballpen, Biro’ (class #418) with 57.031250% confidence

6、Hello World官网教程(TX2)第一部分相关推荐

  1. java官网教程(基础篇)—— 基础的Java类 —— 基础 I / O

    目录 基本 Java 类 基础 I/O I/O流 字节流 字符流 缓冲流 扫描和格式化 扫描 格式化 从命令行中进行IO操作 数据流 对象流 文件 I/O(采用 NIO.2) 什么是路径? Path类 ...

  2. Away3D 4.0官网教程(翻译)

    使用Away3D 4.Stage3D 创建3D游戏和应用程序 (此帖每天都会更新,一定让大家完全的搞明白) 补充区:        'vase.awd' 可以使用 Prefab3D打开(在帖子后面回复 ...

  3. CMake学习笔记(一)——CMake官网教程

    CMake学习笔记(一)--CMake官网教程 前言: 经历了一星期痛苦的交叉编译,笔者深刻认知到Linux下make的重要性.所以准备放缓两三天自己的工作进度,并学习一下CMake与Makefile ...

  4. 新学Python之学习官网教程序言

      大家好,我是 herosunly.985 院校硕士毕业,现担任算法研究员一职,热衷于机器学习算法研究与应用.曾获得阿里云天池安全恶意程序检测第一名,科大讯飞恶意软件分类挑战赛第三名,CCF 恶意软 ...

  5. [pytorch] 官网教程+注释

    pytorch官网教程+注释 Classifier import torch import torchvision import torchvision.transforms as transform ...

  6. MNE溯源fieldtrip官网教程

    MNE溯源fieldtrip官网教程 Introduction 在本教程中,您可以找到有关如何使用最小范数估计进行源重构的信息,以重构单个主题的事件相关字段(MEG).我们将使用预处理教程中描述的数据 ...

  7. Spring Cloud学习笔记—网关Spring Cloud Gateway官网教程实操练习

    Spring Cloud学习笔记-网关Spring Cloud Gateway官网教程实操练习 1.Spring Cloud Gateway介绍 2.在Spring Tool Suite4或者IDEA ...

  8. Gem5模拟器,详解官网教程Event-driven programming(五)

    目录 一.解释一下gem5中的event-driven? 二.Creating a simple event callback (1)定义一个新的 C++ 类,并继承自 SimObject 抽象基类 ...

  9. Angular官网教程示例知识点总结

    Angular官网教程示例知识点总结 1.背景 2.知识点 2.1 应用的外壳 2.1.1 使用 Angular CLI 创建初始的应用结构 2.1.2 启动应用服务器 2.1.3 双花括号表达式 2 ...

  10. Docker 官网教程实践 自定义 bridge 网络

    前言 这篇笔记是 docker 官网教程 自定义 bridge 网络的实践. 用户自定义 bridge 网络是在生产环境中推荐到最佳方式,因此这篇教程要特别注意. 这个教程中,启动了2个 alpine ...

最新文章

  1. Dell Fluid FS 集群NAS系统在4K非编环境的卓越表现
  2. 在ubuntn kylin系统eclipse中Java语言helloworld程序
  3. 双引擎驱动Quick BI十亿数据0.3秒分析,首屏展示时间缩短30%
  4. Java GUI简单点名器
  5. 图象和文本的绝对位置(九)
  6. AcWing 1884. COW(前缀和)
  7. 条件随机场Conditional Random Field,CRF、隐马尔可夫模型Hidden Markov Model,HMM、马尔可夫随机场、马尔可夫性质傻傻分不清楚?帮你理理关系
  8. Java题 细胞分裂
  9. 测国外服务器速度的网站,美国服务器如何测试速度?
  10. ioi 赛制_徐明宽IOI2017参赛总结及他的信息学竞赛之路
  11. 循环小数(Repeating Decimals, ACM/ICPC World Finals 1990, UVa202)
  12. 新手必学:Linux配置WiFi网络连接
  13. android 深度自定义View
  14. HTTP中的301、302、303、307、308
  15. 爬微医挂号网并把数据导入oracle数据库
  16. LabVIEW中的数据通信方法
  17. python环境问题(环境切换)
  18. luncene.NET 实现全文检索,模糊搜索
  19. Python笔记_第二篇_面向过程_第二部分_4.常用模块的简单使用_操作系统模块(os)和队列模块(collections)...
  20. 【亲身体验】华强北的AirPods Pro和Airpods能用吗?? ?

热门文章

  1. 使用script#编写Xrm的Javascript
  2. vsCode使vue中的代码高亮
  3. Knowledge 1命题逻辑语义蕴含
  4. 客流量总是少?是你门店选址出了问题!
  5. 丁腈橡胶的广泛应用及其特点
  6. Java项目:jsp+servlet图书管理系统
  7. Linux下串口编程总结
  8. Oracle11安装教程
  9. 计算机基础课教学心得,浅谈高专院校计算机基础课程的教学心得体会养
  10. 【华为OD机试真题】促销活动(货币兑换)100%通过率