[Model] Refactor PaddleClas module (#505)

* Refactor the PaddleClas module

* fix bug

* remove debug code

* clean unused code

* support pybind

* Update fd_tensor.h

* Update fd_tensor.cc

* temporary revert python api

* fix ci error

* fix code style problem
This commit is contained in:
Jason
2022-11-07 19:33:47 +08:00
committed by GitHub
parent a0a8ace174
commit 3589c0fa94
15 changed files with 527 additions and 142 deletions

View File

@@ -14,9 +14,6 @@
#include "fastdeploy/vision/classification/ppcls/model.h"
#include "fastdeploy/vision/utils/utils.h"
#include "yaml-cpp/yaml.h"
namespace fastdeploy {
namespace vision {
namespace classification {
@@ -25,8 +22,7 @@ PaddleClasModel::PaddleClasModel(const std::string& model_file,
const std::string& params_file,
const std::string& config_file,
const RuntimeOption& custom_option,
const ModelFormat& model_format) {
config_file_ = config_file;
const ModelFormat& model_format) : preprocessor_(config_file) {
valid_cpu_backends = {Backend::ORT, Backend::OPENVINO, Backend::PDINFER,
Backend::LITE};
valid_gpu_backends = {Backend::ORT, Backend::PDINFER, Backend::TRT};
@@ -38,11 +34,6 @@ PaddleClasModel::PaddleClasModel(const std::string& model_file,
}
bool PaddleClasModel::Initialize() {
if (!BuildPreprocessPipelineFromConfig()) {
FDERROR << "Failed to build preprocess pipeline from configuration file."
<< std::endl;
return false;
}
if (!InitRuntime()) {
FDERROR << "Failed to initialize fastdeploy backend." << std::endl;
return false;
@@ -50,105 +41,41 @@ bool PaddleClasModel::Initialize() {
return true;
}
bool PaddleClasModel::BuildPreprocessPipelineFromConfig() {
processors_.clear();
YAML::Node cfg;
try {
cfg = YAML::LoadFile(config_file_);
} catch (YAML::BadFile& e) {
FDERROR << "Failed to load yaml file " << config_file_
<< ", maybe you should check this file." << std::endl;
return false;
}
auto preprocess_cfg = cfg["PreProcess"]["transform_ops"];
processors_.push_back(std::make_shared<BGR2RGB>());
for (const auto& op : preprocess_cfg) {
FDASSERT(op.IsMap(),
"Require the transform information in yaml be Map type.");
auto op_name = op.begin()->first.as<std::string>();
if (op_name == "ResizeImage") {
int target_size = op.begin()->second["resize_short"].as<int>();
bool use_scale = false;
int interp = 1;
processors_.push_back(
std::make_shared<ResizeByShort>(target_size, 1, use_scale));
} else if (op_name == "CropImage") {
int width = op.begin()->second["size"].as<int>();
int height = op.begin()->second["size"].as<int>();
processors_.push_back(std::make_shared<CenterCrop>(width, height));
} else if (op_name == "NormalizeImage") {
auto mean = op.begin()->second["mean"].as<std::vector<float>>();
auto std = op.begin()->second["std"].as<std::vector<float>>();
auto scale = op.begin()->second["scale"].as<float>();
FDASSERT((scale - 0.00392157) < 1e-06 && (scale - 0.00392157) > -1e-06,
"Only support scale in Normalize be 0.00392157, means the pixel "
"is in range of [0, 255].");
processors_.push_back(std::make_shared<Normalize>(mean, std));
} else if (op_name == "ToCHWImage") {
processors_.push_back(std::make_shared<HWC2CHW>());
} else {
FDERROR << "Unexcepted preprocess operator: " << op_name << "."
<< std::endl;
return false;
}
}
return true;
}
bool PaddleClasModel::Preprocess(Mat* mat, FDTensor* output) {
for (size_t i = 0; i < processors_.size(); ++i) {
if (!(*(processors_[i].get()))(mat)) {
FDERROR << "Failed to process image data in " << processors_[i]->Name()
<< "." << std::endl;
return false;
}
}
int channel = mat->Channels();
int width = mat->Width();
int height = mat->Height();
output->name = InputInfoOfRuntime(0).name;
output->SetExternalData({1, channel, height, width}, FDDataType::FP32,
mat->Data());
return true;
}
bool PaddleClasModel::Postprocess(const FDTensor& infer_result,
ClassifyResult* result, int topk) {
int num_classes = infer_result.shape[1];
const float* infer_result_buffer =
reinterpret_cast<const float*>(infer_result.Data());
topk = std::min(num_classes, topk);
result->label_ids =
utils::TopKIndices(infer_result_buffer, num_classes, topk);
result->scores.resize(topk);
for (int i = 0; i < topk; ++i) {
result->scores[i] = *(infer_result_buffer + result->label_ids[i]);
}
return true;
}
bool PaddleClasModel::Predict(cv::Mat* im, ClassifyResult* result, int topk) {
Mat mat(*im);
std::vector<FDTensor> processed_data(1);
if (!Preprocess(&mat, &(processed_data[0]))) {
FDERROR << "Failed to preprocess input data while using model:"
<< ModelName() << "." << std::endl;
postprocessor_.SetTopk(topk);
if (!Predict(*im, result)) {
return false;
}
return true;
}
bool PaddleClasModel::Predict(const cv::Mat& im, ClassifyResult* result) {
std::vector<ClassifyResult> results;
if (!BatchPredict({im}, &results)) {
return false;
}
*result = std::move(results[0]);
return true;
}
bool PaddleClasModel::BatchPredict(const std::vector<cv::Mat>& images, std::vector<ClassifyResult>* results) {
std::vector<FDMat> fd_images = WrapMat(images);
if (!preprocessor_.Run(&fd_images, &reused_input_tensors)) {
FDERROR << "Failed to preprocess the input image." << std::endl;
return false;
}
std::vector<FDTensor> infer_result(1);
if (!Infer(processed_data, &infer_result)) {
FDERROR << "Failed to inference while using model:" << ModelName() << "."
<< std::endl;
reused_input_tensors[0].name = InputInfoOfRuntime(0).name;
if (!Infer(reused_input_tensors, &reused_output_tensors)) {
FDERROR << "Failed to inference by runtime." << std::endl;
return false;
}
if (!Postprocess(infer_result[0], result, topk)) {
FDERROR << "Failed to postprocess while using model:" << ModelName() << "."
<< std::endl;
if (!postprocessor_.Run(reused_output_tensors, results)) {
FDERROR << "Failed to postprocess the inference results by runtime." << std::endl;
return false;
}
return true;
}

View File

@@ -14,8 +14,8 @@
#pragma once
#include "fastdeploy/fastdeploy_model.h"
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
#include "fastdeploy/vision/classification/ppcls/preprocessor.h"
#include "fastdeploy/vision/classification/ppcls/postprocessor.h"
namespace fastdeploy {
namespace vision {
@@ -43,28 +43,46 @@ class FASTDEPLOY_DECL PaddleClasModel : public FastDeployModel {
/// Get model's name
virtual std::string ModelName() const { return "PaddleClas/Model"; }
/** \brief Predict the classification result for an input image
/** \brief DEPRECATED Predict the classification result for an input image, remove at 1.0 version
*
* \param[in] im The input image data, comes from cv::imread()
* \param[in] result The output classification result will be writen to this structure
* \param[in] topk (int)The topk result by the classify confidence score, default 1
* \return true if the prediction successed, otherwise false
*/
// TODO(jiangjiajun) Batch is on the way
virtual bool Predict(cv::Mat* im, ClassifyResult* result, int topk = 1);
/** \brief Predict the classification result for an input image
*
* \param[in] img The input image data, comes from cv::imread()
* \param[in] result The output classification result
* \return true if the prediction successed, otherwise false
*/
virtual bool Predict(const cv::Mat& img, ClassifyResult* result);
/** \brief Predict the classification results for a batch of input images
*
* \param[in] imgs, The input image list, each element comes from cv::imread()
* \param[in] results The output classification result list
* \return true if the prediction successed, otherwise false
*/
virtual bool BatchPredict(const std::vector<cv::Mat>& imgs,
std::vector<ClassifyResult>* results);
/// Get preprocessor reference of PaddleClasModel
virtual PaddleClasPreprocessor& GetPreprocessor() {
return preprocessor_;
}
/// Get postprocessor reference of PaddleClasModel
virtual PaddleClasPostprocessor& GetPostprocessor() {
return postprocessor_;
}
protected:
bool Initialize();
bool BuildPreprocessPipelineFromConfig();
bool Preprocess(Mat* mat, FDTensor* outputs);
bool Postprocess(const FDTensor& infer_result, ClassifyResult* result,
int topk = 1);
std::vector<std::shared_ptr<Processor>> processors_;
std::string config_file_;
PaddleClasPreprocessor preprocessor_;
PaddleClasPostprocessor postprocessor_;
};
typedef PaddleClasModel PPLCNet;

View File

@@ -0,0 +1,53 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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.
#include "fastdeploy/vision/classification/ppcls/postprocessor.h"
#include "fastdeploy/vision/utils/utils.h"
namespace fastdeploy {
namespace vision {
namespace classification {
PaddleClasPostprocessor::PaddleClasPostprocessor(int topk) {
topk_ = topk;
initialized_ = true;
}
bool PaddleClasPostprocessor::Run(const std::vector<FDTensor>& infer_result, std::vector<ClassifyResult>* results) {
if (!initialized_) {
FDERROR << "Postprocessor is not initialized." << std::endl;
return false;
}
int batch = infer_result[0].shape[0];
int num_classes = infer_result[0].shape[1];
const float* infer_result_data = reinterpret_cast<const float*>(infer_result[0].Data());
results->resize(batch);
int topk = std::min(num_classes, topk_);
for (int i = 0; i < batch; ++i) {
(*results)[i].label_ids = utils::TopKIndices(infer_result_data + i * num_classes, num_classes, topk);
(*results)[i].scores.resize(topk);
for (int j = 0; j < topk; ++j) {
(*results)[i].scores[j] = infer_result_data[i * num_classes + (*results)[i].label_ids[j]];
}
}
return true;
}
} // namespace classification
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,55 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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.
#pragma once
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
namespace fastdeploy {
namespace vision {
namespace classification {
/*! @brief Postprocessor object for PaddleClas serials model.
*/
class FASTDEPLOY_DECL PaddleClasPostprocessor {
public:
/** \brief Create a postprocessor instance for PaddleClas serials model
*
* \param[in] topk The topk result filtered by the classify confidence score, default 1
*/
explicit PaddleClasPostprocessor(int topk = 1);
/** \brief Process the result of runtime and fill to ClassifyResult structure
*
* \param[in] tensors The inference result from runtime
* \param[in] result The output result of classification
* \return true if the postprocess successed, otherwise false
*/
bool Run(const std::vector<FDTensor>& tensors,
std::vector<ClassifyResult>* result);
/// Set topk value
void SetTopk(int topk) { topk_ = topk; }
/// Get topk value
int GetTopk() const { return topk_; }
private:
int topk_ = 1;
bool initialized_ = false;
};
} // namespace classification
} // namespace vision
} // namespace fastdeploy

View File

@@ -15,16 +15,62 @@
namespace fastdeploy {
void BindPaddleClas(pybind11::module& m) {
pybind11::class_<vision::classification::PaddleClasPreprocessor>(
m, "PaddleClasPreprocessor")
.def(pybind11::init<std::string>())
.def("run", [](vision::classification::PaddleClasPreprocessor& self, std::vector<pybind11::array>& im_list) {
std::vector<vision::FDMat> images;
for (size_t i = 0; i < im_list.size(); ++i) {
images.push_back(vision::WrapMat(PyArrayToCvMat(im_list[i])));
}
std::vector<FDTensor> outputs;
if (!self.Run(&images, &outputs)) {
pybind11::eval("raise Exception('Failed to preprocess the input data in PaddleClasPreprocessor.')");
}
return outputs;
});
pybind11::class_<vision::classification::PaddleClasPostprocessor>(
m, "PaddleClasPostprocessor")
.def(pybind11::init<int>())
.def("run", [](vision::classification::PaddleClasPostprocessor& self, std::vector<FDTensor>& inputs) {
std::vector<vision::ClassifyResult> results;
if (!self.Run(inputs, &results)) {
pybind11::eval("raise Exception('Failed to postprocess the runtime result in PaddleClasPostprocessor.')");
}
return results;
})
.def("run", [](vision::classification::PaddleClasPostprocessor& self, std::vector<pybind11::array>& input_array) {
std::vector<vision::ClassifyResult> results;
std::vector<FDTensor> inputs;
PyArrayToTensorList(input_array, &inputs, /*share_buffer=*/true);
if (!self.Run(inputs, &results)) {
pybind11::eval("raise Exception('Failed to postprocess the runtime result in PaddleClasPostprocessor.')");
}
return results;
})
.def_property("topk", &vision::classification::PaddleClasPostprocessor::GetTopk, &vision::classification::PaddleClasPostprocessor::SetTopk);
pybind11::class_<vision::classification::PaddleClasModel, FastDeployModel>(
m, "PaddleClasModel")
.def(pybind11::init<std::string, std::string, std::string, RuntimeOption,
ModelFormat>())
.def("predict", [](vision::classification::PaddleClasModel& self,
pybind11::array& data, int topk = 1) {
auto mat = PyArrayToCvMat(data);
vision::ClassifyResult res;
self.Predict(&mat, &res, topk);
return res;
});
.def("predict", [](vision::classification::PaddleClasModel& self, pybind11::array& data) {
cv::Mat im = PyArrayToCvMat(data);
vision::ClassifyResult result;
self.Predict(im, &result);
return result;
})
.def("batch_predict", [](vision::classification::PaddleClasModel& self, std::vector<pybind11::array>& data) {
std::vector<cv::Mat> images;
for (size_t i = 0; i < data.size(); ++i) {
images.push_back(PyArrayToCvMat(data[i]));
}
std::vector<vision::ClassifyResult> results;
self.BatchPredict(images, &results);
return results;
})
.def_property_readonly("preprocessor", &vision::classification::PaddleClasModel::GetPreprocessor)
.def_property_readonly("postprocessor", &vision::classification::PaddleClasModel::GetPostprocessor);
}
} // namespace fastdeploy

View File

@@ -0,0 +1,108 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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.
#include "fastdeploy/vision/classification/ppcls/preprocessor.h"
#include "fastdeploy/function/concat.h"
#include "yaml-cpp/yaml.h"
namespace fastdeploy {
namespace vision {
namespace classification {
PaddleClasPreprocessor::PaddleClasPreprocessor(const std::string& config_file) {
FDASSERT(BuildPreprocessPipelineFromConfig(config_file), "Failed to create PaddleClasPreprocessor.");
initialized_ = true;
}
bool PaddleClasPreprocessor::BuildPreprocessPipelineFromConfig(const std::string& config_file) {
processors_.clear();
YAML::Node cfg;
try {
cfg = YAML::LoadFile(config_file);
} catch (YAML::BadFile& e) {
FDERROR << "Failed to load yaml file " << config_file
<< ", maybe you should check this file." << std::endl;
return false;
}
auto preprocess_cfg = cfg["PreProcess"]["transform_ops"];
processors_.push_back(std::make_shared<BGR2RGB>());
for (const auto& op : preprocess_cfg) {
FDASSERT(op.IsMap(),
"Require the transform information in yaml be Map type.");
auto op_name = op.begin()->first.as<std::string>();
if (op_name == "ResizeImage") {
int target_size = op.begin()->second["resize_short"].as<int>();
bool use_scale = false;
int interp = 1;
processors_.push_back(
std::make_shared<ResizeByShort>(target_size, 1, use_scale));
} else if (op_name == "CropImage") {
int width = op.begin()->second["size"].as<int>();
int height = op.begin()->second["size"].as<int>();
processors_.push_back(std::make_shared<CenterCrop>(width, height));
} else if (op_name == "NormalizeImage") {
auto mean = op.begin()->second["mean"].as<std::vector<float>>();
auto std = op.begin()->second["std"].as<std::vector<float>>();
auto scale = op.begin()->second["scale"].as<float>();
FDASSERT((scale - 0.00392157) < 1e-06 && (scale - 0.00392157) > -1e-06,
"Only support scale in Normalize be 0.00392157, means the pixel "
"is in range of [0, 255].");
processors_.push_back(std::make_shared<Normalize>(mean, std));
} else if (op_name == "ToCHWImage") {
processors_.push_back(std::make_shared<HWC2CHW>());
} else {
FDERROR << "Unexcepted preprocess operator: " << op_name << "."
<< std::endl;
return false;
}
}
// Fusion will improve performance
FuseTransforms(&processors_);
return true;
}
bool PaddleClasPreprocessor::Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs) {
if (!initialized_) {
FDERROR << "The preprocessor is not initialized." << std::endl;
return false;
}
if (images->size() == 0) {
FDERROR << "The size of input images should be greater than 0." << std::endl;
return false;
}
for (size_t i = 0; i < images->size(); ++i) {
for (size_t j = 0; j < processors_.size(); ++j) {
if (!(*(processors_[j].get()))(&((*images)[i]))) {
FDERROR << "Failed to processs image:" << i << " in " << processors_[i]->Name() << "." << std::endl;
return false;
}
}
}
outputs->resize(1);
// Concat all the preprocessed data to a batch tensor
std::vector<FDTensor> tensors(images->size());
for (size_t i = 0; i < images->size(); ++i) {
(*images)[i].ShareWithTensor(&(tensors[i]));
tensors[i].ExpandDim(0);
}
Concat(tensors, &((*outputs)[0]), 0);
return true;
}
} // namespace classification
} // namespace vision
} // namespace fastdeploy

View File

@@ -0,0 +1,50 @@
// Copyright (c) 2022 PaddlePaddle Authors. 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.
#pragma once
#include "fastdeploy/vision/common/processors/transform.h"
#include "fastdeploy/vision/common/result.h"
namespace fastdeploy {
namespace vision {
namespace classification {
/*! @brief Preprocessor object for PaddleClas serials model.
*/
class FASTDEPLOY_DECL PaddleClasPreprocessor {
public:
/** \brief Create a preprocessor instance for PaddleClas serials model
*
* \param[in] config_file Path of configuration file for deployment, e.g resnet/infer_cfg.yml
*/
explicit PaddleClasPreprocessor(const std::string& config_file);
/** \brief Process the input image and prepare input tensors for runtime
*
* \param[in] images The input image data list, all the elements are returned by cv::imread()
* \param[in] outputs The output tensors which will feed in runtime
* \return true if the preprocess successed, otherwise false
*/
bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs);
private:
bool BuildPreprocessPipelineFromConfig(const std::string& config_file);
std::vector<std::shared_ptr<Processor>> processors_;
bool initialized_ = false;
};
} // namespace classification
} // namespace vision
} // namespace fastdeploy

View File

@@ -51,7 +51,7 @@ bool ResizeByShort::ImplByFlyCV(Mat* mat) {
} else if (interp_ == 2) {
interp_method = fcv::InterpolationType::INTER_CUBIC;
} else {
FDERROR << "LimitLong: Only support interp_ be 0/1/2 with FlyCV, but "
FDERROR << "LimitByShort: Only support interp_ be 0/1/2 with FlyCV, but "
"now it's "
<< interp_ << "." << std::endl;
return false;

View File

@@ -35,6 +35,14 @@ std::string ClassifyResult::Str() {
return out;
}
ClassifyResult& ClassifyResult::operator=(ClassifyResult&& other) {
if (&other != this) {
label_ids = std::move(other.label_ids);
scores = std::move(other.scores);
}
return *this;
}
void Mask::Reserve(int size) { data.reserve(size); }
void Mask::Resize(int size) { data.resize(size); }

View File

@@ -44,6 +44,7 @@ struct FASTDEPLOY_DECL BaseResult {
/*! @brief Classify result structure for all the image classify models
*/
struct FASTDEPLOY_DECL ClassifyResult : public BaseResult {
ClassifyResult() = default;
/// Classify result for an image
std::vector<int32_t> label_ids;
/// The confidence for each classify result
@@ -53,6 +54,11 @@ struct FASTDEPLOY_DECL ClassifyResult : public BaseResult {
/// Clear result
void Clear();
/// Copy constructor
ClassifyResult(const ClassifyResult& other) = default;
/// Move assignment
ClassifyResult& operator=(ClassifyResult&& other);
/// Debug function, convert the result to string to print
std::string Str();
};