Skip to content

Commit

Permalink
[Other] Add tests for TIMVX (PaddlePaddle#1605)
Browse files Browse the repository at this point in the history
* add tests for timvx

* add mobilenetv1 test

* update code

* fix log info

* update log

* fix test

---------

Co-authored-by: DefTruth <[email protected]>
  • Loading branch information
yeliang2258 and DefTruth authored Apr 14, 2023
1 parent b30f62a commit 81fbd54
Show file tree
Hide file tree
Showing 13 changed files with 762 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmake/toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ if (DEFINED TARGET_ABI)
if(WITH_TIMVX)
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux-aarch64-timvx-20230316.tgz")
else()
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux-arm64-20221209.tgz")
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux-arm64-20230316.tgz")
endif()
set(THIRD_PARTY_PATH ${CMAKE_CURRENT_BINARY_DIR}/third_libs)
set(OpenCV_DIR ${THIRD_PARTY_PATH}/install/opencv/lib/cmake/opencv4)
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ if(WITH_TESTING)
message(STATUS "")
message(STATUS "*************FastDeploy Unittest Summary**********")
file(GLOB_RECURSE ALL_TEST_SRCS ${PROJECT_SOURCE_DIR}/tests/*/test_*.cc)

file(GLOB_RECURSE TIMVX_SRCS ${PROJECT_SOURCE_DIR}/tests/timvx/test_*.cc)
list(REMOVE_ITEM ALL_TEST_SRCS ${TIMVX_SRCS})
if(NOT ENABLE_VISION)
# vision_preprocess and release_task need vision
file(GLOB_RECURSE VISION_TEST_SRCS ${PROJECT_SOURCE_DIR}/tests/vision_preprocess/test_*.cc)
Expand Down
46 changes: 46 additions & 0 deletions tests/timvx/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
PROJECT(infer_demo C CXX)
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)

# 指定下载解压后的fastdeploy库路径
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")

include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)

# 添加FastDeploy依赖头文件
include_directories(${FASTDEPLOY_INCS})
include_directories(${FastDeploy_INCLUDE_DIRS})

set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build/timvx_tests)

# add test for yolov5
add_executable(test_yolov5 ${PROJECT_SOURCE_DIR}/test_yolov5.cc)
target_link_libraries(test_yolov5 ${FASTDEPLOY_LIBS})
install(TARGETS test_yolov5 DESTINATION ./)

# add test for ppyoloe
add_executable(test_ppyoloe ${PROJECT_SOURCE_DIR}/test_ppyoloe.cc)
target_link_libraries(test_ppyoloe ${FASTDEPLOY_LIBS})
install(TARGETS test_ppyoloe DESTINATION ./)

# add test for paddleclas
add_executable(test_clas ${PROJECT_SOURCE_DIR}/test_clas.cc)
target_link_libraries(test_clas ${FASTDEPLOY_LIBS})
install(TARGETS test_clas DESTINATION ./)

# add test for pp-liteseg
add_executable(test_ppliteseg ${PROJECT_SOURCE_DIR}/test_ppliteseg.cc)
target_link_libraries(test_ppliteseg ${FASTDEPLOY_LIBS})
install(TARGETS test_ppliteseg DESTINATION ./)


install(DIRECTORY models DESTINATION ./)
install(DIRECTORY images DESTINATION ./)
install(DIRECTORY results DESTINATION ./)

file(GLOB RUN_TEST run_test.sh)
install(PROGRAMS ${RUN_TEST} DESTINATION ./)

file(GLOB_RECURSE FASTDEPLOY_LIBS ${FASTDEPLOY_INSTALL_DIR}/lib/lib*.so*)
file(GLOB_RECURSE ALL_LIBS ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/lib*.so*)
list(APPEND ALL_LIBS ${FASTDEPLOY_LIBS})
install(PROGRAMS ${ALL_LIBS} DESTINATION lib)
242 changes: 242 additions & 0 deletions tests/timvx/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// 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 <fstream>
#include "fastdeploy/vision.h"

std::vector<std::string> stringSplit(const std::string& str, char delim) {
std::stringstream ss(str);
std::string item;
std::vector<std::string> elems;
while (std::getline(ss, item, delim)) {
if (!item.empty()) {
elems.push_back(item);
}
}
return elems;
}


bool CompareDetResult(const fastdeploy::vision::DetectionResult& res,
const std::string& det_result_file) {
std::ifstream res_str(det_result_file);
if (!res_str.is_open()) {
std::cout<< "Could not open detect result file : "
<< det_result_file <<"\n"<< std::endl;
return false;
}
int obj_num = 0;
while (!res_str.eof()) {
std::string line;
std::getline(res_str, line);
if (line.find("DetectionResult") == line.npos
&& line.find(",") != line.npos ) {
auto strs = stringSplit(line, ',');
if (strs.size() != 6) {
std::cout<< "Failed to parse result file : "
<< det_result_file <<"\n"<< std::endl;
return false;
}
std::vector<float> vals;
for (auto str : strs) {
vals.push_back(atof(str.c_str()));
}
if (abs(res.scores[obj_num] - vals[4]) > 0.3) {
std::cout<< "Score error, the result is: "
<< res.scores[obj_num] << " but the expected is: "
<< vals[4] << std::endl;
return false;
}
if (abs(res.label_ids[obj_num] - vals[5]) > 0) {
std::cout<< "label error, the result is: "
<< res.label_ids[obj_num] << " but the expected is: "
<< vals[5] <<std::endl;
return false;
}
std::array<float, 4> boxes = res.boxes[obj_num++];
for (auto i = 0; i < 4; i++) {
if (abs(boxes[i] - vals[i]) > 5) {
std::cout<< "position error, the result is: "
<< boxes[i] << " but the expected is: " << vals[i] <<std::endl;
return false;
}
}
}
}
return true;
}


bool CompareClsResult(const fastdeploy::vision::ClassifyResult& res,
const std::string& cls_result_file) {
std::ifstream res_str(cls_result_file);
if (!res_str.is_open()) {
std::cout<< "Could not open detect result file : "
<< cls_result_file << "\n" << std::endl;
return false;
}
int obj_num = 0;
while (!res_str.eof()) {
std::string line;
std::getline(res_str, line);
if (line.find("label_ids") != line.npos
&& line.find(":") != line.npos) {
auto strs = stringSplit(line, ':');
if (strs.size() != 2) {
std::cout<< "Failed to parse result file : "
<< cls_result_file <<"\n"<< std::endl;
return false;
}
int32_t label = static_cast<int32_t>(atof(strs[1].c_str()));
if (res.label_ids[obj_num] != label) {
std::cout<< "label error, the result is: "
<< res.label_ids[obj_num] << " but the expected is: "
<< label<< "\n" << std::endl;
return false;
}
} else if (line.find("scores") != line.npos
&& line.find(":") != line.npos) {
auto strs = stringSplit(line, ':');
if (strs.size() != 2) {
std::cout<< "Failed to parse result file : "
<< cls_result_file << "\n" << std::endl;
return false;
}
float score = atof(strs[1].c_str());
if (abs(res.scores[obj_num] - score) > 1e-1) {
std::cout << "score error, the result is: "
<< res.scores[obj_num] << " but the expected is: "
<< score << "\n" << std::endl;
return false;
} else {
obj_num++;
}
} else if (line.size()) {
std::cout << "Unknown File. \n" << std::endl;
return false;
}
}
return true;
}

bool WriteSegResult(const fastdeploy::vision::SegmentationResult& res,
const std::string& seg_result_file) {
std::ofstream res_str(seg_result_file);
if (!res_str.is_open()) {
std::cerr<< "Could not open segmentation result file : "
<< seg_result_file <<" to write.\n"<< std::endl;
return false;
}
std::string out;
out = "";
// save shape
for (auto shape : res.shape) {
out += std::to_string(shape) + ",";
}
out += "\n";
// save label
for (auto label : res.label_map) {
out += std::to_string(label) + ",";
}
out += "\n";
// save score
if (res.contain_score_map) {
for (auto score : res.score_map) {
out += std::to_string(score) + ",";
}
}
res_str << out;
return true;
}

bool CompareSegResult(const fastdeploy::vision::SegmentationResult& res,
const std::string& seg_result_file) {
std::ifstream res_str(seg_result_file);
if (!res_str.is_open()) {
std::cout<< "Could not open detect result file : "
<< seg_result_file <<"\n"<< std::endl;
return false;
}
std::string line;
std::getline(res_str, line);
if (line.find(",") == line.npos) {
std::cout << "Unexpected File." << std::endl;
return false;
}
// check shape diff
auto shape_strs = stringSplit(line, ',');
std::vector<int64_t> shape;
for (auto str : shape_strs) {
shape.push_back(static_cast<int64_t>(atof(str.c_str())));
}
if (shape.size() != res.shape.size()) {
std::cout << "Output shape and expected shape size mismatch, shape size: "
<< res.shape.size() << " expected shape size: "
<< shape.size() << std::endl;
return false;
}
for (auto i = 0; i < res.shape.size(); i++) {
if (res.shape[i] != shape[i]) {
std::cout << "Output Shape and expected shape mismatch, shape: "
<< res.shape[i] << " expected: " << shape[i] << std::endl;
return false;
}
}
std::cout << "Shape check passed!" << std::endl;

std::getline(res_str, line);
if (line.find(",") == line.npos) {
std::cout << "Unexpected File." << std::endl;
return false;
}
// check label
auto label_strs = stringSplit(line, ',');
std::vector<uint8_t> labels;
for (auto str : label_strs) {
labels.push_back(static_cast<uint8_t>(atof(str.c_str())));
}
if (labels.size() != res.label_map.size()) {
std::cout << "Output labels and expected shape size mismatch." << std::endl;
return false;
}
for (auto i = 0; i < res.label_map.size(); i++) {
if (res.label_map[i] != labels[i]) {
std::cout << "Output labels and expected labels mismatch." << std::endl;
return false;
}
}
std::cout << "Label check passed!" << std::endl;

// check score_map
if (res.contain_score_map) {
auto scores_strs = stringSplit(line, ',');
std::vector<float> scores;
for (auto str : scores_strs) {
scores.push_back(static_cast<float>(atof(str.c_str())));
}
if (scores.size() != res.score_map.size()) {
std::cout << "Output scores and expected score_map size mismatch."
<<std::endl;
return false;
}
for (auto i = 0; i < res.score_map.size(); i++) {
if (abs(res.score_map[i] - scores[i]) > 3e-1) {
std::cout << "Output scores and expected scores mismatch."
<< std::endl;
return false;
}
}
}
return true;
}
Loading

0 comments on commit 81fbd54

Please sign in to comment.