!12837 update interface for yolov4 310 infer

From: @lihongkang1
Reviewed-by: 
Signed-off-by:
pull/12837/MERGE
mindspore-ci-bot 4 years ago committed by Gitee
commit 578f3b851e

@ -27,6 +27,6 @@
std::vector<std::string> GetAllFiles(std::string_view dirName); std::vector<std::string> GetAllFiles(std::string_view dirName);
DIR *OpenDir(std::string_view dirName); DIR *OpenDir(std::string_view dirName);
std::string RealPath(std::string_view path); std::string RealPath(std::string_view path);
std::shared_ptr<mindspore::api::Tensor> ReadFileToTensor(const std::string &file); mindspore::MSTensor ReadFileToTensor(const std::string &file);
int WriteResult(const std::string& imageFile, const std::vector<mindspore::api::Buffer> &outputs); int WriteResult(const std::string& imageFile, const std::vector<mindspore::MSTensor> &outputs);
#endif #endif

@ -27,23 +27,22 @@
#include "include/api/model.h" #include "include/api/model.h"
#include "include/api/serialization.h" #include "include/api/serialization.h"
#include "include/api/context.h" #include "include/api/context.h"
#include "minddata/dataset/include/minddata_eager.h" #include "include/minddata/dataset/include/execute.h"
#include "include/minddata/dataset/include/vision_ascend.h"
#include "../inc/utils.h" #include "../inc/utils.h"
#include "include/api/types.h" #include "include/api/types.h"
#include "minddata/dataset/include/vision.h"
using mindspore::api::Context; using mindspore::Context;
using mindspore::api::Serialization; using mindspore::GlobalContext;
using mindspore::api::Model; using mindspore::ModelContext;
using mindspore::api::kModelOptionInsertOpCfgPath; using mindspore::Serialization;
using mindspore::api::kModelOptionPrecisionMode; using mindspore::Model;
using mindspore::api::kModelOptionOpSelectImplMode; using mindspore::Status;
using mindspore::api::Status; using mindspore::dataset::Execute;
using mindspore::api::MindDataEager; using mindspore::MSTensor;
using mindspore::api::Buffer; using mindspore::ModelType;
using mindspore::api::ModelType; using mindspore::GraphCell;
using mindspore::api::GraphCell; using mindspore::kSuccess;
using mindspore::api::SUCCESS;
using mindspore::dataset::vision::DvppDecodeResizeJpeg; using mindspore::dataset::vision::DvppDecodeResizeJpeg;
DEFINE_string(mindir_path, "", "mindir path"); DEFINE_string(mindir_path, "", "mindir path");
@ -51,94 +50,103 @@ DEFINE_string(dataset_path, ".", "dataset path");
DEFINE_int32(device_id, 0, "device id"); DEFINE_int32(device_id, 0, "device id");
DEFINE_string(precision_mode, "allow_fp32_to_fp16", "precision mode"); DEFINE_string(precision_mode, "allow_fp32_to_fp16", "precision mode");
DEFINE_string(op_select_impl_mode, "", "op select impl mode"); DEFINE_string(op_select_impl_mode, "", "op select impl mode");
DEFINE_string(input_shape, "img_data:1, 3, 768, 1280; img_info:1, 4", "input shape");
DEFINE_string(input_format, "nchw", "input format");
DEFINE_string(aipp_path, "./aipp.cfg", "aipp path"); DEFINE_string(aipp_path, "./aipp.cfg", "aipp path");
int main(int argc, char **argv) { int main(int argc, char **argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
if (RealPath(FLAGS_mindir_path).empty()) { if (RealPath(FLAGS_mindir_path).empty()) {
std::cout << "Invalid mindir" << std::endl; std::cout << "Invalid mindir" << std::endl;
return 1; return 1;
} }
if (RealPath(FLAGS_aipp_path).empty()) { if (RealPath(FLAGS_aipp_path).empty()) {
std::cout << "Invalid aipp path" << std::endl; std::cout << "Invalid aipp path" << std::endl;
return 1; return 1;
} }
Context::Instance().SetDeviceTarget("Ascend310").SetDeviceID(FLAGS_device_id); GlobalContext::SetGlobalDeviceTarget(mindspore::kDeviceTypeAscend310);
auto graph = Serialization::LoadModel(FLAGS_mindir_path, ModelType::kMindIR); GlobalContext::SetGlobalDeviceID(FLAGS_device_id);
Model model((GraphCell(graph)));
std::map<std::string, std::string> build_options; auto graph = Serialization::LoadModel(FLAGS_mindir_path, ModelType::kMindIR);
if (!FLAGS_precision_mode.empty()) { auto model_context = std::make_shared<Context>();
build_options.emplace(kModelOptionPrecisionMode, FLAGS_precision_mode);
}
if (!FLAGS_op_select_impl_mode.empty()) {
build_options.emplace(kModelOptionOpSelectImplMode, FLAGS_op_select_impl_mode);
}
if (!FLAGS_aipp_path.empty()) { if (!FLAGS_precision_mode.empty()) {
build_options.emplace(kModelOptionInsertOpCfgPath, FLAGS_aipp_path); ModelContext::SetPrecisionMode(model_context, FLAGS_precision_mode);
} }
if (!FLAGS_op_select_impl_mode.empty()) {
ModelContext::SetOpSelectImplMode(model_context, FLAGS_op_select_impl_mode);
}
if (!FLAGS_aipp_path.empty()) {
ModelContext::SetInsertOpConfigPath(model_context, FLAGS_aipp_path);
}
Status ret = model.Build(build_options); Model model(GraphCell(graph), model_context);
if (ret != SUCCESS) { Status ret = model.Build();
std::cout << "EEEEEEEERROR Build failed." << std::endl; if (ret != kSuccess) {
return 1; std::cout << "EEEEEEEERROR Build failed." << std::endl;
} return 1;
}
auto all_files = GetAllFiles(FLAGS_dataset_path); std::vector<MSTensor> model_inputs = model.GetInputs();
if (all_files.empty()) { auto all_files = GetAllFiles(FLAGS_dataset_path);
std::cout << "ERROR: no input data." << std::endl; if (all_files.empty()) {
return 1; std::cout << "ERROR: no input data." << std::endl;
} return 1;
}
std::map<double, double> costTime_map; std::map<double, double> costTime_map;
size_t size = all_files.size(); size_t size = all_files.size();
MindDataEager SingleOp({DvppDecodeResizeJpeg({608, 608})}); Execute preprocess(std::shared_ptr<DvppDecodeResizeJpeg>(new DvppDecodeResizeJpeg({608, 608})));
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
struct timeval start = {0}; struct timeval start = {0};
struct timeval end = {0}; struct timeval end = {0};
double startTime_ms; double startTime_ms;
double endTime_ms; double endTime_ms;
std::vector<Buffer> inputs; std::vector<MSTensor> inputs;
std::vector<Buffer> outputs; std::vector<MSTensor> outputs;
std::cout << "Start predict input files:" << all_files[i] << std::endl; std::cout << "Start predict input files:" << all_files[i] << std::endl;
auto imgDvpp = SingleOp(ReadFileToTensor(all_files[i]));
std::vector<float> input_shape = {608, 608};
inputs.clear(); auto img = MSTensor();
inputs.emplace_back(imgDvpp->Data(), imgDvpp->DataSize()); ret = preprocess(ReadFileToTensor(all_files[i]), &img);
inputs.emplace_back(input_shape.data(), input_shape.size() * sizeof(float)); if (ret != kSuccess) {
gettimeofday(&start, NULL); std::cout << "preprocess " << all_files[i] << " failed." << std::endl;
ret = model.Predict(inputs, &outputs); return 1;
gettimeofday(&end, NULL);
if (ret != SUCCESS) {
std::cout << "Predict " << all_files[i] << " failed." << std::endl;
return 1;
}
startTime_ms = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000;
endTime_ms = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000;
costTime_map.insert(std::pair<double, double>(startTime_ms, endTime_ms));
WriteResult(all_files[i], outputs);
} }
double average = 0.0; std::vector<float> input_shape = {608, 608};
int infer_cnt = 0;
char tmpCh[256] = {0}; inputs.clear();
for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) { inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(),
double diff = 0.0; img.Data().get(), img.DataSize());
diff = iter->second - iter->first; inputs.emplace_back(model_inputs[1].Name(), model_inputs[1].DataType(), model_inputs[1].Shape(),
average += diff; input_shape.data(), input_shape.size() * sizeof(float));
infer_cnt++;
gettimeofday(&start, NULL);
ret = model.Predict(inputs, &outputs);
gettimeofday(&end, NULL);
if (ret != kSuccess) {
std::cout << "Predict " << all_files[i] << " failed." << std::endl;
return 1;
} }
average = average/infer_cnt; startTime_ms = (1.0 * start.tv_sec * 1000000 + start.tv_usec) / 1000;
snprintf(tmpCh, sizeof(tmpCh), "NN inference cost average time: %4.3f ms of infer_count %d \n", average, infer_cnt); endTime_ms = (1.0 * end.tv_sec * 1000000 + end.tv_usec) / 1000;
std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << infer_cnt << std::endl; costTime_map.insert(std::pair<double, double>(startTime_ms, endTime_ms));
std::string file_name = "./time_Result" + std::string("/test_perform_static.txt"); WriteResult(all_files[i], outputs);
std::ofstream file_stream(file_name.c_str(), std::ios::trunc); }
file_stream << tmpCh; double average = 0.0;
file_stream.close(); int infer_cnt = 0;
costTime_map.clear(); char tmpCh[256] = {0};
return 0; for (auto iter = costTime_map.begin(); iter != costTime_map.end(); iter++) {
double diff = 0.0;
diff = iter->second - iter->first;
average += diff;
infer_cnt++;
}
average = average/infer_cnt;
snprintf(tmpCh, sizeof(tmpCh), "NN inference cost average time: %4.3f ms of infer_count %d \n", average, infer_cnt);
std::cout << "NN inference cost average time: "<< average << "ms of infer_count " << infer_cnt << std::endl;
std::string file_name = "./time_Result" + std::string("/test_perform_static.txt");
std::ofstream file_stream(file_name.c_str(), std::ios::trunc);
file_stream << tmpCh;
file_stream.close();
costTime_map.clear();
return 0;
} }

