TensorRT/samples/common/argsParser.h源碼研讀

  • argsParser.h
  • namespace
  • struct的繼承
  • caffe特有參數
  • UFF格式
  • 不需要typedef struct xxx yyy?
  • struct初始化
  • struct option,getopt_long
  • inline
  • DLA
  • 參考連結

argsParser.h

/** Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
#ifndef TENSORRT_ARGS_PARSER_H
#define TENSORRT_ARGS_PARSER_H#include <string>
#include <vector>
#ifdef _MSC_VER
#include "..\common\windows\getopt.h"
#else
#include <getopt.h>
#endif
#include <iostream>namespace samplesCommon
{//!
//! \brief The SampleParams structure groups the basic parameters required by
//!        all sample networks.
//!
struct SampleParams
{int batchSize{1};                  //!< Number of inputs in a batchint dlaCore{-1};                   //!< Specify the DLA core to run network on.bool int8{false};                  //!< Allow runnning the network in Int8 mode.bool fp16{false};                  //!< Allow running the network in FP16 mode.std::vector<std::string> dataDirs; //!< Directory paths where sample data files are storedstd::vector<std::string> inputTensorNames;std::vector<std::string> outputTensorNames;
};//!
//! \brief The CaffeSampleParams structure groups the additional parameters required by
//!         networks that use caffe
//!
// 在SampleParams的基礎上加上caffe特有的參數
struct CaffeSampleParams : public SampleParams
{std::string prototxtFileName; //!< Filename of prototxt design file of a networkstd::string weightsFileName;  //!< Filename of trained weights file of a networkstd::string meanFileName;     //!< Filename of mean file of a network
};//!
//! \brief The OnnxSampleParams structure groups the additional parameters required by
//!         networks that use ONNX
//!
struct OnnxSampleParams : public SampleParams
{std::string onnxFileName; //!< Filename of ONNX file of a network
};//!
//! \brief The UffSampleParams structure groups the additional parameters required by
//!         networks that use Uff
//!
struct UffSampleParams : public SampleParams
{std::string uffFileName; //!< Filename of uff file of a network
};//!
//! /brief Struct to maintain command-line arguments.
//!
//parseArgs解析完命令行的參數後,會把得到的資訊存入此結構體內
struct Args
{bool runInInt8{false};bool runInFp16{false};bool help{false};int useDLACore{-1};int batch{1};std::vector<std::string> dataDirs;//?bool useILoop{false};
};//!
//! \brief Populates the Args struct with the provided command-line parameters.
//!
//! \throw invalid_argument if any of the arguments are not valid
//!
//! \return boolean If return value is true, execution can continue, otherwise program should exit
//!
//解析argv,將所得到的資訊存入args這個struct Args型別的變數內
//如果出錯會回傳false,反之則回傳true
inline bool parseArgs(Args& args, int argc, char* argv[])
{//無窮迴圈。等到參數解析完了,arg會自動變成-1,接著跳出迴圈while (1){int arg;static struct option long_options[] = {{"help", no_argument, 0, 'h'}, {"datadir", required_argument, 0, 'd'},{"int8", no_argument, 0, 'i'}, {"fp16", no_argument, 0, 'f'}, {"useILoop", no_argument, 0, 'l'},{"useDLACore", required_argument, 0, 'u'}, {"batch", required_argument, 0, 'b'}, {nullptr, 0, nullptr, 0}};int option_index = 0;// getopt_long用於解析傳入的參數,定義於Linux系統自帶的getopt.h,// 亦或是samples/common/windows/getopt.c// 在getopt_long被呼叫的過程中,option_index會隨之變化,代表該次解析的是long_options裡的第幾個參數//getopt_long的回傳值是struct option裡最後一個元素,即'h','d','i','f','l','u','b'或0//注意到d後面跟著一個冒號,這代表-d後面還跟著一個參數//為何這裡optstring只有'h','d','i','u',少了'f','l','b'及0?arg = getopt_long(argc, argv, "hd:iu", long_options, &option_index);// 如果所有可選參數都解析完了,則getopt_long回傳-1if (arg == -1){break;}switch (arg){//parseArgs的參數args傳入時是空的,在這個block裡依不同情況來為args設值case 'h': args.help = true; return true;case 'd'://optarg定義於samples/common/windows/getopt.c,由getopt_long間接地被賦值,表示可選參數//-d後跟著的參數會被解析到optarg內if (optarg){args.dataDirs.push_back(optarg);}else{std::cerr << "ERROR: --datadir requires option argument" << std::endl;return false;}break;case 'i': args.runInInt8 = true; break;//'f'不在optstring內,能被解析出來?case 'f': args.runInFp16 = true; break;//?//'l'不在optstring內,能被解析出來?case 'l': args.useILoop = true; break;case 'u'://在optstring中,u後面沒加冒號,為何這裡可以有參數?if (optarg){args.useDLACore = std::stoi(optarg);}break;case 'b'://'b'不在optstring內,能被解析出來?if (optarg){args.batch = std::stoi(optarg);}break;//如果傳入的參數不是上面的任一個,則程序執行失敗default: return false;}}return true;
}} // namespace samplesCommon#endif // TENSORRT_ARGS_PARSER_H

namespace

代碼中定義了名為samplesCommon的namespace。關於namespace,詳見C++ namespace。

struct的繼承

CaffeSampleParams結構體是繼承自SampleParams結構體,
關於struct的繼承,詳見C++ struct的繼承。

caffe特有參數

  • prototxtFileName:預期的參數是以.prototxt結尾的檔名,代表網路結構定義

  • weightsFileName:預期的參數是以.caffemodel結尾的檔名,代表網路權重

  • meanFileName:預期的參數是以.binaryproto結尾的檔名,代表圖像的平均值

UFF格式

摘自4.3.1. Example 1: Adding A Custom Layer To A TensorFlow Model:

In order to run a TensorFlow network with TensorRT, you must first convert it to the UFF format.

UFF格式似乎是為了將TensorFlow與TensorRT的模型互相轉換而定義的一種中間表示方式?

不需要typedef struct xxx yyy?

記得在C語言中,定義完一個struct之後,我們通常還會加上typedef,用以簡化之後與該struct相關的代碼。但是在argsParser.h中,定義完Args這個結構體後,我們卻可以直接在parseArgs函數裡使用它,這是為什麼呢?詳見C++ typedef struct v.s. struct。

struct初始化

parseArgs函數中定義了一個struct option型別的變數long_options,對它初始化的方式就像在初始化array一樣。關於struct的初始化方式,詳見C++ struct初始化。

struct option,getopt_long

parseArgs函數中定義了一個struct option型別的變數long_options,還用到了getopt_long這個函數,詳見C getopt.h。

inline

parseArgs函數被宣告為inline function,關於inline function,詳見C++ inline function。

DLA

NVDLA是NVIDIA開源的深度學習加速架構,包含了硬件及軟件部份。

參考連結

4.3.1. Example 1: Adding A Custom Layer To A TensorFlow Model

NVDLA

THE NVIDIA DEEP LEARNING ACCELERATOR

nvdla硬件

nvdla軟件

C++ namespace

C++ struct的繼承

C++ typedef struct v.s. struct

C++ struct初始化

C getopt.h

C++ inline function

TensorRT/samples/common/argsParser.h源碼研讀相关推荐

  1. PCL - MLS代碼研讀(十五)- VOXEL_GRID_DILATION上採樣方法

    PCL - MLS代碼研讀(十五)- VOXEL_GRID_DILATION上採樣方法 前言 成員變數 MLSVoxelGrid MLSVoxelGrid建構子 dilate函數 getter &am ...

  2. React 源碼解析 - Fiber/Reconcile 系列:Fiber 與 Diff

    React 源碼解析 - Fiber/Reconcile 系列:Fiber 與 Diff 前言 正文 從 DOM 到 Fiber 對象 DOM VDOM React 元素(React Element) ...

  3. Android小項目之---時間線程應用(附源碼)

    进程 在Android 中,进程是应用程序的完整实现,而不是用户通常了解的那样.他们主要用途很简单: • 提高稳定性和安全性,将不信任或者不稳定的代码移动到其他进程. • 可将多个.apk 包运行在同 ...

  4. asp.net源碼坊論壇上線

    asp.net源碼坊論壇上線http://bbs.csaspx.com 转载于:https://www.cnblogs.com/liubo175175/p/3651818.html

  5. Android小項目之---吃飯選哪?--》選擇對話框(附源碼)

    還記得早先我們做的記算器的例子嗎?當中的驗證判斷用到了對話框,今天我們來做一個不一樣的對話框,要做的這個小例子是一個可供選擇效果的對話框 即層層迭迭的Alert Dialog:界面方面我們擺放一個Bu ...

  6. epoll.h 源码记录

    epoll.h源码: /* Copyright (C) 2002-2013 Free Software Foundation, Inc. This file is part of the GNU C ...

  7. Android小項目之---ListView实现论坛管理效果(附源碼)

    在android系統中,ListView的用法稍微複雜一點,配置Adpater就有幾種方法,如ArrayAdapter,SimpleAdapter等.查了一些網上的相關例子,有很多都是用ListAct ...

  8. Android 小項目之--猜名字有獎!RadionButton 和RadionGroup應用(附源碼)

    有想過友Android的小屏幕上把asp.net 的RadionButtonList搬進來用嗎?答案是肯定的,Android 的Tool工具提供了一個RadioGroup控件,可將各大自不同的Radi ...

  9. 机械祭天法力无边:C++primer之书店程序包含Sales_item.h源码

    好吧,作为一个老菜鸟,还有第一章几道题实在写不下去了,直接写书店程序,这个书店程序只能满足2 2  2 3 3 这种连续的情况,无法满足2 3 2 3这种情况. 首先上Sales_item.h头文件源 ...

最新文章

  1. 【Python】数据科学家提高效率的 40 个 Python 技巧
  2. JDK1.8源码(二)——java.lang.Integer 类
  3. vue-devtools chrome 开发工具
  4. hello-world
  5. Python操作mySql数据库封装类
  6. .NET的轻量级IOC框架芮双随笔
  7. Kubernetes-命名空间namespace(ns)(十四)
  8. java——Random类和Math.Rondom
  9. 解决报错(4种情况):0x00007FF614F73B96 处(位于 XXX.exe 中)引发的异常: Microsoft C++ 异常: cv::Exception,位于内存位置 0x000XXX
  10. php rsa2 pkcs8,关于pkcs8 与 rsa 互转的一个坑
  11. 微信自定义分享链接内容,wx.updateAppMessageShareData、wx.updateTimelineShareData、wx.onMenuShareTimeline
  12. Hadoop集群搭 Hadoop分布式文件系统架构和设计
  13. Nature:为什么免疫系统可产生多样性抗体和T细胞受体?
  14. Windows日志分析(中)
  15. 网络安全工程师有没有发展前景?
  16. Python_机器学习_常用科学计算库_第6章_ Seaborn+综合案例
  17. 电控测试团队建设回顾
  18. 达内培训python 好不好
  19. 如果想入手软路由我推荐友善官方出品的NanoPi-R2S和NanoPi-R5S
  20. BLDC电机FOC控制技术学习笔记

热门文章

  1. 知乎里面别人发的视频怎么保存下来?
  2. 不完美才美—少有人知道的幸福之路
  3. mysql的结构化编程_月光软件站 - 编程文档 - 其他语言 - 结构化子查询:在mysql4.1中的应用...
  4. [宋史学习] 对西夏战争中暴露出的积弱形势 范仲淹
  5. F1C100S裸奔nes游戏模拟器-f1c100s裸跑-WINDOWS下keil-MDK纯裸跑 回味80后的小幸福
  6. 计算机网络:P4.1-网络层(上)
  7. 快讯:今日一颗小行星2014 RC掠过地球轨道
  8. 深入浅出matplotlib(12):椭圆示意图
  9. 北京买房的10点建议
  10. 【2021 MCM】 Problem A: Fungi by 2100454