Commit e33ed6e7 authored by liucong's avatar liucong
Browse files

修改yolov3工程格式

parent 48ae7535
...@@ -22,18 +22,8 @@ ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializ ...@@ -22,18 +22,8 @@ ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializ
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());
// 获取模型输入/输出节点信息 // 获取模型输入/输出节点信息
std::cout<<"inputs:"<<std::endl;
std::unordered_map<std::string, migraphx::shape> inputs=net.get_inputs(); std::unordered_map<std::string, migraphx::shape> inputs=net.get_inputs();
for(auto i:inputs)
{
std::cout<<i.first<<":"<<i.second<<std::endl;
}
std::cout<<"outputs:"<<std::endl;
std::unordered_map<std::string, migraphx::shape> outputs=net.get_outputs(); std::unordered_map<std::string, migraphx::shape> outputs=net.get_outputs();
for(auto i:outputs)
{
std::cout<<i.first<<":"<<i.second<<std::endl;
}
inputName=inputs.begin()->first; inputName=inputs.begin()->first;
inputShape=inputs.begin()->second; inputShape=inputs.begin()->second;
int N=inputShape.lens()[0]; int N=inputShape.lens()[0];
...@@ -53,7 +43,7 @@ ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializ ...@@ -53,7 +43,7 @@ ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializ
// 编译模型 // 编译模型
migraphx::compile_options options; migraphx::compile_options options;
options.device_id=0; // 设置GPU设备,默认为0号设备 options.device_id=0; // 设置GPU设备,默认为0号设备
options.offload_copy=true; // 设置offload_copy options.offload_copy=true; // 设置offload_copy
net.compile(gpuTarget,options); net.compile(gpuTarget,options);
LOG_INFO(stdout,"succeed to compile model: %s\n",GetFileName(modelPath).c_str()); LOG_INFO(stdout,"succeed to compile model: %s\n",GetFileName(modelPath).c_str());
...@@ -77,13 +67,13 @@ ErrorCode DetectorYOLOV3::Detect(const cv::Mat &srcImage, std::vector<ResultOfDe ...@@ -77,13 +67,13 @@ ErrorCode DetectorYOLOV3::Detect(const cv::Mat &srcImage, std::vector<ResultOfDe
// 预处理并转换为NCHW // 预处理并转换为NCHW
cv::Mat inputBlob; cv::Mat inputBlob;
blobFromImage(srcImage, // 输入数据 blobFromImage(srcImage, // 输入数据
inputBlob, // 输出数据 inputBlob, // 输出数据
1 / 255.0, //归一化 1 / 255.0, // 归一化
inputSize, //YOLOV3输入尺寸,本示例为416x416 inputSize, // YOLOV3输入尺寸,本示例为416x416
Scalar(0, 0, 0), //未减去均值 Scalar(0, 0, 0), // 未减去均值
true, //转换RB通道 true, // 转换RB通道
false); false);
... ...
} }
......
...@@ -50,18 +50,11 @@ class YOLOv3: ...@@ -50,18 +50,11 @@ class YOLOv3:
self.model = migraphx.parse_onnx(path) self.model = migraphx.parse_onnx(path)
# 获取模型输入/输出节点信息 # 获取模型输入/输出节点信息
print("inputs:")
inputs = self.model.get_inputs() inputs = self.model.get_inputs()
for key,value in inputs.items():
print("{}:{}".format(key,value))
print("outputs:")
outputs = self.model.get_outputs() outputs = self.model.get_outputs()
for key,value in outputs.items():
print("{}:{}".format(key,value))
# 获取模型的输入name # 获取模型的输入name
self.inputName = "images" self.inputName = self.model.get_parameter_names()[0]
# 获取模型的输入尺寸 # 获取模型的输入尺寸
inputShape = inputShape=inputs[self.inputName].lens() inputShape = inputShape=inputs[self.inputName].lens()
...@@ -80,12 +73,10 @@ def detect(self, image): ...@@ -80,12 +73,10 @@ def detect(self, image):
# 模型编译 # 模型编译
self.model.compile(t=migraphx.get_target("gpu"), device_id=0) # device_id: 设置GPU设备,默认为0号设备 self.model.compile(t=migraphx.get_target("gpu"), device_id=0) # device_id: 设置GPU设备,默认为0号设备
print("Success to compile")
# 执行推理 # 执行推理
print("Start to inference")
start = time.time()
result = self.model.run({self.model.get_parameter_names()[0]: input_img}) result = self.model.run({self.model.get_parameter_names()[0]: input_img})
print('net forward time: {:.4f}'.format(time.time() - start))
# 模型输出结果后处理 # 模型输出结果后处理
boxes, scores, class_ids = self.process_output(result) boxes, scores, class_ids = self.process_output(result)
......
...@@ -20,18 +20,11 @@ class YOLOv3: ...@@ -20,18 +20,11 @@ class YOLOv3:
self.model = migraphx.parse_onnx(path) self.model = migraphx.parse_onnx(path)
# 获取模型输入/输出节点信息 # 获取模型输入/输出节点信息
print("inputs:")
inputs = self.model.get_inputs() inputs = self.model.get_inputs()
for key,value in inputs.items():
print("{}:{}".format(key,value))
print("outputs:")
outputs = self.model.get_outputs() outputs = self.model.get_outputs()
for key,value in outputs.items():
print("{}:{}".format(key,value))
# 获取模型的输入name # 获取模型的输入name
self.inputName = "images" self.inputName = self.model.get_parameter_names()[0]
# 获取模型的输入尺寸 # 获取模型的输入尺寸
inputShape = inputShape=inputs[self.inputName].lens() inputShape = inputShape=inputs[self.inputName].lens()
...@@ -45,12 +38,10 @@ class YOLOv3: ...@@ -45,12 +38,10 @@ class YOLOv3:
# 模型编译 # 模型编译
self.model.compile(t=migraphx.get_target("gpu"), device_id=0) # device_id: 设置GPU设备,默认为0号设备 self.model.compile(t=migraphx.get_target("gpu"), device_id=0) # device_id: 设置GPU设备,默认为0号设备
print("Success to compile")
# 执行推理 # 执行推理
print("Start to inference")
start = time.time()
result = self.model.run({self.model.get_parameter_names()[0]: input_img}) result = self.model.run({self.model.get_parameter_names()[0]: input_img})
print('net forward time: {:.4f}'.format(time.time() - start))
# 模型输出结果后处理 # 模型输出结果后处理
boxes, scores, class_ids = self.process_output(result) boxes, scores, class_ids = self.process_output(result)
......
...@@ -15,16 +15,14 @@ DetectorYOLOV3::DetectorYOLOV3() ...@@ -15,16 +15,14 @@ DetectorYOLOV3::DetectorYOLOV3()
DetectorYOLOV3::~DetectorYOLOV3() DetectorYOLOV3::~DetectorYOLOV3()
{ {
configurationFile.release(); configurationFile.release();
} }
ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializationParameterOfDetector) ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializationParameterOfDetector)
{ {
// 读取配置文件 // 读取配置文件
std::string configFilePath=initializationParameterOfDetector.configFilePath; std::string configFilePath=initializationParameterOfDetector.configFilePath;
if(Exists(configFilePath)==false) if(!Exists(configFilePath))
{ {
LOG_ERROR(stdout, "no configuration file!\n"); LOG_ERROR(stdout, "no configuration file!\n");
return CONFIG_FILE_NOT_EXIST; return CONFIG_FILE_NOT_EXIST;
...@@ -47,7 +45,7 @@ ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializ ...@@ -47,7 +45,7 @@ ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializ
useFP16=(bool)(int)netNode["UseFP16"]; useFP16=(bool)(int)netNode["UseFP16"];
// 加载模型 // 加载模型
if(Exists(modelPath)==false) if(!Exists(modelPath))
{ {
LOG_ERROR(stdout,"%s not exist!\n",modelPath.c_str()); LOG_ERROR(stdout,"%s not exist!\n",modelPath.c_str());
return MODEL_NOT_EXIST; return MODEL_NOT_EXIST;
...@@ -56,18 +54,8 @@ ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializ ...@@ -56,18 +54,8 @@ ErrorCode DetectorYOLOV3::Initialize(InitializationParameterOfDetector initializ
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());
// 获取模型输入/输出节点信息 // 获取模型输入/输出节点信息
std::cout<<"inputs:"<<std::endl;
std::unordered_map<std::string, migraphx::shape> inputs=net.get_inputs(); std::unordered_map<std::string, migraphx::shape> inputs=net.get_inputs();
for(auto i:inputs)
{
std::cout<<i.first<<":"<<i.second<<std::endl;
}
std::cout<<"outputs:"<<std::endl;
std::unordered_map<std::string, migraphx::shape> outputs=net.get_outputs(); std::unordered_map<std::string, migraphx::shape> outputs=net.get_outputs();
for(auto i:outputs)
{
std::cout<<i.first<<":"<<i.second<<std::endl;
}
inputName=inputs.begin()->first; inputName=inputs.begin()->first;
inputShape=inputs.begin()->second; inputShape=inputs.begin()->second;
int N=inputShape.lens()[0]; int N=inputShape.lens()[0];
......
...@@ -24,11 +24,7 @@ int main() ...@@ -24,11 +24,7 @@ int main()
// 推理 // 推理
std::vector<migraphxSamples::ResultOfDetection> predictions; std::vector<migraphxSamples::ResultOfDetection> predictions;
double time1 = cv::getTickCount();
detector.Detect(srcImage,predictions); detector.Detect(srcImage,predictions);
double time2 = cv::getTickCount();
double elapsedTime = (time2 - time1)*1000 / cv::getTickFrequency();
LOG_INFO(stdout, "inference time:%f ms\n", elapsedTime);
// 获取推理结果 // 获取推理结果
LOG_INFO(stdout,"========== Detection Results ==========\n"); LOG_INFO(stdout,"========== Detection Results ==========\n");
......
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