Jetson Inference
DNN Vision Library
detectNet.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __DETECT_NET_H__
24 #define __DETECT_NET_H__
25 
26 
27 #include "tensorNet.h"
28 
29 
34 #define DETECTNET_DEFAULT_INPUT "data"
35 
40 #define DETECTNET_DEFAULT_COVERAGE "coverage"
41 
46 #define DETECTNET_DEFAULT_BBOX "bboxes"
47 
52 #define DETECTNET_DEFAULT_THRESHOLD 0.5f
53 
58 #define DETECTNET_USAGE_STRING "detectNet arguments: \n" \
59  " --network NETWORK pre-trained model to load, one of the following:\n" \
60  " * pednet (default)\n" \
61  " * multiped\n" \
62  " * facenet\n" \
63  " * ssd-mobilenet-v1\n" \
64  " * ssd-mobilenet-v2\n" \
65  " * ssd-inception-v2\n" \
66  " * coco-airplane\n" \
67  " * coco-bottle\n" \
68  " * coco-chair\n" \
69  " * coco-dog\n" \
70  " --model MODEL path to custom model to load (.caffemodel, .uff, or .onnx)\n" \
71  " --prototxt PROTOTXT path to custom prototxt to load (for .caffemodel only)\n" \
72  " --class_labels LABELS path to text file containing the labels for each class\n" \
73  " --threshold THRESHOLD minimum threshold for detection (default is 0.5)\n" \
74  " --input_blob INPUT name of the input layer (default is '" DETECTNET_DEFAULT_INPUT "')\n" \
75  " --output_cvg COVERAGE name of the coverge output layer (default is '" DETECTNET_DEFAULT_COVERAGE "')\n" \
76  " --output_bbox BOXES name of the bounding output layer (default is '" DETECTNET_DEFAULT_BBOX "')\n" \
77  " --mean_pixel PIXEL mean pixel value to subtract from input (default is 0.0)\n" \
78  " --batch_size BATCH maximum batch size (default is 1)\n"
79 
80 
85 class detectNet : public tensorNet
86 {
87 public:
91  struct Detection
92  {
93  // Object Info
94  uint32_t Instance;
95  uint32_t ClassID;
96  float Confidence;
98  // Bounding Box Coordinates
99  float Left;
100  float Right;
101  float Top;
102  float Bottom;
105  inline float Width() const { return Right - Left; }
106 
108  inline float Height() const { return Bottom - Top; }
109 
111  inline float Area() const { return Width() * Height(); }
112 
114  inline void Center( float* x, float* y ) const { if(x) *x = Left + Width() * 0.5f; if(y) *y = Top + Height() * 0.5f; }
115 
117  inline bool Contains( float x, float y ) const { return x >= Left && x <= Right && y >= Top && y <= Bottom; }
118 
120  inline bool Overlaps( const Detection& det ) const { return !(det.Left > Right || det.Right < Left || det.Top > Bottom || det.Bottom < Top); }
121 
123  inline bool Overlaps( float x1, float y1, float x2, float y2 ) const { return !(x1 > Right || x2 < Left || y1 > Bottom || y2 < Top); }
124 
126  inline bool Expand( float x1, float y1, float x2, float y2 ) { if(!Overlaps(x1, y1, x2, y2)) return false; Left = fminf(x1, Left); Top = fminf(y1, Top); Right = fmaxf(x2, Right); Bottom = fmaxf(y2, Bottom); return true; }
127 
129  inline bool Expand( const Detection& det ) { if(!Overlaps(det)) return false; Left = fminf(det.Left, Left); Top = fminf(det.Top, Top); Right = fmaxf(det.Right, Right); Bottom = fmaxf(det.Bottom, Bottom); return true; }
130 
132  inline void Reset() { Instance = 0; ClassID = 0; Confidence = 0; Left = 0; Right = 0; Top = 0; Bottom = 0; }
133 
135  inline Detection() { Reset(); }
136  };
137 
142  {
144  OVERLAY_BOX = (1 << 0),
145  OVERLAY_LABEL = (1 << 1)
146  };
147 
152  {
153  CUSTOM = 0,
162 #if NV_TENSORRT_MAJOR > 4
163  SSD_MOBILENET_V1,
164  SSD_MOBILENET_V2,
165  SSD_INCEPTION_V2
166 #endif
167  };
168 
175  static NetworkType NetworkTypeFromStr( const char* model_name );
176 
183  static detectNet* Create( NetworkType networkType=PEDNET_MULTI, float threshold=DETECTNET_DEFAULT_THRESHOLD,
184  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE, precisionType precision=TYPE_FASTEST,
185  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
186 
199  static detectNet* Create( const char* prototxt_path, const char* model_path, const char* mean_binary,
200  const char* class_labels, float threshold=DETECTNET_DEFAULT_THRESHOLD,
201  const char* input = DETECTNET_DEFAULT_INPUT,
202  const char* coverage = DETECTNET_DEFAULT_COVERAGE,
203  const char* bboxes = DETECTNET_DEFAULT_BBOX,
204  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
205  precisionType precision=TYPE_FASTEST,
206  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
207 
220  static detectNet* Create( const char* prototxt_path, const char* model_path, float mean_pixel=0.0f,
221  const char* class_labels=NULL, float threshold=DETECTNET_DEFAULT_THRESHOLD,
222  const char* input = DETECTNET_DEFAULT_INPUT,
223  const char* coverage = DETECTNET_DEFAULT_COVERAGE,
224  const char* bboxes = DETECTNET_DEFAULT_BBOX,
225  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
226  precisionType precision=TYPE_FASTEST,
227  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
228 
240  static detectNet* Create( const char* model_path, const char* class_labels, float threshold,
241  const char* input, const Dims3& inputDims,
242  const char* output, const char* numDetections,
243  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
244  precisionType precision=TYPE_FASTEST,
245  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
246 
250  static detectNet* Create( int argc, char** argv );
251 
255  static inline const char* Usage() { return DETECTNET_USAGE_STRING; }
256 
260  virtual ~detectNet();
261 
271  int Detect( float* input, uint32_t width, uint32_t height, Detection** detections, uint32_t overlay=OVERLAY_BOX );
272 
283  int Detect( float* input, uint32_t width, uint32_t height, Detection* detections, uint32_t overlay=OVERLAY_BOX );
284 
292  bool Overlay( float* input, float* output, uint32_t width, uint32_t height, Detection* detections, uint32_t numDetections, uint32_t flags=OVERLAY_BOX );
293 
298  inline float GetThreshold() const { return mCoverageThreshold; }
299 
303  inline void SetThreshold( float threshold ) { mCoverageThreshold = threshold; }
304 
309  inline uint32_t GetMaxDetections() const { return mMaxDetections; }
310 
314  inline uint32_t GetNumClasses() const { return mNumClasses; }
315 
319  inline const char* GetClassDesc( uint32_t index ) const { return mClassDesc[index].c_str(); }
320 
324  inline const char* GetClassSynset( uint32_t index ) const { return mClassSynset[index].c_str(); }
325 
329  inline const char* GetClassPath() const { return mClassPath.c_str(); }
330 
334  void SetClassColor( uint32_t classIndex, float r, float g, float b, float a=255.0f );
335 
336 
337 protected:
338 
339  // constructor
340  detectNet( float meanPixel=0.0f );
341 
342  bool allocDetections();
343  bool defaultColors();
344  void defaultClassDesc();
345  bool loadClassDesc( const char* filename );
346 
347  bool init( const char* prototxt_path, const char* model_path, const char* mean_binary, const char* class_labels,
348  float threshold, const char* input, const char* coverage, const char* bboxes, uint32_t maxBatchSize,
349  precisionType precision, deviceType device, bool allowGPUFallback );
350 
351  int clusterDetections( Detection* detections, uint32_t width, uint32_t height );
352 
354  float* mClassColors[2];
355  float mMeanPixel;
356 
357  std::vector<std::string> mClassDesc;
358  std::vector<std::string> mClassSynset;
359 
360  std::string mClassPath;
361  uint32_t mCustomClasses;
362  uint32_t mNumClasses;
363 
364  Detection* mDetectionSets[2]; // list of detections, mNumDetectionSets * mMaxDetections
365  uint32_t mDetectionSet; // index of next detection set to use
366  uint32_t mMaxDetections; // number of raw detections in the grid
367 
368  static const uint32_t mNumDetectionSets = 16; // size of detection ringbuffer
369 };
370 
371 
372 #endif
Detection * mDetectionSets[2]
Definition: detectNet.h:364
Pedestrian / person detector.
Definition: detectNet.h:159
MS-COCO chair class.
Definition: detectNet.h:156
bool Contains(float x, float y) const
Return true if the bounding boxes overlap.
Definition: detectNet.h:117
void SetThreshold(float threshold)
Set the minimum threshold for detection.
Definition: detectNet.h:303
float Top
Top bounding box cooridnate (in pixels)
Definition: detectNet.h:101
precisionType
Enumeration for indicating the desired precision that the network should run in, if available in hard...
Definition: tensorNet.h:79
GPU (if multiple GPUs are present, a specific GPU can be selected with cudaSetDevice() ...
Definition: tensorNet.h:108
std::vector< std::string > mClassSynset
Definition: detectNet.h:358
bool Expand(const Detection &det)
Reset all member variables to zero.
Definition: detectNet.h:129
float Left
Left bounding box coordinate (in pixels)
Definition: detectNet.h:99
MS-COCO bottle class.
Definition: detectNet.h:155
Overlay the object bounding boxes.
Definition: detectNet.h:144
#define DETECTNET_DEFAULT_BBOX
Name of default output blob of the grid of bounding boxes for DetectNet caffe model.
Definition: detectNet.h:46
std::string mClassPath
Definition: detectNet.h:360
int Detect(float *input, uint32_t width, uint32_t height, Detection **detections, uint32_t overlay=OVERLAY_BOX)
Detect object locations from an RGBA image, returning an array containing the detection results...
Detection()
Definition: detectNet.h:135
#define DETECTNET_DEFAULT_COVERAGE
Name of default output blob of the coverage map for DetectNet caffe model.
Definition: detectNet.h:40
const char * GetClassSynset(uint32_t index) const
Retrieve the class synset category of a particular class.
Definition: detectNet.h:324
#define DETECTNET_USAGE_STRING
Command-line options able to be passed to imageNet::Create()
Definition: detectNet.h:58
void SetClassColor(uint32_t classIndex, float r, float g, float b, float a=255.0f)
Set the visualization color of a particular class of object.
float GetThreshold() const
Retrieve the minimum threshold for detection.
Definition: detectNet.h:298
deviceType
Enumeration for indicating the desired device that the network should run on, if available in hardwar...
Definition: tensorNet.h:106
The fastest detected precision should be use (i.e.
Definition: tensorNet.h:82
void defaultClassDesc()
Overlay the class description labels.
Definition: detectNet.h:145
float * mClassColors[2]
Definition: detectNet.h:354
std::vector< std::string > mClassDesc
Definition: detectNet.h:357
static detectNet * Create(NetworkType networkType=PEDNET_MULTI, float threshold=DETECTNET_DEFAULT_THRESHOLD, uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE, precisionType precision=TYPE_FASTEST, deviceType device=DEVICE_GPU, bool allowGPUFallback=true)
Load a new network instance.
static NetworkType NetworkTypeFromStr(const char *model_name)
Parse a string to one of the built-in pretrained models.
bool allocDetections()
bool init(const char *prototxt_path, const char *model_path, const char *mean_binary, const char *class_labels, float threshold, const char *input, const char *coverage, const char *bboxes, uint32_t maxBatchSize, precisionType precision, deviceType device, bool allowGPUFallback)
uint32_t mDetectionSet
Definition: detectNet.h:365
MS-COCO dog class.
Definition: detectNet.h:157
uint32_t ClassID
Class index of the detected object.
Definition: detectNet.h:95
#define DETECTNET_DEFAULT_INPUT
Name of default input blob for DetectNet caffe model.
Definition: detectNet.h:34
Multi-class pedestrian + baggage detector.
Definition: detectNet.h:160
uint32_t GetNumClasses() const
Retrieve the number of object classes supported in the detector.
Definition: detectNet.h:314
bool Expand(float x1, float y1, float x2, float y2)
Expand the bounding box if they overlap (return true if so)
Definition: detectNet.h:126
MS-COCO airplane class.
Definition: detectNet.h:154
Object Detection result.
Definition: detectNet.h:91
float mMeanPixel
Definition: detectNet.h:355
const char * GetClassDesc(uint32_t index) const
Retrieve the description of a particular class.
Definition: detectNet.h:319
uint32_t Instance
Index of this unique object instance.
Definition: detectNet.h:94
static const uint32_t mNumDetectionSets
Definition: detectNet.h:368
No overlay.
Definition: detectNet.h:143
void Center(float *x, float *y) const
Return true if the coordinate is inside the bounding box.
Definition: detectNet.h:114
Human facial detector trained on FDDB.
Definition: detectNet.h:158
bool loadClassDesc(const char *filename)
float Height() const
Calculate the area of the object.
Definition: detectNet.h:108
float Right
Right bounding box coordinate (in pixels)
Definition: detectNet.h:100
uint32_t mCustomClasses
Definition: detectNet.h:361
float Area() const
Return the center of the object.
Definition: detectNet.h:111
#define DEFAULT_MAX_BATCH_SIZE
Default maximum batch size.
Definition: tensorNet.h:65
uint32_t GetMaxDetections() const
Retrieve the maximum number of simultaneous detections the network supports.
Definition: detectNet.h:309
Abstract class for loading a tensor network with TensorRT.
Definition: tensorNet.h:188
Object recognition and localization networks with TensorRT support.
Definition: detectNet.h:85
bool defaultColors()
uint32_t mMaxDetections
Definition: detectNet.h:366
static const char * Usage()
Usage string for command line arguments to Create()
Definition: detectNet.h:255
bool Overlaps(float x1, float y1, float x2, float y2) const
Expand the bounding box if they overlap (return true if so)
Definition: detectNet.h:123
float mCoverageThreshold
Definition: detectNet.h:353
const char * GetClassPath() const
Retrieve the path to the file containing the class descriptions.
Definition: detectNet.h:329
float Bottom
Bottom bounding box coordinate (in pixels)
Definition: detectNet.h:102
NetworkType
Network choice enumeration.
Definition: detectNet.h:151
float Confidence
Confidence value of the detected object.
Definition: detectNet.h:96
detectNet(float meanPixel=0.0f)
#define DETECTNET_DEFAULT_THRESHOLD
Default value of the minimum detection threshold.
Definition: detectNet.h:52
uint32_t mNumClasses
Definition: detectNet.h:362
bool Overlaps(const Detection &det) const
Return true if the bounding boxes overlap.
Definition: detectNet.h:120
virtual ~detectNet()
Destory.
OverlayFlags
Overlay flags (can be OR&#39;d together).
Definition: detectNet.h:141
void Reset()
Default constructor.
Definition: detectNet.h:132
int clusterDetections(Detection *detections, uint32_t width, uint32_t height)
float Width() const
Calculate the height of the object.
Definition: detectNet.h:105
Custom model from user.
Definition: detectNet.h:153
bool Overlay(float *input, float *output, uint32_t width, uint32_t height, Detection *detections, uint32_t numDetections, uint32_t flags=OVERLAY_BOX)
Draw the detected bounding boxes overlayed on an RGBA image.
nvinfer1::Dims3 Dims3
Definition: tensorNet.h:48