Jetson Inference
DNN Vision Library
segNet.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 __SEGMENTATION_NET_H__
24 #define __SEGMENTATION_NET_H__
25 
26 
27 #include "tensorNet.h"
28 
29 
34 #define SEGNET_DEFAULT_INPUT "data"
35 
40 #define SEGNET_DEFAULT_OUTPUT "score_fr_21classes"
41 
42 
47 class segNet : public tensorNet
48 {
49 public:
54  {
63  /* add new models here */
65  };
66 
71  {
74  };
75 
81  static NetworkType NetworkTypeFromStr( const char* model_name );
82 
88  static FilterMode FilterModeFromStr( const char* str, FilterMode default_value=FILTER_LINEAR );
89 
93  static segNet* Create( NetworkType networkType=FCN_ALEXNET_CITYSCAPES_SD, uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
94  precisionType precision=TYPE_FASTEST, deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
95 
106  static segNet* Create( const char* prototxt_path, const char* model_path,
107  const char* class_labels, const char* class_colors=NULL,
108  const char* input = SEGNET_DEFAULT_INPUT,
109  const char* output = SEGNET_DEFAULT_OUTPUT,
110  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
111  precisionType precision=TYPE_FASTEST,
112  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
113 
114 
118  static segNet* Create( int argc, char** argv );
119 
123  virtual ~segNet();
124 
133  bool Process( float* input, uint32_t width, uint32_t height, const char* ignore_class="void" );
134 
139  bool Mask( uint8_t* output, uint32_t width, uint32_t height );
140 
144  bool Mask( float* output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR );
145 
156  bool Overlay( float* output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR );
157 
161  int FindClassID( const char* label_name );
162 
166  inline uint32_t GetNumClasses() const { return DIMS_C(mOutputs[0].dims); }
167 
171  inline const char* GetClassLabel( uint32_t id ) const { return id < mClassLabels.size() ? mClassLabels[id].c_str() : NULL; }
172 
176  inline float* GetClassColor( uint32_t id ) const { return mClassColors[0] + (id*4); }
177 
181  void SetClassColor( uint32_t classIndex, float r, float g, float b, float a=255.0f );
182 
187  void SetGlobalAlpha( float alpha, bool explicit_exempt=true );
188 
192  inline const char* GetClassPath() const { return mClassPath.c_str(); }
193 
198  inline uint32_t GetGridWidth() const { return DIMS_W(mOutputs[0].dims); }
199 
204  inline uint32_t GetGridHeight() const { return DIMS_H(mOutputs[0].dims); }
205 
209  inline NetworkType GetNetworkType() const { return mNetworkType; }
210 
214  inline const char* GetNetworkName() const { return (mNetworkType != SEGNET_CUSTOM ? "FCN_Alexnet" : "segNet"); }
215 
216 protected:
217  segNet();
218 
219  bool classify( const char* ignore_class );
220 
221  bool overlayPoint( float* input, uint32_t in_width, uint32_t in_height, float* output, uint32_t out_width, uint32_t out_height, bool mask_only );
222  bool overlayLinear( float* input, uint32_t in_width, uint32_t in_height, float* output, uint32_t out_width, uint32_t out_height, bool mask_only );
223 
224  bool loadClassColors( const char* filename );
225  bool loadClassLabels( const char* filename );
226 
227  std::vector<std::string> mClassLabels;
228  std::string mClassPath;
229 
230  float* mClassColors[2];
231  uint8_t* mClassMap[2];
233  float* mLastInputImg;
234  uint32_t mLastInputWidth;
235  uint32_t mLastInputHeight;
238 };
239 
240 
241 #endif
242 
FilterMode
Enumeration of mask/overlay filtering modes.
Definition: segNet.h:70
precisionType
Enumeration for indicating the desired precision that the network should run in, if available in hard...
Definition: tensorNet.h:79
bool overlayPoint(float *input, uint32_t in_width, uint32_t in_height, float *output, uint32_t out_width, uint32_t out_height, bool mask_only)
static FilterMode FilterModeFromStr(const char *str, FilterMode default_value=FILTER_LINEAR)
Parse a string from one of the FilterMode values.
GPU (if multiple GPUs are present, a specific GPU can be selected with cudaSetDevice() ...
Definition: tensorNet.h:108
#define DIMS_W(x)
Definition: tensorNet.h:52
std::vector< std::string > mClassLabels
Definition: segNet.h:227
NetworkType mNetworkType
Pretrained built-in model type enumeration.
Definition: segNet.h:237
const char * GetClassLabel(uint32_t id) const
Retrieve the description of a particular class.
Definition: segNet.h:171
FCN-Alexnet trained on SYNTHIA SEQS summer datasets.
Definition: segNet.h:58
FCN-Alexnet trained on SYNTHIA SEQS summer datasets.
Definition: segNet.h:57
FCN-Alexnet trained on Pascal VOC dataset.
Definition: segNet.h:55
void SetGlobalAlpha(float alpha, bool explicit_exempt=true)
Set a global alpha value for all classes (between 0-255), (optionally except for those that have been...
FCN-Alexnet trained on Cityscapes dataset with 21 classes.
Definition: segNet.h:59
static NetworkType NetworkTypeFromStr(const char *model_name)
Parse a string from one of the built-in pretrained models.
std::vector< outputLayer > mOutputs
Definition: tensorNet.h:551
bool overlayLinear(float *input, uint32_t in_width, uint32_t in_height, float *output, uint32_t out_width, uint32_t out_height, bool mask_only)
deviceType
Enumeration for indicating the desired device that the network should run on, if available in hardwar...
Definition: tensorNet.h:106
#define DIMS_C(x)
Definition: tensorNet.h:50
uint32_t GetNumClasses() const
Retrieve the number of object classes supported in the detector.
Definition: segNet.h:166
const char * GetNetworkName() const
Retrieve a string describing the network name.
Definition: segNet.h:214
The fastest detected precision should be use (i.e.
Definition: tensorNet.h:82
FCN-Alexnet trained on SYNTHIA CVPR16 dataset.
Definition: segNet.h:56
bool loadClassLabels(const char *filename)
bool Overlay(float *output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR)
Produce the segmentation overlay alpha blended on top of the original image.
#define SEGNET_DEFAULT_INPUT
Name of default input blob for segmentation model.
Definition: segNet.h:34
int FindClassID(const char *label_name)
Find the ID of a particular class (by label name).
FCN-Alexnet trained on aerial first-person view of the horizon line for drones, 1280x720 and 21 outpu...
Definition: segNet.h:61
Definition: segNet.h:64
uint32_t mLastInputWidth
width in pixels of last input image to be processed
Definition: segNet.h:234
const char * GetClassPath() const
Retrieve the path to the file containing the class label descriptions.
Definition: segNet.h:192
virtual ~segNet()
Destroy.
float * mClassColors[2]
array of overlay colors in shared CPU/GPU memory
Definition: segNet.h:230
uint8_t * mClassMap[2]
runtime buffer for the argmax-classified class index of each tile
Definition: segNet.h:231
Bilinear filtering.
Definition: segNet.h:73
Nearest point sampling.
Definition: segNet.h:72
std::string mClassPath
Definition: segNet.h:228
float * mLastInputImg
last input image to be processed, stored for overlay
Definition: segNet.h:233
bool Mask(uint8_t *output, uint32_t width, uint32_t height)
Produce a grayscale binary segmentation mask, where the pixel values correspond to the class ID of th...
bool classify(const char *ignore_class)
FCN-Alexnet trained on Cityscapes dataset with 21 classes.
Definition: segNet.h:60
Image segmentation with FCN-Alexnet or custom models, using TensorRT.
Definition: segNet.h:47
uint32_t GetGridWidth() const
Retrieve the number of columns in the classification grid.
Definition: segNet.h:198
#define DEFAULT_MAX_BATCH_SIZE
Default maximum batch size.
Definition: tensorNet.h:65
Abstract class for loading a tensor network with TensorRT.
Definition: tensorNet.h:188
#define SEGNET_DEFAULT_OUTPUT
Name of default output blob for segmentation model.
Definition: segNet.h:40
NetworkType GetNetworkType() const
Retrieve the network type (alexnet or googlenet)
Definition: segNet.h:209
bool loadClassColors(const char *filename)
float * GetClassColor(uint32_t id) const
Retrieve the class synset category of a particular class.
Definition: segNet.h:176
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.
uint32_t mLastInputHeight
height in pixels of last input image to be processed
Definition: segNet.h:235
bool Process(float *input, uint32_t width, uint32_t height, const char *ignore_class="void")
Perform the initial inferencing processing portion of the segmentation.
NetworkType
Enumeration of pretrained/built-in network models.
Definition: segNet.h:53
static segNet * Create(NetworkType networkType=FCN_ALEXNET_CITYSCAPES_SD, uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE, precisionType precision=TYPE_FASTEST, deviceType device=DEVICE_GPU, bool allowGPUFallback=true)
Load a new network instance.
uint32_t GetGridHeight() const
Retrieve the number of rows in the classification grid.
Definition: segNet.h:204
#define DIMS_H(x)
Definition: tensorNet.h:51