提升网站性能,生活信息网站如何推广,太原建设厅网站,广东睿营建设有限公司网站ResNet50是一种深度残差网络#xff0c;50表示包含50层深度。该模型可以用于图像分类#xff0c;物体检测等。
现在用DeepStream测试ResNet50分类模型。
1 资源
模型地址#xff1a;https://github.com/onnx/models/blob/main/vision/classification/resnet/model/resnet…ResNet50是一种深度残差网络50表示包含50层深度。该模型可以用于图像分类物体检测等。
现在用DeepStream测试ResNet50分类模型。
1 资源
模型地址https://github.com/onnx/models/blob/main/vision/classification/resnet/model/resnet50-v2-7.onnx,模型信息详见https://github.com/onnx/models/tree/main/vision/classification/resnet。
label文件https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a
TensorRT的python测试代码https://github.com/NVIDIA/TensorRT/blob/release/8.6/samples/python/introductory_parser_samples/onnx_resnet50.py nvinfer配置文件dstest_appsrc_config.txt [property] gpu-id0 labelfile-pathlabels.txt model-engine-fileresnet50.onnx_b1_gpu0_fp16.engine onnx-fileresnet50-v2-7.onnx infer-dims3;224;224 net-scale-factor0.01742919 offsets114.75;114.75;114.75 network-type1 input-object-min-width64 input-object-min-height64 model-color-format1 #gie-unique-id2 #operate-on-gie-id1 #operate-on-class-ids0 #classifier-async-mode1 #classifier-threshold0.51 #force-implicit-batch-dim1 batch-size1 network-mode1 num-detected-classes1000 interval0 gie-unique-id1 output-blob-names495 #scaling-filter0 #scaling-compute-hw0 cluster-mode2 is-classifier1 [class-attrs-all] pre-cluster-threshold0.2 topk20 nms-iou-threshold0.5 测试图片broom.JPG: 如果要用DeepStream跑这个模型只需要修改nvifner的配置文件。现在问题是怎么python处理转成nvinfer的配置文件。有几个注意的地方
# 归一化
Python版的归一化是按这个公式https://github.com/NVIDIA/TensorRT/blob/release/8.6/samples/python/introductory_parser_samples/onnx_resnet50.py#L73 # This particular ResNet50 model requires some preprocessing, specifically, mean normalization. return (image_arr / 255.0 - 0.45) / 0.225 而nvinfer支持的是这个公式 y net scale factor*(x-mean) 这就需要把分子分母同乘个数最终变为y0.01742919 * (x - 114.75).
#第一个模型为分类模型
#nvinfer的大部分例子第一个模型都是检测模型这个例子第一个模型为分类模型。需要做如下设置 network-type1 #后处理 python例子中要对推理后的数据要做个argmax操作也就是从1000个结果里取可能性最大的。如果nvifner没有设parse-bbox-func-name, 那插件用的resnet的bbox解析函数刚好就是从可能性里找最大的。
2 运行
将模型resnet50-v2-7.onnxdstest_appsrc_config.txt测试图片放在一起后执行命令
gst-launch-1.0 filesrc locationbroom.JPG ! jpegdec ! videoconvert ! video/x-raw,formatI420 ! nvvideoconvert ! video/x-raw\(memory:NVMM\),formatNV12 ! mux.sink_0 nvstreammux namemux batch-size1 width1280 height720 ! nvinfer config-file-path./dstest_appsrc_config.txt ! nvvideoconvert ! video/x-raw\(memory:NVMM\),formatRGBA ! nvdsosd ! nvvideoconvert ! video/x-raw,formatI420 ! jpegenc ! filesink locationout.jpg
3 问题
执行命令后发现生成的图片也没有分类的字符串。
nvinfer插件和nvinfer底层库是开源改了源代码之后需要编译替换对应库。在/opt/nvidia/deepstream/deepstream/sources/libs/nvdsinfer/nvdsinfer_context_impl_output_parsing.cpp加点打印之后发现的到的分类是对的索引462在label文件里就是broom只是没有取到分类的标签字符串。打印如下 ...... probability:2.433594, m_ClassifierThreshold:0.000000 probability:1.642578, m_ClassifierThreshold:0.000000 fd, attr.attributeValue:462 ...... nvinfer的label解析函数InferPostprocessor::parseLabelsFile要求文件是以分号相隔的而这个label不是的。所以解析不成nvinfer的代码是开源的用户可以修改这个函数。相关代码如下 NvDsInferStatus InferPostprocessor::parseLabelsFile(const std::string labelsFilePath) { std::ifstream labels_file(labelsFilePath); std::string delim{;}; if (!labels_file.is_open()) ......