Skip to content
Snippets Groups Projects
Commit 9d0a0153 authored by 张瑜's avatar 张瑜
Browse files

add hed

parent 03f7c052
No related branches found
No related tags found
No related merge requests found
Showing
with 1044 additions and 0 deletions
#!/bin/bash
# Copyright 2022 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.
# ============================================================================
model_path=$1
output_model_name=$2
atc \
--model=$model_path \
--input_format=NCHW \
--framework=1 \
--output=$output_model_name \
--log=error \
--soc_version=Ascend310 \
--precision_mode=allow_fp32_to_fp16 \
--output_type=FP32 \
--op_select_implmode=high_precision
{
"hed": {
"stream_config": {
"deviceId": "0"
},
"appsrc0": {
"props": {
"blocksize": "409600"
},
"factory": "appsrc",
"next": "mxpi_tensorinfer0"
},
"mxpi_tensorinfer0": {
"props": {
"dataSource": "appsrc0",
"modelPath": "../convert/hed_return1.om"
},
"factory": "mxpi_tensorinfer",
"next": "appsink0"
},
"appsink0": {
"props": {
"blocksize": "409600"
},
"factory": "appsink"
}
}
}
\ No newline at end of file
#!/bin/bash
# Copyright(C) 2022 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 -u root --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 /root/ascend/log:/root/ascend/log ${docker_image} \
/bin/bash
cmake_minimum_required(VERSION 3.14.0)
project(hed)
set(TARGET hed)
add_definitions(-DENABLE_DVPP_INTERFACE)
add_link_options(-Wl,-z,relro,-z,now,-z,noexecstack -s -pie)
add_compile_options(-std=c++14 -fPIE -fstack-protector-all -fPIC -Wall
-Dgoogle=mindxsdk_private -D_GLIBCXX_USE_CXX11_ABI=0)
#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}/acllib/include)
set(ACL_LIB_DIR $ENV{ASCEND_HOME}/$ENV{ASCEND_VERSION}/acllib/lib64)
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)
if(NOT DEFINED ENV{MXSDK_OPENSOURCE_DIR})
message(WARNING "please define environment variable:MXSDK_OPENSOURCE_DIR")
endif()
set(OPENSOURCE_DIR $ENV{MXSDK_OPENSOURCE_DIR})
include_directories(src)
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} main.cpp HedEdgeDetection.cpp)
target_link_libraries(${TARGET} glog cpprest mxbase opencv_world stdc++fs)
install(TARGETS ${TARGET} RUNTIME DESTINATION ${PROJECT_SOURCE_DIR}/)
/**
* Copyright 2022 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 "HedEdgeDetection.h"
#include <memory>
#include <vector>
#include <string>
#include<fstream>
#include "MxBase/DeviceManager/DeviceManager.h"
#include "MxBase/DvppWrapper/DvppWrapper.h"
#include "MxBase/Log/Log.h"
APP_ERROR HedEdgeDetection::Init(const InitParam &initParam) {
deviceId_ = initParam.deviceId;
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;
}
uint32_t inputModelChannel = modelDesc_.inputTensors[0].tensorDims[MxBase::VECTOR_SECOND_INDEX];
uint32_t inputModelHeight = modelDesc_.inputTensors[0].tensorDims[MxBase::VECTOR_THIRD_INDEX];
uint32_t inputModelWidth = modelDesc_.inputTensors[0].tensorDims[MxBase::VECTOR_FOURTH_INDEX];
TargetChannel_ = inputModelChannel;
TargetHeight_ = inputModelHeight;
TargetWidth_ = inputModelWidth;
LogInfo << " TargetChannel_:" << TargetChannel_;
LogInfo << " TargetHeight_:" << TargetHeight_ << " TargetWidth_:" <<TargetWidth_;
return APP_ERR_OK;
}
APP_ERROR HedEdgeDetection::DeInit() {
model_->DeInit();
MxBase::DeviceManager::GetInstance()->DestroyDevices();
return APP_ERR_OK;
}
int HedEdgeDetection::getBinSize(std::string path) {
int size = 0;
std::ifstream infile(path, std::ifstream::binary);
infile.seekg(0, infile.end);
size = infile.tellg();
infile.seekg(0, infile.beg);
infile.close();
std::cout << "path: " <<path << " size/sizeof(float): " <<
size/sizeof(float) << std::endl;
return size/sizeof(float);
}
APP_ERROR HedEdgeDetection::readBin(std::string path, float *buf, int size) {
std::ifstream infile(path, std::ifstream::binary);
infile.read(reinterpret_cast<char*>(buf), sizeof(float)*size);
infile.close();
return APP_ERR_OK;
}
APP_ERROR HedEdgeDetection::writeBin(std::string path, float *buf, int size) {
std::ofstream outfile(path, std::ifstream::binary);
outfile.write(reinterpret_cast<char*>(buf), size*sizeof(float));
outfile.close();
return APP_ERR_OK;
}
APP_ERROR HedEdgeDetection::BinToTensorBase(const int length, float *image, const uint32_t target_channel,
const uint32_t target_width, const uint32_t target_height,
MxBase::TensorBase *tensorBase) {
const uint32_t dataSize = static_cast<uint32_t>(length);
MxBase::MemoryData memoryDataDst(dataSize*4, MxBase::MemoryData::MEMORY_DEVICE, deviceId_);
MxBase::MemoryData memoryDataSrc(image, dataSize*4, 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 = {1, target_channel, target_height, target_width};
*tensorBase = MxBase::TensorBase(memoryDataDst, false, shape, MxBase::TENSOR_DTYPE_FLOAT32);
return APP_ERR_OK;
}
APP_ERROR HedEdgeDetection::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 = {};
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 HedEdgeDetection::PostProcess(std::vector<MxBase::TensorBase> *inputs, float *buf) {
MxBase::TensorBase tensor = *inputs->begin();
int ret = tensor.ToHost();
if (ret != APP_ERR_OK) {
LogError << GetError(ret) << "Tensor deploy to host failed.";
return ret;
}
uint32_t outputModelChannel = tensor.GetShape()[MxBase::VECTOR_SECOND_INDEX];
uint32_t outputModelHeight = tensor.GetShape()[MxBase::VECTOR_THIRD_INDEX];
uint32_t outputModelWidth = tensor.GetShape()[MxBase::VECTOR_FOURTH_INDEX];
LogInfo << "Channel:" << outputModelChannel << " Height:"
<< outputModelHeight << " Width:" <<outputModelWidth;
auto data = reinterpret_cast<float(*)[outputModelChannel]
[outputModelHeight][outputModelWidth]>(tensor.GetBuffer());
for (size_t c = 0; c < outputModelChannel; ++c) {
for (size_t x = 0; x < outputModelHeight; ++x) {
for (size_t y = 0; y < outputModelWidth; ++y) {
size_t index = y + x * outputModelWidth +
c * outputModelWidth * outputModelHeight;
buf[index] = data[0][c][x][y];
}
}
}
return APP_ERR_OK;
}
APP_ERROR HedEdgeDetection::Process(const std::string &imgPath, const std::string &resultPath) {
int length = getBinSize(imgPath);
float* image = new float[length];
APP_ERROR ret = readBin(imgPath, image, length);
if (ret != APP_ERR_OK) {
LogError << "ReadImage failed, ret=" << ret << ".";
return ret;
}
MxBase::TensorBase tensorBase;
ret = BinToTensorBase(length, image, TargetChannel_, TargetWidth_, TargetHeight_, &tensorBase);
if (ret != APP_ERR_OK) {
LogError << "BinToTensorBase failed, ret=" << ret << ".";
return ret;
}
std::vector<MxBase::TensorBase> inputs = {};
std::vector<MxBase::TensorBase> outputs = {};
inputs.push_back(tensorBase);
ret = Inference(&inputs, &outputs);
if (ret != APP_ERR_OK) {
LogError << "Inference failed, ret=" << ret << ".";
return ret;
}
float* output = new float[length/TargetChannel_];
ret = PostProcess(&outputs, output);
if (ret != APP_ERR_OK) {
LogError << "PostProcess failed, ret=" << ret << ".";
return ret;
}
std::string imgName = imgPath;
size_t pos_begin = imgName.find_last_of("/");
if (static_cast<int>(pos_begin) == -1) {
imgName = "./" + imgName;
pos_begin = imgName.find_last_of("/");
}
imgName.replace(imgName.begin(), imgName.begin()+pos_begin, "");
size_t pos_end = imgName.find_last_of(".");
imgName.replace(imgName.begin() + pos_end, imgName.end(), ".bin");
std::string resultPathfile = resultPath + imgName;
LogInfo << "resultPathfile: " << resultPathfile;
ret = writeBin(resultPathfile, output, length/TargetChannel_);
if (ret != APP_ERR_OK) {
LogError << "writeBin failed, ret=" << ret << ".";
return ret;
}
return APP_ERR_OK;
}
/**
* Copyright 2022 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 HED_EDGEDETECTION_H
#define HED_EDGEDETECTION_H
#include <memory>
#include <vector>
#include <string>
#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 HedEdgeDetection {
public:
APP_ERROR Init(const InitParam &initParam);
APP_ERROR DeInit();
APP_ERROR ReadImage(const std::string &imgPath, cv::Mat *imageMat);
APP_ERROR BinToTensorBase(const int length, float *image, const uint32_t target_channel,
const uint32_t target_width, const uint32_t target_height,
MxBase::TensorBase *tensorBase);
APP_ERROR Inference(std::vector<MxBase::TensorBase> *inputs, std::vector<MxBase::TensorBase> *outputs);
APP_ERROR Process(const std::string &imgPath, const std::string &resultPath);
APP_ERROR PostProcess(std::vector<MxBase::TensorBase> *inputs, float *buf);
APP_ERROR readBin(std::string path, float *buf, int size);
APP_ERROR writeBin(std::string path, float *buf, int size);
int getBinSize(std::string path);
private:
std::shared_ptr<MxBase::ModelInferenceProcessor> model_;
MxBase::ModelDesc modelDesc_;
uint32_t deviceId_ = 0;
uint32_t TargetChannel_ = 0;
uint32_t TargetWidth_ = 0;
uint32_t TargetHeight_ = 0;
};
#endif
#!/bin/bash
# Copyright 2022 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.
# ============================================================================
# env
export ASCEND_HOME=/usr/local/Ascend
export ARCH_PATTERN=.
export MXSDK_OPENSOURCE_DIR=/usr/local/sdk_home/mxManufacture/opensource
export ASCEND_OPP_PATH="/usr/local/Ascend/ascend-toolkit/latest/opp"
export ASCEND_AICPU_PATH="/usr/local/Ascend/ascend-toolkit/latest"
export ASCEND_VERSION=ascend-toolkit/latest
mkdir -p build
cd build || exit
function make_plugin() {
if ! cmake ..;
then
echo "cmake failed."
return 1
fi
if ! (make);
then
echo "make failed."
return 1
fi
return 0
}
if make_plugin;
then
echo "INFO: Build successfully."
else
echo "ERROR: Build failed."
fi
cd - || exit
/**
* Copyright 2022 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 "HedEdgeDetection.h"
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "MxBase/Log/Log.h"
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;
}
dirent *direntPtr = nullptr;
while ((direntPtr = readdir(dirPtr)) != nullptr) {
std::string fileName = direntPtr->d_name;
if (fileName == "." || fileName == "..") {
continue;
}
imgFiles.emplace_back(path + "/" + fileName);
}
closedir(dirPtr);
return APP_ERR_OK;
}
// infer an image
int main(int argc, char *argv[]) {
if (argc <= 2) {
LogWarn << "Please input image path and result path, such as './hed ./test.png ./result'";
return APP_ERR_OK;
}
InitParam initParam = {};
initParam.deviceId = 0;
initParam.modelPath = "../convert/hed_return1.om";
HedEdgeDetection hedED;
APP_ERROR ret = hedED.Init(initParam);
if (ret != APP_ERR_OK) {
LogError << "HedEdgeDetection init failed, ret=" << ret << ".";
return ret;
}
std::string imgPath = argv[1];
std::string resultPath = argv[2];
DIR *dirPtr = opendir(resultPath.c_str());
// if it has no dir
if (dirPtr == nullptr) {
LogInfo << "opendir failed. so make dir" << resultPath.c_str();
ret = mkdir(resultPath.c_str(), 0755);
if (ret != 0) {
return -1;
}
}
std::vector<std::string> imgFilePaths;
ret = ScanImages(imgPath, imgFilePaths);
if (ret != APP_ERR_OK) {
hedED.DeInit();
return ret;
}
// deal an image
for (auto &imgFile : imgFilePaths) {
ret = hedED.Process(imgFile, resultPath);
if (ret != APP_ERR_OK) {
LogError << "hed process failed, ret=" << ret << ".";
hedED.DeInit();
return ret;
}
}
if (ret != APP_ERR_OK) {
LogError << "HedEdgeDetection process failed, ret=" << ret << ".";
hedED.DeInit();
return ret;
}
hedED.DeInit();
return APP_ERR_OK;
}
import os
from argparse import ArgumentParser
from src.eval_edge import eval_edge
from src.nms_process import nms_process
def main(args):
alg = [args.alg] # algorithms for plotting
model_name_list = [args.model_name_list] # model name
result_dir = os.path.abspath(args.result_dir) # forward result directory
save_dir = os.path.abspath(args.save_dir) # nms result directory
gt_dir = os.path.abspath(args.gt_dir) # ground truth directory
key = args.key # x = scipy.io.loadmat(filename)[key]
file_format = args.file_format # ".mat" or ".npy"
workers = args.workers # number workers
nms_process(model_name_list, result_dir, save_dir, key, file_format)
eval_edge(alg, model_name_list, save_dir, gt_dir, workers)
if __name__ == '__main__':
parser = ArgumentParser("edge eval")
parser.add_argument("--alg", type=str, default="HED", help="algorithms for plotting.")
parser.add_argument("--model_name_list", type=str, default="hed", help="model name")
parser.add_argument("--result_dir", type=str, default="./mxbase_res", help="results directory")
parser.add_argument("--save_dir", type=str, default="./mxbase_res_eval", help="nms result directory")
parser.add_argument("--gt_dir", type=str, default="../data/BSR/BSDS500/data/groundTruth/test",
help="ground truth directory")
parser.add_argument("--key", type=str, default="result", help="key")
parser.add_argument("--file_format", type=str, default=".bin", help=".mat or .npy")
parser.add_argument("--workers", type=int, default="-1", help="number workers, -1 for all workers")
argps = parser.parse_args()
main(argps)
#!/bin/bash
# Copyright 2022 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.
# ============================================================================
# The number of parameters must be 3.
if [ $# -ne 3 ]
then
echo "Wrong parameter format."
echo "Usage:"
echo " bash $0 [OUTPUT_PATH] [SAVE_PATH] [GT_PATH]"
echo "Example: "
echo " bash run.sh ./mxbase_res/ ./mxbase_eval_res/ ../data/BSR/BSDS500/data/groundTruth/test"
exit 1
fi
# The path of a folder used to store all results.
output_path=$1
# The path of a folder used to store all eval results.
save_path=$2
# The path of a folder containing images groundtruth.
gt_dir=$3
if [ ! -d $gt_dir ]
then
echo "Please input the correct directory containing images groundtruth."
exit
fi
if [ ! -d $output_path ]
then
mkdir -p $output_path
fi
if [ ! -d $save_path ]
then
mkdir -p $save_path
fi
file_path="./mxbase_eval.py"
target_dir="../../"
echo cp -r $file_path $target_dir
cp -r $file_path $target_dir
set -e
CUR_PATH=$(cd "$(dirname "$0")" || { warn "Failed to check path/to/run.sh" ; exit ; } ; pwd)
echo "enter $CUR_PATH"
# 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" ; }
#to set PYTHONPATH, import the StreamManagerApi.py
export PYTHONPATH=$PYTHONPATH:${MX_SDK_HOME}/python
if [ ! "${MX_SDK_HOME}" ]
then
export GST_PLUGIN_SCANNER=${MX_SDK_HOME}/opensource/libexec/gstreamer-1.0/gst-plugin-scanner
fi
if [ ! "${MX_SDK_HOME}" ]
then
export GST_PLUGIN_PATH=${MX_SDK_HOME}/opensource/lib/gstreamer-1.0:${MX_SDK_HOME}/lib/plugins
fi
python3 ../../mxbase_eval.py --result_dir=$output_path \
--save_dir=$save_path \
--gt_dir=$gt_dir
target_dir="../../mxbase_eval.py"
echo rm -rf $target_dir
rm -rf $target_dir
exit 0
# Copyright 2022 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.
# ============================================================================
"""super resolution infer"""
import json
from StreamManagerApi import StreamManagerApi
from StreamManagerApi import StringVector
from StreamManagerApi import InProtobufVector
from StreamManagerApi import MxProtobufIn
import MxpiDataType_pb2 as MxpiDataType
import numpy as np
from PIL import Image
import cv2
DEFAULT_IMAGE_WIDTH = 321
DEFAULT_IMAGE_HEIGHT = 481
CHANNELS = 3
SCALE = 2
def get_input(img_file):
img_file = np.array(cv2.imread(img_file), dtype=np.float32)
if img_file.shape[0] > img_file.shape[1]:
img_file = np.rot90(img_file, 1).copy()
img_file -= np.array((104.00698793, 116.66876762, 122.67891434))
img_file = np.transpose(img_file, (2, 0, 1))
print("img_file: ", img_file)
print("img_file.shape: ", img_file.shape)
return img_file
class SDKInferWrapper:
"""SDKInferWrapper"""
def __init__(self):
self.stream_name = None
self.streamManagerApi = StreamManagerApi()
# init stream manager
if self.streamManagerApi.InitManager() != 0:
raise RuntimeError("Failed to init stream manager.")
def load_pipeline(self, pipeline_path):
# create streams by pipeline config file
with open(pipeline_path, 'r') as f:
pipeline = json.load(f)
self.stream_name = list(pipeline.keys())[0].encode()
pipelineStr = json.dumps(pipeline).encode()
if self.streamManagerApi.CreateMultipleStreams(pipelineStr) != 0:
raise RuntimeError("Failed to create stream.")
def do_infer(self, image_path):
"""do infer stream"""
# construct the input of the stream
if image_path.endswith(".jpg") or image_path.endswith(".png"):
image = get_input(image_path)
elif image_path.endswith(".bin"):
image = np.fromfile(image_path, dtype=np.float32)
image.shape = 3, 321, 481
tensor_pkg_list = MxpiDataType.MxpiTensorPackageList()
tensor_pkg = tensor_pkg_list.tensorPackageVec.add()
tensor_vec = tensor_pkg.tensorVec.add()
tensor_vec.deviceId = 0
tensor_vec.memType = 0
for dim in [1, *image.shape]:
tensor_vec.tensorShape.append(dim)
input_data = image.tobytes()
tensor_vec.dataStr = input_data
tensor_vec.tensorDataSize = len(input_data)
protobuf_vec = InProtobufVector()
protobuf = MxProtobufIn()
protobuf.key = b'appsrc0'
protobuf.type = b'MxTools.MxpiTensorPackageList'
protobuf.protobuf = tensor_pkg_list.SerializeToString()
protobuf_vec.push_back(protobuf)
unique_id = self.streamManagerApi.SendProtobuf(
self.stream_name, 0, protobuf_vec)
if unique_id < 0:
raise RuntimeError("Failed to send data to stream.")
# get plugin output data
key = b"mxpi_tensorinfer0"
keyVec = StringVector()
keyVec.push_back(key)
inferResult = self.streamManagerApi.GetProtobuf(self.stream_name, 0, keyVec)
if inferResult.size() == 0:
raise RuntimeError("inferResult is null")
if inferResult[0].errorCode != 0:
raise RuntimeError("GetProtobuf error. errorCode=%d, errorMsg=%s" % (
inferResult[0].errorCode, inferResult[0].messageName.decode()))
# get the infer result
inferList0 = MxpiDataType.MxpiTensorPackageList()
inferList0.ParseFromString(inferResult[0].messageBuf)
inferVisionData = inferList0.tensorPackageVec[0].tensorVec[0].dataStr
# converting the byte data into 32 bit float array
output_img_data = np.frombuffer(inferVisionData, dtype=np.float32)
print("len(np from buffer): ", output_img_data.shape)
result_mat = np.reshape(output_img_data, (DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT))
print("result_mat: ", result_mat)
print("result_mat.shape: ", result_mat.shape)
result_png = np.round(output_img_data*255).astype(np.uint8)
result_png = np.reshape(result_png, (DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT))
print("result_png.shape: ", result_png.shape)
result_png = Image.fromarray(result_png)
return result_png, result_mat
# Copyright 2022 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.
# ============================================================================
"""sdk infer"""
import argparse
import os
from infer_wrapper import SDKInferWrapper
import scipy.io
def parser_args():
parser = argparse.ArgumentParser()
parser.add_argument("--input_dir", type=str, default="../data/BSR/BSDS500/data/images/test",
help="path of input images directory")
parser.add_argument("--pipeline_path", type=str, default="../data/pipeline/hed.pipeline",
help="path of pipeline file")
parser.add_argument("--output_dir", type=str, default="./result/hed_result",
help="path of output images directory")
return parser.parse_args()
if __name__ == '__main__':
args = parser_args()
hed_infer = SDKInferWrapper()
hed_infer.load_pipeline(args.pipeline_path)
path_list = os.listdir(args.input_dir)
path_list.sort()
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
for img_path in path_list:
print(img_path)
if img_path.endswith('.jpg') or img_path.endswith('.png') or img_path.endswith('.bin'):
# 先推理
result_png, result_mat = hed_infer.do_infer(os.path.join(args.input_dir, img_path))
filename = img_path.split('/')[-1]
filename = filename.split('.')[0]
print(filename)
try:
result_path_png = os.path.join(args.output_dir, "{}.png".format(filename))
print("result_path_png: ", result_path_png)
result_png.save(result_path_png, quality=95)
except OSError:
pass
try:
result_path_mat = os.path.join(args.output_dir, "{}.mat".format(filename))
print("result_path_mat: ", result_path_mat)
scipy.io.savemat(result_path_mat, {'result': result_mat})
except OSError:
pass
#!/bin/bash
# Copyright 2022 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.
# ============================================================================
# The number of parameters must be 4.
if [ $# -ne 5 ]
then
echo "Wrong parameter format."
echo "Usage:"
echo " bash $0 [INPUT_PATH] [PIPELINE_PATH] [OUTPUT_PATH/RESULT_PATH] [SAVE_PATH] [GT_PATH]"
echo "Example: "
echo " bash run.sh ../data/BSR/BSDS500/data/images/test ../data/pipeline/hed.pipeline \
./result/hed_result ./result/hed_eval_result ../data/BSR/BSDS500/data/groundTruth/test"
exit 1
fi
# The path of a folder containing eval images.
input_dir=$1
# The path of pipeline file.
pipeline_path=$2
# The path of a folder used to store all results.
output_dir=$3
# The path of a folder used to store all eval results.
save_dir=$4
# The path of a folder containing groundtruth.
gt_dir=$5
if [ ! -d $input_dir ]
then
echo "Please input the correct directory containing images."
exit
fi
if [ ! -d $output_dir ]
then
mkdir -p $output_dir
fi
if [ ! -d $save_dir ]
then
mkdir -p $save_dir
fi
set -e
CUR_PATH=$(cd "$(dirname "$0")" || { warn "Failed to check path/to/run.sh" ; exit ; } ; pwd)
echo "enter $CUR_PATH"
# 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" ; }
#to set PYTHONPATH, import the StreamManagerApi.py
export PYTHONPATH=$PYTHONPATH:${MX_SDK_HOME}/python
if [ ! "${MX_SDK_HOME}" ]
then
export GST_PLUGIN_SCANNER=${MX_SDK_HOME}/opensource/libexec/gstreamer-1.0/gst-plugin-scanner
fi
if [ ! "${MX_SDK_HOME}" ]
then
export GST_PLUGIN_PATH=${MX_SDK_HOME}/opensource/lib/gstreamer-1.0:${MX_SDK_HOME}/lib/plugins
fi
file_path="./sdk_eval.py"
target_dir="../../"
echo cp -r $file_path $target_dir
cp -r $file_path $target_dir
python3 main.py --input_dir=$input_dir \
--pipeline_path=$pipeline_path \
--output_dir=$output_dir \
python3 ../../sdk_eval.py --result_dir=$output_dir \
--save_dir=$save_dir \
--gt_dir=$gt_dir
target_dir="../../sdk_eval.py"
echo rm -rf $target_dir
rm -rf $target_dir
exit 0
\ No newline at end of file
import os
from argparse import ArgumentParser
from src.eval_edge import eval_edge
from src.nms_process import nms_process
def main(args):
alg = [args.alg] # algorithms for plotting
model_name_list = [args.model_name_list] # model name
result_dir = os.path.abspath(args.result_dir) # forward result directory
save_dir = os.path.abspath(args.save_dir) # nms result directory
gt_dir = os.path.abspath(args.gt_dir) # ground truth directory
key = args.key # x = scipy.io.loadmat(filename)[key]
file_format = args.file_format # ".mat" or ".npy"
workers = args.workers # number workers
nms_process(model_name_list, result_dir, save_dir, key, file_format)
eval_edge(alg, model_name_list, save_dir, gt_dir, workers)
if __name__ == '__main__':
parser = ArgumentParser("edge eval")
parser.add_argument("--alg", type=str, default="HED", help="algorithms for plotting.")
parser.add_argument("--model_name_list", type=str, default="hed", help="model name")
parser.add_argument("--result_dir", type=str, default="./result/hed_result", help="results directory")
parser.add_argument("--save_dir", type=str, default="./result/hed_eval_result", help="nms result directory")
parser.add_argument("--gt_dir", type=str, default="../data/BSR/BSDS500/data/groundTruth/test",
help="ground truth directory")
parser.add_argument("--key", type=str, default="result", help="key")
parser.add_argument("--file_format", type=str, default=".mat", help=".mat or .npy")
parser.add_argument("--workers", type=int, default="-1", help="number workers, -1 for all workers")
argps = parser.parse_args()
main(argps)
# Copyright 2022 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.
# ============================================================================
"""get input data."""
import os
import argparse
import numpy as np
import cv2
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--dataset_path', type=str, default='../data/BSR/BSDS500/data/images/test',
help='evaling image path')
parser.add_argument('--output_path', type=str, default='../data/input_bin',
help='output image path')
opt = parser.parse_args()
if not os.path.exists(opt.output_path):
os.makedirs(opt.output_path)
path_list = os.listdir(opt.dataset_path)
path_list.sort()
if not os.path.exists(opt.dataset_path):
os.makedirs(opt.dataset_path)
for i, img_name in enumerate(path_list):
print(img_name)
if img_name.endswith('.jpg') or img_name.endswith('.png'):
img_bin = img_name.split(".")[0] + '.bin'
print(img_bin)
img_path = os.path.join(opt.dataset_path, img_name)
img_file = np.array(cv2.imread(img_path), dtype=np.float32)
if img_file.shape[0] > img_file.shape[1]:
img_file = np.rot90(img_file, 1).copy()
img_file_origin = img_file.copy()
print(img_file_origin)
img_file -= np.array((104.00698793, 116.66876762, 122.67891434))
print(img_file)
img_file = np.transpose(img_file, (2, 0, 1))
print(img_file.shape)
print(opt.output_path+'/'+img_bin)
img_file.tofile(opt.output_path+'/'+img_bin)
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment