-
Notifications
You must be signed in to change notification settings - Fork 2
/
load_trt_engine.cpp
95 lines (86 loc) · 2.68 KB
/
load_trt_engine.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
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include "NvInfer.h"
#include "NvUtils.h"
#include "flattenConcatCustom.h"
using namespace std;
using namespace nvinfer1;
class Logger : public nvinfer1::ILogger
{
public:
Logger(Severity severity = Severity::kINFO)
: reportableSeverity(severity)
{
}
void log(Severity severity, const char* msg) override
{
// suppress messages with severity enum value greater than the reportable
if (severity > reportableSeverity)
return;
switch (severity)
{
case Severity::kINTERNAL_ERROR: break;
case Severity::kERROR: break;
case Severity::kWARNING: break;
case Severity::kINFO: break;
default:break;
}
std::cerr << msg << std::endl;
}
Severity reportableSeverity;
};
static Logger gLogger;
int main(int argc, char ** argv){
std::cout<<"To read engine" << endl;
std::ifstream ins;
ins.open(argv[1], std::ofstream::binary);
ins.seekg(0, std::ios::end);
int slength = ins.tellg();
ins.seekg(0, std::ios::beg);
char* sbuffer = new char[slength];
ins.read(sbuffer, sizeof(char)*slength);
ins.close();
std::cout << "Read engine file " << argv[1] << ", size " << slength << endl;
IRuntime* runtime = createInferRuntime(gLogger);
ICudaEngine* engine = runtime->deserializeCudaEngine(sbuffer, slength, nullptr);
std::cout<< "engine pointer " << engine << "batch size " << engine->getMaxBatchSize()<<endl;
vector<vector<int>> inputs_shape;
vector<vector<int>> outputs_shape;
int nb_bindings = engine->getNbBindings();
std::cout<<"nb bindings" << nb_bindings << endl;
for (int i = 0; i < nb_bindings; i++) {
std::cout<<"index binding " << i << endl;
auto dims = engine->getBindingDimensions(i);
if (engine->bindingIsInput(i)) {
vector<int> shapes;
for (int j = 0; j < dims.nbDims; ++j) {
shapes.push_back(dims.d[j]);
//std::cout<<dims.d[j]<<endl;
}
inputs_shape.push_back(shapes);
} else {
vector<int> shapes;
for (int j = 0; j < dims.nbDims; ++j) {
shapes.push_back(dims.d[j]);
}
outputs_shape.push_back(shapes);
}
}
std::cout << "input shapes" << endl;
for(auto vec: inputs_shape){
for(auto v: vec){
std::cout << v << " ";
}
std::cout << endl;
}
std::cout << "output shapes" << endl;
for(auto vec: outputs_shape){
for(auto v: vec){
std::cout << v << " ";
}
std::cout << endl;
}
return 0;
}