Skip to content

Commit

Permalink
[Hackthon_4th 242] Support en_ppstructure_mobile_v2.0_SLANet (PaddleP…
Browse files Browse the repository at this point in the history
…addle#1816)

* first draft

* update api name

* fix bug

* fix bug and

* fix bug in c api

* fix bug in c_api

---------

Co-authored-by: DefTruth <[email protected]>
  • Loading branch information
thunder95 and DefTruth authored Apr 27, 2023
1 parent ef576ce commit 2c5fd91
Show file tree
Hide file tree
Showing 35 changed files with 2,505 additions and 39 deletions.
3 changes: 3 additions & 0 deletions benchmark/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_executable(benchmark_ppmatting ${PROJECT_SOURCE_DIR}/benchmark_ppmatting.cc)
add_executable(benchmark_ppocr_det ${PROJECT_SOURCE_DIR}/benchmark_ppocr_det.cc)
add_executable(benchmark_ppocr_cls ${PROJECT_SOURCE_DIR}/benchmark_ppocr_cls.cc)
add_executable(benchmark_ppocr_rec ${PROJECT_SOURCE_DIR}/benchmark_ppocr_rec.cc)
add_executable(benchmark_structurev2_table ${PROJECT_SOURCE_DIR}/benchmark_structurev2_table.cc)
add_executable(benchmark_ppyoloe_r ${PROJECT_SOURCE_DIR}/benchmark_ppyoloe_r.cc)
add_executable(benchmark_ppyoloe_r_sophgo ${PROJECT_SOURCE_DIR}/benchmark_ppyoloe_r_sophgo.cc)
add_executable(benchmark_ppyolo ${PROJECT_SOURCE_DIR}/benchmark_ppyolo.cc)
Expand Down Expand Up @@ -55,6 +56,7 @@ if(UNIX AND (NOT APPLE) AND (NOT ANDROID))
target_link_libraries(benchmark_ppocr_det ${FASTDEPLOY_LIBS} gflags pthread)
target_link_libraries(benchmark_ppocr_cls ${FASTDEPLOY_LIBS} gflags pthread)
target_link_libraries(benchmark_ppocr_rec ${FASTDEPLOY_LIBS} gflags pthread)
target_link_libraries(benchmark_structurev2_table ${FASTDEPLOY_LIBS} gflags pthread)
target_link_libraries(benchmark_ppyolo ${FASTDEPLOY_LIBS} gflags pthread)
target_link_libraries(benchmark_yolov3 ${FASTDEPLOY_LIBS} gflags pthread)
target_link_libraries(benchmark_fasterrcnn ${FASTDEPLOY_LIBS} gflags pthread)
Expand Down Expand Up @@ -85,6 +87,7 @@ else()
target_link_libraries(benchmark_ppocr_det ${FASTDEPLOY_LIBS} gflags)
target_link_libraries(benchmark_ppocr_cls ${FASTDEPLOY_LIBS} gflags)
target_link_libraries(benchmark_ppocr_rec ${FASTDEPLOY_LIBS} gflags)
target_link_libraries(benchmark_structurev2_table ${FASTDEPLOY_LIBS} gflags)
target_link_libraries(benchmark_ppyolo ${FASTDEPLOY_LIBS} gflags)
target_link_libraries(benchmark_yolov3 ${FASTDEPLOY_LIBS} gflags)
target_link_libraries(benchmark_fasterrcnn ${FASTDEPLOY_LIBS} gflags)
Expand Down
1 change: 1 addition & 0 deletions benchmark/cpp/benchmark_gpu.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ fi
./benchmark_ppocr_rec --model ch_PP-OCRv3_rec_infer --image rec_img.jpg --rec_label_file ppocr_keys_v1.txt --config_path $CONFIG_PATH
./benchmark_ppocr_det --model ch_PP-OCRv2_det_infer --image 12.jpg --config_path $CONFIG_PATH
./benchmark_ppocr_rec --model ch_PP-OCRv2_rec_infer --image rec_img.jpg --rec_label_file ppocr_keys_v1.txt --config_path $CONFIG_PATH
./benchmark_ppocr_table --model en_ppstructure_mobile_v2.0_SLANet_infer --image table.jpg --table_char_dict_path table_structure_dict.txt --config_path $CONFIG_PATH

# PaddleDetection
./benchmark_ppyolov5 --model yolov5_s_300e_coco --image 000000014439.jpg --config_path $CONFIG_PATH
Expand Down
161 changes: 161 additions & 0 deletions benchmark/cpp/benchmark_structurev2_table.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright (c) 2023 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 "flags.h"
#include "macros.h"
#include "option.h"

namespace vision = fastdeploy::vision;
namespace benchmark = fastdeploy::benchmark;

DEFINE_string(table_char_dict_path, "",
"Path of table character dict of PPOCR.");
DEFINE_string(trt_shape, "1,3,48,10:4,3,48,320:8,3,48,2304",
"Set min/opt/max shape for trt/paddle_trt backend."
"eg:--trt_shape 1,3,48,10:4,3,48,320:8,3,48,2304");

int main(int argc, char *argv[]) {
#if defined(ENABLE_BENCHMARK) && defined(ENABLE_VISION)
// Initialization
auto option = fastdeploy::RuntimeOption();
if (!CreateRuntimeOption(&option, argc, argv, true)) {
return -1;
}
auto im = cv::imread(FLAGS_image);
std::unordered_map<std::string, std::string> config_info;
benchmark::ResultManager::LoadBenchmarkConfig(FLAGS_config_path,
&config_info);
std::string model_name, params_name, config_name;
auto model_format = fastdeploy::ModelFormat::PADDLE;
if (!UpdateModelResourceName(&model_name, &params_name, &config_name,
&model_format, config_info, false)) {
return -1;
}
auto model_file = FLAGS_model + sep + model_name;
auto params_file = FLAGS_model + sep + params_name;
if (config_info["backend"] == "paddle_trt") {
option.paddle_infer_option.collect_trt_shape = true;
}
if (config_info["backend"] == "paddle_trt" ||
config_info["backend"] == "trt") {
std::vector<std::vector<int32_t>> trt_shapes =
benchmark::ResultManager::GetInputShapes(FLAGS_trt_shape);
option.trt_option.SetShape("x", trt_shapes[0], trt_shapes[1],
trt_shapes[2]);
}

auto model_ppocr_table = vision::ocr::StructureV2Table(
model_file, params_file, FLAGS_table_char_dict_path, option,
model_format);
fastdeploy::vision::OCRResult result;

if (config_info["precision_compare"] == "true") {
std::string expect_structure_html =
"<html><body><table><thead><tr><td></td><td></td><td></td><td></"
"td><td></td></tr></thead><tbody><tr><td></td><td></td><td></td><td></"
"td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></"
"tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></"
"td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></"
"td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></"
"td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></"
"td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></"
"tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></"
"td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></"
"td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></"
"td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></"
"td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></"
"tr><tr><td></td><td></td><td></td><td></td><td></td></tr></tbody></"
"table></body></html>";
std::vector<int> expect_box_coord{
41, 4, 97, 18, 161, 4, 173, 18, 216, 4, 225, 17, 272, 4,
283, 17, 321, 4, 348, 18, 33, 20, 106, 38, 150, 22, 180, 38,
202, 22, 235, 38, 262, 21, 293, 38, 326, 23, 343, 37, 27, 38,
109, 56, 150, 39, 179, 56, 204, 39, 236, 56, 263, 39, 292, 55,
329, 40, 343, 54, 22, 57, 118, 74, 152, 58, 176, 74, 204, 58,
236, 75, 262, 58, 291, 74, 326, 58, 344, 74, 27, 75, 119, 92,
150, 75, 177, 92, 204, 75, 235, 92, 260, 75, 292, 92, 326, 75,
346, 92, 44, 92, 102, 110, 150, 92, 177, 110, 205, 92, 236, 110,
262, 92, 290, 110, 329, 93, 339, 110, 41, 109, 102, 128, 151, 110,
175, 128, 205, 110, 236, 128, 262, 110, 291, 127, 329, 110, 338, 127,
42, 128, 102, 146, 149, 128, 177, 146, 205, 128, 237, 146, 262, 128,
291, 146, 329, 128, 339, 145, 31, 145, 110, 163, 150, 145, 178, 163,
206, 145, 237, 164, 262, 145, 292, 163, 324, 145, 342, 162, 40, 162,
108, 180, 154, 162, 175, 180, 209, 162, 231, 180, 266, 162, 286, 180,
325, 162, 341, 179, 38, 180, 105, 197, 152, 180, 177, 197, 207, 180,
236, 197, 262, 180, 291, 197, 329, 181, 339, 196, 42, 196, 102, 214,
151, 197, 179, 214, 205, 197, 236, 214, 263, 197, 291, 214, 320, 197,
349, 214, 46, 215, 100, 233, 149, 216, 179, 233, 204, 216, 238, 233,
262, 216, 291, 233, 321, 216, 345, 232, 42, 233, 104, 251, 147, 234,
179, 251, 203, 233, 237, 251, 260, 233, 294, 251, 326, 234, 341, 250,
19, 251, 120, 269, 148, 253, 180, 270, 202, 252, 240, 270, 259, 252,
294, 270, 324, 252, 347, 268, 16, 270, 123, 286, 146, 270, 182, 287,
200, 270, 238, 287, 256, 270, 294, 286, 319, 270, 353, 286};

// Run once at least
if (!model_ppocr_table.Predict(im, &result)) {
std::cerr << "Failed to predict." << std::endl;
return -1;
}

// 1. Test result diff
std::cout << "=============== Test Table Result diff =================\n";
// Calculate diff between two results.
std::string result_table_structure;
for (auto &structure : result.table_structure) {
result_table_structure += structure;
}
if (expect_structure_html == result_table_structure) {
std::cout << "PPOCR Table structure has no diff" << std::endl;
} else {
std::cout << "PPOCR Table structure has diff" << std::endl;
std::cout << "expected: " << expect_structure_html << std::endl;
std::cout << "result: " << result_table_structure << std::endl;
}

std::vector<int> table_box_coord;
for (auto &box : result.table_boxes) {
// x1 y1 x2 y1 x2 y2 x1 y2 => x1 y1 x2 y2
table_box_coord.push_back(box[0]);
table_box_coord.push_back(box[1]);
table_box_coord.push_back(box[2]);
table_box_coord.push_back(box[5]);
}

if (expect_box_coord.size() == table_box_coord.size()) {
std::cout << "table boxes num matched with expected: "
<< table_box_coord.size() << std::endl;
int max_diff = 0;
int total_diff = 0;
for (int i = 0; i < table_box_coord.size(); i++) {
int diff = std::abs(table_box_coord[i] - expect_box_coord[i]);
if (diff > max_diff) {
max_diff = diff;
}
total_diff += diff;
}
std::cout << "box coords, max_diff: " << max_diff << ", "
<< ", total diff: " << total_diff << ", average diff: "
<< total_diff / float(table_box_coord.size()) << std::endl;
} else {
std::cout << "boxes num has diff, expect box num: "
<< expect_box_coord.size() / 4
<< ", result box num:" << table_box_coord.size() / 4
<< std::endl;
}
}

BENCHMARK_MODEL(model_ppocr_table, model_ppocr_table.Predict(im, &result));
#endif
return 0;
}
1 change: 1 addition & 0 deletions benchmark/cpp/get_models.sh
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ download_common_model_xvf https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP
download_common_model_xvf https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar ch_ppocr_mobile_v2.0_cls_infer.tar
download_common_model_xvf https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_det_infer.tar ch_PP-OCRv2_det_infer.tar
download_common_model_xvf https://paddleocr.bj.bcebos.com/PP-OCRv2/chinese/ch_PP-OCRv2_rec_infer.tar ch_PP-OCRv2_rec_infer.tar
download_common_model_xvf https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/en_ppstructure_mobile_v2.0_SLANet_infer.tar

# download images
download_common_file https://bj.bcebos.com/paddlehub/fastdeploy/rec_img.jpg rec_img.jpg
Expand Down
2 changes: 1 addition & 1 deletion c_api/fastdeploy_capi/core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
#endif

#ifndef ENABLE_TEXT
#define ENABLE_TEXT
/* #undef ENABLE_TEXT */
#endif
7 changes: 7 additions & 0 deletions c_api/fastdeploy_capi/core/fd_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ typedef struct FD_C_OneDimArrayCstr {
FD_C_Cstr* data;
} FD_C_OneDimArrayCstr; // std::vector<std::string>

typedef struct FD_C_TwoDimArrayCstr {
size_t size;
FD_C_OneDimArrayCstr* data;
} FD_C_TwoDimArrayCstr; // std::vector<std::vector<std::string>>

typedef struct FD_C_TwoDimArraySize {
size_t size;
FD_C_OneDimArraySize* data;
Expand Down Expand Up @@ -134,6 +139,8 @@ DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimArrayFloat);
DECLARE_DESTROY_FD_TYPE_FUNCTION(Cstr);
// FD_C_OneDimArrayCstr
DECLARE_DESTROY_FD_TYPE_FUNCTION(OneDimArrayCstr);
// FD_C_TwoDimArrayCstr
DECLARE_DESTROY_FD_TYPE_FUNCTION(TwoDimArrayCstr);
// FD_C_TwoDimArraySize
DECLARE_DESTROY_FD_TYPE_FUNCTION(TwoDimArraySize);
// FD_C_TwoDimArrayInt8
Expand Down
Loading

0 comments on commit 2c5fd91

Please sign in to comment.