From 36fe418d77c8f2a6cebaa5251ebe742f58416d57 Mon Sep 17 00:00:00 2001 From: Atlas_ymc <253233345@qq.com> Date: Sat, 11 Sep 2021 18:21:45 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E8=AF=B4=E6=98=8E?= =?UTF-8?q?=E3=80=91=E4=BF=AE=E6=94=B9CI=E9=97=A8=E7=A6=81=E6=8A=A5?= =?UTF-8?q?=E9=94=99=20=E3=80=90=E6=A8=A1=E5=9E=8B=E3=80=91ResNet18=20?= =?UTF-8?q?=E3=80=90=E9=AA=8C=E8=AF=81=E7=BB=93=E6=9E=9C=E3=80=91=E6=98=87?= =?UTF-8?q?=E8=85=BE910=E3=80=81310=E8=8A=AF=E7=89=87=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20=E3=80=90=E4=BF=AE=E6=94=B9=E4=BA=BA?= =?UTF-8?q?=E3=80=91yumingchuan=20=E3=80=90=E8=AF=84=E5=AE=A1=E4=BA=BA?= =?UTF-8?q?=E3=80=91chenshushu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .jenkins/check/config/filter_cpplint.txt | 1 + .../mxbase/Resnet18ClassifyOpencv.cpp | 48 ++++++++++--------- .../ResNet18/mxbase/Resnet18ClassifyOpencv.h | 6 +-- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/.jenkins/check/config/filter_cpplint.txt b/.jenkins/check/config/filter_cpplint.txt index c0f1a9e03df..805b38edda6 100644 --- a/.jenkins/check/config/filter_cpplint.txt +++ b/.jenkins/check/config/filter_cpplint.txt @@ -18,6 +18,7 @@ # Modelzoo "mindspore/model_zoo/official/cv/yolov4_tiny/infer/mxbase/src/Yolov4TinyDetection.h" "runtime/references" "mindspore/model_zoo/official/cv/yolov4_tiny/infer/mxbase/src/PostProcess/Yolov4TinyMindsporePost.h" "runtime/references" +"mindspore/model_zoo/official/cv/resnet/infer/ResNet18/mxbase/Resnet18ClassifyOpencv.h" "runtime/references" # MindData "mindspore/mindspore/ccsrc/minddata/mindrecord/include/shard_page.h" "runtime/string" diff --git a/model_zoo/official/cv/resnet/infer/ResNet18/mxbase/Resnet18ClassifyOpencv.cpp b/model_zoo/official/cv/resnet/infer/ResNet18/mxbase/Resnet18ClassifyOpencv.cpp index 61714368ffc..cbaeb5f41f6 100644 --- a/model_zoo/official/cv/resnet/infer/ResNet18/mxbase/Resnet18ClassifyOpencv.cpp +++ b/model_zoo/official/cv/resnet/infer/ResNet18/mxbase/Resnet18ClassifyOpencv.cpp @@ -18,7 +18,11 @@ #include "MxBase/DeviceManager/DeviceManager.h" #include "MxBase/Log/Log.h" -using namespace MxBase; +using MxBase::DeviceManager; +using MxBase::TensorBase; +using MxBase::MemoryData; +using MxBase::ClassInfo; + namespace { const uint32_t YUV_BYTE_NU = 3; const uint32_t YUV_BYTE_DE = 2; @@ -27,7 +31,7 @@ namespace { APP_ERROR Resnet18ClassifyOpencv::Init(const InitParam &initParam) { deviceId_ = initParam.deviceId; - APP_ERROR ret = MxBase::DeviceManager::GetInstance()->InitDevices(); + APP_ERROR ret = DeviceManager::GetInstance()->InitDevices(); if (ret != APP_ERR_OK) { LogError << "Init devices failed, ret=" << ret << "."; return ret; @@ -76,36 +80,36 @@ APP_ERROR Resnet18ClassifyOpencv::DeInit() { dvppWrapper_->DeInit(); model_->DeInit(); post_->DeInit(); - MxBase::DeviceManager::GetInstance()->DestroyDevices(); + DeviceManager::GetInstance()->DestroyDevices(); return APP_ERR_OK; } -APP_ERROR Resnet18ClassifyOpencv::ConvertImageToTensorBase(std::string &imgPath, - MxBase::TensorBase &tensorBase) { +APP_ERROR Resnet18ClassifyOpencv::ConvertImageToTensorBase(const std::string &imgPath, + TensorBase &tensorBase) { static constexpr uint32_t resizeHeight = 304; static constexpr uint32_t resizeWidth = 304; cv::Mat imageMat = cv::imread(imgPath, cv::IMREAD_COLOR); cv::resize(imageMat, imageMat, cv::Size(resizeWidth, resizeHeight)); - const uint32_t dataSize = imageMat.cols * imageMat.rows * XRGB_WIDTH_NU; + const uint32_t dataSize = imageMat.cols * imageMat.rows * MxBase::XRGB_WIDTH_NU; LogInfo << "image size after resize" << imageMat.cols << " " << imageMat.rows; MemoryData memoryDataDst(dataSize, MemoryData::MEMORY_DEVICE, deviceId_); MemoryData memoryDataSrc(imageMat.data, dataSize, MemoryData::MEMORY_HOST_MALLOC); - APP_ERROR ret = MemoryHelper::MxbsMallocAndCopy(memoryDataDst, memoryDataSrc); + APP_ERROR ret = MxBase::MemoryHelper::MxbsMallocAndCopy(memoryDataDst, memoryDataSrc); if (ret != APP_ERR_OK) { LogError << GetError(ret) << "Memory malloc failed."; return ret; } - std::vector shape = {imageMat.rows * XRGB_WIDTH_NU, static_cast(imageMat.cols)}; - tensorBase = TensorBase(memoryDataDst, false, shape, TENSOR_DTYPE_UINT8); + std::vector shape = {imageMat.rows * MxBase::XRGB_WIDTH_NU, static_cast(imageMat.cols)}; + tensorBase = TensorBase(memoryDataDst, false, shape, MxBase::TENSOR_DTYPE_UINT8); return APP_ERR_OK; } -APP_ERROR Resnet18ClassifyOpencv::Inference(std::vector &inputs, - std::vector &outputs) { +APP_ERROR Resnet18ClassifyOpencv::Inference(std::vector &inputs, + std::vector &outputs) { auto dtypes = model_->GetOutputDataType(); for (size_t i = 0; i < modelDesc_.outputTensors.size(); ++i) { std::vector shape = {}; @@ -120,8 +124,8 @@ APP_ERROR Resnet18ClassifyOpencv::Inference(std::vector &inp } outputs.push_back(tensor); } - DynamicInfo dynamicInfo = {}; - dynamicInfo.dynamicType = DynamicType::STATIC_BATCH; + MxBase::DynamicInfo dynamicInfo = {}; + dynamicInfo.dynamicType = MxBase::DynamicType::STATIC_BATCH; auto startTime = std::chrono::high_resolution_clock::now(); APP_ERROR ret = model_->ModelInference(inputs, outputs, dynamicInfo); auto endTime = std::chrono::high_resolution_clock::now(); @@ -134,8 +138,8 @@ APP_ERROR Resnet18ClassifyOpencv::Inference(std::vector &inp return APP_ERR_OK; } -APP_ERROR Resnet18ClassifyOpencv::PostProcess(std::vector &inputs, - std::vector> &clsInfos) { +APP_ERROR Resnet18ClassifyOpencv::PostProcess(std::vector &inputs, + std::vector> &clsInfos) { APP_ERROR ret = post_->Process(inputs, clsInfos); if (ret != APP_ERR_OK) { LogError << "Process failed, ret=" << ret << "."; @@ -144,8 +148,8 @@ APP_ERROR Resnet18ClassifyOpencv::PostProcess(std::vector &i return APP_ERR_OK; } -APP_ERROR Resnet18ClassifyOpencv::SaveResult(std::string &imgPath, - std::vector> &batchClsInfos) { +APP_ERROR Resnet18ClassifyOpencv::SaveResult(const std::string &imgPath, + std::vector> &batchClsInfos) { LogInfo << "image path" << imgPath; std::string fileName = imgPath.substr(imgPath.find_last_of("/") + 1); size_t dot = fileName.find_last_of("."); @@ -174,10 +178,10 @@ APP_ERROR Resnet18ClassifyOpencv::SaveResult(std::string &imgPath, return APP_ERR_OK; } -APP_ERROR Resnet18ClassifyOpencv::Process(std::string &imgPath) { - MxBase::TensorBase tensorBase; - std::vector inputs = {}; - std::vector outputs = {}; +APP_ERROR Resnet18ClassifyOpencv::Process(const std::string &imgPath) { + TensorBase tensorBase; + std::vector inputs; + std::vector outputs; APP_ERROR ret = ConvertImageToTensorBase(imgPath, tensorBase); if (ret != APP_ERR_OK) { @@ -196,7 +200,7 @@ APP_ERROR Resnet18ClassifyOpencv::Process(std::string &imgPath) { LogError << "Inference failed, ret=" << ret << "."; return ret; } - std::vector> BatchClsInfos = {}; + std::vector> BatchClsInfos; ret = PostProcess(outputs, BatchClsInfos); if (ret != APP_ERR_OK) { LogError << "PostProcess failed, ret=" << ret << "."; diff --git a/model_zoo/official/cv/resnet/infer/ResNet18/mxbase/Resnet18ClassifyOpencv.h b/model_zoo/official/cv/resnet/infer/ResNet18/mxbase/Resnet18ClassifyOpencv.h index d351190ed56..c4037f4dc3a 100644 --- a/model_zoo/official/cv/resnet/infer/ResNet18/mxbase/Resnet18ClassifyOpencv.h +++ b/model_zoo/official/cv/resnet/infer/ResNet18/mxbase/Resnet18ClassifyOpencv.h @@ -41,17 +41,17 @@ class Resnet18ClassifyOpencv { public: APP_ERROR Init(const InitParam &initParam); APP_ERROR DeInit(); - APP_ERROR ConvertImageToTensorBase(std::string &imgPath, MxBase::TensorBase &tensorBase); + APP_ERROR ConvertImageToTensorBase(const std::string &imgPath, MxBase::TensorBase &tensorBase); APP_ERROR Inference(std::vector &inputs, std::vector &outputs); APP_ERROR PostProcess(std::vector &inputs, std::vector> &clsInfos); - APP_ERROR Process(std::string &imgPath); + APP_ERROR Process(const std::string &imgPath); // get infer time double GetInferCostMilliSec() const {return inferCostTimeMilliSec;} private: - APP_ERROR SaveResult(std::string &imgPath, + APP_ERROR SaveResult(const std::string &imgPath, std::vector> &batchClsInfos); std::shared_ptr dvppWrapper_; std::shared_ptr model_;