1#先看ONNX 查看大概结构
首先我们来看一下这个onnx:
可以看到这是一个全卷积网络,因为输入输出的宽高都一样
后来的这里是加入了softmax,所以获得的是概率值,所以最终的输出output是1*512*596*4的概率值。仅仅是不确定四个通道代表什么?
有以下可能:
1#车道线2#可行使区域
3#不可行使区域
4#马路牙子
2#查看预处理方法
#elif defined(MODEL_TYPE_ONNX)
#include "inference_helper_tensorrt.h" // to call SetDlaCore
#define MODEL_NAME "road-segmentation-adas-0001.onnx"
#define TENSORTYPE TensorInfo::kTensorTypeFp32
#define INPUT_NAME "data"
#define INPUT_DIMS { 1, 512, 896, 3 }
#define IS_NCHW false
#define IS_RGB false
#define OUTPUT_NAME "tf.identity"
这里就能看到首先输入的名字是data,输入是1*512*896*3,输出的name是identity。至今为止一切都和onnx对的上
input_tensor_info.normalize.mean[0] = 0;input_tensor_info.normalize.mean[1] = 0;input_tensor_info.normalize.mean[2] = 0;input_tensor_info.normalize.norm[0] = 1 / 255.f;input_tensor_info.normalize.norm[1] = 1 / 255.f;input_tensor_info.normalize.norm[2] = 1 / 255.f;input_tensor_info_list_.push_back(input_tensor_info);
这里是做了一个norm
int32_t SemanticSegmentationEngine::Process(const cv::Mat& original_mat, Result& result)
{
#ifndef ENABLE_SEGMENTATIONreturn kRetOk;
#endifif (!inference_helper_) {PRINT_E("Inference helper is not created\n");return kRetErr;}/*** PreProcess ***/const auto& t_pre_process0 = std::chrono::steady_clock::now();InputTensorInfo& input_tensor_info = input_tensor_info_list_[0];/* do resize and color conversion here because some inference engine doesn't support these operations */int32_t crop_x = 0;int32_t crop_y = 0;int32_t crop_w = original_mat.cols;int32_t crop_h = original_mat.rows;cv::Mat img_src = cv::Mat::zeros(input_tensor_info.GetHeight(), input_tensor_info.GetWidth(), CV_8UC3);CommonHelper::CropResizeCvt(original_mat, img_src, crop_x, crop_y, crop_w, crop_h, IS_RGB, CommonHelper::kCropTypeStretch);//CommonHelper::CropResizeCvt(original_mat, img_src, crop_x, crop_y, crop_w, crop_h, IS_RGB, CommonHelper::kCropTypeCut);//CommonHelper::CropResizeCvt(original_mat, img_src, crop_x, crop_y, crop_w, crop_h, IS_RGB, CommonHelper::kCropTypeExpand);
然后就是process了,因为我的cropx和y都是0,而且type是Stretch,所以这里是仅仅只做了一个resize,其他的都没作。
将process打开
因为swap_color是false,所以BGR----》RGB 不会做
最终,因为作者要提速:
/* Convert normalize parameter to speed up */for (auto& input_tensor_info : input_tensor_info_list) {ConvertNormalizeParameters(input_tensor_info);}
所以又将norm变回去了。
手写predict
import onnxruntime
import cv2
import numpy as np
session = onnxruntime.InferenceSession("workspace/road-segmentation-adas.onnx",providers=["CPUExecutionProvider"])
image= cv2.imread("workspace/imgs/dashcam_00.jpg")
image = cv2.resize(image,(896,512))
image_tensor = image.astype(np.float32)
image_tensor = image_tensor.transpose(2,0,1)[None]
prob = session.run(["tf.identity"],{"data":image_tensor}
)[0]cv2.imwrite("prob.jpg" , prob[0 , : , : , 0])
cv2.imwrite("prob.jpg" , prob[0 , : , : , 1] *255)
cv2.imwrite("prob.jpg" , prob[0 , : , : , 2] *255)
cv2.imwrite("prob.jpg" , prob[0 , : , : , 3] *255)