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

修改动态推理C++示例

parent 3157901f
......@@ -54,15 +54,15 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
yolov5Parameter.numberOfClasses=(int)netNode["NumberOfClasses"];
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(Exists(modelPath)==false)
{
LOG_ERROR(stdout,"%s not exist!\n",modelPath.c_str());
return MODEL_NOT_EXIST;
}
migraphx::onnx_options onnx_options;
onnx_options.map_input_dims["images"]={1,3,800,800};//
net = migraphx::parse_onnx(modelPath, onnx_options);
......@@ -83,6 +83,12 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
}
else
{
// 加载模型
if(Exists(modelPath)==false)
{
LOG_ERROR(stdout,"%s not exist!\n",modelPath.c_str());
return MODEL_NOT_EXIST;
}
net = migraphx::parse_onnx(modelPath);
LOG_INFO(stdout,"succeed to load model: %s\n",GetFileName(modelPath).c_str());
......@@ -142,11 +148,12 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
classNames.resize(yolov5Parameter.numberOfClasses);
}
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)
{
......@@ -154,33 +161,16 @@ ErrorCode DetectorYOLOV5::Detect(const cv::Mat &srcImage, std::vector<ResultOfDe
return IMAGE_ERROR;
}
// 数据预处理
// 数据预处理并转换为NCHW格式
inputSize = cv::Size(relInputShape[3], relInputShape[2]);
cv::Mat inputBlob;
std::vector<std::size_t> relInputShape;
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,
cv::dnn::blobFromImage(srcImage,
inputBlob,
1 / 255.0,
inputSize,
cv::Scalar(0, 0, 0),
true,
false);
}
// 创建输入数据
migraphx::parameter_map inputData;
......@@ -193,6 +183,7 @@ ErrorCode DetectorYOLOV5::Detect(const cv::Mat &srcImage, std::vector<ResultOfDe
inputData[inputName]= migraphx::argument{inputShape, (float*)inputBlob.data};
}
// 推理
std::vector<migraphx::argument> inferenceResults = net.eval(inputData);
......
......@@ -25,7 +25,7 @@ public:
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:
cv::FileStorage configurationFile;
......
......@@ -65,11 +65,14 @@ void Sample_YOLOV5()
// 读取测试图片
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;
double time1 = cv::getTickCount();
detector.Detect(srcImage, predictions, false);
detector.Detect(srcImage,inputShape,predictions,false);
double time2 = cv::getTickCount();
double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency();
LOG_INFO(stdout, "inference time:%f ms\n", elapsedTime);
......@@ -119,7 +122,7 @@ void Sample_YOLOV5_Dynamic()
cv::glob(folder,imagePathList);
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);
}
......@@ -130,13 +133,10 @@ void Sample_YOLOV5_Dynamic()
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;
double time1 = cv::getTickCount();
detector.Detect(srcImages[i], predictions, true);
detector.Detect(srcImages[i], inputShapes[i], predictions, true);
double time2 = cv::getTickCount();
double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency();
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