Multimedia API Reference

November 16, 2016 | 24.2.1 Release

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gie_inference.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA CORPORATION nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
48 #ifndef GIE_INFERENCE_H_
49 #define GIE_INFERENCE_H_
50 
51 #include <assert.h>
52 #include <fstream>
53 #include <sstream>
54 #include <iostream>
55 #include <cmath>
56 #include <sys/stat.h>
57 #include <cmath>
58 #include <queue>
59 #include <time.h>
60 #include <cuda_runtime_api.h>
61 
62 #include "NvInfer.h"
63 #include "NvCaffeParser.h"
64 
65 #include "opencv2/video/tracking.hpp"
66 #include "opencv2/imgproc/imgproc.hpp"
67 #include "opencv2/highgui/highgui.hpp"
68 #include <opencv2/objdetect/objdetect.hpp>
69 using namespace nvinfer1;
70 using namespace nvcaffeparser1;
71 using namespace std;
72 
73 #define CHECK(status) \
74 { \
75  if (status != 0) \
76  { \
77  std::cout << "Cuda failure: " << status; \
78  abort(); \
79  } \
80 }
81 
82 static const int TIMING_ITERATIONS = 1000;
83 static const int NUM_BINDINGS = 3;
84 
85 // Logger for GIE info/warning/errors.
86 class Logger : public ILogger
87 {
88  void log(Severity severity, const char* msg) override
89  {
90  // Suppresses info-level messages.
91  if (severity != Severity::kINFO)
92  std::cout << msg << std::endl;
93  }
94 };
95 
96 
97 struct Profiler : public IProfiler
98 {
99  typedef std::pair<std::string, float> Record;
100  std::vector<Record> mProfile;
101 
102  virtual void reportLayerTime(const char* layerName, float ms)
103  {
104  auto record = std::find_if(mProfile.begin(), mProfile.end(),
105  [&](const Record& r){ return r.first == layerName; });
106  if (record == mProfile.end())
107  mProfile.push_back(std::make_pair(layerName, ms));
108  else
109  record->second += ms;
110  }
111 
113  {
114  float totalTime = 0;
115  for (size_t i = 0; i < mProfile.size(); i++)
116  {
117  printf("%-40.40s %4.3fms\n", mProfile[i].first.c_str(),
118  mProfile[i].second / TIMING_ITERATIONS);
119  totalTime += mProfile[i].second;
120  }
121  printf("Time over all layers: %4.3f\n", totalTime / TIMING_ITERATIONS);
122  }
123 
124 };
125 
127 {
128 public:
129  unsigned int net_width;
130  unsigned int net_height;
131  unsigned int image_width;
132  unsigned int image_height;
133  unsigned int filter_num;
134  void *buffers[NUM_BINDINGS];
135  float *input_buf;
138  float helnet_scale[4];
139  int hel_net;
140  IRuntime *runtime;
141  ICudaEngine *engine;
142  IExecutionContext *context;
143  uint32_t *pResultArray;
144  string deployfile;
145  string modelfile;
146  unsigned int channel; //input file's channel
147  unsigned int num_bindings;
148  unsigned int inference_num; //inference channel num
149  unsigned int batch_size;
150  stringstream gieModelStream;
151  vector<string> outputs;
152  class Logger Logger;
154  unsigned int forced_fp32;
155  unsigned int dump_result;
156  string result_file;
157  ofstream fstream;
159 
160 private:
161  unsigned int frame_num;
162  float thresh;
163  unsigned int stride;
164  unsigned int force_not_use_fp16;
165  string input_blob_name;
166  string output_blob_name;
167  string output_bbox_name;
168  uint64_t elapsed_frame_num;
169  uint64_t elapsed_time;
170  void parse_bbox(Dims3 outputDims, Dims3 outputDimsBBOX,
171  vector<cv::Rect>& rectList, int batch_th);
172  void parse_hel_bbox(Dims3 outputDims, Dims3 outputDimsBBOX,
173  vector<cv::Rect>& rectList, int class_num, int batch_th);
174 
175 public:
176  void gie_alloc();
177  void gie_free();
178  void caffeToGIEModel();
179  void doInference(float* input, queue< vector<cv::Rect> >& rectList_queue);
180  int parse_net();
181 };
182 
184 #endif
float * input_buf
unsigned int inference_num
uint32_t * pResultArray
ICudaEngine * engine
std::vector< Record > mProfile
uint32_t enable_gie_profiler
unsigned int channel
unsigned int image_width
float * output_cov_buf
std::pair< std::string, float > Record
Definition: gie_inference.h:99
unsigned int net_width
unsigned int dump_result
void printLayerTimes()
static const int NUM_BINDINGS
Definition: gie_inference.h:83
IRuntime * runtime
string modelfile
unsigned int num_bindings
string result_file
IExecutionContext * context
unsigned int batch_size
unsigned int filter_num
unsigned int forced_fp32
virtual void reportLayerTime(const char *layerName, float ms)
unsigned int image_height
unsigned int net_height
float * output_bbox_buf
vector< string > outputs
string deployfile
ofstream fstream
static const int TIMING_ITERATIONS
Definition: gie_inference.h:82
stringstream gieModelStream