-
Notifications
You must be signed in to change notification settings - Fork 4
/
tensorNet.h
75 lines (58 loc) · 1.94 KB
/
tensorNet.h
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
#include "pluginImplement.h"
using namespace nvinfer1;
using namespace nvcaffeparser1;
/******************************/
// TensorRT utility
/******************************/
class Logger : public ILogger
{
void log(Severity severity, const char* msg) override
{
if (severity!=Severity::kINFO) std::cout << msg << std::endl;
}
};
struct Profiler : public IProfiler
{
typedef std::pair<std::string, float> Record;
std::vector<Record> mProfile;
virtual void reportLayerTime(const char* layerName, float ms)
{
auto record = std::find_if(mProfile.begin(), mProfile.end(), [&](const Record& r){ return r.first == layerName; });
if (record == mProfile.end()) mProfile.push_back(std::make_pair(layerName, ms));
else record->second += ms;
}
void printLayerTimes(const int TIMING_ITERATIONS)
{
float totalTime = 0;
for (size_t i = 0; i < mProfile.size(); i++)
{
printf("%-40.40s %4.3fms\n", mProfile[i].first.c_str(), mProfile[i].second / TIMING_ITERATIONS);
totalTime += mProfile[i].second;
}
printf("Time over all layers: %4.3f\n", totalTime / TIMING_ITERATIONS);
}
};
/******************************/
// TensorRT Main
/******************************/
class TensorNet
{
public:
void caffeToTRTModel(const std::string& deployFile,
const std::string& modelFile,
const std::vector<std::string>& outputs,
unsigned int maxBatchSize);
void createInference();
void imageInference(void** buffers, int nbBuffer, int batchSize);
void timeInference(int iteration, int batchSize);
DimsCHW getTensorDims(std::string name);
void printTimes(int iteration);
void destroy();
private:
PluginFactory pluginFactory;
IHostMemory *gieModelStream{nullptr};
IRuntime* infer;
ICudaEngine* engine;
Logger gLogger;
Profiler gProfiler;
};