diff --git a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/CMakeLists.txt b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/CMakeLists.txt index 617c5dd39858f6df17f1d97e96ec09af3012ae42..4685de84747af36e235c37e9632adbdeb7b62613 100644 --- a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/CMakeLists.txt +++ b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/CMakeLists.txt @@ -47,5 +47,6 @@ link_directories(${MXBASE_POST_LIB_DIR}) add_executable(${TARGET} src/main.cpp src/SpansNet100.cpp) target_link_libraries(${TARGET} glog cpprest mxbase opencv_world stdc++fs) +target_link_libraries(${TARGET} glog cpprest mxbase resnet50postprocess opencv_world stdc++fs) install(TARGETS ${TARGET} RUNTIME DESTINATION ${PROJECT_SOURCE_DIR}/) diff --git a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/build.sh b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/build.sh index a4ad292b21cb703b2d7d4e1d9fcf4b2f898078d1..7514c9ffc2a15243e0322f674163f78dc6ead04b 100644 --- a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/build.sh +++ b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/build.sh @@ -36,7 +36,11 @@ function check_env() { function build_spansnet100() { cd $path_cur rm -rf build + if [ -d "output" ]; then + rm -rf output + fi mkdir -p build + mkdir -p output cd build cmake .. make diff --git a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/SpansNet100.cpp b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/SpansNet100.cpp index b6de70f0bc42a92578318cbecb1fe88fdbcad6c7..d50e27ed5ecf954dbe90401f42cd5119ac92c8af 100644 --- a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/SpansNet100.cpp +++ b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/SpansNet100.cpp @@ -23,6 +23,7 @@ #include #include "MxBase/DeviceManager/DeviceManager.h" #include "MxBase/Log/Log.h" +#include APP_ERROR SpansNet100Infer::Init(const InitParam &initParam) { deviceId_ = initParam.deviceId; @@ -42,6 +43,26 @@ APP_ERROR SpansNet100Infer::Init(const InitParam &initParam) { LogError << "ModelInferenceProcessor init failed, ret=" << ret << "."; return ret; } + MxBase::ConfigData configData; + const std::string softmax = initParam.softmax ? "true" : "false"; + const std::string checkTensor = initParam.checkTensor ? "true" : "false"; + + configData.SetJsonValue("CLASS_NUM", std::to_string(initParam.classNum)); + configData.SetJsonValue("TOP_K", std::to_string(initParam.topk)); + configData.SetJsonValue("SOFTMAX", softmax); + configData.SetJsonValue("CHECK_MODEL", checkTensor); + + auto jsonStr = configData.GetCfgJson().serialize(); + std::map> config; + config["postProcessConfigContent"] = std::make_shared(jsonStr); + config["labelPath"] = std::make_shared(initParam.labelPath); + + post_ = std::make_shared(); + ret = post_->Init(config); + if (ret != APP_ERR_OK) { + LogError << "Resnet50PostProcess init failed, ret=" << ret << "."; + return ret; + } return APP_ERR_OK; } @@ -77,6 +98,7 @@ APP_ERROR SpansNet100Infer::BinToTensorBase(const std::string file_path, MxBase: APP_ERROR SpansNet100Infer::DeInit() { model_->DeInit(); + post_->DeInit(); MxBase::DeviceManager::GetInstance()->DestroyDevices(); return APP_ERR_OK; } @@ -112,49 +134,45 @@ APP_ERROR SpansNet100Infer::Inference(const std::vector &inp return APP_ERR_OK; } -APP_ERROR SpansNet100Infer::PostProcess(std::vector *outputs, - std::vector *predict) { - MxBase::TensorBase &tensor = outputs->at(0); - APP_ERROR ret = tensor.ToHost(); +APP_ERROR SpansNet100Infer::PostProcess(const std::vector &inputs, + std::vector> &clsInfos) +{ + APP_ERROR ret = post_->Process(inputs, clsInfos); if (ret != APP_ERR_OK) { - LogError << GetError(ret) << "Tensor deploy to host failed."; + LogError << "Process failed, ret=" << ret << "."; return ret; } - // check tensor is available - auto outputShape = tensor.GetShape(); - uint32_t length = outputShape[0] * outputShape[1]; - void *data = tensor.GetBuffer(); - for (uint32_t i = 0; i < length; i++) { - float value = *(reinterpret_cast(data) + i); - predict->push_back(value); - } return APP_ERR_OK; } -APP_ERROR SpansNet100Infer::WriteResult(const std::string &fileName, const std::vector &predict, - const std::string name) { +APP_ERROR SpansNet100Infer::SaveResult(const std::vector> &batchClsInfos, const std::string name) +{ // create result file - std::ofstream tfile(fileName, std::ofstream::app); + std::string file_name = "./output/" + name + ".txt"; + std::ofstream tfile(file_name, std::ofstream::app); if (tfile.fail()) { - LogError << "Failed to open result file: " << fileName; + LogError << "Failed to open result file: " << file_name; return APP_ERR_COMM_OPEN_FAIL; } // write inference result into file LogInfo << "=============================================================="; LogInfo << "Infer finished!"; - - tfile< outputs = {}; inputs.push_back(tensorBase); ret = Inference(inputs, outputs); - // output to vector - std::vector predict; - ret = PostProcess(&outputs, &predict); + // postprocess + std::vector> batchClsInfos = {}; + ret = PostProcess(outputs, batchClsInfos); if (ret != APP_ERR_OK) { LogError << "PostProcess failed, ret=" << ret << "."; return ret; @@ -177,9 +195,9 @@ APP_ERROR SpansNet100Infer::Process(const std::string &imgPath, const std::strin int index = imgPath.find_last_of("/"); std::string name = imgPath.substr(index + 1, 23); // save result - ret = WriteResult(outputPath, predict, name); + ret = SaveResult(batchClsInfos, name); if (ret != APP_ERR_OK) { - LogError << "save result failed, ret=" << ret << "."; + LogError << "Save infer results into file failed. ret = " << ret << "."; return ret; } return APP_ERR_OK; diff --git a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/SpansNet100.h b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/SpansNet100.h index 03b1b45b1176297d65c20dc748edb02aa9500f27..cf28ea867936693069270f08e6233c864dab6752 100644 --- a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/SpansNet100.h +++ b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/SpansNet100.h @@ -24,6 +24,7 @@ #include #include "MxBase/DvppWrapper/DvppWrapper.h" #include "MxBase/ModelInfer/ModelInferenceProcessor.h" +#include "ClassPostProcessors/Resnet50PostProcess.h" #include "MxBase/Tensor/TensorContext/TensorContext.h" extern std::vector g_inferCost; @@ -31,7 +32,10 @@ extern std::vector g_inferCost; struct InitParam { uint32_t deviceId; std::string labelPath; - uint32_t topk=5; + uint32_t classNum; + uint32_t topk; + bool softmax; + bool checkTensor; std::string modelPath; }; @@ -42,17 +46,15 @@ public: APP_ERROR BinToTensorBase(const std::string file_path, MxBase::TensorBase &tensorBase); APP_ERROR Inference(const std::vector &inputs, std::vector &outputs); - APP_ERROR PostProcess(std::vector *outputs, - std::vector *predict); - APP_ERROR Process(const std::string &imgPath, - const std::string &outputPath); - APP_ERROR WriteResult(const std::string &fileName, - const std::vector &predict, - const std::string name); + APP_ERROR PostProcess(const std::vector &inputs, + std::vector> &clsInfos); + APP_ERROR Process(const std::string &imgPath); + APP_ERROR SaveResult(const std::vector> &batchClsInfos, const std::string name); private: std::shared_ptr dvppWrapper_; std::shared_ptr model_; + std::shared_ptr post_; MxBase::ModelDesc modelDesc_; uint32_t deviceId_ = 0; }; diff --git a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/main.cpp b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/main.cpp index 8229156abe964763a9c119fa5989d49f8a692776..435c97b3c8e53f07def479854deb84f3ed543557 100644 --- a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/main.cpp +++ b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/src/main.cpp @@ -22,15 +22,24 @@ namespace fs = std::experimental::filesystem; std::vector g_inferCost; +namespace { + const uint32_t CLASS_NUM = 1000; +} // namespace int main(int argc, char *argv[]) { if (argc <= 1) { - LogWarn << "Please input image path and ouput path, such as '../data/input/preprocess ../data/input/preprocess_result.txt"; + LogWarn << "Please input image path and ouput path, such as '../data/input/preprocess"; return APP_ERR_OK; } InitParam initParam = {}; initParam.deviceId = 0; + initParam.classNum = CLASS_NUM; + initParam.topk=5; + initParam.softmax = false; + initParam.checkTensor = true; + initParam.modelPath = "../data/model/spnasnet_100_bs1.om"; + initParam.labelPath = "../data/input/imagenet1000_clsidx_to_labels.txt"; auto spansnet = std::make_shared(); APP_ERROR ret = spansnet->Init(initParam); if (ret != APP_ERR_OK) { @@ -38,12 +47,11 @@ int main(int argc, char *argv[]) { return ret; } std::string imgDir = argv[1]; - std::string outputDir = argv[2]; int index = 0; for (auto &entry : fs::directory_iterator(imgDir)) { index++; LogInfo << "read image path " << entry.path(); - ret = spansnet->Process(entry.path(), outputDir); + ret = spansnet->Process(entry.path()); if (ret != APP_ERR_OK) { LogError << "SpansNet100Infer process failed, ret=" << ret << "."; spansnet->DeInit(); diff --git a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/utils/eval.py b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/utils/eval.py index 66e0007872229565f27cceb7756c236ea437bb30..c9a169d32c3947b1f2f609c23c4f9c843fc374b1 100644 --- a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/utils/eval.py +++ b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/mxbase/utils/eval.py @@ -17,13 +17,6 @@ import sys import os import json -def w2txt(file_path, filename_list, data): - with open(file_path, "w") as file: - for i in range(data.shape[0]): - s = filename_list[i].split('.')[0] + ' ' - s += ' '.join(str(int(num)) for num in data[i]) - file.write(s+"\n") - def cre_groundtruth_dict_fromtxt(gtfile_path): """ :param filename: file contains the imagename and label number @@ -53,54 +46,42 @@ def run(): table_dict["value"] = [] count = 0 filename_list = [] - sort_index_sents = [] - with open(infer_reault, 'r')as f: - for line in f.readlines(): - prediction = [] - temp = line.strip().split(" ") - img_name = temp[0] - filename_list.append(img_name.split('.')[0]) - print(img_name, "====", count) - count += 1 - prediction = np.array([float(temp[i]) for i in range(1, len(temp))]) - n_labels = len(prediction) - gt = img_gt_dict[img_name] - sort_index = np.argsort(-prediction) - if (n_labels == 1000): - realLabel = int(gt) - elif (n_labels == 1001): - realLabel = int(gt) + 1 - else: + file_list = os.listdir(infer_reault) + for file in file_list: + with open(os.path.join(infer_reault, file), 'r')as f: + for line in f.readlines(): + temp = line.strip().split(" ") + img_name = temp[0] + filename_list.append(img_name.split('.')[0]) + print(img_name, "====", count) + count += 1 + prediction = np.array([int(temp[i]) for i in range(1, len(temp))]) + n_labels = len(prediction) + gt = img_gt_dict[img_name] realLabel = int(gt) - resCnt = min(len(sort_index), topn) - for i in range(resCnt): - if (str(realLabel) == str(sort_index[i])): - count_hit[i] += 1 - break - sort_index_sents.append(sort_index[:5]) - file_path = "../mxbase_postprocess_result.txt" - sort_index_sents = np.array(sort_index_sents).astype(np.int32) - if(os.path.exists(file_path)): - os.remove(file_path) - w2txt(file_path, filename_list, sort_index_sents) + resCnt = topn + for i in range(resCnt): + if (str(realLabel) == str(prediction[i])): + count_hit[i] += 1 + break - print(count) - if 'value' not in table_dict.keys(): - print("the item value does not exist!") + print(count) + if 'value' not in table_dict.keys(): + print("the item value does not exist!") + else: + table_dict["value"].extend( + [{"key": "Number of images", "value": str(count)}, + {"key": "Number of classes", "value": str(n_labels)}]) + if count == 0: + accuracy = 0 else: - table_dict["value"].extend( - [{"key": "Number of images", "value": str(count)}, - {"key": "Number of classes", "value": str(n_labels)}]) - if count == 0: - accuracy = 0 - else: - accuracy = np.cumsum(count_hit) / count - for i in range(resCnt): - table_dict["value"].append({"key": "Top" + str(i + 1) + " accuracy", - "value": str( - round(accuracy[i] * 100, 2)) + '%'}) - json.dump(table_dict, writer) - writer.close() + accuracy = np.cumsum(count_hit) / count + for i in range(resCnt): + table_dict["value"].append({"key": "Top" + str(i + 1) + " accuracy", + "value": str( + round(accuracy[i] * 100, 2)) + '%'}) + json.dump(table_dict, writer) + writer.close() if __name__ == '__main__': diff --git a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/sdk/main.py b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/sdk/main.py index 9eaa70eba03e57950f48bf08f68be76cecf16207..44e8eff439ceff6f5e9c0f40187beb086d190bcb 100644 --- a/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/sdk/main.py +++ b/PyTorch/contrib/cv/classification/SPNASNet_100_for_PyTorch/infer/sdk/main.py @@ -18,6 +18,7 @@ import os import json import sys +import shutil import time import MxpiDataType_pb2 as MxpiDataType import numpy as np @@ -26,12 +27,11 @@ from StreamManagerApi import StreamManagerApi, InProtobufVector, \ MxProtobufIn, StringVector -def w2txt(file_path, filename_list, data): - with open(file_path, "w") as file: - for i in range(data.shape[0]): - s = filename_list[i].split('.')[0] + ' ' - s += ' '.join(str(int(num)) for num in data[i]) - file.write(s+"\n") +def w2txt(file_path, filename_name, data): + with open(os.path.join(file_path, filename_name + ".txt"), "w") as file: + s = filename_name.split('.')[0] + ' ' + s += ' '.join(str(int(num)) for num in data) + file.write(s+"\n") def send_source_data(appsrc_id, tensor, stream_name, stream_manager): @@ -176,8 +176,11 @@ def run(): count = 0 resCnt = 0 n_labels = 0 - filename_list = [] - sort_index_sents = [] + out_path = "output" + if(os.path.exists(out_path)): + shutil.rmtree(out_path) + else: + os.mkdir(out_path) for file in file_list: print(file, "====", count) count += 1 @@ -214,7 +217,6 @@ def run(): result = MxpiDataType.MxpiTensorPackageList() result.ParseFromString(infer_result[0].messageBuf) res = np.frombuffer(result.tensorPackageVec[0].tensorVec[0].dataStr, dtype='