Commit 8e6414e6 authored by shizhm's avatar shizhm
Browse files

修改动态推理C++示例

parent 3157901f
...@@ -54,15 +54,15 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ ...@@ -54,15 +54,15 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
yolov5Parameter.numberOfClasses=(int)netNode["NumberOfClasses"]; yolov5Parameter.numberOfClasses=(int)netNode["NumberOfClasses"];
useFP16=(bool)(int)netNode["UseFP16"]; useFP16=(bool)(int)netNode["UseFP16"];
if(Exists(modelPath)==false)
{
LOG_ERROR(stdout,"%s not exist!\n",modelPath.c_str());
return MODEL_NOT_EXIST;
}
// 加载模型
if(dynamic) if(dynamic)
{ {
// 加载模型
if(Exists(modelPath)==false)
{
LOG_ERROR(stdout,"%s not exist!\n",modelPath.c_str());
return MODEL_NOT_EXIST;
}
migraphx::onnx_options onnx_options; migraphx::onnx_options onnx_options;
onnx_options.map_input_dims["images"]={1,3,800,800};// onnx_options.map_input_dims["images"]={1,3,800,800};//
net = migraphx::parse_onnx(modelPath, onnx_options); net = migraphx::parse_onnx(modelPath, onnx_options);
...@@ -83,6 +83,12 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ ...@@ -83,6 +83,12 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
} }
else else
{ {
// 加载模型
if(Exists(modelPath)==false)
{
LOG_ERROR(stdout,"%s not exist!\n",modelPath.c_str());
return MODEL_NOT_EXIST;
}
net = migraphx::parse_onnx(modelPath); net = migraphx::parse_onnx(modelPath);
LOG_INFO(stdout,"succeed to load model: %s\n",GetFileName(modelPath).c_str()); LOG_INFO(stdout,"succeed to load model: %s\n",GetFileName(modelPath).c_str());
...@@ -142,11 +148,12 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ ...@@ -142,11 +148,12 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
classNames.resize(yolov5Parameter.numberOfClasses); classNames.resize(yolov5Parameter.numberOfClasses);
} }
return SUCCESS; return SUCCESS;
} }
ErrorCode DetectorYOLOV5::Detect(const cv::Mat &srcImage, std::vector<ResultOfDetection> &resultsOfDetection, bool dynamic) ErrorCode DetectorYOLOV5::Detect(const cv::Mat &srcImage, std::vector<std::size_t> &relInputShape, std::vector<ResultOfDetection> &resultsOfDetection, bool dynamic)
{ {
if(srcImage.empty()||srcImage.type()!=CV_8UC3) if(srcImage.empty()||srcImage.type()!=CV_8UC3)
{ {
...@@ -154,33 +161,16 @@ ErrorCode DetectorYOLOV5::Detect(const cv::Mat &srcImage, std::vector<ResultOfDe ...@@ -154,33 +161,16 @@ ErrorCode DetectorYOLOV5::Detect(const cv::Mat &srcImage, std::vector<ResultOfDe
return IMAGE_ERROR; return IMAGE_ERROR;
} }
// 数据预处理 // 数据预处理并转换为NCHW格式
inputSize = cv::Size(relInputShape[3], relInputShape[2]);
cv::Mat inputBlob; cv::Mat inputBlob;
std::vector<std::size_t> relInputShape; cv::dnn::blobFromImage(srcImage,
int height, width;
if(dynamic)
{
width = srcImage.rows;
height = srcImage.cols;
relInputShape = {1,3,height,width};
cv::dnn::blobFromImage(srcImage,
inputBlob,
1 / 255.0,
cv::Size(width, height),
cv::Scalar(0, 0, 0),
true,
false);
}
else
{
cv::dnn::blobFromImage(srcImage,
inputBlob, inputBlob,
1 / 255.0, 1 / 255.0,
inputSize, inputSize,
cv::Scalar(0, 0, 0), cv::Scalar(0, 0, 0),
true, true,
false); false);
}
// 创建输入数据 // 创建输入数据
migraphx::parameter_map inputData; migraphx::parameter_map inputData;
...@@ -193,6 +183,7 @@ ErrorCode DetectorYOLOV5::Detect(const cv::Mat &srcImage, std::vector<ResultOfDe ...@@ -193,6 +183,7 @@ ErrorCode DetectorYOLOV5::Detect(const cv::Mat &srcImage, std::vector<ResultOfDe
inputData[inputName]= migraphx::argument{inputShape, (float*)inputBlob.data}; inputData[inputName]= migraphx::argument{inputShape, (float*)inputBlob.data};
} }
// 推理 // 推理
std::vector<migraphx::argument> inferenceResults = net.eval(inputData); std::vector<migraphx::argument> inferenceResults = net.eval(inputData);
......
...@@ -25,7 +25,7 @@ public: ...@@ -25,7 +25,7 @@ public:
ErrorCode Initialize(InitializationParameterOfDetector initializationParameterOfDetector, bool dynamic); ErrorCode Initialize(InitializationParameterOfDetector initializationParameterOfDetector, bool dynamic);
ErrorCode Detect(const cv::Mat &srcImage, std::vector<ResultOfDetection> &resultsOfDetection, bool dynamic); ErrorCode Detect(const cv::Mat &srcImage, std::vector<std::size_t> &relInputShape, std::vector<ResultOfDetection> &resultsOfDetection, bool dynamic);
private: private:
cv::FileStorage configurationFile; cv::FileStorage configurationFile;
......
...@@ -65,11 +65,14 @@ void Sample_YOLOV5() ...@@ -65,11 +65,14 @@ void Sample_YOLOV5()
// 读取测试图片 // 读取测试图片
cv::Mat srcImage = cv::imread("../Resource/Images/DynamicPics/image1.jpg",1); cv::Mat srcImage = cv::imread("../Resource/Images/DynamicPics/image1.jpg",1);
// 静态推理固定尺寸
std::vector<std::size_t> inputShape={1,3,608,608};
// 推理 // 推理
std::vector<migraphxSamples::ResultOfDetection> predictions; std::vector<migraphxSamples::ResultOfDetection> predictions;
double time1 = cv::getTickCount(); double time1 = cv::getTickCount();
detector.Detect(srcImage, predictions, false); detector.Detect(srcImage,inputShape,predictions,false);
double time2 = cv::getTickCount(); double time2 = cv::getTickCount();
double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency(); double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency();
LOG_INFO(stdout, "inference time:%f ms\n", elapsedTime); LOG_INFO(stdout, "inference time:%f ms\n", elapsedTime);
...@@ -119,7 +122,7 @@ void Sample_YOLOV5_Dynamic() ...@@ -119,7 +122,7 @@ void Sample_YOLOV5_Dynamic()
cv::glob(folder,imagePathList); cv::glob(folder,imagePathList);
for (int i = 0; i < imagePathList.size(); ++i) for (int i = 0; i < imagePathList.size(); ++i)
{ {
cv::Mat srcImage=cv::imread(imagePathList[i], 1); cv:: Mat srcImage=cv::imread(imagePathList[i], 1);
srcImages.push_back(srcImage); srcImages.push_back(srcImage);
} }
...@@ -130,13 +133,10 @@ void Sample_YOLOV5_Dynamic() ...@@ -130,13 +133,10 @@ void Sample_YOLOV5_Dynamic()
for (int i = 0; i < srcImages.size(); ++i) for (int i = 0; i < srcImages.size(); ++i)
{ {
// 生成不同尺寸的图像
cv::resize(srcImages[i], srcImages[i], cv::Size(inputShapes[i][3], inputShapes[i][2]));
// 推理 // 推理
std::vector<migraphxSamples::ResultOfDetection> predictions; std::vector<migraphxSamples::ResultOfDetection> predictions;
double time1 = cv::getTickCount(); double time1 = cv::getTickCount();
detector.Detect(srcImages[i], predictions, true); detector.Detect(srcImages[i], inputShapes[i], predictions, true);
double time2 = cv::getTickCount(); double time2 = cv::getTickCount();
double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency(); double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency();
LOG_INFO(stdout, "inference image%d time:%f ms\n", i, elapsedTime); LOG_INFO(stdout, "inference image%d time:%f ms\n", i, elapsedTime);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment