-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NNAdapter] Add 35 unit tests for model validation from PaddleClas, PaddleDetection, PaddleSeg, PaddleOCR etc. #8401
Merged
hong19860320
merged 11 commits into
PaddlePaddle:develop
from
shentanyue:add_3rd_model_ut
Feb 28, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8fc9f03
add ut for nnadapter
shentanyue f6cf628
add ut for nnadapter
shentanyue 80dfff8
Merge branch 'add_3rd_model_ut' of https://github.com/shentanyue/Padd…
shentanyue 78a4403
add more paddle-clas models ut
shentanyue af86d09
test=huawei_ascend_npu
shentanyue 5a4bac0
test=huawei_ascend_npu
shentanyue 7259815
test=huawei_ascend_npu
shentanyue b1580ef
test=huawei_ascend_npu
shentanyue a4c3167
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle-Lite…
shentanyue f46f0a2
test=huawei_ascend_npu
shentanyue 1129347
rerun ci: add more model ut test=huawei_ascend_npu
shentanyue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,125 @@ | ||
// Copyright (c) 2019 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 <gflags/gflags.h> | ||
#include <gtest/gtest.h> | ||
#include <vector> | ||
#include "lite/api/paddle_api.h" | ||
#include "lite/api/test/lite_api_test_helper.h" | ||
#include "lite/api/test/test_helper.h" | ||
#include "lite/tests/api/utility.h" | ||
#include "lite/utils/string.h" | ||
|
||
DEFINE_string(data_dir, "", "data dir"); | ||
DEFINE_int32(iteration, 1, "iteration times to run"); | ||
|
||
namespace paddle { | ||
namespace lite { | ||
|
||
TEST(bisenet, test_bisenet_fp32_v2_3_nnadapter) { | ||
std::vector<std::string> nnadapter_device_names; | ||
std::string nnadapter_context_properties; | ||
std::vector<paddle::lite_api::Place> valid_places; | ||
valid_places.push_back( | ||
lite_api::Place{TARGET(kNNAdapter), PRECISION(kFloat)}); | ||
#if defined(LITE_WITH_ARM) | ||
valid_places.push_back(lite_api::Place{TARGET(kARM), PRECISION(kFloat)}); | ||
#elif defined(LITE_WITH_X86) | ||
valid_places.push_back(lite_api::Place{TARGET(kX86), PRECISION(kFloat)}); | ||
#else | ||
LOG(INFO) << "Unsupported host arch!"; | ||
return; | ||
#endif | ||
#if defined(NNADAPTER_WITH_HUAWEI_ASCEND_NPU) | ||
nnadapter_device_names.emplace_back("huawei_ascend_npu"); | ||
nnadapter_context_properties = "HUAWEI_ASCEND_NPU_SELECTED_DEVICE_IDS=0"; | ||
#else | ||
LOG(INFO) << "Unsupported NNAdapter device!"; | ||
return; | ||
#endif | ||
std::shared_ptr<paddle::lite_api::PaddlePredictor> predictor = nullptr; | ||
// Use the full api with CxxConfig to generate the optimized model | ||
lite_api::CxxConfig cxx_config; | ||
cxx_config.set_model_dir(FLAGS_model_dir); | ||
cxx_config.set_valid_places(valid_places); | ||
cxx_config.set_nnadapter_device_names(nnadapter_device_names); | ||
cxx_config.set_nnadapter_context_properties(nnadapter_context_properties); | ||
predictor = lite_api::CreatePaddlePredictor(cxx_config); | ||
predictor->SaveOptimizedModel(FLAGS_model_dir, | ||
paddle::lite_api::LiteModelType::kNaiveBuffer); | ||
// Use the light api with MobileConfig to load and run the optimized model | ||
paddle::lite_api::MobileConfig mobile_config; | ||
mobile_config.set_model_from_file(FLAGS_model_dir + ".nb"); | ||
mobile_config.set_threads(FLAGS_threads); | ||
mobile_config.set_power_mode( | ||
static_cast<lite_api::PowerMode>(FLAGS_power_mode)); | ||
mobile_config.set_nnadapter_device_names(nnadapter_device_names); | ||
mobile_config.set_nnadapter_context_properties(nnadapter_context_properties); | ||
predictor = paddle::lite_api::CreatePaddlePredictor(mobile_config); | ||
|
||
std::string input_data_dir = | ||
FLAGS_data_dir + std::string("/bisenet_input.txt"); | ||
std::string output_data_dir = | ||
FLAGS_data_dir + std::string("/bisenet_output.txt"); | ||
std::vector<std::vector<std::vector<uint8_t>>> input_data_set; | ||
std::vector<std::vector<std::vector<int64_t>>> input_data_set_shapes; | ||
LoadSpecificData(input_data_dir, | ||
input_data_set, | ||
input_data_set_shapes, | ||
predictor, | ||
"input"); | ||
std::vector<std::vector<std::vector<uint8_t>>> output_data_set; | ||
std::vector<std::vector<std::vector<int64_t>>> output_data_set_shapes; | ||
LoadSpecificData(output_data_dir, | ||
output_data_set, | ||
output_data_set_shapes, | ||
predictor, | ||
"output"); | ||
|
||
FLAGS_warmup = 1; | ||
for (int i = 0; i < FLAGS_warmup; i++) { | ||
FillModelInput(input_data_set[i], input_data_set_shapes[i], predictor); | ||
predictor->Run(); | ||
} | ||
|
||
double cost_time = 0; | ||
std::vector<std::vector<float>> results; | ||
for (int i = 0; i < FLAGS_iteration; i++) { | ||
FillModelInput(input_data_set[i], input_data_set_shapes[i], predictor); | ||
|
||
double start = GetCurrentUS(); | ||
predictor->Run(); | ||
cost_time += (GetCurrentUS() - start); | ||
|
||
std::vector<float> abs_error; | ||
GetModelOutputAndAbsError( | ||
predictor, output_data_set[i], output_data_set_shapes[i], abs_error); | ||
results.push_back(abs_error); | ||
} | ||
|
||
for (float abs_error : {1e-0, 1e-1, 1e-2}) { | ||
float acc = CalOutAccuracy(results, abs_error); | ||
LOG(INFO) << "acc: " << acc << ", if abs_error < " << abs_error; | ||
ASSERT_GE(acc, 0.99); | ||
} | ||
|
||
LOG(INFO) << "================== Speed Report ==================="; | ||
LOG(INFO) << "Model: " << FLAGS_model_dir << ", threads num " << FLAGS_threads | ||
<< ", warmup: " << FLAGS_warmup | ||
<< ", iteration: " << FLAGS_iteration << ", spend " | ||
<< cost_time / FLAGS_iteration / 1000.0 << " ms in average."; | ||
} | ||
|
||
} // namespace lite | ||
} // namespace paddle |
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
141 changes: 141 additions & 0 deletions
141
lite/tests/api/test_ch_ppocr_server_v2_0_det_fp32_v2_3_nnadapter.cc
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,141 @@ | ||
// Copyright (c) 2019 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 <gflags/gflags.h> | ||
#include <gtest/gtest.h> | ||
#include <vector> | ||
#include "lite/api/paddle_api.h" | ||
#include "lite/api/test/lite_api_test_helper.h" | ||
#include "lite/api/test/test_helper.h" | ||
#include "lite/tests/api/ocr_data_utility.h" | ||
#include "lite/tests/api/utility.h" | ||
#include "lite/utils/string.h" | ||
|
||
DEFINE_string(data_dir, "", "data dir"); | ||
DEFINE_int32(iteration, 10, "iteration times to run"); | ||
|
||
namespace paddle { | ||
namespace lite { | ||
|
||
TEST(ch_ppocr_server_v2_0_det, | ||
test_ch_ppocr_server_v2_0_det_fp32_v2_3_nnadapter) { | ||
std::vector<std::string> nnadapter_device_names; | ||
std::string nnadapter_context_properties; | ||
std::vector<paddle::lite_api::Place> valid_places; | ||
valid_places.push_back( | ||
lite_api::Place{TARGET(kNNAdapter), PRECISION(kFloat)}); | ||
#if defined(LITE_WITH_ARM) | ||
valid_places.push_back(lite_api::Place{TARGET(kARM), PRECISION(kFloat)}); | ||
#elif defined(LITE_WITH_X86) | ||
valid_places.push_back(lite_api::Place{TARGET(kX86), PRECISION(kFloat)}); | ||
#else | ||
LOG(INFO) << "Unsupported host arch!"; | ||
return; | ||
#endif | ||
#if defined(NNADAPTER_WITH_HUAWEI_ASCEND_NPU) | ||
nnadapter_device_names.emplace_back("huawei_ascend_npu"); | ||
nnadapter_context_properties = "HUAWEI_ASCEND_NPU_SELECTED_DEVICE_IDS=0"; | ||
#else | ||
LOG(INFO) << "Unsupported NNAdapter device!"; | ||
return; | ||
#endif | ||
std::shared_ptr<paddle::lite_api::PaddlePredictor> predictor = nullptr; | ||
// Use the full api with CxxConfig to generate the optimized model | ||
lite_api::CxxConfig cxx_config; | ||
cxx_config.set_model_dir(FLAGS_model_dir); | ||
cxx_config.set_valid_places(valid_places); | ||
cxx_config.set_nnadapter_device_names(nnadapter_device_names); | ||
cxx_config.set_nnadapter_context_properties(nnadapter_context_properties); | ||
predictor = lite_api::CreatePaddlePredictor(cxx_config); | ||
predictor->SaveOptimizedModel(FLAGS_model_dir, | ||
paddle::lite_api::LiteModelType::kNaiveBuffer); | ||
// Use the light api with MobileConfig to load and run the optimized model | ||
paddle::lite_api::MobileConfig mobile_config; | ||
mobile_config.set_model_from_file(FLAGS_model_dir + ".nb"); | ||
mobile_config.set_threads(FLAGS_threads); | ||
mobile_config.set_power_mode( | ||
static_cast<lite_api::PowerMode>(FLAGS_power_mode)); | ||
mobile_config.set_nnadapter_device_names(nnadapter_device_names); | ||
mobile_config.set_nnadapter_context_properties(nnadapter_context_properties); | ||
predictor = paddle::lite_api::CreatePaddlePredictor(mobile_config); | ||
|
||
std::string raw_data_dir = | ||
FLAGS_data_dir + std::string("/ICDAR_2015_50/raw_data"); | ||
std::string out_data_dir = | ||
FLAGS_data_dir + | ||
std::string("/ICDAR_2015_50/ch_ppocr_mobile_v2_0_out_data"); | ||
std::string images_shape_path = | ||
FLAGS_data_dir + std::string("/ICDAR_2015_50/images_shape.txt"); | ||
|
||
auto input_lines = ReadLines(images_shape_path); | ||
std::vector<std::string> input_names; | ||
std::vector<std::vector<int64_t>> input_shapes; | ||
for (auto line : input_lines) { | ||
input_names.push_back(Split(line, ":")[0]); | ||
input_shapes.push_back(Split<int64_t>(Split(line, ":")[1], " ")); | ||
} | ||
|
||
std::vector<std::vector<float>> raw_data; | ||
std::vector<std::vector<float>> gt_data; | ||
for (size_t i = 0; i < FLAGS_iteration; i++) { | ||
raw_data.push_back( | ||
ReadRawData(raw_data_dir, input_names[i], input_shapes[i])); | ||
} | ||
|
||
FLAGS_warmup = 1; | ||
for (int i = 0; i < FLAGS_warmup; ++i) { | ||
fill_tensor(predictor, 0, raw_data[i].data(), input_shapes[i]); | ||
predictor->Run(); | ||
} | ||
|
||
double cost_time = 0; | ||
std::vector<std::vector<float>> results; | ||
for (size_t i = 0; i < raw_data.size(); ++i) { | ||
fill_tensor(predictor, 0, raw_data[i].data(), input_shapes[i]); | ||
predictor->Run(); | ||
|
||
double start = GetCurrentUS(); | ||
predictor->Run(); | ||
cost_time += (GetCurrentUS() - start); | ||
|
||
auto output_tensor = predictor->GetOutput(0); | ||
auto output_shape = output_tensor->shape(); | ||
auto output_data = output_tensor->data<float>(); | ||
ASSERT_EQ(output_shape.size(), 4UL); | ||
|
||
int64_t output_size = 1; | ||
for (auto dim : output_shape) { | ||
output_size *= dim; | ||
} | ||
std::vector<float> ret(output_size); | ||
memcpy(ret.data(), output_data, sizeof(float) * output_size); | ||
results.push_back(ret); | ||
gt_data.push_back(ReadRawData(out_data_dir, input_names[i], output_shape)); | ||
} | ||
|
||
for (float abs_error : {1e-1, 1e-2, 1e-3, 1e-4}) { | ||
float acc = CalOutAccuracy(results, gt_data, abs_error); | ||
LOG(INFO) << "acc: " << acc << ", if abs_error < " << abs_error; | ||
ASSERT_GE(CalOutAccuracy(results, gt_data, abs_error), 0.99); | ||
} | ||
|
||
LOG(INFO) << "================== Speed Report ==================="; | ||
LOG(INFO) << "Model: " << FLAGS_model_dir << ", threads num " << FLAGS_threads | ||
<< ", warmup: " << FLAGS_warmup | ||
<< ", iteration: " << FLAGS_iteration << ", spend " | ||
<< cost_time / FLAGS_iteration / 1000.0 << " ms in average."; | ||
} | ||
|
||
} // namespace lite | ||
} // namespace paddle |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么把这个check去掉?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
因为新加的检测模型的输入有各种shape大小,现在数据集的raw数据就608x608。后续准备改写成直接读取图片,每个模型单测用例自行做resize和预处理。
而当前检测模型主要是跑通,精度暂时不验证。所以就按上述原因去掉了check。