-
Notifications
You must be signed in to change notification settings - Fork 704
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lite): add
FaceParsingBiSeNet
model ORT/MNN C++ (#332)
- Loading branch information
Showing
12 changed files
with
516 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
// | ||
|
||
#include "mnn_face_parsing_bisenet.h" | ||
#include "lite/utils.h" | ||
|
||
using mnncv::MNNFaceParsingBiSeNet; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// | ||
// Created by DefTruth on 2022/7/2. | ||
// | ||
|
||
#include "ncnn_face_parsing_bisenet.h" | ||
|
||
using ncnncv::NCNNFaceParsingBiSeNet; | ||
|
||
NCNNFaceParsingBiSeNet::NCNNFaceParsingBiSeNet(const std::string &_param_path, | ||
const std::string &_bin_path, | ||
unsigned int _num_threads, | ||
unsigned int _input_height, | ||
unsigned int _input_width) : | ||
BasicNCNNHandler(_param_path, _bin_path, _num_threads), | ||
input_height(_input_height), input_width(_input_width) | ||
{ | ||
} | ||
|
||
void NCNNFaceParsingBiSeNet::transform(const cv::Mat &mat, ncnn::Mat &in) | ||
{ | ||
cv::Mat mat_rs; | ||
cv::resize(mat, mat_rs, cv::Size(input_width, input_height)); | ||
// will do deepcopy inside ncnn | ||
in = ncnn::Mat::from_pixels(mat_rs.data, ncnn::Mat::PIXEL_BGR2RGB, input_width, input_height); | ||
in.substract_mean_normalize(mean_vals, norm_vals); | ||
} | ||
|
||
void NCNNFaceParsingBiSeNet::detect(const cv::Mat &mat, types::FaceParsingContent &content, | ||
bool minimum_post_process) | ||
{ | ||
if (mat.empty()) return; | ||
|
||
// 1. make input tensor | ||
ncnn::Mat input; | ||
this->transform(mat, input); | ||
// 2. inference & extract | ||
auto extractor = net->create_extractor(); | ||
extractor.set_light_mode(false); // default | ||
extractor.set_num_threads(num_threads); | ||
extractor.input("input", input); | ||
// 3. generate mask | ||
this->generate_mask(extractor, mat, content, minimum_post_process); | ||
} | ||
|
||
static inline uchar __argmax_find(float *mutable_ptr, const unsigned int &step) | ||
{ | ||
std::vector<float> logits(19, 0.f); | ||
for (unsigned int i = 0; i < 19; ++i) | ||
logits[i] = *(mutable_ptr + i * step); | ||
uchar label = 0; | ||
float max_logit = logits[0]; | ||
for (unsigned int i = 1; i < 19; ++i) | ||
{ | ||
if (logits[i] > max_logit) | ||
{ | ||
max_logit = logits[i]; | ||
label = (uchar) i; | ||
} | ||
} | ||
return label; | ||
} | ||
|
||
static const uchar part_colors[20][3] = { | ||
{255, 0, 0}, | ||
{255, 85, 0}, | ||
{255, 170, 0}, | ||
{255, 0, 85}, | ||
{255, 0, 170}, | ||
{0, 255, 0}, | ||
{85, 255, 0}, | ||
{170, 255, 0}, | ||
{0, 255, 85}, | ||
{0, 255, 170}, | ||
{0, 0, 255}, | ||
{85, 0, 255}, | ||
{170, 0, 255}, | ||
{0, 85, 255}, | ||
{0, 170, 255}, | ||
{255, 255, 0}, | ||
{255, 255, 85}, | ||
{255, 255, 170}, | ||
{255, 0, 255}, | ||
{255, 85, 255} | ||
}; | ||
|
||
void NCNNFaceParsingBiSeNet::generate_mask(ncnn::Extractor &extractor, const cv::Mat &mat, | ||
types::FaceParsingContent &content, | ||
bool minimum_post_process) | ||
{ | ||
ncnn::Mat output; | ||
extractor.extract("out", output); | ||
#ifdef LITENCNN_DEBUG | ||
BasicNCNNHandler::print_shape(output, "out"); | ||
#endif | ||
const unsigned int h = mat.rows; | ||
const unsigned int w = mat.cols; | ||
|
||
const unsigned int out_h = input_height; | ||
const unsigned int out_w = input_width; | ||
const unsigned int channel_step = out_h * out_w; | ||
|
||
float *output_ptr = (float *) output.data; | ||
std::vector<uchar> elements(channel_step, 0); // allocate | ||
for (unsigned int i = 0; i < channel_step; ++i) | ||
elements[i] = __argmax_find(output_ptr + i, channel_step); | ||
|
||
cv::Mat label(out_h, out_w, CV_8UC1, elements.data()); | ||
|
||
if (!minimum_post_process) | ||
{ | ||
const uchar *label_ptr = label.data; | ||
cv::Mat color_mat(out_h, out_w, CV_8UC3, cv::Scalar(255, 255, 255)); | ||
for (unsigned int i = 0; i < color_mat.rows; ++i) | ||
{ | ||
cv::Vec3b *p = color_mat.ptr<cv::Vec3b>(i); | ||
for (unsigned int j = 0; j < color_mat.cols; ++j) | ||
{ | ||
if (label_ptr[i * out_w + j] == 0) continue; | ||
p[j][0] = part_colors[label_ptr[i * out_w + j]][0]; | ||
p[j][1] = part_colors[label_ptr[i * out_w + j]][1]; | ||
p[j][2] = part_colors[label_ptr[i * out_w + j]][2]; | ||
} | ||
} | ||
if (out_h != h || out_w != w) | ||
cv::resize(color_mat, color_mat, cv::Size(w, h)); | ||
cv::addWeighted(mat, 0.4, color_mat, 0.6, 0., content.merge); | ||
} | ||
if (out_h != h || out_w != w) cv::resize(label, label, cv::Size(w, h)); | ||
|
||
content.label = label; | ||
content.flag = true; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// Created by DefTruth on 2022/7/2. | ||
// | ||
|
||
#ifndef LITE_AI_TOOLKIT_NCNN_CV_NCNN_FACE_PARSING_BISENET_H | ||
#define LITE_AI_TOOLKIT_NCNN_CV_NCNN_FACE_PARSING_BISENET_H | ||
|
||
#include "lite/ncnn/core/ncnn_core.h" | ||
|
||
namespace ncnncv | ||
{ | ||
class LITE_EXPORTS NCNNFaceParsingBiSeNet : public BasicNCNNHandler | ||
{ | ||
public: | ||
explicit NCNNFaceParsingBiSeNet(const std::string &_param_path, | ||
const std::string &_bin_path, | ||
unsigned int _num_threads = 1, | ||
unsigned int _input_height = 512, | ||
unsigned int _input_width = 512); | ||
|
||
~NCNNFaceParsingBiSeNet() override = default; | ||
|
||
private: | ||
const int input_height; | ||
const int input_width; | ||
const float mean_vals[3] = {0.485f * 255.f, 0.456f * 255.f, 0.406f * 255.f}; // RGB | ||
const float norm_vals[3] = {1.f / (0.229f * 255.f), 1.f / (0.224f * 255.f), 1.f / (0.225f * 255.f)}; | ||
|
||
private: | ||
void transform(const cv::Mat &mat, ncnn::Mat &in) override; | ||
|
||
void generate_mask(ncnn::Extractor &extractor, | ||
const cv::Mat &mat, types::FaceParsingContent &content, | ||
bool minimum_post_process = false); | ||
|
||
public: | ||
void detect(const cv::Mat &mat, types::FaceParsingContent &content, | ||
bool minimum_post_process = false); | ||
|
||
}; | ||
} | ||
|
||
#endif //LITE_AI_TOOLKIT_NCNN_CV_NCNN_FACE_PARSING_BISENET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.