33//
44
55#include < assert.h>
6- #include < vector>
76#include < onnxruntime_cxx_api.h>
87
8+ #include < iostream>
9+ #include < vector>
10+
911#ifdef HAVE_TENSORRT_PROVIDER_FACTORY_H
1012#include < tensorrt_provider_factory.h>
1113#include < tensorrt_provider_options.h>
@@ -60,13 +62,15 @@ void run_ort_trt() {
6062
6163 // **************************************************************************************************************************
6264 // It's suggested to use CreateTensorRTProviderOptions() to get provider options
63- // since ORT takes care of valid options for you
65+ // since ORT takes care of valid options for you
6466 // **************************************************************************************************************************
6567 api.CreateTensorRTProviderOptions (&tensorrt_options);
66- std::unique_ptr<OrtTensorRTProviderOptionsV2, decltype (api.ReleaseTensorRTProviderOptions )> rel_trt_options (tensorrt_options, api.ReleaseTensorRTProviderOptions );
67- api.SessionOptionsAppendExecutionProvider_TensorRT_V2 (static_cast <OrtSessionOptions*>(session_options), rel_trt_options.get ());
68+ std::unique_ptr<OrtTensorRTProviderOptionsV2, decltype (api.ReleaseTensorRTProviderOptions )> rel_trt_options (
69+ tensorrt_options, api.ReleaseTensorRTProviderOptions );
70+ api.SessionOptionsAppendExecutionProvider_TensorRT_V2 (static_cast <OrtSessionOptions*>(session_options),
71+ rel_trt_options.get ());
6872
69- printf ( " Runing ORT TRT EP with default provider options\n " ) ;
73+ std::cout << " Running ORT TRT EP with default provider options" << std::endl ;
7074
7175 Ort::Session session (env, model_path, session_options);
7276
@@ -75,60 +79,69 @@ void run_ort_trt() {
7579 Ort::AllocatorWithDefaultOptions allocator;
7680
7781 // print number of model input nodes
78- size_t num_input_nodes = session.GetInputCount ();
79- std::vector<const char *> input_node_names (num_input_nodes);
82+ const size_t num_input_nodes = session.GetInputCount ();
83+ std::vector<Ort::AllocatedStringPtr> input_names_ptr;
84+ std::vector<const char *> input_node_names;
85+ input_names_ptr.reserve (num_input_nodes);
86+ input_node_names.reserve (num_input_nodes);
8087 std::vector<int64_t > input_node_dims; // simplify... this model has only 1 input node {1, 3, 224, 224}.
8188 // Otherwise need vector<vector<>>
8289
83- printf ( " Number of inputs = %zu \n " , num_input_nodes) ;
90+ std::cout << " Number of inputs = " << num_input_nodes << std::endl ;
8491
8592 // iterate over all input nodes
86- for (int i = 0 ; i < num_input_nodes; i++) {
93+ for (size_t i = 0 ; i < num_input_nodes; i++) {
8794 // print input node names
88- char * input_name = session.GetInputName (i, allocator);
89- printf (" Input %d : name=%s\n " , i, input_name);
90- input_node_names[i] = input_name;
95+ auto input_name = session.GetInputNameAllocated (i, allocator);
96+ std::cout << " Input " << i << " : name =" << input_name.get () << std::endl;
97+ input_node_names.push_back (input_name.get ());
98+ input_names_ptr.push_back (std::move (input_name));
9199
92100 // print input node types
93- Ort::TypeInfo type_info = session.GetInputTypeInfo (i);
101+ auto type_info = session.GetInputTypeInfo (i);
94102 auto tensor_info = type_info.GetTensorTypeAndShapeInfo ();
95103
96104 ONNXTensorElementDataType type = tensor_info.GetElementType ();
97- printf ( " Input %d : type=%d \n " , i, type) ;
105+ std::cout << " Input " << i << " : type = " << type << std::endl ;
98106
99107 // print input shapes/dims
100108 input_node_dims = tensor_info.GetShape ();
101- printf (" Input %d : num_dims=%zu\n " , i, input_node_dims.size ());
102- for (size_t j = 0 ; j < input_node_dims.size (); j++)
103- printf (" Input %d : dim %zu=%jd\n " , i, j, input_node_dims[j]);
109+ std::cout << " Input " << i << " : num_dims = " << input_node_dims.size () << ' \n ' ;
110+ for (size_t j = 0 ; j < input_node_dims.size (); j++) {
111+ std::cout << " Input " << i << " : dim[" << j << " ] =" << input_node_dims[j] << ' \n ' ;
112+ }
113+ std::cout << std::flush;
104114 }
105115
106- size_t input_tensor_size = 224 * 224 * 3 ; // simplify ... using known dim values to calculate size
107- // use OrtGetTensorShapeElementCount() to get official size!
116+ constexpr size_t input_tensor_size = 224 * 224 * 3 ; // simplify ... using known dim values to calculate size
117+ // use OrtGetTensorShapeElementCount() to get official size!
108118
109119 std::vector<float > input_tensor_values (input_tensor_size);
110120 std::vector<const char *> output_node_names = {" softmaxout_1" };
111121
112122 // initialize input data with values in [0.0, 1.0]
113- for (unsigned int i = 0 ; i < input_tensor_size; i++)
114- input_tensor_values[i] = (float )i / (input_tensor_size + 1 );
123+ for (unsigned int i = 0 ; i < input_tensor_size; i++) input_tensor_values[i] = (float )i / (input_tensor_size + 1 );
115124
116125 // create input tensor object from data values
117126 auto memory_info = Ort::MemoryInfo::CreateCpu (OrtArenaAllocator, OrtMemTypeDefault);
118- Ort::Value input_tensor = Ort::Value::CreateTensor<float >(memory_info, input_tensor_values.data (), input_tensor_size, input_node_dims.data (), 4 );
127+ auto input_tensor = Ort::Value::CreateTensor<float >(memory_info, input_tensor_values.data (), input_tensor_size,
128+ input_node_dims.data (), 4 );
119129 assert (input_tensor.IsTensor ());
120130
121131 // score model & input tensor, get back output tensor
122- auto output_tensors = session.Run (Ort::RunOptions{nullptr }, input_node_names.data (), &input_tensor, 1 , output_node_names.data (), 1 );
132+ auto output_tensors =
133+ session.Run (Ort::RunOptions{nullptr }, input_node_names.data (), &input_tensor, 1 , output_node_names.data (), 1 );
123134 assert (output_tensors.size () == 1 && output_tensors.front ().IsTensor ());
124135
125136 // Get pointer to output tensor float values
126137 float * floatarr = output_tensors.front ().GetTensorMutableData <float >();
127138 assert (abs (floatarr[0 ] - 0.000045 ) < 1e-6 );
128139
129140 // score the model, and print scores for first 5 classes
130- for (int i = 0 ; i < 5 ; i++)
131- printf (" Score for class [%d] = %f\n " , i, floatarr[i]);
141+ for (int i = 0 ; i < 5 ; i++) {
142+ std::cout << " Score for class [" << i << " ] = " << floatarr[i] << ' \n ' ;
143+ }
144+ std::cout << std::flush;
132145
133146 // Results should be as below...
134147 // Score for class[0] = 0.000045
@@ -137,15 +150,10 @@ void run_ort_trt() {
137150 // Score for class[3] = 0.001180
138151 // Score for class[4] = 0.001317
139152
140-
141- // release buffers allocated by ORT alloctor
142- for (const char * node_name : input_node_names)
143- allocator.Free (const_cast <void *>(reinterpret_cast <const void *>(node_name)));
144-
145- printf (" Done!\n " );
153+ std::cout << " Done!" << std::endl;
146154}
147155
148- int main (int argc, char * argv []) {
156+ int main (int /* argc*/ , char *[]) {
149157 run_ort_trt ();
150158 return 0 ;
151159}
0 commit comments