forked from mindspore-Ecosystem/mindspore
【修改说明】修改CI门禁报错
【模型】ResNet18 【验证结果】昇腾910、310芯片验证通过 【修改人】yumingchuan 【评审人】chenshushu
This commit is contained in:
parent
6492760c84
commit
308b5e43db
|
@ -40,7 +40,7 @@ link_directories(${OPENSOURCE_DIR}/lib)
|
|||
link_directories(${MXBASE_LIB_DIR})
|
||||
link_directories(${MXBASE_POST_LIB_DIR})
|
||||
|
||||
add_executable(${TARGET} main_opencv.cpp Resnet50ClassifyOpencv.cpp)
|
||||
add_executable(${TARGET} main.cpp Resnet18ClassifyOpencv.cpp)
|
||||
|
||||
target_link_libraries(${TARGET} glog cpprest mxbase resnet50postprocess opencv_world stdc++fs)
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "Resnet50ClassifyOpencv.h"
|
||||
#include "Resnet18ClassifyOpencv.h"
|
||||
#include "MxBase/DeviceManager/DeviceManager.h"
|
||||
#include "MxBase/Log/Log.h"
|
||||
|
||||
|
@ -25,8 +25,7 @@ namespace {
|
|||
const uint32_t VPC_H_ALIGN = 2;
|
||||
}
|
||||
|
||||
APP_ERROR Resnet50ClassifyOpencv::Init(const InitParam &initParam)
|
||||
{
|
||||
APP_ERROR Resnet18ClassifyOpencv::Init(const InitParam &initParam) {
|
||||
deviceId_ = initParam.deviceId;
|
||||
APP_ERROR ret = MxBase::DeviceManager::GetInstance()->InitDevices();
|
||||
if (ret != APP_ERR_OK) {
|
||||
|
@ -73,8 +72,7 @@ APP_ERROR Resnet50ClassifyOpencv::Init(const InitParam &initParam)
|
|||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
APP_ERROR Resnet50ClassifyOpencv::DeInit()
|
||||
{
|
||||
APP_ERROR Resnet18ClassifyOpencv::DeInit() {
|
||||
dvppWrapper_->DeInit();
|
||||
model_->DeInit();
|
||||
post_->DeInit();
|
||||
|
@ -82,25 +80,16 @@ APP_ERROR Resnet50ClassifyOpencv::DeInit()
|
|||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
APP_ERROR Resnet50ClassifyOpencv::ReadImage(const std::string &imgPath, cv::Mat &imageMat)
|
||||
{
|
||||
imageMat = cv::imread(imgPath, cv::IMREAD_COLOR);
|
||||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
APP_ERROR Resnet50ClassifyOpencv::ResizeImage(const cv::Mat &srcImageMat, cv::Mat &dstImageMat)
|
||||
{
|
||||
APP_ERROR Resnet18ClassifyOpencv::ConvertImageToTensorBase(std::string &imgPath,
|
||||
MxBase::TensorBase &tensorBase) {
|
||||
static constexpr uint32_t resizeHeight = 304;
|
||||
static constexpr uint32_t resizeWidth = 304;
|
||||
|
||||
cv::resize(srcImageMat, dstImageMat, cv::Size(resizeWidth, resizeHeight));
|
||||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
APP_ERROR Resnet50ClassifyOpencv::CVMatToTensorBase(const cv::Mat &imageMat, MxBase::TensorBase &tensorBase)
|
||||
{
|
||||
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;
|
||||
LogInfo << "image size after crop" << imageMat.cols << " " << imageMat.rows;
|
||||
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);
|
||||
|
||||
|
@ -115,9 +104,8 @@ APP_ERROR Resnet50ClassifyOpencv::CVMatToTensorBase(const cv::Mat &imageMat, MxB
|
|||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
APP_ERROR Resnet50ClassifyOpencv::Inference(const std::vector<MxBase::TensorBase> &inputs,
|
||||
std::vector<MxBase::TensorBase> &outputs)
|
||||
{
|
||||
APP_ERROR Resnet18ClassifyOpencv::Inference(std::vector<MxBase::TensorBase> &inputs,
|
||||
std::vector<MxBase::TensorBase> &outputs) {
|
||||
auto dtypes = model_->GetOutputDataType();
|
||||
for (size_t i = 0; i < modelDesc_.outputTensors.size(); ++i) {
|
||||
std::vector<uint32_t> shape = {};
|
||||
|
@ -137,7 +125,7 @@ APP_ERROR Resnet50ClassifyOpencv::Inference(const std::vector<MxBase::TensorBase
|
|||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
APP_ERROR ret = model_->ModelInference(inputs, outputs, dynamicInfo);
|
||||
auto endTime = std::chrono::high_resolution_clock::now();
|
||||
double costMs = std::chrono::duration<double, std::milli>(endTime - startTime).count();// save time
|
||||
double costMs = std::chrono::duration<double, std::milli>(endTime - startTime).count();
|
||||
inferCostTimeMilliSec += costMs;
|
||||
if (ret != APP_ERR_OK) {
|
||||
LogError << "ModelInference failed, ret=" << ret << ".";
|
||||
|
@ -146,9 +134,8 @@ APP_ERROR Resnet50ClassifyOpencv::Inference(const std::vector<MxBase::TensorBase
|
|||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
APP_ERROR Resnet50ClassifyOpencv::PostProcess(const std::vector<MxBase::TensorBase> &inputs,
|
||||
std::vector<std::vector<MxBase::ClassInfo>> &clsInfos)
|
||||
{
|
||||
APP_ERROR Resnet18ClassifyOpencv::PostProcess(std::vector<MxBase::TensorBase> &inputs,
|
||||
std::vector<std::vector<MxBase::ClassInfo>> &clsInfos) {
|
||||
APP_ERROR ret = post_->Process(inputs, clsInfos);
|
||||
if (ret != APP_ERR_OK) {
|
||||
LogError << "Process failed, ret=" << ret << ".";
|
||||
|
@ -157,8 +144,8 @@ APP_ERROR Resnet50ClassifyOpencv::PostProcess(const std::vector<MxBase::TensorBa
|
|||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
APP_ERROR Resnet50ClassifyOpencv::SaveResult(const std::string &imgPath, const std::vector<std::vector<MxBase::ClassInfo>> &batchClsInfos)
|
||||
{
|
||||
APP_ERROR Resnet18ClassifyOpencv::SaveResult(std::string &imgPath,
|
||||
std::vector<std::vector<MxBase::ClassInfo>> &batchClsInfos) {
|
||||
LogInfo << "image path" << imgPath;
|
||||
std::string fileName = imgPath.substr(imgPath.find_last_of("/") + 1);
|
||||
size_t dot = fileName.find_last_of(".");
|
||||
|
@ -187,24 +174,14 @@ APP_ERROR Resnet50ClassifyOpencv::SaveResult(const std::string &imgPath, const s
|
|||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
APP_ERROR Resnet50ClassifyOpencv::Process(const std::string &imgPath)
|
||||
{
|
||||
cv::Mat imageMat;
|
||||
APP_ERROR ret = ReadImage(imgPath, imageMat);
|
||||
if (ret != APP_ERR_OK) {
|
||||
LogError << "ReadImage failed, ret=" << ret << ".";
|
||||
return ret;
|
||||
}
|
||||
|
||||
ResizeImage(imageMat, imageMat);
|
||||
|
||||
APP_ERROR Resnet18ClassifyOpencv::Process(std::string &imgPath) {
|
||||
MxBase::TensorBase tensorBase;
|
||||
std::vector<MxBase::TensorBase> inputs = {};
|
||||
std::vector<MxBase::TensorBase> outputs = {};
|
||||
|
||||
TensorBase tensorBase;
|
||||
ret = CVMatToTensorBase(imageMat, tensorBase);
|
||||
APP_ERROR ret = ConvertImageToTensorBase(imgPath, tensorBase);
|
||||
if (ret != APP_ERR_OK) {
|
||||
LogError << "CVMatToTensorBase failed, ret=" << ret << ".";
|
||||
LogError << "Convert image to TensorBase failed, ret=" << ret << ".";
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -213,7 +190,7 @@ APP_ERROR Resnet50ClassifyOpencv::Process(const std::string &imgPath)
|
|||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
ret = Inference(inputs, outputs);
|
||||
auto endTime = std::chrono::high_resolution_clock::now();
|
||||
double costMs = std::chrono::duration<double, std::milli>(endTime - startTime).count();// save time
|
||||
double costMs = std::chrono::duration<double, std::milli>(endTime - startTime).count();
|
||||
inferCostTimeMilliSec += costMs;
|
||||
if (ret != APP_ERR_OK) {
|
||||
LogError << "Inference failed, ret=" << ret << ".";
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright (c) 2021. 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 MXBASE_RESNET18CLASSIFYOPENCV_H
|
||||
#define MXBASE_RESNET18CLASSIFYOPENCV_H
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "MxBase/DvppWrapper/DvppWrapper.h"
|
||||
#include "MxBase/ModelInfer/ModelInferenceProcessor.h"
|
||||
#include "ClassPostProcessors/Resnet50PostProcess.h"
|
||||
#include "MxBase/Tensor/TensorContext/TensorContext.h"
|
||||
|
||||
struct InitParam {
|
||||
uint32_t deviceId;
|
||||
std::string labelPath;
|
||||
uint32_t classNum;
|
||||
uint32_t topk;
|
||||
bool softmax;
|
||||
bool checkTensor;
|
||||
std::string modelPath;
|
||||
};
|
||||
|
||||
class Resnet18ClassifyOpencv {
|
||||
public:
|
||||
APP_ERROR Init(const InitParam &initParam);
|
||||
APP_ERROR DeInit();
|
||||
APP_ERROR ConvertImageToTensorBase(std::string &imgPath, MxBase::TensorBase &tensorBase);
|
||||
APP_ERROR Inference(std::vector<MxBase::TensorBase> &inputs,
|
||||
std::vector<MxBase::TensorBase> &outputs);
|
||||
APP_ERROR PostProcess(std::vector<MxBase::TensorBase> &inputs,
|
||||
std::vector<std::vector<MxBase::ClassInfo>> &clsInfos);
|
||||
APP_ERROR Process(std::string &imgPath);
|
||||
// get infer time
|
||||
double GetInferCostMilliSec() const {return inferCostTimeMilliSec;}
|
||||
|
||||
private:
|
||||
APP_ERROR SaveResult(std::string &imgPath,
|
||||
std::vector<std::vector<MxBase::ClassInfo>> &batchClsInfos);
|
||||
std::shared_ptr<MxBase::DvppWrapper> dvppWrapper_;
|
||||
std::shared_ptr<MxBase::ModelInferenceProcessor> model_;
|
||||
std::shared_ptr<MxBase::Resnet50PostProcess> post_;
|
||||
MxBase::ModelDesc modelDesc_;
|
||||
uint32_t deviceId_ = 0;
|
||||
// infer time
|
||||
double inferCostTimeMilliSec = 0.0;
|
||||
};
|
||||
|
||||
#endif
|
|
@ -1,64 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021. 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 MXBASE_RESNET50CLASSIFYOPENCV_H
|
||||
#define MXBASE_RESNET50CLASSIFYOPENCV_H
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#include "MxBase/DvppWrapper/DvppWrapper.h"
|
||||
#include "MxBase/ModelInfer/ModelInferenceProcessor.h"
|
||||
#include "ClassPostProcessors/Resnet50PostProcess.h"
|
||||
#include "MxBase/Tensor/TensorContext/TensorContext.h"
|
||||
|
||||
struct InitParam {
|
||||
uint32_t deviceId;
|
||||
std::string labelPath;
|
||||
uint32_t classNum;
|
||||
uint32_t topk;
|
||||
bool softmax;
|
||||
bool checkTensor;
|
||||
std::string modelPath;
|
||||
};
|
||||
|
||||
class Resnet50ClassifyOpencv {
|
||||
public:
|
||||
APP_ERROR Init(const InitParam &initParam);
|
||||
APP_ERROR DeInit();
|
||||
APP_ERROR ReadImage(const std::string &imgPath, cv::Mat &imageMat);
|
||||
APP_ERROR ResizeImage(const cv::Mat &srcImageMat, cv::Mat &dstImageMat);
|
||||
APP_ERROR CenterCropImage(const cv::Mat &img, cv::Mat &crop_im);
|
||||
APP_ERROR CVMatToTensorBase(const cv::Mat &imageMat, MxBase::TensorBase &tensorBase);
|
||||
APP_ERROR Inference(const std::vector<MxBase::TensorBase> &inputs, std::vector<MxBase::TensorBase> &outputs);
|
||||
APP_ERROR PostProcess(const std::vector<MxBase::TensorBase> &inputs,
|
||||
std::vector<std::vector<MxBase::ClassInfo>> &clsInfos);
|
||||
APP_ERROR Process(const std::string &imgPath);
|
||||
// get infer time
|
||||
double GetInferCostMilliSec() const {return inferCostTimeMilliSec;}
|
||||
|
||||
private:
|
||||
APP_ERROR SaveResult(const std::string &imgPath, const std::vector<std::vector<MxBase::ClassInfo>> &batchClsInfos);
|
||||
private:
|
||||
std::shared_ptr<MxBase::DvppWrapper> dvppWrapper_;
|
||||
std::shared_ptr<MxBase::ModelInferenceProcessor> model_;
|
||||
std::shared_ptr<MxBase::Resnet50PostProcess> post_;
|
||||
MxBase::ModelDesc modelDesc_;
|
||||
uint32_t deviceId_ = 0;
|
||||
// infer time
|
||||
double inferCostTimeMilliSec = 0.0;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
|
@ -33,7 +33,7 @@ function check_env()
|
|||
fi
|
||||
}
|
||||
|
||||
function build_resnet50()
|
||||
function build_resnet18()
|
||||
{
|
||||
cd $path_cur
|
||||
rm -rf build
|
||||
|
@ -43,11 +43,11 @@ function build_resnet50()
|
|||
make
|
||||
ret=$?
|
||||
if [ ${ret} -ne 0 ]; then
|
||||
echo "Failed to build resnet50."
|
||||
echo "Failed to build resnet18."
|
||||
exit ${ret}
|
||||
fi
|
||||
make install
|
||||
}
|
||||
|
||||
check_env
|
||||
build_resnet50
|
||||
build_resnet18
|
||||
|
|
|
@ -14,39 +14,38 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "Resnet50ClassifyOpencv.h"
|
||||
#include "Resnet18ClassifyOpencv.h"
|
||||
#include "MxBase/Log/Log.h"
|
||||
#include <dirent.h>
|
||||
|
||||
namespace {
|
||||
const uint32_t CLASS_NUM = 1001;
|
||||
} // namespace
|
||||
}
|
||||
|
||||
APP_ERROR ScanImages(const std::string &path, std::vector<std::string> &imgFiles)
|
||||
{
|
||||
DIR *dirPtr = opendir(path.c_str());
|
||||
if (dirPtr == nullptr) {
|
||||
LogError << "opendir failed. dir:" << path;
|
||||
return APP_ERR_INTERNAL_ERROR;
|
||||
APP_ERROR ReadFilesFromPath(const std::string &path, std::vector<std::string> *files) {
|
||||
DIR *dir = NULL;
|
||||
struct dirent *ptr = NULL;
|
||||
|
||||
if ((dir=opendir(path.c_str())) == NULL) {
|
||||
LogError << "Open dir error: " << path;
|
||||
return APP_ERR_COMM_OPEN_FAIL;
|
||||
}
|
||||
dirent *direntPtr = nullptr;
|
||||
while ((direntPtr = readdir(dirPtr)) != nullptr) {
|
||||
std::string fileName = direntPtr->d_name;
|
||||
if (fileName == "." || fileName == "..") {
|
||||
continue;
|
||||
|
||||
while ((ptr=readdir(dir)) != NULL) {
|
||||
// d_type == 8 is file
|
||||
if (ptr->d_type == 8) {
|
||||
files->push_back(path + ptr->d_name);
|
||||
}
|
||||
|
||||
imgFiles.emplace_back(path + "/" + fileName);
|
||||
}
|
||||
closedir(dirPtr);
|
||||
closedir(dir);
|
||||
// sort ascending order
|
||||
sort(files->begin(), files->end());
|
||||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc <= 1) {
|
||||
LogWarn << "Please input image path, such as './resnet50 image_dir'.";
|
||||
LogWarn << "Please input image path, such as './resnet image_dir'.";
|
||||
return APP_ERR_OK;
|
||||
}
|
||||
|
||||
|
@ -58,32 +57,34 @@ int main(int argc, char* argv[])
|
|||
initParam.softmax = false;
|
||||
initParam.checkTensor = true;
|
||||
initParam.modelPath = "../data/model/resnet18-304_304.om";
|
||||
auto resnet50 = std::make_shared<Resnet50ClassifyOpencv>();
|
||||
APP_ERROR ret = resnet50->Init(initParam);
|
||||
auto resnet18 = std::make_shared<Resnet18ClassifyOpencv>();
|
||||
APP_ERROR ret = resnet18->Init(initParam);
|
||||
if (ret != APP_ERR_OK) {
|
||||
LogError << "Resnet50Classify init failed, ret=" << ret << ".";
|
||||
LogError << "resnet18Classify init failed, ret=" << ret << ".";
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string imgPath = argv[1];
|
||||
std::vector<std::string> imgFilePaths;
|
||||
ret = ScanImages(imgPath, imgFilePaths);
|
||||
std::string inferPath = argv[1];
|
||||
std::vector<std::string> files;
|
||||
ret = ReadFilesFromPath(inferPath, &files);
|
||||
if (ret != APP_ERR_OK) {
|
||||
LogError << "Read files from path failed, ret=" << ret << ".";
|
||||
return ret;
|
||||
}
|
||||
|
||||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
for (auto &imgFile : imgFilePaths) {
|
||||
ret = resnet50->Process(imgFile);
|
||||
for (uint32_t i = 0; i < files.size(); i++) {
|
||||
ret = resnet18->Process(files[i]);
|
||||
if (ret != APP_ERR_OK) {
|
||||
LogError << "Resnet50Classify process failed, ret=" << ret << ".";
|
||||
resnet50->DeInit();
|
||||
LogError << "resnet18Classify process failed, ret=" << ret << ".";
|
||||
resnet18->DeInit();
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
auto endTime = std::chrono::high_resolution_clock::now();
|
||||
resnet50->DeInit();
|
||||
resnet18->DeInit();
|
||||
double costMilliSecs = std::chrono::duration<double, std::milli>(endTime - startTime).count();
|
||||
double fps = 1000.0 * imgFilePaths.size() / resnet50->GetInferCostMilliSec();
|
||||
double fps = 1000.0 * files.size() / resnet18->GetInferCostMilliSec();
|
||||
LogInfo << "[Process Delay] cost: " << costMilliSecs << " ms\tfps: " << fps << " imgs/sec";
|
||||
return APP_ERR_OK;
|
||||
}
|
Loading…
Reference in New Issue