forked from chenzhi1992/TensorRT-SSD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_parse_two_model.txt
81 lines (68 loc) · 3.57 KB
/
sample_parse_two_model.txt
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
// It is a sample that loading two models or many models in TensorRT.
// Related websites: https://devtalk.nvidia.com/default/topic/1028003/jetson-tx2/why-tensorrt-can-t-parse-two-model-simultaneously-/
// the sample code:
void caffeToGIEModel(const std::string& deployFile, // name for caffe prototxt
const std::string& modelFile, // name for model
const std::string& deployFile2, // name for caffe prototxt
const std::string& modelFile2, // name for model
const std::vector<std::string>& outputs, // network outputs
unsigned int maxBatchSize, // batch size - NB must be at least as large as the batch we want to run with)
nvcaffeparser1::IPluginFactory* pluginFactory, // factory for plugin layers
IHostMemory **gieModelStream,
nvcaffeparser1::IPluginFactory* pluginFactory2, // factory for plugin layers
IHostMemory **gieModelStream2) // output stream for the GIE model
{
// create the builder
IBuilder* builder = createInferBuilder(gLogger);
IBuilder* builder2 = createInferBuilder(gLogger);
INetworkDefinition* network = builder->createNetwork();
INetworkDefinition* network2 = builder->createNetwork();
ICaffeParser* parser = createCaffeParser();
ICaffeParser* parser2 = createCaffeParser();
parser->setPluginFactory(pluginFactory);
parser2->setPluginFactory(pluginFactory2);
std::cout << "Begin parsing model1..." << std::endl;
const IBlobNameToTensor* blobNameToTensor = parser->parse(deployFile.c_str(),
modelFile.c_str(),
*network,
DataType::kFLOAT);
std::cout << "End parsing model1..." << std::endl;
// specify which tensors are outputs
for (auto& s : outputs)
network->markOutput(*blobNameToTensor->find(s.c_str()));
// Build the engine
builder->setMaxBatchSize(maxBatchSize);
builder->setMaxWorkspaceSize(16 << 20);
std::cout << "Begin building engine1..." << std::endl;
ICudaEngine* engine = builder->buildCudaEngine(*network);
assert(engine);
std::cout << "End building engine1..." << std::endl;
network->destroy();
parser->destroy();
(*gieModelStream) = engine->serialize();
std::cout << "Begin parsing model2..." << std::endl;
const IBlobNameToTensor* blobNameToTensor2 = parser2->parse(deployFile.c_str(),
modelFile.c_str(),
*network2,
DataType::kHALF);
builder2->setHalf2Mode(true);
std::cout << "End parsing model2..." << std::endl;
// specify which tensors are outputs
for (auto& s : outputs)
network2->markOutput(*blobNameToTensor2->find(s.c_str()));
// Build the engine
builder2->setMaxBatchSize(maxBatchSize);
builder2->setMaxWorkspaceSize(16 << 20);
std::cout << "Begin building engine2..." << std::endl;
ICudaEngine* engine2 = builder2->buildCudaEngine(*network2);
assert(engine2);
std::cout << "End building engine2..." << std::endl;
network2->destroy();
parser2->destroy();
(*gieModelStream2) = engine2->serialize();
engine->destroy();
engine2->destroy();
builder->destroy();
builder2->destroy();
shutdownProtobufLibrary();
}