NANO Hackathon 问答

本次NANO Hackathon活动主要向用户推广在JetsonNANO平台上利用TensorRT,TransferLearning Toolkit这两个开发工具,在实际应用中解决深度学习推理和训练上的问题。本次活动以口罩检测为主题,学生利用TransferLearning Toolkit训练ssd_mobilenet_v2等目标检测模型,并把训练好的模型利用TensorRT部署在Jetson NANO平台上来对视频进行人脸口罩检测。以下是从活动的微信群,webex会议等渠道收集概括的学生提出的问题。主要帮助学生解决了环境部署,API调用,代码调优等方面产生的问题。

  1. TensorRT哪里能找到比较好的教程?最好是中文的

目前建议最好的教程就是官方的Development Guide,您可以参考:

https://docs.nvidia.com/deeplearning/tensorrt/developer-guide/index.html

目前没有中文版,只有英文版。如果有更多问题,可以访问NVIDIA TensorRT的论坛:

https://forums.developer.nvidia.com/c/ai-deep-learning/libraries-sdk/tensorrt/92

  1. TensorRT目前在Python版本中支持的好吗?

目前TensorRT是能够很好的支持Python的,我们有SDK中有很多Python的例子。如果对于Python的结构细节,可以访问:

https://docs.nvidia.com/deeplearning/tensorrt/api/python_api/index.html

  1. TensorRT目前支持优化YOLO v3吗?

目前能够支持YOLO V3的

  1. 麻烦推荐一下比较好的TensorRT比较好的安装教程?

这里推荐您官方提供的安装教程,也是最全的。目前官方支持到最新的7.1.3版本,您可以访问下面的连接:

https://docs.nvidia.com/deeplearning/tensorrt/archives/index.html#trt_7.1

  1. TensorRT的相关性能测试标准有吗?

您可以访问西面的连接查看:

https://developer.nvidia.com/deep-learning-performance-training-inference#deeplearningperformance_inference

  1. TensorRT优化SSD的代码能提供一下吗?

您可以直接下载您访问的NANO上面的ipython文件,那个里面提供了详细的TensorRT优化SSD的代码。

  1. TensorRT的版本可以向下支持吗?

TensorRT大部分内容可以向下支持,不过会在一些部分做优化,导致有些功能在使用最新的方法后会有更好的性能提升

  1. TensorRT支持Pytorch吗?

可以支持的,您可了解一下TensorRT优化ONNX格式的模型。您在SDK的sample里面找到。

  1. TensorRT目前支持GeForce系列显卡吗?

目前您可以利用GeForce显卡来优化推理引擎,不过使用Tesla系列的显卡能让您得到更好的性能提升

  1. TLT怎么安装?

您可以访问下面的网址:

https://docs.nvidia.com/metropolis/TLT/tlt-getting-started-guide/index.html

这里面会详细的介绍

  1. TLT导出的模型可以直接转换成TensorRT格式吗?

TLT导出的模型经过tlt-converter工具转换后,可以直接利用TensorRT来执行推理任务。不过您需要在您部署的设备上利用tlt-converter转换。

  1. TLT有开源代码吗?

TLT目前的提供的是镜像的安装方式,没有提供源代吗。

  1. TLT可以不通过Docker安装吗?

TLT目前只能通过DOCKER的方式安装

  1. TLT可以再Geforce系列显卡上安装吗?

目前可以部署在GeForce系列显卡的机器上

  1. TLT装换工具tlt-converter的参数含义?
tlt-converter [-h] -k<encryption_key>
   -d <input_dimensions>
   -o <comma separated outputnodes>
   [-c <path to calibration cachefile>]
   [-e <path to outputengine>]
   [-b <calibration batchsize>]
   [-m <maximum batch size of theTRT engine>]
   [-t <engine datatype>]
   [-w <maximum workspace size ofthe TRT Engine>]
   [-i <input dimensionordering>]
   input_file

Required arguments:

•        input_file: Path to the model exported using tlt-export.
•        -k: The API key used to configure the ngc cli to download the models.
•        -d: Comma-separated list of input dimensions that should match the dimensions used for tlt-export. Unlike tlt-export this cannot be inferred from calibration data.
•        -o: Comma-separated list of output blob names that should match the output configuration used for tlt-export.
o        For classification use: predictions/Softmax.
o        For DetectNet_v2: output_bbox/BiasAdd,output_cov/Sigmoid
o        For FasterRCNN: dense_class_td/Softmax,dense_regress_td/BiasAdd, proposal
o        For SSD, DSSD, RetinaNet: NMS
o        For YOLOv3: BatchedNMS

Optional arguments:

·        -e:Path to save the engine to. (default: ./saved.engine)
·        -t:Desired engine data type, generates calibration cache if in INT8 mode. Thedefault value is fp32.The options are {fp32, fp16, int8}
·        -w:Maximum workspace size for the TensorRT engine. The default value is 1<<30.
·        -i:Input dimension ordering, all other tlt commands use NCHW. The default value isnchw. The options are {nchw, nhwc, nc}.

