Cpp OpenVino DenseNet辅助函数
Python版本导引
CMake工程示例
测试性质的工程结构如下:
Project:
Network
: 存放网络推理相关- DebugTools: 打印网络结构
- DenseGradeWrapper: 推理主结构
- NetworkTools: 部分CV辅助函数
Script
: 存放前面博客中的导出以及验证Py文件CMakeLists.txt
: cmake工程文件main.cpp
: 简单的执行文件
DebugTools
此处简单的修改自官方示例源码,用于打印模型输入输出中的关键信息,由于我的权重由torch直接转换而来,所以输出输出的名字将会难以阅读,仅作为快速实现以及debug使用
#include "DebugTools.h"
void printInputAndOutputsInfo(const ov::Model& network){
qDebug() << "Network inputs:" ;
for (auto&& input : network.inputs()) {
std::string in_name;
std::string node_name;
// Workaround for "tensor has no name" issue
try {
for (const auto& name : input.get_names()) {
in_name += name + " , ";
}
in_name = in_name.substr(0, in_name.size() - 3);
} catch (const ov::Exception&) {
}
try {
node_name = input.get_node()->get_friendly_name();
} catch (const ov::Exception&) {
}
if (in_name == "") {
in_name = "***NO_NAME***";
}
if (node_name == "") {
node_name = "***NO_NAME***";
}
qDebug() << " " << in_name << " (node: " << node_name << ") : " << input.get_element_type().get_type_name() << " / "
<< ov::layout::get_layout(input).to_string() << " / " << input.get_partial_shape().to_string() ;
}
qDebug() << "Network outputs:" ;
for (auto&& output : network.outputs()) {
std::string out_name;
std::string node_name;
// Workaround for "tensor has no name" issue
try {
for (const auto& name : output.get_names()) {
out_name += name + " , ";
}
out_name = out_name.substr(0, out_name.size() - 3);
} catch (const ov::Exception&) {
}
try {
node_name = output.get_node()->get_input_node_ptr(0)->get_friendly_name();
} catch (const ov::Exception&) {
}
if (out_name == "") {
out_name = "***NO_NAME***";
}
if (node_name == "") {
node_name = "***NO_NAME***";
}
qDebug() << " " << out_name << " (node: " << node_name << ") : " << output.get_element_type().get_type_name() << " / "
<< ov::layout::get_layout(output).to_string() << " / " << output.get_partial_shape().to_string() ;
}
}
NetworkTools
此处为一些个人模型中的辅助函数,如果没有使用到的地方可以直接跳过,或者进行参考修改为自己的工具函数
#include "NetworkTools.h"
cv::Mat NetworkTool::ToolCropImageFromMask(QString imageSrc){
// load
cv::Mat image = cv::imread(imageSrc.toStdString());
// binary
cv::Mat grayImage, binaryImage;
cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
cv::threshold(grayImage, binaryImage, 7, 255, cv::THRESH_BINARY);
// cal roi
cv::Rect bound = cv::boundingRect(binaryImage);
int x, y, centerW, centerH, radius;
centerW = (int)(bound.width / 2);
centerH = (int)(bound.height / 2);
radius = MIN(centerW, centerH);
y = bound.y + centerH - radius;
x = bound.x + centerW - radius;
// gen mask
cv::Mat copyImage = cv::Mat(image, cv::Range(y, y + 2 * radius), cv::Range(x, x + 2 * radius));
cv::Mat mask = cv::Mat::zeros(copyImage.size(), CV_8UC3);
cv::circle(mask, cv::Point(radius, radius), radius, cv::Scalar(255, 255, 255), -1);
// get ori
cv::Mat maskImage;
cv::bitwise_and(copyImage, mask, maskImage);
return maskImage;
}
cv::Mat NetworkTool::ToolCropWithGaussian(cv::Mat inMat){
cv::Mat resizeImage, gaussianImage;
cv::resize(inMat, resizeImage, cv::Size(224, 224));
cv::GaussianBlur(resizeImage, gaussianImage, cv::Size(11, 11), 10.0);
cv::Mat exposure, rgbImage;
cv::addWeighted(resizeImage, 4.0, gaussianImage, -4.0, 128.0, exposure);
cv::cvtColor(exposure, rgbImage, cv::COLOR_BGR2RGB);
return rgbImage;
}