@ -19,127 +19,118 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
using mindspore::api::Tensor; using mindspore::MSTensor;
using mindspore::api::Buffer; using mindspore::DataType;
using mindspore::api::DataType;
std::vector<std::string> GetAllFiles(std::string_view dirName) { std::vector<std::string> GetAllFiles(std::string_view dirName) {
struct dirent *filename; struct dirent *filename;
DIR *dir = OpenDir(dirName); DIR *dir = OpenDir(dirName);
if (dir == nullptr) { if (dir == nullptr) {
return {}; return {};
} }
std::vector<std::string> res; std::vector<std::string> res;
while ((filename = readdir(dir)) != nullptr) { while ((filename = readdir(dir)) != nullptr) {
std::string dName = std::string(filename->d_name); std::string dName = std::string(filename->d_name);
if (dName == "." || if (dName == "." ||
dName == ".." || dName == ".." ||
filename->d_type != DT_REG) filename->d_type != DT_REG)
continue; continue;
res.emplace_back(std::string(dirName) + "/" + filename->d_name); res.emplace_back(std::string(dirName) + "/" + filename->d_name);
} }
std::sort(res.begin(), res.end()); std::sort(res.begin(), res.end());
for (auto &f : res) { for (auto &f : res) {
std::cout << "image file: " << f << std::endl; std::cout << "image file: " << f << std::endl;
} }
return res; return res;
} }
int WriteResult(const std::string& imageFile, const std::vector<Buffer> &outputs) { int WriteResult(const std::string& imageFile, const std::vector<MSTensor> &outputs) {
std::string homePath = "./result_Files"; std::string homePath = "./result_Files";
for (size_t i = 0; i < outputs.size(); ++i) { for (size_t i = 0; i < outputs.size(); ++i) {
size_t outputSize; size_t outputSize;
const void * netOutput; std::shared_ptr<const void> netOutput;
netOutput = outputs[i].Data(); netOutput = outputs[i].Data();
outputSize = outputs[i].DataSize(); outputSize = outputs[i].DataSize();
int pos = imageFile.rfind('/'); int pos = imageFile.rfind('/');
std::string fileName(imageFile, pos + 1); std::string fileName(imageFile, pos + 1);
fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin");
std::string outFileName = homePath + "/" + fileName; std::string outFileName = homePath + "/" + fileName;
FILE * outputFile = fopen(outFileName.c_str(), "wb"); FILE * outputFile = fopen(outFileName.c_str(), "wb");
fwrite(netOutput, outputSize, sizeof(char), outputFile); fwrite(netOutput.get(), outputSize, sizeof(char), outputFile);
fclose(outputFile); fclose(outputFile);
outputFile = nullptr; outputFile = nullptr;
} }
return 0; return 0;
} }
std::shared_ptr<Tensor> ReadFileToTensor(const std::string &file) { MSTensor ReadFileToTensor(const std::string &file) {
auto buffer = std::make_shared<Tensor>(); if (file.empty()) {
if (file.empty()) { std::cout << "Pointer file is nullptr" << std::endl;
std::cout << "Pointer file is nullptr" << std::endl; return MSTensor();
return buffer; }
}
std::ifstream ifs(file);
std::ifstream ifs(file); if (!ifs.good()) {
if (!ifs.good()) { std::cout << "File: " << file << " is not exist" << std::endl;
std::cout << "File: " << file << " is not exist" << std::endl; return MSTensor();
return buffer; }
}
if (!ifs.is_open()) {
if (!ifs.is_open()) { std::cout << "File: " << file << "open failed" << std::endl;
std::cout << "File: " << file << "open failed" << std::endl; return MSTensor();
return buffer; }
}
ifs.seekg(0, std::ios::end);
ifs.seekg(0, std::ios::end); size_t size = ifs.tellg();
size_t size = ifs.tellg(); MSTensor buffer(file, DataType::kNumberTypeUInt8, {static_cast<int64_t>(size)}, nullptr, size);
buffer->ResizeData(size);
if (buffer->DataSize() != size) { ifs.seekg(0, std::ios::beg);
std::cout << "Malloc buf failed, file: " << file << std::endl; ifs.read(reinterpret_cast<char *>(buffer.MutableData()), size);
ifs.close(); ifs.close();
return buffer;
} return buffer;
ifs.seekg(0, std::ios::beg);
ifs.read(reinterpret_cast<char *>(buffer->MutableData()), size);
ifs.close();
buffer->SetDataType(DataType::kMsUint8);
buffer->SetShape({static_cast<int64_t>(size)});
return buffer;
} }
DIR *OpenDir(std::string_view dirName) { DIR *OpenDir(std::string_view dirName) {
if (dirName.empty()) { if (dirName.empty()) {
std::cout << " dirName is null ! " << std::endl; std::cout << " dirName is null ! " << std::endl;
return nullptr; return nullptr;
} }
std::string realPath = RealPath(dirName); std::string realPath = RealPath(dirName);
struct stat s; struct stat s;
lstat(realPath.c_str(), &s); lstat(realPath.c_str(), &s);
if (!S_ISDIR(s.st_mode)) { if (!S_ISDIR(s.st_mode)) {
std::cout << "dirName is not a valid directory !" << std::endl; std::cout << "dirName is not a valid directory !" << std::endl;
return nullptr; return nullptr;
} }
DIR *dir; DIR *dir;
dir = opendir(realPath.c_str()); dir = opendir(realPath.c_str());
if (dir == nullptr) { if (dir == nullptr) {
std::cout << "Can not open dir " << dirName << std::endl; std::cout << "Can not open dir " << dirName << std::endl;
return nullptr; return nullptr;
} }
std::cout << "Successfully opened the dir " << dirName << std::endl; std::cout << "Successfully opened the dir " << dirName << std::endl;
return dir; return dir;
} }
std::string RealPath(std::string_view path) { std::string RealPath(std::string_view path) {
char real_path_mem[PATH_MAX] = {0}; char real_path_mem[PATH_MAX] = {0};
char *real_path_ret = nullptr; char *real_path_ret = nullptr;
real_path_ret = realpath(path.data(), real_path_mem); real_path_ret = realpath(path.data(), real_path_mem);
if (real_path_ret == nullptr) { if (real_path_ret == nullptr) {
std::cout << "File: " << path << " is not exist."; std::cout << "File: " << path << " is not exist.";
return ""; return "";
} }
std::string real_path(real_path_mem); std::string real_path(real_path_mem);
std::cout << path << " realpath is: " << real_path << std::endl; std::cout << path << " realpath is: " << real_path << std::endl;
return real_path; return real_path;
} }

Loading…
Cancel
Save