"docs/git@developer.sourcefind.cn:hehl2/torchaudio.git" did not exist on "f5dbb0021b7c6b4681c55e9ebe9b601faa1fd4da"
Commit 4ec98d75 authored by liucong's avatar liucong
Browse files

修改yolov5工程格式

parent 84e926a8
...@@ -54,18 +54,11 @@ class YOLOv5: ...@@ -54,18 +54,11 @@ class YOLOv5:
self.model = migraphx.parse_onnx(path, map_input_dims=maxInput) self.model = migraphx.parse_onnx(path, map_input_dims=maxInput)
# 获取模型输入/输出节点信息 # 获取模型输入/输出节点信息
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()
......
...@@ -22,18 +22,11 @@ class YOLOv5: ...@@ -22,18 +22,11 @@ class YOLOv5:
self.model = migraphx.parse_onnx(path, map_input_dims=maxInput) self.model = migraphx.parse_onnx(path, map_input_dims=maxInput)
# 获取模型输入/输出节点信息 # 获取模型输入/输出节点信息
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()
...@@ -43,18 +36,11 @@ class YOLOv5: ...@@ -43,18 +36,11 @@ class YOLOv5:
else: else:
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()
...@@ -64,7 +50,6 @@ class YOLOv5: ...@@ -64,7 +50,6 @@ class YOLOv5:
# 模型编译 # 模型编译
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")
def detect(self, image, input_shape=None): def detect(self, image, input_shape=None):
if(self.isDynamic): if(self.isDynamic):
...@@ -74,9 +59,8 @@ class YOLOv5: ...@@ -74,9 +59,8 @@ class YOLOv5:
input_img = self.prepare_input(image) input_img = self.prepare_input(image)
# 执行推理 # 执行推理
start = time.time()
result = self.model.run({self.inputName: input_img}) result = self.model.run({self.inputName: 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)
...@@ -211,6 +195,7 @@ if __name__ == '__main__': ...@@ -211,6 +195,7 @@ if __name__ == '__main__':
# 静态推理 # 静态推理
if args.staticInfer: if args.staticInfer:
yolov5_Static(args.imgPath, args.staticModelPath, args.objectThreshold, args.confThreshold, args.nmsThreshold) yolov5_Static(args.imgPath, args.staticModelPath, args.objectThreshold, args.confThreshold, args.nmsThreshold)
# 动态推理 # 动态推理
if args.dynamicInfer: if args.dynamicInfer:
yolov5_dynamic(args.imgFolderPath, args.dynamicModelPath, args.objectThreshold, args.confThreshold, args.nmsThreshold) yolov5_dynamic(args.imgFolderPath, args.dynamicModelPath, args.objectThreshold, args.confThreshold, args.nmsThreshold)
......
...@@ -16,16 +16,14 @@ DetectorYOLOV5::DetectorYOLOV5() ...@@ -16,16 +16,14 @@ DetectorYOLOV5::DetectorYOLOV5()
DetectorYOLOV5::~DetectorYOLOV5() DetectorYOLOV5::~DetectorYOLOV5()
{ {
configurationFile.release(); configurationFile.release();
} }
ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializationParameterOfDetector, bool dynamic) ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializationParameterOfDetector, bool dynamic)
{ {
// 读取配置文件 // 读取配置文件
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;
...@@ -57,7 +55,7 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ ...@@ -57,7 +55,7 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
if(dynamic) if(dynamic)
{ {
// 加载模型 // 加载模型
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;
...@@ -69,18 +67,8 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ ...@@ -69,18 +67,8 @@ ErrorCode DetectorYOLOV5::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];
...@@ -95,7 +83,7 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ ...@@ -95,7 +83,7 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ
else else
{ {
// 加载模型 // 加载模型
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;
...@@ -104,18 +92,8 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ ...@@ -104,18 +92,8 @@ ErrorCode DetectorYOLOV5::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];
...@@ -170,9 +148,7 @@ ErrorCode DetectorYOLOV5::Initialize(InitializationParameterOfDetector initializ ...@@ -170,9 +148,7 @@ 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<std::size_t> &relInputShape, 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)
......
...@@ -71,11 +71,7 @@ void Sample_YOLOV5() ...@@ -71,11 +71,7 @@ void Sample_YOLOV5()
// 推理 // 推理
std::vector<migraphxSamples::ResultOfDetection> predictions; std::vector<migraphxSamples::ResultOfDetection> predictions;
double time1 = cv::getTickCount();
detector.Detect(srcImage,inputShape,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);
// 获取推理结果 // 获取推理结果
LOG_INFO(stdout,"========== Detection Results ==========\n"); LOG_INFO(stdout,"========== Detection Results ==========\n");
...@@ -135,11 +131,7 @@ void Sample_YOLOV5_Dynamic() ...@@ -135,11 +131,7 @@ void Sample_YOLOV5_Dynamic()
{ {
// 推理 // 推理
std::vector<migraphxSamples::ResultOfDetection> predictions; std::vector<migraphxSamples::ResultOfDetection> predictions;
double time1 = cv::getTickCount();
detector.Detect(srcImages[i], inputShapes[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);
// 获取推理结果 // 获取推理结果
LOG_INFO(stdout,"========== Detection Image%d Results ==========\n", i); LOG_INFO(stdout,"========== Detection Image%d Results ==========\n", i);
......
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