Skip to content

Commit 81fbd54

Browse files
[Other] Add tests for TIMVX (#1605)
* add tests for timvx * add mobilenetv1 test * update code * fix log info * update log * fix test --------- Co-authored-by: DefTruth <[email protected]>
1 parent b30f62a commit 81fbd54

13 files changed

+762
-1
lines changed

cmake/toolchain.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ if (DEFINED TARGET_ABI)
3535
if(WITH_TIMVX)
3636
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux-aarch64-timvx-20230316.tgz")
3737
else()
38-
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux-arm64-20221209.tgz")
38+
set(PADDLELITE_URL "https://bj.bcebos.com/fastdeploy/third_libs/lite-linux-arm64-20230316.tgz")
3939
endif()
4040
set(THIRD_PARTY_PATH ${CMAKE_CURRENT_BINARY_DIR}/third_libs)
4141
set(OpenCV_DIR ${THIRD_PARTY_PATH}/install/opencv/lib/cmake/opencv4)

tests/CMakeLists.txt

100644100755
+3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ if(WITH_TESTING)
7272
message(STATUS "")
7373
message(STATUS "*************FastDeploy Unittest Summary**********")
7474
file(GLOB_RECURSE ALL_TEST_SRCS ${PROJECT_SOURCE_DIR}/tests/*/test_*.cc)
75+
76+
file(GLOB_RECURSE TIMVX_SRCS ${PROJECT_SOURCE_DIR}/tests/timvx/test_*.cc)
77+
list(REMOVE_ITEM ALL_TEST_SRCS ${TIMVX_SRCS})
7578
if(NOT ENABLE_VISION)
7679
# vision_preprocess and release_task need vision
7780
file(GLOB_RECURSE VISION_TEST_SRCS ${PROJECT_SOURCE_DIR}/tests/vision_preprocess/test_*.cc)

tests/timvx/CMakeLists.txt

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
PROJECT(infer_demo C CXX)
2+
CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
3+
4+
# 指定下载解压后的fastdeploy库路径
5+
option(FASTDEPLOY_INSTALL_DIR "Path of downloaded fastdeploy sdk.")
6+
7+
include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake)
8+
9+
# 添加FastDeploy依赖头文件
10+
include_directories(${FASTDEPLOY_INCS})
11+
include_directories(${FastDeploy_INCLUDE_DIRS})
12+
13+
set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/build/timvx_tests)
14+
15+
# add test for yolov5
16+
add_executable(test_yolov5 ${PROJECT_SOURCE_DIR}/test_yolov5.cc)
17+
target_link_libraries(test_yolov5 ${FASTDEPLOY_LIBS})
18+
install(TARGETS test_yolov5 DESTINATION ./)
19+
20+
# add test for ppyoloe
21+
add_executable(test_ppyoloe ${PROJECT_SOURCE_DIR}/test_ppyoloe.cc)
22+
target_link_libraries(test_ppyoloe ${FASTDEPLOY_LIBS})
23+
install(TARGETS test_ppyoloe DESTINATION ./)
24+
25+
# add test for paddleclas
26+
add_executable(test_clas ${PROJECT_SOURCE_DIR}/test_clas.cc)
27+
target_link_libraries(test_clas ${FASTDEPLOY_LIBS})
28+
install(TARGETS test_clas DESTINATION ./)
29+
30+
# add test for pp-liteseg
31+
add_executable(test_ppliteseg ${PROJECT_SOURCE_DIR}/test_ppliteseg.cc)
32+
target_link_libraries(test_ppliteseg ${FASTDEPLOY_LIBS})
33+
install(TARGETS test_ppliteseg DESTINATION ./)
34+
35+
36+
install(DIRECTORY models DESTINATION ./)
37+
install(DIRECTORY images DESTINATION ./)
38+
install(DIRECTORY results DESTINATION ./)
39+
40+
file(GLOB RUN_TEST run_test.sh)
41+
install(PROGRAMS ${RUN_TEST} DESTINATION ./)
42+
43+
file(GLOB_RECURSE FASTDEPLOY_LIBS ${FASTDEPLOY_INSTALL_DIR}/lib/lib*.so*)
44+
file(GLOB_RECURSE ALL_LIBS ${FASTDEPLOY_INSTALL_DIR}/third_libs/install/lib*.so*)
45+
list(APPEND ALL_LIBS ${FASTDEPLOY_LIBS})
46+
install(PROGRAMS ${ALL_LIBS} DESTINATION lib)

tests/timvx/common.h

+242
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
#pragma once
15+
#include <fstream>
16+
#include "fastdeploy/vision.h"
17+
18+
std::vector<std::string> stringSplit(const std::string& str, char delim) {
19+
std::stringstream ss(str);
20+
std::string item;
21+
std::vector<std::string> elems;
22+
while (std::getline(ss, item, delim)) {
23+
if (!item.empty()) {
24+
elems.push_back(item);
25+
}
26+
}
27+
return elems;
28+
}
29+
30+
31+
bool CompareDetResult(const fastdeploy::vision::DetectionResult& res,
32+
const std::string& det_result_file) {
33+
std::ifstream res_str(det_result_file);
34+
if (!res_str.is_open()) {
35+
std::cout<< "Could not open detect result file : "
36+
<< det_result_file <<"\n"<< std::endl;
37+
return false;
38+
}
39+
int obj_num = 0;
40+
while (!res_str.eof()) {
41+
std::string line;
42+
std::getline(res_str, line);
43+
if (line.find("DetectionResult") == line.npos
44+
&& line.find(",") != line.npos ) {
45+
auto strs = stringSplit(line, ',');
46+
if (strs.size() != 6) {
47+
std::cout<< "Failed to parse result file : "
48+
<< det_result_file <<"\n"<< std::endl;
49+
return false;
50+
}
51+
std::vector<float> vals;
52+
for (auto str : strs) {
53+
vals.push_back(atof(str.c_str()));
54+
}
55+
if (abs(res.scores[obj_num] - vals[4]) > 0.3) {
56+
std::cout<< "Score error, the result is: "
57+
<< res.scores[obj_num] << " but the expected is: "
58+
<< vals[4] << std::endl;
59+
return false;
60+
}
61+
if (abs(res.label_ids[obj_num] - vals[5]) > 0) {
62+
std::cout<< "label error, the result is: "
63+
<< res.label_ids[obj_num] << " but the expected is: "
64+
<< vals[5] <<std::endl;
65+
return false;
66+
}
67+
std::array<float, 4> boxes = res.boxes[obj_num++];
68+
for (auto i = 0; i < 4; i++) {
69+
if (abs(boxes[i] - vals[i]) > 5) {
70+
std::cout<< "position error, the result is: "
71+
<< boxes[i] << " but the expected is: " << vals[i] <<std::endl;
72+
return false;
73+
}
74+
}
75+
}
76+
}
77+
return true;
78+
}
79+
80+
81+
bool CompareClsResult(const fastdeploy::vision::ClassifyResult& res,
82+
const std::string& cls_result_file) {
83+
std::ifstream res_str(cls_result_file);
84+
if (!res_str.is_open()) {
85+
std::cout<< "Could not open detect result file : "
86+
<< cls_result_file << "\n" << std::endl;
87+
return false;
88+
}
89+
int obj_num = 0;
90+
while (!res_str.eof()) {
91+
std::string line;
92+
std::getline(res_str, line);
93+
if (line.find("label_ids") != line.npos
94+
&& line.find(":") != line.npos) {
95+
auto strs = stringSplit(line, ':');
96+
if (strs.size() != 2) {
97+
std::cout<< "Failed to parse result file : "
98+
<< cls_result_file <<"\n"<< std::endl;
99+
return false;
100+
}
101+
int32_t label = static_cast<int32_t>(atof(strs[1].c_str()));
102+
if (res.label_ids[obj_num] != label) {
103+
std::cout<< "label error, the result is: "
104+
<< res.label_ids[obj_num] << " but the expected is: "
105+
<< label<< "\n" << std::endl;
106+
return false;
107+
}
108+
} else if (line.find("scores") != line.npos
109+
&& line.find(":") != line.npos) {
110+
auto strs = stringSplit(line, ':');
111+
if (strs.size() != 2) {
112+
std::cout<< "Failed to parse result file : "
113+
<< cls_result_file << "\n" << std::endl;
114+
return false;
115+
}
116+
float score = atof(strs[1].c_str());
117+
if (abs(res.scores[obj_num] - score) > 1e-1) {
118+
std::cout << "score error, the result is: "
119+
<< res.scores[obj_num] << " but the expected is: "
120+
<< score << "\n" << std::endl;
121+
return false;
122+
} else {
123+
obj_num++;
124+
}
125+
} else if (line.size()) {
126+
std::cout << "Unknown File. \n" << std::endl;
127+
return false;
128+
}
129+
}
130+
return true;
131+
}
132+
133+
bool WriteSegResult(const fastdeploy::vision::SegmentationResult& res,
134+
const std::string& seg_result_file) {
135+
std::ofstream res_str(seg_result_file);
136+
if (!res_str.is_open()) {
137+
std::cerr<< "Could not open segmentation result file : "
138+
<< seg_result_file <<" to write.\n"<< std::endl;
139+
return false;
140+
}
141+
std::string out;
142+
out = "";
143+
// save shape
144+
for (auto shape : res.shape) {
145+
out += std::to_string(shape) + ",";
146+
}
147+
out += "\n";
148+
// save label
149+
for (auto label : res.label_map) {
150+
out += std::to_string(label) + ",";
151+
}
152+
out += "\n";
153+
// save score
154+
if (res.contain_score_map) {
155+
for (auto score : res.score_map) {
156+
out += std::to_string(score) + ",";
157+
}
158+
}
159+
res_str << out;
160+
return true;
161+
}
162+
163+
bool CompareSegResult(const fastdeploy::vision::SegmentationResult& res,
164+
const std::string& seg_result_file) {
165+
std::ifstream res_str(seg_result_file);
166+
if (!res_str.is_open()) {
167+
std::cout<< "Could not open detect result file : "
168+
<< seg_result_file <<"\n"<< std::endl;
169+
return false;
170+
}
171+
std::string line;
172+
std::getline(res_str, line);
173+
if (line.find(",") == line.npos) {
174+
std::cout << "Unexpected File." << std::endl;
175+
return false;
176+
}
177+
// check shape diff
178+
auto shape_strs = stringSplit(line, ',');
179+
std::vector<int64_t> shape;
180+
for (auto str : shape_strs) {
181+
shape.push_back(static_cast<int64_t>(atof(str.c_str())));
182+
}
183+
if (shape.size() != res.shape.size()) {
184+
std::cout << "Output shape and expected shape size mismatch, shape size: "
185+
<< res.shape.size() << " expected shape size: "
186+
<< shape.size() << std::endl;
187+
return false;
188+
}
189+
for (auto i = 0; i < res.shape.size(); i++) {
190+
if (res.shape[i] != shape[i]) {
191+
std::cout << "Output Shape and expected shape mismatch, shape: "
192+
<< res.shape[i] << " expected: " << shape[i] << std::endl;
193+
return false;
194+
}
195+
}
196+
std::cout << "Shape check passed!" << std::endl;
197+
198+
std::getline(res_str, line);
199+
if (line.find(",") == line.npos) {
200+
std::cout << "Unexpected File." << std::endl;
201+
return false;
202+
}
203+
// check label
204+
auto label_strs = stringSplit(line, ',');
205+
std::vector<uint8_t> labels;
206+
for (auto str : label_strs) {
207+
labels.push_back(static_cast<uint8_t>(atof(str.c_str())));
208+
}
209+
if (labels.size() != res.label_map.size()) {
210+
std::cout << "Output labels and expected shape size mismatch." << std::endl;
211+
return false;
212+
}
213+
for (auto i = 0; i < res.label_map.size(); i++) {
214+
if (res.label_map[i] != labels[i]) {
215+
std::cout << "Output labels and expected labels mismatch." << std::endl;
216+
return false;
217+
}
218+
}
219+
std::cout << "Label check passed!" << std::endl;
220+
221+
// check score_map
222+
if (res.contain_score_map) {
223+
auto scores_strs = stringSplit(line, ',');
224+
std::vector<float> scores;
225+
for (auto str : scores_strs) {
226+
scores.push_back(static_cast<float>(atof(str.c_str())));
227+
}
228+
if (scores.size() != res.score_map.size()) {
229+
std::cout << "Output scores and expected score_map size mismatch."
230+
<<std::endl;
231+
return false;
232+
}
233+
for (auto i = 0; i < res.score_map.size(); i++) {
234+
if (abs(res.score_map[i] - scores[i]) > 3e-1) {
235+
std::cout << "Output scores and expected scores mismatch."
236+
<< std::endl;
237+
return false;
238+
}
239+
}
240+
}
241+
return true;
242+
}

0 commit comments

Comments
 (0)