新框架样例模型后处理代码
后处理头文件(SamplePostProcess.h)
/* * Copyright(C) 2023. Huawei Technologies Co.,Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef SAMPLE_POST_PROCESS_H #define SAMPLE_POST_PROCESS_H #include "MxBase/PostProcessBases/ClassPostProcessBase.h" namespace MxBase { class SamplePostProcess : public ClassPostProcessBase { public: SamplePostProcess() = default; ~SamplePostProcess() = default; SamplePostProcess(const SamplePostProcess &other) = default; APP_ERROR Init(const std::map<std::string, std::shared_ptr<void>> &postConfig) override; APP_ERROR DeInit() override; APP_ERROR Process(const std::vector<TensorBase> &tensors, std::vector<std::vector<ClassInfo>> &classInfos, const std::map<std::string, std::shared_ptr<void>> &configParamMap = {}) override; SamplePostProcess &operator=(const SamplePostProcess &other); bool IsValidTensors(const std::vector<TensorBase> &tensors) const; private: uint32_t classNum_ = 0; bool softmax_ = true; uint32_t topK_ = 1; }; extern "C" { std::shared_ptr<MxBase::SamplePostProcess> GetClassInstance(); } } #endif
后处理源文件(SamplePostProcess.cpp)
/* * Copyright(C) 2023. Huawei Technologies Co.,Ltd. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "SamplePostProcess.h" #include "MxBase/Log/Log.h" #include "MxBase/Maths/FastMath.h" namespace MxBase { SamplePostProcess& SamplePostProcess::operator=(const SamplePostProcess &other) { if (this == &other) { return *this; } ClassPostProcessBase::operator=(other); softmax_ = other.softmax_; classNum_ = other.classNum_; topK_ = other.topK_; return *this; } APP_ERROR SamplePostProcess::Init(const std::map<std::string, std::shared_ptr<void>> &postConfig) { LogDebug << "Start to Init SamplePostProcess."; APP_ERROR ret = ClassPostProcessBase::Init(postConfig); if (ret != APP_ERR_OK) { LogError << GetError(ret) << "Fail to superInit in ClassPostProcessBase."; return ret; } configData_.GetFileValue("SOFTMAX", softmax_); configData_.GetFileValue("CLASS_NUM", classNum_); configData_.GetFileValue("TOP_K", topK_); LogDebug << "End to Init SamplePostProcess."; return APP_ERR_OK; } APP_ERROR SamplePostProcess::DeInit() { return APP_ERR_OK; } bool SamplePostProcess::IsValidTensors(const std::vector<TensorBase> &tensors) const { const uint32_t softmaxTensorIndex = 0; auto softmaxTensor = tensors[softmaxTensorIndex]; auto softmaxShape = softmaxTensor.GetShape(); if (softmaxShape[1] != classNum_) { LogError << "input size(" << softmaxShape[1] << ") " << "classNumber(" << classNum_ << ")"; return false; } return true; } APP_ERROR SamplePostProcess::Process(const std::vector<TensorBase>& tensors, std::vector<std::vector<ClassInfo>> &classInfos, const std::map<std::string, std::shared_ptr<void>> &configParamMap) { LogDebug << "Start to Process SamplePostProcess."; APP_ERROR ret = APP_ERR_OK; auto inputs = tensors; ret = CheckAndMoveTensors(inputs); if (ret != APP_ERR_OK) { LogError << "CheckAndMoveTensors failed. ret=" << ret; return ret; } const uint32_t softmaxTensorIndex = 0; auto softmaxTensor = inputs[softmaxTensorIndex]; auto shape = softmaxTensor.GetShape(); uint32_t batchSize = shape[0]; void *softmaxTensorPtr = softmaxTensor.GetBuffer(); uint32_t topk = std::min(topK_, classNum_); for (uint32_t i = 0; i < batchSize; i++) { std::vector<uint32_t> idx = {}; for (uint32_t j = 0; j < classNum_; j++) { idx.push_back(j); } std::vector<float> softmax = {}; for (uint32_t j = 0; j < classNum_; j++) { float value = *((float*)softmaxTensorPtr + i * classNum_ + j); softmax.push_back(value); } if (softmax_) { fastmath::softmax(softmax); } auto cmp = [&softmax] (uint32_t index_1, uint32_t index_2) { return softmax[index_1] > softmax[index_2]; }; std::sort(idx.begin(), idx.end(), cmp); std::vector<ClassInfo> topkClassInfos = {}; for (uint32_t j = 0; j < topk; j++) { ClassInfo clsInfo = {}; clsInfo.classId = idx[j]; clsInfo.confidence = softmax[idx[j]]; clsInfo.className = configData_.GetClassName(idx[j]); topkClassInfos.push_back(clsInfo); } classInfos.push_back(topkClassInfos); } LogDebug << "End to Process SamplePostProcess."; return APP_ERR_OK; } extern "C" { std::shared_ptr<MxBase::SamplePostProcess> GetClassInstance() { LogInfo << "Begin to get SamplePostProcess instance."; auto instance = std::make_shared<MxBase::SamplePostProcess>(); LogInfo << "End to get SamplePostProcess instance."; return instance; } } }
CMakeLists(CMakeLists.txt)
cmake_minimum_required(VERSION 3.5.2) project(samplepostprocess) add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) add_definitions(-Dgoogle=mindxsdk_private) set(PLUGIN_NAME "samplepostprocess") set(TARGET_LIBRARY ${PLUGIN_NAME}) include_directories(${PROJECT_SOURCE_DIR}/../../include) include_directories(${PROJECT_SOURCE_DIR}/../../opensource/include) include_directories(${PROJECT_SOURCE_DIR}/../../opensource/include/gstreamer-1.0) include_directories(${PROJECT_SOURCE_DIR}/../../opensource/include/glib-2.0) include_directories(${PROJECT_SOURCE_DIR}/../../opensource/lib/glib-2.0/include) link_directories(${PROJECT_SOURCE_DIR}/../../opensource/lib/) link_directories(${PROJECT_SOURCE_DIR}/../../lib) add_compile_options(-std=c++11 -fPIC -fstack-protector-all -pie -Wno-deprecated-declarations) add_compile_options("-DPLUGIN_NAME=${PLUGIN_NAME}") add_definitions(-DENABLE_DVPP_INTERFACE) add_library(${TARGET_LIBRARY} SHARED SamplePostProcess.cpp) target_link_libraries(${TARGET_LIBRARY} glib-2.0 gstreamer-1.0 gobject-2.0 gstbase-1.0 gmodule-2.0) target_link_libraries(${TARGET_LIBRARY} plugintoolkit mxpidatatype mxbase) target_link_libraries(${TARGET_LIBRARY} -Wl,-z,relro,-z,now,-z,noexecstack -s) install(TARGETS ${TARGET_LIBRARY} PERMISSIONS OWNER_WRITE OWNER_READ GROUP_READ LIBRARY DESTINATION ${PROJECT_SOURCE_DIR}/../../lib/modelpostprocessors)
父主题: 代码参考