Skip to content
Snippets Groups Projects
Commit d8b5be0a authored by JessonGuo's avatar JessonGuo
Browse files

.

Update Files
parent 3b7d78c4
Branches
Tags
No related merge requests found
Showing
with 1373 additions and 2 deletions
......@@ -10,3 +10,9 @@
"models/research/cv/FaceRecognitionForTracking/infer/mxbase/src/FaceRecognitionForTracking.cpp" "runtime/references"
"models/research/cv/FaceRecognitionForTracking/infer/util/plugins/MxpiTransposePlugin.h" "runtime/references"
"models/research/cv/FaceRecognitionForTracking/infer/util/plugins/MxpiTransposePlugin.h" "build/namespaces"
"models/research/cv/FaceQualityAssessment/infer/mxbase/src/FQA.h" "runtime/references"
"models/research/cv/FaceQualityAssessment/infer/mxbase/src/FQA.cpp" "runtime/references"
"models/research/cv/FaceQualityAssessment/infer/util/plugins/MxpiTransposePlugin.h" "runtime/references"
"models/research/cv/FaceQualityAssessment/infer/util/plugins/MxpiTransposePlugin.h" "build/namespaces"
"models/research/cv/FaceQualityAssessment/infer/util/plugins/MxpiTransposePlugin.cpp" "runtime/references"
\ No newline at end of file
......@@ -474,4 +474,4 @@ sh run_export_cpu.sh [PRETRAINED_BACKBONE] [BATCH_SIZE] [FILE_NAME](optional)
# [ModelZoo Homepage](#contents)
Please check the official [homepage](https://gitee.com/mindspore/models).
Please check the official [homepage](https://gitee.com/mindspore/mindspore/tree/master/model_zoo).
#!/usr/bin/env bash
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
# air model file path
model_path=$1
# result om model file name
output_model_name=$2
atc \
--input_format=NCHW \
--framework=1 \
--model=$model_path \
--output=$output_model_name \
--output_type=FP32 \
--soc_version=Ascend310
\ No newline at end of file
{
"face_quality_assessment": {
"stream_config": {
"deviceId": "0"
},
"appsrc0": {
"props": {
"blocksize": "4096000"
},
"factory": "appsrc",
"next": "mxpi_imagedecoder0"
},
"mxpi_imagedecoder0": {
"props": {
"cvProcessor": "opencv",
"outputDataFormat": "RGB",
"dataType": "float32"
},
"factory": "mxpi_imagedecoder",
"next": "mxpi_imageresize0"
},
"mxpi_imageresize0": {
"props": {
"cvProcessor": "opencv",
"resizeType": "Resizer_Stretch",
"dataSource": "mxpi_imagedecoder0",
"resizeHeight": "96",
"resizeWidth": "96"
},
"factory": "mxpi_imageresize",
"next": "mxpi_transposeplugin0"
},
"mxpi_transposeplugin0": {
"props": {
"dataSource": "mxpi_imageresize0"
},
"factory": "mxpi_transposeplugin",
"next": "mxpi_imagenormalize0"
},
"mxpi_imagenormalize0": {
"props": {
"dataSource": "mxpi_transposeplugin0",
"alpha": "0, 0, 0",
"beta": "255, 255, 255",
"dataType": "FLOAT32"
},
"factory": "mxpi_imagenormalize",
"next": "mxpi_tensorinfer0"
},
"mxpi_tensorinfer0": {
"props": {
"dataSource": "mxpi_imagenormalize0",
"modelPath": "../convert/FQA.om"
},
"factory": "mxpi_tensorinfer",
"next": "mxpi_dumpdata0"
},
"mxpi_dumpdata0": {
"props": {
"requiredMetaDataKeys": "mxpi_tensorinfer0"
},
"factory": "mxpi_dumpdata",
"next": "appsink0"
},
"appsink0": {
"props": {
"blocksize": "4096000"
},
"factory": "appsink"
}
}
}
\ No newline at end of file
#!/usr/bin/env bash
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
docker_image=$1
model_dir=$2
if [ -z "${docker_image}" ]; then
echo "please input docker_image"
exit 1
fi
if [ ! -d "${model_dir}" ]; then
echo "please input model_dir"
exit 1
fi
docker run -it \
--device=/dev/davinci1 \
--device=/dev/davinci_manager \
--device=/dev/devmm_svm \
--device=/dev/hisi_hdc \
-v /usr/local/Ascend/driver:/usr/local/Ascend/driver \
-v ${model_dir}:${model_dir} \
${docker_image} \
/bin/bash
cmake_minimum_required(VERSION 3.14.0)
project(fqa_opencv)
set(TARGET fqa_opencv)
add_definitions(-DENABLE_DVPP_INTERFACE)
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
add_definitions(-Dgoogle=mindxsdk_private)
add_compile_options(-std=c++11 -fPIE -fstack-protector-all -fPIC -Wall)
add_link_options(-Wl,-z,relro,-z,now,-z,noexecstack -s -pie)
# Check environment variable
if(NOT DEFINED ENV{ASCEND_HOME})
message(FATAL_ERROR "please define environment variable:ASCEND_HOME")
endif()
if(NOT DEFINED ENV{ASCEND_VERSION})
message(WARNING "please define environment variable:ASCEND_VERSION")
endif()
if(NOT DEFINED ENV{ARCH_PATTERN})
message(WARNING "please define environment variable:ARCH_PATTERN")
endif()
set(ACL_INC_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/acllib/include)
set(ACL_LIB_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/$ENV{ARCH_PATTERN}/acllib/lib64)
# set MxBase header and Dynamic link library
set(MXBASE_ROOT_DIR $ENV{MX_SDK_HOME})
set(MXBASE_INC ${MXBASE_ROOT_DIR}/include)
set(MXBASE_LIB_DIR ${MXBASE_ROOT_DIR}/lib)
set(MXBASE_POST_LIB_DIR ${MXBASE_ROOT_DIR}/lib/modelpostprocessors)
set(MXBASE_POST_PROCESS_DIR ${MXBASE_ROOT_DIR}/include/MxBase/postprocess/include)
# set opensource header and Dynamic link library
# include OpenCV Google log
if(DEFINED ENV{MXSDK_OPENSOURCE_DIR})
set(OPENSOURCE_DIR ${ENV{MXSDK_OPENSOURCE_DIR})
else()
set(OPENSOURCE_DIR ${MXBASE_ROOT_DIR}/opensource)
endif()
include_directories(${ACL_INC_DIR})
include_directories(${OPENSOURCE_DIR}/include)
include_directories(${OPENSOURCE_DIR}/include/opencv4)
include_directories(${MXBASE_INC})
include_directories(${MXBASE_POST_PROCESS_DIR})
link_directories(${ACL_LIB_DIR})
link_directories(${OPENSOURCE_DIR}/lib)
link_directories(${MXBASE_LIB_DIR})
link_directories(${MXBASE_POST_LIB_DIR})
add_executable(${TARGET} src/main.cpp src/FQA.cpp)
target_link_libraries(${TARGET} glog cpprest mxbase opencv_world stdc++fs)
install(TARGETS ${TARGET} RUNTIME DESTINATION ${PROJECT_SOURCE_DIR}/)
#!/usr/bin/env bash
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
export ASCEND_HOME=/usr/local/Ascend
export ASCEND_VERSION=nnrt/latest
export ARCH_PATTERN=.
export LD_LIBRARY_PATH=${MX_SDK_HOME}/lib/modelpostprocessors:${LD_LIBRARY_PATH}
dataset_path=$1
model_path=$2
if [ -d build ] ;then
rm -rf build;
fi
if [ ! -d infer_result ] ;then
mkdir infer_result;
fi
cmake -S . -B build
make -C ./build -j
./build/fqa_opencv $dataset_path $model_path
\ No newline at end of file
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* 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 "FQA.h"
#include <math.h>
#include <dirent.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include "MxBase/DeviceManager/DeviceManager.h"
#include "MxBase/DvppWrapper/DvppWrapper.h"
#include "MxBase/Log/Log.h"
template<class Iter>
inline size_t argmax(Iter first, Iter last) {
return std::distance(first, std::max_element(first, last));
}
APP_ERROR FQA::Init(const InitParam& initParam) {
APP_ERROR ret = MxBase::DeviceManager::GetInstance()->InitDevices();
if (ret != APP_ERR_OK) {
LogError << "Init devices failed, ret=" << ret << ".";
return ret;
}
ret = MxBase::TensorContext::GetInstance()->SetContext(initParam.deviceId);
if (ret != APP_ERR_OK) {
LogError << "Set context failed, ret=" << ret << ".";
return ret;
}
model_ = std::make_shared<MxBase::ModelInferenceProcessor>();
ret = model_->Init(initParam.modelPath, modelDesc_);
if (ret != APP_ERR_OK) {
LogError << "ModelInferenceProcessor init failed, ret=" << ret << ".";
return ret;
}
return APP_ERR_OK;
}
APP_ERROR FQA::DeInit() {
model_->DeInit();
MxBase::DeviceManager::GetInstance()->DestroyDevices();
return APP_ERR_OK;
}
APP_ERROR FQA::ReadImage(const std::string& imgPath, cv::Mat& imageMat) {
imageMat = cv::imread(imgPath, cv::IMREAD_COLOR);
cv::cvtColor(imageMat, imageMat, cv::COLOR_BGR2RGB);
return APP_ERR_OK;
}
APP_ERROR FQA::ResizeImage(const cv::Mat& srcImageMat, float* transMat) {
static constexpr uint32_t resizeHeight = 96;
static constexpr uint32_t resizeWidth = 96;
cv::Mat dstImageMat;
cv::resize(srcImageMat, dstImageMat, cv::Size(resizeHeight, resizeWidth));
// convert NHWC to NCHW
for (int i = 0; i < dstImageMat.rows; i++) {
for (int j = 0; j < dstImageMat.cols; j++) {
transMat[i*96+j] = static_cast<float>(dstImageMat.at<cv::Vec3b>(i, j)[0]) / 255;
transMat[96*96+i*96+j] = static_cast<float>(dstImageMat.at<cv::Vec3b>(i, j)[1]) / 255;
transMat[2*96*96+i*96+j] = static_cast<float>(dstImageMat.at<cv::Vec3b>(i, j)[2]) / 255;
}
}
return APP_ERR_OK;
}
APP_ERROR FQA::VectorToTensorBase(float* transMat, MxBase::TensorBase& tensorBase) {
const uint32_t dataSize = 3*96*96*sizeof(float);
MxBase::MemoryData memoryDataDst(dataSize, MxBase::MemoryData::MEMORY_DEVICE, deviceId_);
MxBase::MemoryData memoryDataSrc(transMat, dataSize, MxBase::MemoryData::MEMORY_HOST_MALLOC);
APP_ERROR ret = MxBase::MemoryHelper::MxbsMallocAndCopy(memoryDataDst, memoryDataSrc);
if (ret != APP_ERR_OK) {
LogError << GetError(ret) << "Memory malloc failed.";
return ret;
}
std::vector<uint32_t> shape = {3, static_cast<uint32_t>(96*96)};
tensorBase = MxBase::TensorBase(memoryDataDst, false, shape, MxBase::TENSOR_DTYPE_FLOAT32);
return APP_ERR_OK;
}
APP_ERROR FQA::Inference(const 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 = {};
for (size_t j = 0; j < modelDesc_.outputTensors[i].tensorDims.size(); ++j) {
shape.push_back((uint32_t)modelDesc_.outputTensors[i].tensorDims[j]);
}
MxBase::TensorBase tensor(shape, dtypes[i], MxBase::MemoryData::MemoryType::MEMORY_DEVICE, deviceId_);
APP_ERROR ret = MxBase::TensorBase::TensorBaseMalloc(tensor);
if (ret != APP_ERR_OK) {
LogError << "TensorBaseMalloc failed, ret=" << ret << ".";
return ret;
}
outputs.push_back(tensor);
}
MxBase::DynamicInfo dynamicInfo = {};
dynamicInfo.dynamicType = MxBase::DynamicType::STATIC_BATCH;
APP_ERROR ret = model_->ModelInference(inputs, outputs, dynamicInfo);
if (ret != APP_ERR_OK) {
LogError << "ModelInference failed, ret=" << ret << ".";
return ret;
}
return APP_ERR_OK;
}
APP_ERROR FQA::PreProcess(
const std::string& imgPath, std::vector<MxBase::TensorBase>& inputs, int& width, int& height) {
cv::Mat imageMat;
APP_ERROR ret = ReadImage(imgPath, imageMat);
height = imageMat.cols;
width = imageMat.rows;
if (ret != APP_ERR_OK) {
LogError << "ReadImage failed, ret=" << ret << ".";
return ret;
}
float transMat[27648];
ResizeImage(imageMat, transMat);
MxBase::TensorBase tensorBase;
ret = VectorToTensorBase(transMat, tensorBase);
if (ret != APP_ERR_OK) {
LogError << "VectorToTensorBase failed, ret=" << ret << ".";
return ret;
}
inputs.push_back(tensorBase);
return APP_ERR_OK;
}
bool Readgt(const std::string& txtPath, float* eulgt, int (&kp_list)[5][2], int width, int height) {
std::ifstream in(txtPath);
if (!in) {
return false;
} else {
for (int i = 0; i < 3; i++) {
in >> eulgt[i];
}
for (int i = 0; i < 5; i++) {
float x(0), y(0);
in >> x >> y;
if (x < 0 || y < 0) continue;
kp_list[i][0] = static_cast<int>((x*96)/static_cast<float>(width));
kp_list[i][1] = static_cast<int>((y*96)/static_cast<float>(height));
}
}
return true;
}
int InferResult(float heatmap[5][48][48], int (&kp_coord_ori)[5][2]) {
for (int i = 0; i < 5; i++) {
std::vector<float> map_1(48*48, 0);
float soft_sum(0);
int o(0);
for (int j = 0; j < 48; j++) {
for (int k = 0; k < 48; k++) {
map_1[o] = exp(heatmap[i][j][k]);
o++;
soft_sum += exp(heatmap[i][j][k]);
}
}
for (int j = 0; j < 48*48; j++) {
map_1[j] = map_1[j] / soft_sum;
}
int kp_coor = static_cast<int>(argmax(map_1.begin(), map_1.end()));
kp_coord_ori[i][0] = (kp_coor % 48) * 2;
kp_coord_ori[i][1] = (kp_coor / 48) * 2;
}
return -1;
}
int PrintResult(
std::vector<std::vector<float>> kp_err, std::vector<std::vector<float>> eulers_err, std::vector<float> kp_ipn) {
std::vector<float> kp_ave_error, euler_ave_error;
for (uint32_t i = 0; i < kp_err.size(); i++) {
float kp_error_all_sum(0);
for (uint32_t j = 0; j < kp_err[i].size(); j++) {
kp_error_all_sum += kp_err[i][j];
}
kp_ave_error.push_back(kp_error_all_sum/kp_err[i].size());
}
for (uint32_t i = 0; i < eulers_err.size(); i++) {
float euler_ave_error_sum(0);
for (uint32_t j = 0; j < eulers_err[i].size(); j++) {
euler_ave_error_sum += eulers_err[i][j];
}
euler_ave_error.push_back(euler_ave_error_sum/eulers_err[i].size());
}
LogInfo << "================================== infer result ==================================\n";
LogInfo << "5 keypoints average err: ["
<< kp_ave_error[0] << kp_ave_error[1] << kp_ave_error[2] << kp_ave_error[3] << kp_ave_error[4] << "]\n";
LogInfo << "3 eulers average err: [" << euler_ave_error[0] << euler_ave_error[1] << euler_ave_error[2] << "]\n";
float ipn(0), mae(0);
for (uint32_t i = 0; i < kp_ipn.size(); i++) {
ipn += kp_ipn[i];
}
LogInfo << "IPN of 5 keypoints: " << (ipn / kp_ipn.size()) * 100 << "\n";
for (uint32_t i = 0; i < euler_ave_error.size(); i++) {
mae += euler_ave_error[i];
}
LogInfo << "MAE of elur: " << mae / euler_ave_error.size() << "\n";
LogInfo << "==================================================================================\n";
return 0;
}
APP_ERROR FQA::Process(const std::string& testPath) {
DIR* directory_pointer = NULL;
struct dirent* entry;
if ((directory_pointer = opendir(testPath.c_str())) == NULL) {
printf("Error open\n");
exit(0);
}
std::vector<std::vector<float>> kp_error_all(5), eulers_error_all(3);
std::vector<float> kp_ipn;
while ((entry = readdir(directory_pointer)) != NULL) {
if (entry->d_name[0] == '.') {
continue;
}
std::string s = entry->d_name;
if (s.substr(s.length()-3, 3) != "jpg") {
continue;
}
std::string imgPath = testPath[testPath.length()-1] == '/' ? testPath+s : testPath+"/"+s;
std::string txtPath = imgPath.substr(0, imgPath.length()-3) + "txt";
std::vector<MxBase::TensorBase> inputs = {};
std::vector<MxBase::TensorBase> outputs = {};
int height(0), width(0);
PreProcess(imgPath, inputs, width, height);
APP_ERROR ret = Inference(inputs, outputs);
if (ret != APP_ERR_OK) {
LogError << "Inference failed, ret=" << ret << ".";
return ret;
}
MxBase::TensorBase& tensor0 = outputs[0];
ret = tensor0.ToHost();
if (ret != APP_ERR_OK) {
LogError << GetError(ret) << "Tensor_0 deploy to host failed.";
return ret;
}
auto eulers_ori = reinterpret_cast<float(*)>(tensor0.GetBuffer());
eulers_ori[0] *= 90;
eulers_ori[1] *= 90;
eulers_ori[2] *= 90;
MxBase::TensorBase& tensor1 = outputs[1];
ret = tensor1.ToHost();
if (ret != APP_ERR_OK) {
LogError << GetError(ret) << "Tensor_1 deploy to host failed.";
return ret;
}
auto heatmap = reinterpret_cast<float(*)[48][48]>(tensor1.GetBuffer());
std::string savePath = "infer_result/" + s.substr(0, s.length()-3) + "txt";
std::ofstream out(savePath);
std::ifstream in(txtPath);
float eulgt[3];
int kp_list[5][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}}, kp_coord_ori[5][2];
bool euler_kps_do = Readgt(txtPath, eulgt, kp_list, width, height);
if (!euler_kps_do) {
continue;
}
InferResult(heatmap, kp_coord_ori);
out << "eulers_error:";
for (int i = 0; i < 3; i++) {
eulers_error_all[i].push_back(abs(eulers_ori[i] - eulgt[i]));
out << " " << abs(eulers_ori[i] - eulgt[i]);
}
out << "\n";
bool cur_flag = true;
float eye_dis(1.0);
if (kp_list[0][0] < 0 || kp_list[0][1] < 0 || kp_list[1][0] < 0 || kp_list[1][1] < 0) {
cur_flag = false;
} else {
eye_dis = sqrt(
pow(abs(kp_list[0][0] - kp_list[1][0]), 2) + pow(abs(kp_list[0][1] - kp_list[1][1]), 2));
}
float cur_error_sum(0), cnt(0);
out << "keypoints_error:";
for (int i = 0; i < 5; i++) {
if (kp_list[i][0] != -1) {
float dis = sqrt(
pow(kp_list[i][0] - kp_coord_ori[i][0], 2) +
pow(kp_list[i][1] - kp_coord_ori[i][1], 2));
out << " " << dis;
kp_error_all[i].push_back(dis);
cur_error_sum += dis;
cnt++;
}
}
out << "\n";
if (cur_flag) {
kp_ipn.push_back(cur_error_sum / cnt / eye_dis);
}
}
PrintResult(kp_error_all, eulers_error_all, kp_ipn);
closedir(directory_pointer);
return APP_ERR_OK;
}
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* 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 UNET_SEGMENTATION_H
#define UNET_SEGMENTATION_H
#include <vector>
#include <string>
#include <memory>
#include <opencv2/opencv.hpp>
#include "MxBase/ModelInfer/ModelInferenceProcessor.h"
#include "MxBase/PostProcessBases/PostProcessDataType.h"
#include "MxBase/Tensor/TensorContext/TensorContext.h"
struct InitParam {
uint32_t deviceId;
std::string modelPath;
};
class FQA {
public:
/**
* @api
* @brief Initialize configure parameter.
* @param initParam
* @return APP_ERROR
*/
APP_ERROR Init(const InitParam& initParam);
/**
* @api
* @brief DeInitialize configure parameter.
* @return APP_ERROR
*/
APP_ERROR DeInit();
/**
* @api
* @brief reads a jpg image and convert the BGR format to RGB format.
* @param imgPath:
* @param imageMat: the image matrix stored as cv::Mat
* @return APP_ERROR
*/
APP_ERROR ReadImage(const std::string& imgPath, cv::Mat& imageMat);
/**
* @api
* @brief resize the origin image to size(96,96);
* do transpose(2,0,1) to convert format HWC to CHW;
* convert cv::Mat to float*(1-d float array)
* @param srcImageMat: source image matrix(cv::Mat) from ReadImage
* @param transMat: an 1-d float array pointer to store the image matrix info
* @return APP_ERROR
*/
APP_ERROR ResizeImage(const cv::Mat& srcImageMat, float* transMat);
/**
* @api
* @brief convert the 1-d float array to TensorBase
* @param transMat: an 1-d float array pointer stored the image matrix info
* @param tensorBase: the input of infer model
* @return APP_ERROR
*/
APP_ERROR VectorToTensorBase(float* transMat, MxBase::TensorBase& tensorBase);
/**
* @api
* @brief perform inference process
* @param inputs: the input vector Tensorbase vector
* @param outputs: the output vector of Tensorbase vector
* @return APP_ERROR
*/
APP_ERROR Inference(const std::vector<MxBase::TensorBase>& inputs, std::vector<MxBase::TensorBase>& outputs);
APP_ERROR PreProcess(const std::string& imgPath, std::vector<MxBase::TensorBase>& inputs, int& width, int& height);
/**
* @api
* @brief calculate the accuracy
* @param testPath: test dataset directory
* @return APP_ERROR
*/
APP_ERROR Process(const std::string& testPath);
private:
std::shared_ptr<MxBase::ModelInferenceProcessor> model_;
MxBase::ModelDesc modelDesc_;
uint32_t deviceId_ = 0;
};
#endif
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* 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 "FQA.h"
#include "MxBase/Log/Log.h"
int main(int argc, char *argv[]) {
if (argc <= 2) {
LogWarn << "Please input test dataset path";
return APP_ERR_OK;
}
InitParam initParam = {};
initParam.deviceId = 0;
initParam.modelPath = argv[2];
FQA fqa;
APP_ERROR ret = fqa.Init(initParam);
if (ret != APP_ERR_OK) {
LogError << "FQA init failed, ret=" << ret << ".";
return ret;
}
std::string testPath = argv[1];
ret = fqa.Process(testPath);
if (ret != APP_ERR_OK) {
LogError << "FQA process failed, ret=" << ret << ".";
fqa.DeInit();
return ret;
}
fqa.DeInit();
return APP_ERR_OK;
}
"""
Copyright 2021 Huawei Technologies Co., Ltd
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.
============================================================================
"""
import argparse
import base64
import json
import os
import cv2
import numpy as np
from StreamManagerApi import MxDataInput
from StreamManagerApi import StreamManagerApi
def parse_arg():
"""Return the cmd input args"""
parser = argparse.ArgumentParser(description="FQA infer")
parser.add_argument("-d", "--dataset", type=str, required=True, help="the directory of dataset")
parser.add_argument("-p", "--pipeline", type=str, required=True, help="the path of .pipeline file")
parser.add_argument("-o", "--output", type=str, default="", help="the path of pipeline file")
return parser.parse_args()
def get_dataset(path):
"""
Summary.
yield a image absolutely path when called
Args:
path(string): dataset path.
"""
for _, _, files in os.walk(path):
for file_name in files:
if file_name.endswith('jpg'):
yield os.path.join(path, file_name)
break
def get_stream_manager(pipeline_path):
"""
Summary.
get stream manager, initialize the stream manager api depend on pipeline
Args:
pipeline_path(string): mindx-sdk inference pipeline path.
Returns:
?, stream manager api.
"""
stream_manager_api = StreamManagerApi()
ret = stream_manager_api.InitManager()
if ret != 0:
print("Failed to init Stream manager, ret=%s" % str(ret))
return -1
with open(pipeline_path, 'rb') as f:
pipeline_content = f.read()
ret = stream_manager_api.CreateMultipleStreams(pipeline_content)
if ret != 0:
print("Failed to create stream, ret=%s" % str(ret))
return -1
return stream_manager_api
def do_infer_image(stream_manager_api, image_path):
"""
Summary.
do inference
Args:
stream_manager_api(?): stream manager.
image_path(string): inferred image path.
Returns:
list, reshaped inference result.
"""
stream_name = b'face_quality_assessment'
data_input = MxDataInput()
with open(image_path, 'rb') as f:
data_input.data = f.read()
unique_id = stream_manager_api.SendData(stream_name, 0, data_input)
if unique_id < 0:
print("Failed to send data to stream.")
return -1
infer_result = stream_manager_api.GetResult(stream_name, unique_id)
if infer_result.errorCode != 0:
print(f"GetResult error. errorCode={infer_result.errorCode},"
f"errorMsg={infer_result.data.decode()}")
return -1
infer_result_json = json.loads(infer_result.data.decode())
content = json.loads(infer_result_json['metaData'][0]['content'])
tensor_vec = content['tensorPackageVec'][0]['tensorVec'][0]
data_str = tensor_vec['dataStr']
tensor_shape = tensor_vec['tensorShape']
out_eul = np.frombuffer(base64.b64decode(data_str), dtype=np.float32) * 90
out_eul = np.reshape(out_eul, tensor_shape[1:])
tensor_vec = content['tensorPackageVec'][0]['tensorVec'][1]
data_str = tensor_vec['dataStr']
tensor_shape = tensor_vec['tensorShape']
heatmap = np.frombuffer(base64.b64decode(data_str), dtype=np.float32)
heatmap = np.reshape(heatmap, tensor_shape[1:])
return out_eul, heatmap
def read_ground_truth(img_path):
"""
Summary.
Read ground truth from txt path corresponding to image path
Args:
img_path(string): image path.
Returns:
list, [YAW,PITCH,ROLL], key point list, bool value(is ground truth valid).
"""
txt_path = ""
if img_path.endswith('jpg'):
txt_path = img_path.replace('jpg', 'txt')
else:
print("[ERROR], image format is invalid, REQUIRED .jpg")
return -1
if os.path.exists(txt_path):
img_ori = cv2.imread(img_path)
x_length = img_ori.shape[1]
y_length = img_ori.shape[0]
txt_line = open(txt_path).readline()
# [YAW] [PITCH] [ROLL]
eulers_txt = txt_line.strip().split(" ")[:3]
kp_list = [[-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1]]
box_cur = txt_line.strip().split(" ")[3:]
bndbox = []
for index in range(len(box_cur) // 2):
bndbox.append([box_cur[index * 2], box_cur[index * 2 + 1]])
kp_id = -1
for box in bndbox:
kp_id = kp_id + 1
x_coord = float(box[0])
y_coord = float(box[1])
if x_coord < 0 or y_coord < 0:
continue
kp_list[kp_id][0] = int(float(x_coord) / x_length * 96)
kp_list[kp_id][1] = int(float(y_coord) / y_length * 96)
return eulers_txt, kp_list, True
return None, None, False
def get_infer_info(eulers, heatmap):
"""
Summary.
calculate inferred values from model output
Args:
eulers(np.ndarray): inferred YAW,PITCH,ROLL.
heatmap(np.ndarray): inferred heatmap result.
Returns:
list, inferred YAW,PITCH,ROLL value and 5 key point coordinates..
"""
kp_coord_ori = list()
for i, _ in enumerate(heatmap):
map_1 = heatmap[i].reshape(1, 48 * 48)
# softmax
map_1 = np.exp(map_1) / np.sum(np.exp(map_1), axis=1)
kp_coor = map_1.argmax()
kp_coor = int((kp_coor % 48) * 2.0), int((kp_coor / 48) * 2.0)
kp_coord_ori.append(kp_coor)
return kp_coord_ori, eulers
def save_output(out, output_dir, img_path):
"""
Summary.
Save output to files.
Args:
out(list): items need to be saved.
output_dir(string): saved directory.
img_path(string): image path.
"""
if not output_dir:
return None
file_name = img_path.strip().split(os.path.sep)[-1].split('.')[0]
infer_image_path = os.path.join(output_dir, f"{file_name}_infer.txt")
output = open(infer_image_path, 'w')
for item in out:
output.write(str(item))
return 1
def main(args_):
kp_error_all = [[], [], [], [], []]
eulers_error_all = [[], [], []]
kp_ipn = []
path = args_.dataset
stream_manager_api = get_stream_manager(args_.pipeline)
for img_path in get_dataset(path):
out = []
out_eul, heatmap = do_infer_image(stream_manager_api, img_path)
eulers_gt, kp_list, euler_kps_do = read_ground_truth(img_path)
if not euler_kps_do:
continue
kp_coord_ori, eulers_ori = get_infer_info(out_eul, heatmap)
eulgt = list(eulers_gt)
eulers_error = "eulers_error:"
for euler_id, _ in enumerate(eulers_ori):
eulers_error_all[euler_id].append(abs(eulers_ori[euler_id] - float(eulgt[euler_id])))
eulers_error += " " + str(abs(eulers_ori[euler_id] - float(eulgt[euler_id])))
out.append(eulers_error + "\n")
eye01 = kp_list[0]
eye02 = kp_list[1]
eye_dis = 1
cur_flag = True
if eye01[0] < 0 or eye01[1] < 0 or eye02[0] < 0 or eye02[1] < 0:
cur_flag = False
else:
eye_dis = np.sqrt(np.square(abs(eye01[0] - eye02[0])) + np.square(abs(eye01[1] - eye02[1])))
cur_error_list = []
for i in range(5):
if kp_list[i][0] != -1:
dis = np.sqrt(
np.square(kp_list[i][0] - kp_coord_ori[i][0]) + np.square(kp_list[i][1] - kp_coord_ori[i][1]))
kp_error_all[i].append(dis)
cur_error_list.append(dis)
out.append("keypoint_error: " + str(cur_error_list) + "\n")
save_output(out, args_.output, img_path)
if cur_flag:
kp_ipn.append(sum(cur_error_list) / len(cur_error_list) / eye_dis)
kp_ave_error = []
for kps, _ in enumerate(kp_error_all):
kp_ave_error.append("%.3f" % (sum(kp_error_all[kps]) / len(kp_error_all[kps])))
euler_ave_error = []
elur_mae = []
for eulers, _ in enumerate(eulers_error_all):
euler_ave_error.append("%.3f" % (sum(eulers_error_all[eulers]) / len(eulers_error_all[eulers])))
elur_mae.append((sum(eulers_error_all[eulers]) / len(eulers_error_all[eulers])))
print('========== 5 keypoints average err:' + str(kp_ave_error))
print('========== 3 eulers average err:' + str(euler_ave_error))
print('========== IPN of 5 keypoints:' + str(sum(kp_ipn) / len(kp_ipn) * 100))
print('========== MAE of elur:' + str(sum(elur_mae) / len(elur_mae)))
stream_manager_api.DestroyAllStreams()
return 1
if __name__ == "__main__":
args = parse_arg()
main(args)
#!/usr/bin/env bash
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
# ============================================================================
set -e
# Simple log helper functions
info() { echo -e "\033[1;34m[INFO ][MxStream] $1\033[1;37m" ; }
warn() { echo >&2 -e "\033[1;31m[WARN ][MxStream] $1\033[1;37m" ; }
export MX_SDK_HOME=/usr/local/sdk_home/mxManufacture/
export LD_LIBRARY_PATH=${MX_SDK_HOME}/lib:${MX_SDK_HOME}/opensource/lib:${MX_SDK_HOME}/opensource/lib64:/usr/local/Ascend/ascend-toolkit/latest/acllib/lib64:${LD_LIBRARY_PATH}
export GST_PLUGIN_SCANNER=${MX_SDK_HOME}/opensource/libexec/gstreamer-1.0/gst-plugin-scanner
export GST_PLUGIN_PATH=${MX_SDK_HOME}/opensource/lib/gstreamer-1.0:${MX_SDK_HOME}/lib/plugins
dataset_path=$1
pipeline_path=$2
#to set PYTHONPATH, import the StreamManagerApi.py
export PYTHONPATH=$PYTHONPATH:${MX_SDK_HOME}/python
if [ ! -d infer_result ] ;then
mkdir infer_result;
fi
# compile plugin program
if [ -d ../util/plugins/build ] ;then
rm -rf ../util/plugins/build;
fi
cmake -S ../util/plugins -B ../util/plugins/build
make -C ../util/plugins/build -j
if [ ! -d $MX_SDK_HOME/lib/plugins/ ] ;then
echo "distance plugin directory not exists";
exit 0;
fi
if [ -f ../util/plugins/build/libmxpi_transposeplugin.so ] ;then
echo "copy file libmxpi_transposeplugin.so to plugins";
cp ../util/plugins/build/libmxpi_transposeplugin.so $MX_SDK_HOME/lib/plugins/;
echo "copy successfully";
fi
echo "start infer...";
python3.7 -u fqa_opencv.py \
--dataset=$dataset_path \
--pipeline=$pipeline_path \
--output=infer_result > infer.log 2>&1
exit 0
python3.7 -u fqa_opencv.py --dataset=$dataset_path --pipeline=$pipeline_path --output=infer_result > infer.log 2>&1
\ No newline at end of file
cmake_minimum_required(VERSION 3.5.2)
project(mxplugins)
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
add_definitions(-Dgoogle=mindxsdk_private)
set(PLUGIN_NAME "mxpi_transposeplugin")
set(TARGET_LIBRARY ${PLUGIN_NAME})
set(MXBASE_DIR $ENV{MX_SDK_HOME})
set(OPENSOURCE_DIR $ENV{MX_SDK_HOME}/opensource)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${OPENSOURCE_DIR}/../../include)
include_directories(${OPENSOURCE_DIR}/include)
include_directories(${OPENSOURCE_DIR}/include/gstreamer-1.0)
include_directories(${OPENSOURCE_DIR}/include/glib-2.0)
include_directories(${OPENSOURCE_DIR}/lib/glib-2.0/include)
include_directories(${MXBASE_DIR}/include)
link_directories(${MXBASE_DIR}/lib)
link_directories(${OPENSOURCE_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 MxpiTransposePlugin.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} mxpidatatype plugintoolkit mxbase mindxsdk_protobuf)
\ No newline at end of file
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* 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 "MxpiTransposePlugin.h"
#include <math.h>
#include <iostream>
#include "MxBase/Log/Log.h"
namespace {
const std::string SAMPLE_KEY = "MxpiVisionList";
}
APP_ERROR MxpiTransposePlugin::Init(std::map<std::string, std::shared_ptr<void>>& configParamMap) {
LogInfo << "MxpiTransposePlugin::Init start.";
// Get the property values by key
std::shared_ptr<std::string> parentNamePropSptr = std::static_pointer_cast<std::string>(
configParamMap["dataSource"]);
// parentName_: mxpi_imageresize0
parentName_ = *parentNamePropSptr.get();
return APP_ERR_OK;
}
APP_ERROR MxpiTransposePlugin::DeInit() {
LogInfo << "MxpiTransposePlugin::DeInit end.";
return APP_ERR_OK;
}
APP_ERROR MxpiTransposePlugin::SetMxpiErrorInfo(
MxTools::MxpiBuffer& buffer, const std::string pluginName, const MxTools::MxpiErrorInfo mxpiErrorInfo) {
APP_ERROR ret = APP_ERR_OK;
// Define an object of MxTools::MxpiMetadataManager
MxTools::MxpiMetadataManager mxpiMetadataManager(buffer);
ret = mxpiMetadataManager.AddErrorInfo(pluginName, mxpiErrorInfo);
if (ret != APP_ERR_OK) {
LogError << "Failed to AddErrorInfo.";
return ret;
}
ret = SendData(0, buffer);
return ret;
}
APP_ERROR MxpiTransposePlugin::Transpose(
MxTools::MxpiVisionList srcMxpiVisionList, MxTools::MxpiVisionList& dstMxpiVisionList) {
dstMxpiVisionList = srcMxpiVisionList;
for (int i = 0; i < dstMxpiVisionList.visionvec_size(); i++) {
MxTools::MxpiVision* dstMxpiVision = dstMxpiVisionList.mutable_visionvec(i);
// set MxTools::MxpiVisionData
MxTools::MxpiVisionData* dstMxpiVisionData = dstMxpiVision->mutable_visiondata();
float HWCData[96*96*3], CHWData[3*96*96];
std::memcpy(HWCData, reinterpret_cast<void*>(dstMxpiVisionData->dataptr()), sizeof(HWCData));
for (int c = 0; c < 3; c++) {
for (int k = 0; k < 96*96; k++) {
CHWData[k+c*96*96] = HWCData[3*k+c];
}
}
std::memcpy(reinterpret_cast<void*>(dstMxpiVisionData->dataptr()), CHWData, sizeof(CHWData));
}
return APP_ERR_OK;
}
APP_ERROR MxpiTransposePlugin::Process(std::vector<MxTools::MxpiBuffer*>& mxpiBuffer) {
LogInfo << "MxpiTransposePlugin::Process start";
// Get the data from buffer
MxTools::MxpiBuffer* buffer = mxpiBuffer[0];
// Get metadata by key.
MxTools::MxpiMetadataManager mxpiMetadataManager(*buffer);
MxTools::MxpiErrorInfo mxpiErrorInfo;
ErrorInfo_.str("");
auto errorInfoPtr = mxpiMetadataManager.GetErrorInfo();
if (errorInfoPtr != nullptr) {
ErrorInfo_ << GetError(APP_ERR_COMM_FAILURE, pluginName_) << "MxpiTransposePlugin process is not implemented";
mxpiErrorInfo.ret = APP_ERR_COMM_FAILURE;
mxpiErrorInfo.errorInfo = ErrorInfo_.str();
SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo);
LogError << "MxpiTransposePlugin process is not implemented";
return APP_ERR_COMM_FAILURE;
}
// Get the data from buffer(mxpi_imageresize0)
std::shared_ptr<void> metadata = mxpiMetadataManager.GetMetadata(parentName_);
if (metadata == nullptr) {
ErrorInfo_ << GetError(APP_ERR_METADATA_IS_NULL, pluginName_) << "Metadata is NULL, failed";
mxpiErrorInfo.ret = APP_ERR_METADATA_IS_NULL;
mxpiErrorInfo.errorInfo = ErrorInfo_.str();
SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo);
return APP_ERR_METADATA_IS_NULL;
}
// check whether the proto struct name is MxTools::MxpiVisionList(plugin mxpi_imageresize's output format)
google::protobuf::Message* msg = (google::protobuf::Message*)metadata.get();
const google::protobuf::Descriptor* desc = msg->GetDescriptor();
if (desc->name() != SAMPLE_KEY) {
ErrorInfo_ << GetError(APP_ERR_PROTOBUF_NAME_MISMATCH, pluginName_);
ErrorInfo_ << "Proto struct name is not MxTools::MxpiVisionList, failed";
mxpiErrorInfo.ret = APP_ERR_PROTOBUF_NAME_MISMATCH;
mxpiErrorInfo.errorInfo = ErrorInfo_.str();
SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo);
return APP_ERR_PROTOBUF_NAME_MISMATCH;
}
// Generate sample output
std::shared_ptr<MxTools::MxpiVisionList> src = std::static_pointer_cast<MxTools::MxpiVisionList>(metadata);
std::shared_ptr<MxTools::MxpiVisionList> dst = std::make_shared<MxTools::MxpiVisionList>();
APP_ERROR ret = Transpose(*src, *dst);
if (ret != APP_ERR_OK) {
LogError << GetError(ret, pluginName_) << "MxpiTransposePlugin gets inference information failed.";
mxpiErrorInfo.ret = ret;
mxpiErrorInfo.errorInfo = ErrorInfo_.str();
SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo);
return ret;
}
// Add Generated data to metedata
// call function AddProtoMetadata(). Mount the result to the corresponding Buffer when getting the input
ret = mxpiMetadataManager.AddProtoMetadata(pluginName_, std::static_pointer_cast<void>(dst));
if (ret != APP_ERR_OK) {
ErrorInfo_ << GetError(ret, pluginName_) << "MxpiTransposePlugin add metadata failed.";
mxpiErrorInfo.ret = ret;
mxpiErrorInfo.errorInfo = ErrorInfo_.str();
SetMxpiErrorInfo(*buffer, pluginName_, mxpiErrorInfo);
return ret;
}
// Send the data to downstream plugin
SendData(0, *buffer);
LogInfo << "MxpiTransposePlugin::Process end";
return APP_ERR_OK;
}
std::vector<std::shared_ptr<void>> MxpiTransposePlugin::DefineProperties() {
// Define an A to store properties
std::vector<std::shared_ptr<void>> properties;
// Set the type and related information of the properties, and the key is the name
auto parentNameProSptr = std::make_shared<MxTools::ElementProperty<std::string>>(
MxTools::ElementProperty<std::string>{
MxTools::STRING, "dataSource", "name", "the name of previous plugin", "mxpi_imageresize0", "NULL", "NULL"});
properties.push_back(parentNameProSptr);
return properties;
}
// Register the Sample plugin through macro
MX_PLUGIN_GENERATE(MxpiTransposePlugin)
/**
* Copyright 2021 Huawei Technologies Co., Ltd
*
* 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 SDKMEMORY_MxpiTransposePlugin_H
#define SDKMEMORY_MxpiTransposePlugin_H
#include <map>
#include <memory>
#include <vector>
#include <string>
#include "MxTools/PluginToolkit/base/MxPluginGenerator.h"
#include "MxTools/PluginToolkit/base/MxPluginBase.h"
#include "MxTools/PluginToolkit/metadata/MxpiMetadataManager.h"
#include "MxTools/Proto/MxpiDataType.pb.h"
#include "MxBase/ErrorCode/ErrorCode.h"
using namespace MxTools;
/**
* @api
* @brief Definition of MxpiTransposePlugin class.
*/
class MxpiTransposePlugin : public MxTools::MxPluginBase {
public:
/**
* @api
* @brief Initialize configure parameter.
* @param configParamMap
* @return APP_ERROR
*/
APP_ERROR Init(std::map<std::string, std::shared_ptr<void>> &configParamMap) override;
/**
* @api
* @brief DeInitialize configure parameter.
* @return APP_ERROR
*/
APP_ERROR DeInit() override;
/**
* @api
* @brief Process the data of MxpiBuffer.
* @param mxpiBuffer
* @return APP_ERROR
*/
APP_ERROR Process(std::vector<MxTools::MxpiBuffer *> &mxpiBuffer) override;
/**
* @api
* @brief Definition the parameter of configure properties.
* @return std::vector<std::shared_ptr<void>>
*/
static std::vector<std::shared_ptr<void>> DefineProperties();
/**
* @api
* @brief convert from HWC to CHW.
* @param key
* @param buffer
* @return APP_ERROR
*/
APP_ERROR Transpose(MxTools::MxpiVisionList srcMxpiVisionList, MxTools::MxpiVisionList &dstMxpiVisionList);
private:
APP_ERROR SetMxpiErrorInfo(
MxTools::MxpiBuffer &buffer, std::string pluginName, const MxTools::MxpiErrorInfo mxpiErrorInfo);
std::string parentName_;
std::ostringstream ErrorInfo_;
};
#endif // SDKMEMORY_MxpiTransposePlugin_H
"""
Copyright 2021 Huawei Technologies Co., Ltd
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.
============================================================================
"""
import argparse
import os
import glob
import moxing as mox
import export
import train
from model_utils.config import config
def parse_args():
"""get the cmd input args"""
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--train_url', type=str, default='', help='the path model saved')
parser.add_argument('-d', '--data_url', type=str, default='', help='the training data')
parser.add_argument("--device_id", type=str, default="0", help="device id")
parser.add_argument("--max_epoch", type=int, default=40, help="max epoch")
return parser.parse_args()
def main():
"""start script for model training and exporting"""
args = parse_args()
print("Training setting:", args)
os.environ["DEVICE_ID"] = args.device_id
config.enable_modelarts = True
dataset_list = glob.glob(os.path.join(args.data_url, '*.zip'))
config.modelarts_dataset_unzip_name = dataset_list[0].split("/")[-1].split(".")[0]
label_file_list = glob.glob(os.path.join(args.data_url, '*.txt'))
config.train_label_file = label_file_list[0]
config.max_epoch = args.max_epoch
train.run_train()
os.makedirs(config.load_path, exist_ok=True)
mox.file.copy_parallel(os.path.join(config.output_path, 'output'), config.load_path)
config.pretrained = glob.glob(os.path.join(config.load_path, '*/*.ckpt'))[0]
config.train_url = os.path.join(config.train_url, "model")
os.makedirs(config.train_url, exist_ok=True)
export.run_export()
if __name__ == '__main__':
main()
#!/bin/bash
# Copyright 2021 Huawei Technologies Co., Ltd
#
# 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.
docker_image=$1
data_dir=$2
model_dir=$3
docker run -it --ipc=host \
--device=/dev/davinci0 \
--device=/dev/davinci1 \
--device=/dev/davinci2 \
--device=/dev/davinci3 \
--device=/dev/davinci4 \
--device=/dev/davinci5 \
--device=/dev/davinci6 \
--device=/dev/davinci7 \
--device=/dev/davinci_manager \
--device=/dev/devmm_svm --device=/dev/hisi_hdc \
-v /usr/local/Ascend/driver:/usr/local/Ascend/driver \
-v /usr/local/Ascend/add-ons/:/usr/local/Ascend/add-ons/ \
-v ${model_dir}:${model_dir} \
-v ${data_dir}:${data_dir} \
-v ~/ascend/log/npu/conf/slog/slog.conf:/var/log/npu/conf/slog/slog.conf \
-v ~/ascend/log/npu/slog/:/var/log/npu/slog -v ~/ascend/log/npu/profiling/:/var/log/npu/profiling \
-v ~/ascend/log/npu/dump/:/var/log/npu/dump -v ~/ascend/log/npu/:/usr/slog ${docker_image} \
/bin/bash
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment