diff --git a/model_zoo/official/cv/yolov4/ascend310_infer/inc/utils.h b/model_zoo/official/cv/yolov4/ascend310_infer/inc/utils.h index 54f53bb1ab..abeb8fcbf1 100644 --- a/model_zoo/official/cv/yolov4/ascend310_infer/inc/utils.h +++ b/model_zoo/official/cv/yolov4/ascend310_infer/inc/utils.h @@ -27,6 +27,6 @@ std::vector GetAllFiles(std::string_view dirName); DIR *OpenDir(std::string_view dirName); std::string RealPath(std::string_view path); -std::shared_ptr ReadFileToTensor(const std::string &file); -int WriteResult(const std::string& imageFile, const std::vector &outputs); +mindspore::MSTensor ReadFileToTensor(const std::string &file); +int WriteResult(const std::string& imageFile, const std::vector &outputs); #endif diff --git a/model_zoo/official/cv/yolov4/ascend310_infer/src/main.cc b/model_zoo/official/cv/yolov4/ascend310_infer/src/main.cc index ffc85da0e8..46050f61f5 100644 --- a/model_zoo/official/cv/yolov4/ascend310_infer/src/main.cc +++ b/model_zoo/official/cv/yolov4/ascend310_infer/src/main.cc @@ -27,23 +27,22 @@ #include "include/api/model.h" #include "include/api/serialization.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 "include/api/types.h" -#include "minddata/dataset/include/vision.h" -using mindspore::api::Context; -using mindspore::api::Serialization; -using mindspore::api::Model; -using mindspore::api::kModelOptionInsertOpCfgPath; -using mindspore::api::kModelOptionPrecisionMode; -using mindspore::api::kModelOptionOpSelectImplMode; -using mindspore::api::Status; -using mindspore::api::MindDataEager; -using mindspore::api::Buffer; -using mindspore::api::ModelType; -using mindspore::api::GraphCell; -using mindspore::api::SUCCESS; +using mindspore::Context; +using mindspore::GlobalContext; +using mindspore::ModelContext; +using mindspore::Serialization; +using mindspore::Model; +using mindspore::Status; +using mindspore::dataset::Execute; +using mindspore::MSTensor; +using mindspore::ModelType; +using mindspore::GraphCell; +using mindspore::kSuccess; using mindspore::dataset::vision::DvppDecodeResizeJpeg; DEFINE_string(mindir_path, "", "mindir path"); @@ -51,94 +50,103 @@ DEFINE_string(dataset_path, ".", "dataset path"); DEFINE_int32(device_id, 0, "device id"); DEFINE_string(precision_mode, "allow_fp32_to_fp16", "precision 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"); int main(int argc, char **argv) { - gflags::ParseCommandLineFlags(&argc, &argv, true); - if (RealPath(FLAGS_mindir_path).empty()) { - std::cout << "Invalid mindir" << std::endl; - return 1; - } - if (RealPath(FLAGS_aipp_path).empty()) { - std::cout << "Invalid aipp path" << std::endl; - return 1; - } + gflags::ParseCommandLineFlags(&argc, &argv, true); + if (RealPath(FLAGS_mindir_path).empty()) { + std::cout << "Invalid mindir" << std::endl; + return 1; + } + if (RealPath(FLAGS_aipp_path).empty()) { + std::cout << "Invalid aipp path" << std::endl; + return 1; + } - Context::Instance().SetDeviceTarget("Ascend310").SetDeviceID(FLAGS_device_id); - auto graph = Serialization::LoadModel(FLAGS_mindir_path, ModelType::kMindIR); - Model model((GraphCell(graph))); + GlobalContext::SetGlobalDeviceTarget(mindspore::kDeviceTypeAscend310); + GlobalContext::SetGlobalDeviceID(FLAGS_device_id); - std::map build_options; - if (!FLAGS_precision_mode.empty()) { - build_options.emplace(kModelOptionPrecisionMode, FLAGS_precision_mode); - } - if (!FLAGS_op_select_impl_mode.empty()) { - build_options.emplace(kModelOptionOpSelectImplMode, FLAGS_op_select_impl_mode); - } + auto graph = Serialization::LoadModel(FLAGS_mindir_path, ModelType::kMindIR); + auto model_context = std::make_shared(); - if (!FLAGS_aipp_path.empty()) { - build_options.emplace(kModelOptionInsertOpCfgPath, FLAGS_aipp_path); - } + if (!FLAGS_precision_mode.empty()) { + 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); - if (ret != SUCCESS) { - std::cout << "EEEEEEEERROR Build failed." << std::endl; - return 1; - } + Model model(GraphCell(graph), model_context); + Status ret = model.Build(); + if (ret != kSuccess) { + std::cout << "EEEEEEEERROR Build failed." << std::endl; + return 1; + } - auto all_files = GetAllFiles(FLAGS_dataset_path); - if (all_files.empty()) { - std::cout << "ERROR: no input data." << std::endl; - return 1; - } + std::vector model_inputs = model.GetInputs(); + auto all_files = GetAllFiles(FLAGS_dataset_path); + if (all_files.empty()) { + std::cout << "ERROR: no input data." << std::endl; + return 1; + } - std::map costTime_map; - size_t size = all_files.size(); - MindDataEager SingleOp({DvppDecodeResizeJpeg({608, 608})}); - for (size_t i = 0; i < size; ++i) { - struct timeval start = {0}; - struct timeval end = {0}; - double startTime_ms; - double endTime_ms; - std::vector inputs; - std::vector outputs; - std::cout << "Start predict input files:" << all_files[i] << std::endl; - auto imgDvpp = SingleOp(ReadFileToTensor(all_files[i])); - std::vector input_shape = {608, 608}; + std::map costTime_map; + size_t size = all_files.size(); + Execute preprocess(std::shared_ptr(new DvppDecodeResizeJpeg({608, 608}))); + for (size_t i = 0; i < size; ++i) { + struct timeval start = {0}; + struct timeval end = {0}; + double startTime_ms; + double endTime_ms; + std::vector inputs; + std::vector outputs; + std::cout << "Start predict input files:" << all_files[i] << std::endl; - inputs.clear(); - inputs.emplace_back(imgDvpp->Data(), imgDvpp->DataSize()); - inputs.emplace_back(input_shape.data(), input_shape.size() * sizeof(float)); - gettimeofday(&start, NULL); - ret = model.Predict(inputs, &outputs); - 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(startTime_ms, endTime_ms)); - WriteResult(all_files[i], outputs); + auto img = MSTensor(); + ret = preprocess(ReadFileToTensor(all_files[i]), &img); + if (ret != kSuccess) { + std::cout << "preprocess " << all_files[i] << " failed." << std::endl; + return 1; } - double average = 0.0; - int infer_cnt = 0; - char tmpCh[256] = {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++; + std::vector input_shape = {608, 608}; + + inputs.clear(); + inputs.emplace_back(model_inputs[0].Name(), model_inputs[0].DataType(), model_inputs[0].Shape(), + img.Data().get(), img.DataSize()); + inputs.emplace_back(model_inputs[1].Name(), model_inputs[1].DataType(), model_inputs[1].Shape(), + input_shape.data(), input_shape.size() * sizeof(float)); + + 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; - 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; + 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(startTime_ms, endTime_ms)); + WriteResult(all_files[i], outputs); + } + double average = 0.0; + int infer_cnt = 0; + char tmpCh[256] = {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; } diff --git a/model_zoo/official/cv/yolov4/ascend310_infer/src/utils.cc b/model_zoo/official/cv/yolov4/ascend310_infer/src/utils.cc index e3d518c669..271f2b0eaf 100644 --- a/model_zoo/official/cv/yolov4/ascend310_infer/src/utils.cc +++ b/model_zoo/official/cv/yolov4/ascend310_infer/src/utils.cc @@ -19,127 +19,118 @@ #include #include -using mindspore::api::Tensor; -using mindspore::api::Buffer; -using mindspore::api::DataType; +using mindspore::MSTensor; +using mindspore::DataType; std::vector GetAllFiles(std::string_view dirName) { - struct dirent *filename; - DIR *dir = OpenDir(dirName); - if (dir == nullptr) { - return {}; - } - - std::vector res; - while ((filename = readdir(dir)) != nullptr) { - std::string dName = std::string(filename->d_name); - if (dName == "." || - dName == ".." || - filename->d_type != DT_REG) - continue; - res.emplace_back(std::string(dirName) + "/" + filename->d_name); - } - - std::sort(res.begin(), res.end()); - for (auto &f : res) { - std::cout << "image file: " << f << std::endl; - } - return res; + struct dirent *filename; + DIR *dir = OpenDir(dirName); + if (dir == nullptr) { + return {}; + } + + std::vector res; + while ((filename = readdir(dir)) != nullptr) { + std::string dName = std::string(filename->d_name); + if (dName == "." || + dName == ".." || + filename->d_type != DT_REG) + continue; + res.emplace_back(std::string(dirName) + "/" + filename->d_name); + } + + std::sort(res.begin(), res.end()); + for (auto &f : res) { + std::cout << "image file: " << f << std::endl; + } + return res; } -int WriteResult(const std::string& imageFile, const std::vector &outputs) { - std::string homePath = "./result_Files"; - for (size_t i = 0; i < outputs.size(); ++i) { - size_t outputSize; - const void * netOutput; - netOutput = outputs[i].Data(); - outputSize = outputs[i].DataSize(); - - int pos = imageFile.rfind('/'); - std::string fileName(imageFile, pos + 1); - fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); - std::string outFileName = homePath + "/" + fileName; - FILE * outputFile = fopen(outFileName.c_str(), "wb"); - fwrite(netOutput, outputSize, sizeof(char), outputFile); - - fclose(outputFile); - outputFile = nullptr; - } - return 0; +int WriteResult(const std::string& imageFile, const std::vector &outputs) { + std::string homePath = "./result_Files"; + for (size_t i = 0; i < outputs.size(); ++i) { + size_t outputSize; + std::shared_ptr netOutput; + netOutput = outputs[i].Data(); + outputSize = outputs[i].DataSize(); + + int pos = imageFile.rfind('/'); + std::string fileName(imageFile, pos + 1); + fileName.replace(fileName.find('.'), fileName.size() - fileName.find('.'), '_' + std::to_string(i) + ".bin"); + std::string outFileName = homePath + "/" + fileName; + FILE * outputFile = fopen(outFileName.c_str(), "wb"); + fwrite(netOutput.get(), outputSize, sizeof(char), outputFile); + + fclose(outputFile); + outputFile = nullptr; + } + return 0; } -std::shared_ptr ReadFileToTensor(const std::string &file) { - auto buffer = std::make_shared(); - if (file.empty()) { - std::cout << "Pointer file is nullptr" << std::endl; - return buffer; - } - - std::ifstream ifs(file); - if (!ifs.good()) { - std::cout << "File: " << file << " is not exist" << std::endl; - return buffer; - } - - if (!ifs.is_open()) { - std::cout << "File: " << file << "open failed" << std::endl; - return buffer; - } - - ifs.seekg(0, std::ios::end); - size_t size = ifs.tellg(); - buffer->ResizeData(size); - if (buffer->DataSize() != size) { - std::cout << "Malloc buf failed, file: " << file << std::endl; - ifs.close(); - return buffer; - } - - ifs.seekg(0, std::ios::beg); - ifs.read(reinterpret_cast(buffer->MutableData()), size); - ifs.close(); - - buffer->SetDataType(DataType::kMsUint8); - buffer->SetShape({static_cast(size)}); - return buffer; +MSTensor ReadFileToTensor(const std::string &file) { + if (file.empty()) { + std::cout << "Pointer file is nullptr" << std::endl; + return MSTensor(); + } + + std::ifstream ifs(file); + if (!ifs.good()) { + std::cout << "File: " << file << " is not exist" << std::endl; + return MSTensor(); + } + + if (!ifs.is_open()) { + std::cout << "File: " << file << "open failed" << std::endl; + return MSTensor(); + } + + ifs.seekg(0, std::ios::end); + size_t size = ifs.tellg(); + MSTensor buffer(file, DataType::kNumberTypeUInt8, {static_cast(size)}, nullptr, size); + + ifs.seekg(0, std::ios::beg); + ifs.read(reinterpret_cast(buffer.MutableData()), size); + ifs.close(); + + return buffer; } DIR *OpenDir(std::string_view dirName) { - if (dirName.empty()) { - std::cout << " dirName is null ! " << std::endl; - return nullptr; - } - - std::string realPath = RealPath(dirName); - - struct stat s; - lstat(realPath.c_str(), &s); - if (!S_ISDIR(s.st_mode)) { - std::cout << "dirName is not a valid directory !" << std::endl; - return nullptr; - } - - DIR *dir; - dir = opendir(realPath.c_str()); - if (dir == nullptr) { - std::cout << "Can not open dir " << dirName << std::endl; - return nullptr; - } - std::cout << "Successfully opened the dir " << dirName << std::endl; - return dir; + if (dirName.empty()) { + std::cout << " dirName is null ! " << std::endl; + return nullptr; + } + + std::string realPath = RealPath(dirName); + + struct stat s; + lstat(realPath.c_str(), &s); + if (!S_ISDIR(s.st_mode)) { + std::cout << "dirName is not a valid directory !" << std::endl; + return nullptr; + } + + DIR *dir; + dir = opendir(realPath.c_str()); + if (dir == nullptr) { + std::cout << "Can not open dir " << dirName << std::endl; + return nullptr; + } + std::cout << "Successfully opened the dir " << dirName << std::endl; + return dir; } std::string RealPath(std::string_view path) { - char real_path_mem[PATH_MAX] = {0}; - char *real_path_ret = nullptr; - real_path_ret = realpath(path.data(), real_path_mem); - - if (real_path_ret == nullptr) { - std::cout << "File: " << path << " is not exist."; - return ""; - } - - std::string real_path(real_path_mem); - std::cout << path << " realpath is: " << real_path << std::endl; - return real_path; + char real_path_mem[PATH_MAX] = {0}; + char *real_path_ret = nullptr; + real_path_ret = realpath(path.data(), real_path_mem); + + if (real_path_ret == nullptr) { + std::cout << "File: " << path << " is not exist."; + return ""; + } + + std::string real_path(real_path_mem); + std::cout << path << " realpath is: " << real_path << std::endl; + return real_path; }