mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-16 13:41:30 +08:00
[Model] Update PPOCR code style (#1160)
* 更新代码风格 * 更新代码风格 * 更新代码风格 * 更新代码风格
This commit is contained in:
@@ -20,13 +20,13 @@ namespace fastdeploy {
|
||||
namespace vision {
|
||||
namespace ocr {
|
||||
|
||||
bool SingleBatchPostprocessor(const float* out_data, const size_t& length, int* cls_label, float* cls_score) {
|
||||
bool SingleBatchPostprocessor(const float* out_data, const size_t& length,
|
||||
int* cls_label, float* cls_score) {
|
||||
|
||||
*cls_label = std::distance(
|
||||
&out_data[0], std::max_element(&out_data[0], &out_data[length]));
|
||||
*cls_label = std::distance(&out_data[0],
|
||||
std::max_element(&out_data[0], &out_data[length]));
|
||||
|
||||
*cls_score =
|
||||
float(*std::max_element(&out_data[0], &out_data[length]));
|
||||
*cls_score = float(*std::max_element(&out_data[0], &out_data[length]));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -46,18 +46,24 @@ bool ClassifierPostprocessor::Run(const std::vector<FDTensor>& tensors,
|
||||
|
||||
// For Classifier, the output tensor shape = [batch,2]
|
||||
size_t batch = tensor.shape[0];
|
||||
size_t length = accumulate(tensor.shape.begin()+1, tensor.shape.end(), 1, std::multiplies<int>());
|
||||
size_t length = accumulate(tensor.shape.begin() + 1, tensor.shape.end(), 1,
|
||||
std::multiplies<int>());
|
||||
|
||||
if (batch <= 0) {
|
||||
FDERROR << "The infer outputTensor.shape[0] <=0, wrong infer result." << std::endl;
|
||||
FDERROR << "The infer outputTensor.shape[0] <=0, wrong infer result."
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
if (start_index < 0 || total_size <= 0) {
|
||||
FDERROR << "start_index or total_size error. Correct is: 0 <= start_index < total_size" << std::endl;
|
||||
FDERROR << "start_index or total_size error. Correct is: 0 <= start_index "
|
||||
"< total_size"
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
if ((start_index + batch) > total_size) {
|
||||
FDERROR << "start_index or total_size error. Correct is: start_index + batch(outputTensor.shape[0]) <= total_size" << std::endl;
|
||||
FDERROR << "start_index or total_size error. Correct is: start_index + "
|
||||
"batch(outputTensor.shape[0]) <= total_size"
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -65,17 +71,15 @@ bool ClassifierPostprocessor::Run(const std::vector<FDTensor>& tensors,
|
||||
cls_scores->resize(total_size);
|
||||
const float* tensor_data = reinterpret_cast<const float*>(tensor.Data());
|
||||
for (int i_batch = 0; i_batch < batch; ++i_batch) {
|
||||
if(!SingleBatchPostprocessor(tensor_data+ i_batch * length,
|
||||
SingleBatchPostprocessor(tensor_data+ i_batch * length,
|
||||
length,
|
||||
&cls_labels->at(i_batch + start_index),
|
||||
&cls_scores->at(i_batch + start_index))) {
|
||||
return false;
|
||||
}
|
||||
&cls_scores->at(i_batch + start_index));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace classification
|
||||
} // namespace ocr
|
||||
} // namespace vision
|
||||
} // namespace fastdeploy
|
||||
|
@@ -33,11 +33,11 @@ class FASTDEPLOY_DECL ClassifierPostprocessor {
|
||||
* \return true if the postprocess successed, otherwise false
|
||||
*/
|
||||
bool Run(const std::vector<FDTensor>& tensors,
|
||||
std::vector<int32_t>* cls_labels, std::vector<float>* cls_scores);
|
||||
std::vector<int32_t>* cls_labels, std::vector<float>* cls_scores);
|
||||
|
||||
bool Run(const std::vector<FDTensor>& tensors,
|
||||
std::vector<int32_t>* cls_labels, std::vector<float>* cls_scores,
|
||||
size_t start_index, size_t total_size);
|
||||
std::vector<int32_t>* cls_labels, std::vector<float>* cls_scores,
|
||||
size_t start_index, size_t total_size);
|
||||
|
||||
/// Set threshold for the classification postprocess, default is 0.9
|
||||
void SetClsThresh(float cls_thresh) { cls_thresh_ = cls_thresh; }
|
||||
@@ -45,6 +45,7 @@ class FASTDEPLOY_DECL ClassifierPostprocessor {
|
||||
/// Get threshold value of the classification postprocess.
|
||||
float GetClsThresh() const { return cls_thresh_; }
|
||||
|
||||
private:
|
||||
float cls_thresh_ = 0.9;
|
||||
};
|
||||
|
||||
|
@@ -35,12 +35,12 @@ class FASTDEPLOY_DECL ClassifierPreprocessor {
|
||||
size_t start_index, size_t end_index);
|
||||
|
||||
/// Set mean value for the image normalization in classification preprocess
|
||||
void SetMean(std::vector<float> mean) { mean_ = mean; }
|
||||
void SetMean(const std::vector<float>& mean) { mean_ = mean; }
|
||||
/// Get mean value of the image normalization in classification preprocess
|
||||
std::vector<float> GetMean() const { return mean_; }
|
||||
|
||||
/// Set scale value for the image normalization in classification preprocess
|
||||
void SetScale(std::vector<float> scale) { scale_ = scale; }
|
||||
void SetScale(const std::vector<float>& scale) { scale_ = scale; }
|
||||
/// Get scale value of the image normalization in classification preprocess
|
||||
std::vector<float> GetScale() const { return scale_; }
|
||||
|
||||
@@ -50,11 +50,13 @@ class FASTDEPLOY_DECL ClassifierPreprocessor {
|
||||
bool GetIsScale() const { return is_scale_; }
|
||||
|
||||
/// Set cls_image_shape for the classification preprocess
|
||||
void SetClsImageShape(std::vector<int> cls_image_shape)
|
||||
{ cls_image_shape_ = cls_image_shape; }
|
||||
void SetClsImageShape(const std::vector<int>& cls_image_shape) {
|
||||
cls_image_shape_ = cls_image_shape;
|
||||
}
|
||||
/// Get cls_image_shape for the classification preprocess
|
||||
std::vector<int> GetClsImageShape() const { return cls_image_shape_; }
|
||||
|
||||
private:
|
||||
std::vector<float> mean_ = {0.5f, 0.5f, 0.5f};
|
||||
std::vector<float> scale_ = {0.5f, 0.5f, 0.5f};
|
||||
bool is_scale_ = true;
|
||||
|
@@ -21,12 +21,9 @@ namespace vision {
|
||||
namespace ocr {
|
||||
|
||||
bool DBDetectorPostprocessor::SingleBatchPostprocessor(
|
||||
const float* out_data,
|
||||
int n2,
|
||||
int n3,
|
||||
const std::array<int,4>& det_img_info,
|
||||
std::vector<std::array<int, 8>>* boxes_result
|
||||
) {
|
||||
const float* out_data, int n2, int n3,
|
||||
const std::array<int, 4>& det_img_info,
|
||||
std::vector<std::array<int, 8>>* boxes_result) {
|
||||
int n = n2 * n3;
|
||||
|
||||
// prepare bitmap
|
||||
@@ -52,9 +49,9 @@ bool DBDetectorPostprocessor::SingleBatchPostprocessor(
|
||||
|
||||
std::vector<std::vector<std::vector<int>>> boxes;
|
||||
|
||||
boxes =
|
||||
util_post_processor_.BoxesFromBitmap(pred_map, bit_map, det_db_box_thresh_,
|
||||
det_db_unclip_ratio_, det_db_score_mode_);
|
||||
boxes = util_post_processor_.BoxesFromBitmap(
|
||||
pred_map, bit_map, det_db_box_thresh_, det_db_unclip_ratio_,
|
||||
det_db_score_mode_);
|
||||
|
||||
boxes = util_post_processor_.FilterTagDetRes(boxes, det_img_info);
|
||||
|
||||
@@ -67,31 +64,30 @@ bool DBDetectorPostprocessor::SingleBatchPostprocessor(
|
||||
new_box[k++] = e;
|
||||
}
|
||||
}
|
||||
boxes_result->push_back(new_box);
|
||||
boxes_result->emplace_back(new_box);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DBDetectorPostprocessor::Run(const std::vector<FDTensor>& tensors,
|
||||
std::vector<std::vector<std::array<int, 8>>>* results,
|
||||
const std::vector<std::array<int,4>>& batch_det_img_info) {
|
||||
bool DBDetectorPostprocessor::Run(
|
||||
const std::vector<FDTensor>& tensors,
|
||||
std::vector<std::vector<std::array<int, 8>>>* results,
|
||||
const std::vector<std::array<int, 4>>& batch_det_img_info) {
|
||||
// DBDetector have only 1 output tensor.
|
||||
const FDTensor& tensor = tensors[0];
|
||||
|
||||
// For DBDetector, the output tensor shape = [batch, 1, ?, ?]
|
||||
size_t batch = tensor.shape[0];
|
||||
size_t length = accumulate(tensor.shape.begin()+1, tensor.shape.end(), 1, std::multiplies<int>());
|
||||
size_t length = accumulate(tensor.shape.begin() + 1, tensor.shape.end(), 1,
|
||||
std::multiplies<int>());
|
||||
const float* tensor_data = reinterpret_cast<const float*>(tensor.Data());
|
||||
|
||||
results->resize(batch);
|
||||
for (int i_batch = 0; i_batch < batch; ++i_batch) {
|
||||
if(!SingleBatchPostprocessor(tensor_data,
|
||||
tensor.shape[2],
|
||||
tensor.shape[3],
|
||||
batch_det_img_info[i_batch],
|
||||
&results->at(i_batch)
|
||||
))return false;
|
||||
SingleBatchPostprocessor(tensor_data, tensor.shape[2], tensor.shape[3],
|
||||
batch_det_img_info[i_batch],
|
||||
&results->at(i_batch));
|
||||
tensor_data = tensor_data + length;
|
||||
}
|
||||
return true;
|
||||
|
@@ -42,20 +42,23 @@ class FASTDEPLOY_DECL DBDetectorPostprocessor {
|
||||
double GetDetDBThresh() const { return det_db_thresh_; }
|
||||
|
||||
/// Set det_db_box_thresh for the detection postprocess, default is 0.6
|
||||
void SetDetDBBoxThresh(double det_db_box_thresh)
|
||||
{ det_db_box_thresh_ = det_db_box_thresh; }
|
||||
void SetDetDBBoxThresh(double det_db_box_thresh) {
|
||||
det_db_box_thresh_ = det_db_box_thresh;
|
||||
}
|
||||
/// Get det_db_box_thresh of the detection postprocess
|
||||
double GetDetDBBoxThresh() const { return det_db_box_thresh_; }
|
||||
|
||||
/// Set det_db_unclip_ratio for the detection postprocess, default is 1.5
|
||||
void SetDetDBUnclipRatio(double det_db_unclip_ratio)
|
||||
{ det_db_unclip_ratio_ = det_db_unclip_ratio; }
|
||||
void SetDetDBUnclipRatio(double det_db_unclip_ratio) {
|
||||
det_db_unclip_ratio_ = det_db_unclip_ratio;
|
||||
}
|
||||
/// Get det_db_unclip_ratio_ of the detection postprocess
|
||||
double GetDetDBUnclipRatio() const { return det_db_unclip_ratio_; }
|
||||
|
||||
/// Set det_db_score_mode for the detection postprocess, default is 'slow'
|
||||
void SetDetDBScoreMode(std::string det_db_score_mode)
|
||||
{ det_db_score_mode_ = det_db_score_mode; }
|
||||
void SetDetDBScoreMode(const std::string& det_db_score_mode) {
|
||||
det_db_score_mode_ = det_db_score_mode;
|
||||
}
|
||||
/// Get det_db_score_mode_ of the detection postprocess
|
||||
std::string GetDetDBScoreMode() const { return det_db_score_mode_; }
|
||||
|
||||
@@ -64,17 +67,15 @@ class FASTDEPLOY_DECL DBDetectorPostprocessor {
|
||||
/// Get use_dilation of the detection postprocess
|
||||
int GetUseDilation() const { return use_dilation_; }
|
||||
|
||||
|
||||
private:
|
||||
double det_db_thresh_ = 0.3;
|
||||
double det_db_box_thresh_ = 0.6;
|
||||
double det_db_unclip_ratio_ = 1.5;
|
||||
std::string det_db_score_mode_ = "slow";
|
||||
bool use_dilation_ = false;
|
||||
|
||||
private:
|
||||
PostProcessor util_post_processor_;
|
||||
bool SingleBatchPostprocessor(const float* out_data,
|
||||
int n2,
|
||||
int n3,
|
||||
bool SingleBatchPostprocessor(const float* out_data, int n2, int n3,
|
||||
const std::array<int, 4>& det_img_info,
|
||||
std::vector<std::array<int, 8>>* boxes_result);
|
||||
};
|
||||
|
@@ -31,8 +31,7 @@ class FASTDEPLOY_DECL DBDetectorPreprocessor {
|
||||
* \param[in] batch_det_img_info_ptr The output of preprocess
|
||||
* \return true if the preprocess successed, otherwise false
|
||||
*/
|
||||
bool Run(std::vector<FDMat>* images,
|
||||
std::vector<FDTensor>* outputs,
|
||||
bool Run(std::vector<FDMat>* images, std::vector<FDTensor>* outputs,
|
||||
std::vector<std::array<int, 4>>* batch_det_img_info_ptr);
|
||||
|
||||
/// Set max_side_len for the detection preprocess, default is 960
|
||||
@@ -41,12 +40,12 @@ class FASTDEPLOY_DECL DBDetectorPreprocessor {
|
||||
int GetMaxSideLen() const { return max_side_len_; }
|
||||
|
||||
/// Set mean value for the image normalization in detection preprocess
|
||||
void SetMean(std::vector<float> mean) { mean_ = mean; }
|
||||
void SetMean(const std::vector<float>& mean) { mean_ = mean; }
|
||||
/// Get mean value of the image normalization in detection preprocess
|
||||
std::vector<float> GetMean() const { return mean_; }
|
||||
|
||||
/// Set scale value for the image normalization in detection preprocess
|
||||
void SetScale(std::vector<float> scale) { scale_ = scale; }
|
||||
void SetScale(const std::vector<float>& scale) { scale_ = scale; }
|
||||
/// Get scale value of the image normalization in detection preprocess
|
||||
std::vector<float> GetScale() const { return scale_; }
|
||||
|
||||
@@ -55,6 +54,7 @@ class FASTDEPLOY_DECL DBDetectorPreprocessor {
|
||||
/// Get is_scale of the image normalization in detection preprocess
|
||||
bool GetIsScale() const { return is_scale_; }
|
||||
|
||||
private:
|
||||
int max_side_len_ = 960;
|
||||
std::vector<float> mean_ = {0.485f, 0.456f, 0.406f};
|
||||
std::vector<float> scale_ = {0.229f, 0.224f, 0.225f};
|
||||
|
@@ -23,14 +23,18 @@ PPOCRv2::PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
|
||||
fastdeploy::vision::ocr::Recognizer* rec_model)
|
||||
: detector_(det_model), classifier_(cls_model), recognizer_(rec_model) {
|
||||
Initialized();
|
||||
recognizer_->GetPreprocessor().rec_image_shape_[1] = 32;
|
||||
auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape();
|
||||
preprocess_shape[1] = 32;
|
||||
recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape);
|
||||
}
|
||||
|
||||
PPOCRv2::PPOCRv2(fastdeploy::vision::ocr::DBDetector* det_model,
|
||||
fastdeploy::vision::ocr::Recognizer* rec_model)
|
||||
: detector_(det_model), recognizer_(rec_model) {
|
||||
Initialized();
|
||||
recognizer_->GetPreprocessor().rec_image_shape_[1] = 32;
|
||||
auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape();
|
||||
preprocess_shape[1] = 32;
|
||||
recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape);
|
||||
}
|
||||
|
||||
bool PPOCRv2::SetClsBatchSize(int cls_batch_size) {
|
||||
@@ -145,7 +149,7 @@ bool PPOCRv2::BatchPredict(const std::vector<cv::Mat>& images,
|
||||
return false;
|
||||
}else{
|
||||
for (size_t i_img = start_index; i_img < end_index; ++i_img) {
|
||||
if(cls_labels_ptr->at(i_img) % 2 == 1 && cls_scores_ptr->at(i_img) > classifier_->GetPostprocessor().cls_thresh_) {
|
||||
if(cls_labels_ptr->at(i_img) % 2 == 1 && cls_scores_ptr->at(i_img) > classifier_->GetPostprocessor().GetClsThresh()) {
|
||||
cv::rotate(image_list[i_img], image_list[i_img], 1);
|
||||
}
|
||||
}
|
||||
|
@@ -36,7 +36,9 @@ class FASTDEPLOY_DECL PPOCRv3 : public PPOCRv2 {
|
||||
fastdeploy::vision::ocr::Recognizer* rec_model)
|
||||
: PPOCRv2(det_model, cls_model, rec_model) {
|
||||
// The only difference between v2 and v3
|
||||
recognizer_->GetPreprocessor().rec_image_shape_[1] = 48;
|
||||
auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape();
|
||||
preprocess_shape[1] = 48;
|
||||
recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape);
|
||||
}
|
||||
/** \brief Classification model is optional, so this function is set up the detection model path and recognition model path respectively.
|
||||
*
|
||||
@@ -47,7 +49,9 @@ class FASTDEPLOY_DECL PPOCRv3 : public PPOCRv2 {
|
||||
fastdeploy::vision::ocr::Recognizer* rec_model)
|
||||
: PPOCRv2(det_model, rec_model) {
|
||||
// The only difference between v2 and v3
|
||||
recognizer_->GetPreprocessor().rec_image_shape_[1] = 48;
|
||||
auto preprocess_shape = recognizer_->GetPreprocessor().GetRecImageShape();
|
||||
preprocess_shape[1] = 48;
|
||||
recognizer_->GetPreprocessor().SetRecImageShape(preprocess_shape);
|
||||
}
|
||||
|
||||
/** \brief Clone a new PPOCRv3 with less memory usage when multiple instances of the same model are created
|
||||
|
@@ -38,18 +38,19 @@ class FASTDEPLOY_DECL RecognizerPreprocessor {
|
||||
/// Set static_shape_infer is true or not. When deploy PP-OCR
|
||||
/// on hardware which can not support dynamic input shape very well,
|
||||
/// like Huawei Ascned, static_shape_infer needs to to be true.
|
||||
void SetStaticShapeInfer(bool static_shape_infer)
|
||||
{ static_shape_infer_ = static_shape_infer; }
|
||||
void SetStaticShapeInfer(bool static_shape_infer) {
|
||||
static_shape_infer_ = static_shape_infer;
|
||||
}
|
||||
/// Get static_shape_infer of the recognition preprocess
|
||||
bool GetStaticShapeInfer() const { return static_shape_infer_; }
|
||||
|
||||
/// Set mean value for the image normalization in recognition preprocess
|
||||
void SetMean(std::vector<float> mean) { mean_ = mean; }
|
||||
void SetMean(const std::vector<float>& mean) { mean_ = mean; }
|
||||
/// Get mean value of the image normalization in recognition preprocess
|
||||
std::vector<float> GetMean() const { return mean_; }
|
||||
|
||||
/// Set scale value for the image normalization in recognition preprocess
|
||||
void SetScale(std::vector<float> scale) { scale_ = scale; }
|
||||
void SetScale(const std::vector<float>& scale) { scale_ = scale; }
|
||||
/// Get scale value of the image normalization in recognition preprocess
|
||||
std::vector<float> GetScale() const { return scale_; }
|
||||
|
||||
@@ -59,11 +60,13 @@ class FASTDEPLOY_DECL RecognizerPreprocessor {
|
||||
bool GetIsScale() const { return is_scale_; }
|
||||
|
||||
/// Set rec_image_shape for the recognition preprocess
|
||||
void SetRecImageShape(std::vector<int> rec_image_shape)
|
||||
{ rec_image_shape_ = rec_image_shape; }
|
||||
void SetRecImageShape(const std::vector<int>& rec_image_shape) {
|
||||
rec_image_shape_ = rec_image_shape;
|
||||
}
|
||||
/// Get rec_image_shape for the recognition preprocess
|
||||
std::vector<int> GetRecImageShape() const { return rec_image_shape_; }
|
||||
std::vector<int> GetRecImageShape() { return rec_image_shape_; }
|
||||
|
||||
private:
|
||||
std::vector<int> rec_image_shape_ = {3, 48, 320};
|
||||
std::vector<float> mean_ = {0.5f, 0.5f, 0.5f};
|
||||
std::vector<float> scale_ = {0.5f, 0.5f, 0.5f};
|
||||
|
Reference in New Issue
Block a user