This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_onnx.cpp
86 lines (73 loc) · 2.55 KB
/
test_onnx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <vector>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/core/utils/logger.hpp>
using namespace std;
using namespace cv;
#define _DEBUG
/// @brief A utility function to print out the elements of vector
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
os << "[ ";
std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(os, " "));
os << " ]";
return os;
}
bool test_onnx(string modelFileName, vector<int> inputShape);
int main() {
std::pair<string, vector<int>> attr("attr_b2.onnx",
vector<int>{ 2, 3, 256, 128});
std::pair<string, vector<int>> pose("pose.onnx",
vector<int>{ 5, 3, 192, 256 });
std::pair<string, vector<int>> act("act_b8.onnx",
vector<int>{ 8, 1, 17, 48, 64, 64 });
vector <std::pair<string, vector<int>>> fileWithInputShape{ attr, pose, act };
vector<std::pair<string, bool>> testResults;
for (const auto& pair : fileWithInputShape) {
string prefix = "C:/Users/ha/Desktop/test_onnx/";
string fileOnxx = prefix + pair.first;
vector<int> inputShape = pair.second;
cout << endl << "***********************************************" << endl;
cout << "***TEST -" << fileOnxx << "-********" << endl;
bool modelResult = test_onnx(fileOnxx, inputShape);
testResults.push_back(std::make_pair(fileOnxx, modelResult));
}
cout << endl << "***************FINAL RESULTS********************";
for (const auto& test : testResults) {
string model = test.first;
string result = test.second ? "Succeeded" : "Failed";
cout << endl << model << ":\t" << result;
}
cout << endl << "***************END RESULTS********************" << endl;
return 0;
};
bool test_onnx(string fileOnnx, vector<int> inputShape) {
//cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_VERBOSE);
try {
dnn::Net net;
try {
net = dnn::readNet(fileOnnx);
cout << "readNet(" << fileOnnx << ")" << endl;
}
catch (cv::Exception e) {
cout << "Load ONNX failed" << endl;
return false;
}
net.setPreferableBackend(dnn::DNN_BACKEND_CUDA);
net.setPreferableTarget(dnn::DNN_TARGET_CUDA);
Mat blobInput = Mat::ones(inputShape.size(), inputShape.data(), CV_32F);
cout << endl << "---------------------------------" << endl;
cout << "blobInput(" << blobInput.size << ")" << endl;
net.setInput(blobInput);
Mat out = net.forward();
cout << "netOut1st(" << out.size << ")" << endl;
cout << "---------------------------------" << endl;
return true;
}
catch (cv::Exception e) {
cout << "ERROR:" << e.msg << endl;
return false;
}
}