INT8 Mode Arguments:

·        -c:Path to calibration cache file, only used in INT8 mode. The default valueis ./cal.bin.
·        -b:Batch size used during the tlt-export step for INT8 calibration cachegeneration. (default: 8).
·        -m:Maximum batch size of TensorRT engine. The default value is 16.

您可以参考以上内容。或者访问下面的网址:

https://docs.nvidia.com/metropolis/TLT/tlt-getting-started-guide/index.html得到更多的内容

  1. TLT支持的目标检测的数据集的标注格式是什么?

目前支持的KITTI的数据标注格式,我们的Hackathon项目文件夹里有提供格式转换的工具

  1. TLT剪枝的具体使用方法?
tlt-prune [-h] -pm <pretrained_model>
   -o<output_file> -k <key>
   [-n<normalizer>]
   [-eq<equalization_criterion>]
   [-pg<pruning_granularity>]
   [-pth <pruningthreshold>]
   [-nf<min_num_filters>]
   [-el[<excluded_list>]

您可以访问下面的网址得到详细信息及使用方法:

https://docs.nvidia.com/metropolis/TLT/tlt-getting-started-guide/index.html#pruning_models

  1. TLT数据集图片格式?

目前支持PNG,JPG和JPEG,我们在Hackathon项目的文件目录下也提供了格式转换的工具和图片检查的工具

  1. TLT转换的模型再利用TensorRT做推理任务时总是输出不对?

这个错误可能由很多问题导致,建议通过以下方面进行检查:

A. 模型是否完整,有些时候模型在没传输完就被打断了

B. 输入维度是否正确

C. 输入维度的顺序是否正确

比如:tlt-converter里面-d参数后面的顺序是channel,height,width但是很多resize函数设置的都是width,height

D.检查一下输出的网络层是否正确

比如:SSD的输出层是NMS,而YOLOV3的输出层是BatchedNMS,您可以参考下面的内容:

For classification use:predictions/Softmax.
For DetectNet_v2:output_bbox/BiasAdd,output_cov/Sigmoid
For FasterRCNN:dense_class_td/Softmax,dense_regress_td/BiasAdd, proposal
For SSD, DSSD, RetinaNet: NMS
For YOLOv3: BatchedNMS
  1. TLT镜像下载太慢怎么办?

我们将镜像已经下载下来,您可以通过我们的地址进行下载来解决网络问题

  1. TLT预训练模型用examples里面的命令下载太慢怎么办?

您可以直接通过NGC官网下载,地址如下:

https://ngc.nvidia.com/catalog/models/nvidia:tlt_pretrained_object_detection

  1. TLT可以直接导出FP16 or INT8的模型吗?

TLT可以直接导出FP16和INT8模型

  1. TLT导出的FP16 or INT8的模型可以直接在NANO上部署吗?

TLT导出的FP16模型可以直接部署在NANO上,但是INT8模型不能部署在NANO上。您可以是用Xavier来部署

  1. 这次Hackathon训练的口罩检测使用的是什么模型?

本次Hackathon训练的口罩检测可以自己选择模型,例子中使用的是SSD-mobilenetV2模型

  1. 这次的Hackathon训练的口罩检测需要多少数据集?

训练数据集越多越好,例子中使用的是7000张左右的训练样本

  1. 这次的Hackathon的任务可以自己更换网络吗?

本次的Hackathon的任务可以自己更换网络,并且非常支持同学自己了解更多的其他的网络模型。您可以通过TLT的预训练模型库进行下载。

  1. 能举例说明如何转换TLT训练好的模型到TensorRT

注意:-d后面跟的是channel,height和width,这个顺序不能错

%set_envKEY=cTJhcms3ODdvbHRsOWwxMTNvYW0yN3NuaHA6YjU2YjE1ZjgtNDk5Ni00NTY5LWE0MDAtMjMwNzgwZWE5MGFj
!./tlt-converter -k $KEY \
   -d 3,300,300 \
   -o NMS \
   -essd/TRT_ssd_mobilenet_v2_face.bin \
   -m 1 \
   -t fp32 \
   -i nchw \
   ssd_mobilenet_v2_epoch_040.etlt
  1. 在利用TLT训练的时候loss一直不下降怎么办?

可以增大训练数据集,也可以调节训练设置当中的学习率,如下部分:

training_config {
  batch_size_per_gpu: 64
  num_epochs: 100
  learning_rate {
  soft_start_annealing_schedule {
   min_learning_rate: 5e-7
   max_learning_rate: 5e-3
   soft_start: 0.1
   annealing: 0.3
   }
  }
  1. 能否共享一下示例代码中训练模型的设置文件?
random_seed: 42
ssd_config {
  aspect_ratios_global: "[1.0, 2.0, 0.5, 3.0, 1.0/3.0]"
  scales: "[0.05, 0.1, 0.25, 0.4, 0.55, 0.7, 0.85]"
  two_boxes_for_ar1: true
  clip_boxes: false
  loss_loc_weight: 0.8
  focal_loss_alpha: 0.25
  focal_loss_gamma: 2.0
  variances: "[0.1, 0.1, 0.2, 0.2]"
  arch: "mobilenet_v2"
  freeze_bn: false
}
training_config {
  batch_size_per_gpu: 64
  num_epochs: 100
  learning_rate {
  soft_start_annealing_schedule {
   min_learning_rate: 5e-7
   max_learning_rate: 5e-3
   soft_start: 0.1
   annealing: 0.3
   }
  }
  regularizer {
   type: L2
   weight: 0.00004
  }
}
eval_config {
  validation_period_during_training: 5
  average_precision_mode: SAMPLE
  batch_size: 32
  matching_iou_threshold: 0.5
}
nms_config {
  confidence_threshold: 0.01
  clustering_iou_threshold: 0.6
  top_k: 200
}
augmentation_config {
  preprocessing {
   output_image_width: 300
   output_image_height: 300
   output_image_channel: 3
   crop_right: 300
   crop_bottom: 300
   min_bbox_width: 1.0
   min_bbox_height: 1.0
  }
  spatial_augmentation {
   hflip_probability: 0.5
   vflip_probability: 0.0
   zoom_min: 0.7
   zoom_max: 1.8
   translate_max_x: 8.0
   translate_max_y: 8.0
  }
  color_augmentation {
   hue_rotation_max: 25.0
   saturation_shift_max: 0.20000000298
   contrast_scale_max: 0.10000000149
   contrast_center: 0.5
  }
}
dataset_config {
  data_sources: {
   tfrecords_path: "/workspace-hekun/mydata/tlt-tensorrt-nano/tfrecords/kitti_trainval/kitti_trainval*"
   image_directory_path: "/workspace-hekun/mydata/tlt-tensorrt-nano/data/train/"
  }
  image_extension: "JPEG"
  target_class_mapping {
   key: "face_mask"
   value: "face_mask"
  }
  target_class_mapping {
   key: "face"
   value: "face"
  }
validation_fold: 0
}
  1. 如何设置TLT模型剪枝的-pth参数?

这个在样例代码中可以看到,通常,我们需要设置‘-pth’参数来平衡模型的精度与模型的大小(或者说速度)。更高的‘-pth’数值,会让模型更小(更快的推理速度),但是也会降低模型精度。在本次实验中,作者使用的是0.5,如果精度没问题,我们可以增加‘-pth’的数值,来进一步剪枝。反之,我们则需要减小‘-pth’的数值。

  1. 如何设置推理任务的代码?
def _create_context(self):
   for binding in self.engine:
   size = trt.volume(self.engine.get_binding_shape(binding)) * \
   self.engine.max_batch_size
   host_mem = cuda.pagelocked_empty(size, np.float32)
   cuda_mem = cuda.mem_alloc(host_mem.nbytes)
   self.bindings.append(int(cuda_mem))
   if self.engine.binding_is_input(binding):
   self.host_inputs.append(host_mem)
   self.cuda_inputs.append(cuda_mem)
   else:
   self.host_outputs.append(host_mem)
   self.cuda_outputs.append(cuda_mem)
   print(len(cuda_mem))
   return self.engine.create_execution_context()

   def detect(self, img, conf_th=0.3):
   """Detect objects in the input image."""
   img_resized = _preprocess_trt(img, self.input_shape)
   np.copyto(self.host_inputs[0], img_resized.ravel())
   #将处理好的图片从CPU内存中复制到GPU显存
   cuda.memcpy_htod_async(
   self.cuda_inputs[0], self.host_inputs[0], self.stream)
   #开始执行推理任务
   self.context.execute_async(
   batch_size=1,
   bindings=self.bindings,
   stream_handle=self.stream.handle)
   #将推理结果输出从GPU显存复制到CPU内存
   cuda.memcpy_dtoh_async(
   self.host_outputs[1], self.cuda_outputs[1], self.stream)
   cuda.memcpy_dtoh_async(
   self.host_outputs[0], self.cuda_outputs[0], self.stream)
   self.stream.synchronize()


   output = self.host_outputs[0]
   return _postprocess_trt(img, output, conf_th, self.output_layout)
  1. 如何不使用TLT手动的构建TensorRT推理引擎?
with trt.Builder(TRT_LOGGER) as builder, builder.create_network() as network, trt.UffParser() as parser:
   builder.max_workspace_size = 1 << 28
   builder.max_batch_size = 1
   builder.fp16_mode = True

   parser.register_input('Input', INPUT_DIMS)
   parser.register_output('MarkOutput_0')
   parser.parse(spec['tmp_uff'], network)
   engine = builder.build_cuda_engine(network)