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 "input_0"
35 
40 #define SEGNET_DEFAULT_OUTPUT "output_0"
41 
46 #define SEGNET_DEFAULT_ALPHA 150
47 
52 #define SEGNET_MODEL_TYPE "segmentation"
53 
58 #define SEGNET_USAGE_STRING "segNet arguments: \n" \
59  " --network=NETWORK pre-trained model to load, one of the following:\n" \
60  " * fcn-resnet18-cityscapes-512x256\n" \
61  " * fcn-resnet18-cityscapes-1024x512\n" \
62  " * fcn-resnet18-cityscapes-2048x1024\n" \
63  " * fcn-resnet18-deepscene-576x320\n" \
64  " * fcn-resnet18-deepscene-864x480\n" \
65  " * fcn-resnet18-mhp-512x320\n" \
66  " * fcn-resnet18-mhp-640x360\n" \
67  " * fcn-resnet18-voc-320x320 (default)\n" \
68  " * fcn-resnet18-voc-512x320\n" \
69  " * fcn-resnet18-sun-512x400\n" \
70  " * fcn-resnet18-sun-640x512\n" \
71  " --model=MODEL path to custom model to load (caffemodel, uff, or onnx)\n" \
72  " --prototxt=PROTOTXT path to custom prototxt to load (for .caffemodel only)\n" \
73  " --labels=LABELS path to text file containing the labels for each class\n" \
74  " --colors=COLORS path to text file containing the colors for each class\n" \
75  " --input-blob=INPUT name of the input layer (default: '" SEGNET_DEFAULT_INPUT "')\n" \
76  " --output-blob=OUTPUT name of the output layer (default: '" SEGNET_DEFAULT_OUTPUT "')\n" \
77  " --alpha=ALPHA overlay alpha blending value, range 0-255 (default: 150)\n" \
78  " --visualize=VISUAL visualization flags (e.g. --visualize=overlay,mask)\n" \
79  " valid combinations are: 'overlay', 'mask'\n" \
80  " --profile enable layer profiling in TensorRT\n\n"
81 
82 
87 class segNet : public tensorNet
88 {
89 public:
94  {
97  };
98 
103  {
104  VISUALIZE_OVERLAY = (1 << 0),
105  VISUALIZE_MASK = (1 << 1),
106  };
107 
112  static uint32_t VisualizationFlagsFromStr( const char* str, uint32_t default_value=VISUALIZE_OVERLAY );
113 
119  static FilterMode FilterModeFromStr( const char* str, FilterMode default_value=FILTER_LINEAR );
120 
125  static segNet* Create( const char* network="fcn-resnet18-voc", uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
126  precisionType precision=TYPE_FASTEST, deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
127 
138  static segNet* Create( const char* prototxt_path, const char* model_path,
139  const char* class_labels, const char* class_colors=NULL,
140  const char* input = SEGNET_DEFAULT_INPUT,
141  const char* output = SEGNET_DEFAULT_OUTPUT,
142  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
143  precisionType precision=TYPE_FASTEST,
144  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
145 
146 
150  static segNet* Create( int argc, char** argv );
151 
155  static segNet* Create( const commandLine& cmdLine );
156 
160  static inline const char* Usage() { return SEGNET_USAGE_STRING; }
161 
165  virtual ~segNet();
166 
175  template<typename T> bool Process( T* input, uint32_t width, uint32_t height, const char* ignore_class="void" ) { return Process((void*)input, width, height, imageFormatFromType<T>(), ignore_class); }
176 
185  bool Process( void* input, uint32_t width, uint32_t height, imageFormat format, const char* ignore_class="void" );
186 
196  bool Process( float* input, uint32_t width, uint32_t height, const char* ignore_class="void" );
197 
201  template<typename T> bool Mask( T* output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR ) { return Mask((void*)output, width, height, imageFormatFromType<T>(), filter); }
202 
206  bool Mask( void* output, uint32_t width, uint32_t height, imageFormat format, FilterMode filter=FILTER_LINEAR );
207 
212  bool Mask( float* output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR );
213 
218  bool Mask( uint8_t* output, uint32_t width, uint32_t height );
219 
229  template<typename T> bool Overlay( T* output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR ) { return Overlay((void*)output, width, height, imageFormatFromType<T>(), filter); }
230 
240  bool Overlay( void* output, uint32_t width, uint32_t height, imageFormat format, FilterMode filter=FILTER_LINEAR );
241 
253  bool Overlay( float* output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR );
254 
258  int FindClassID( const char* label_name );
259 
263  inline uint32_t GetNumClasses() const { return DIMS_C(mOutputs[0].dims); }
264 
268  inline const char* GetClassLabel( uint32_t id ) const { return GetClassDesc(id); }
269 
273  inline const char* GetClassDesc( uint32_t id ) const { return id < mClassLabels.size() ? mClassLabels[id].c_str() : NULL; }
274 
278  inline float4 GetClassColor( uint32_t id ) const { return mClassColors[id]; }
279 
283  void SetClassColor( uint32_t classIndex, const float4& color );
284 
288  void SetClassColor( uint32_t classIndex, float r, float g, float b, float a=255.0f );
289 
293  float GetOverlayAlpha() const;
294 
299  void SetOverlayAlpha( float alpha, bool explicit_exempt=true );
300 
304  inline const char* GetClassPath() const { return mClassPath.c_str(); }
305 
310  inline uint32_t GetGridWidth() const { return DIMS_W(mOutputs[0].dims); }
311 
316  inline uint32_t GetGridHeight() const { return DIMS_H(mOutputs[0].dims); }
317 
318 protected:
319  segNet();
320 
321  bool classify( const char* ignore_class );
322 
323  bool overlayPoint( void* input, uint32_t in_width, uint32_t in_height, imageFormat in_format, void* output, uint32_t out_width, uint32_t out_height, imageFormat out_format, bool mask_only );
324  bool overlayLinear( void* input, uint32_t in_width, uint32_t in_height, imageFormat in_format, void* output, uint32_t out_width, uint32_t out_height, imageFormat out_format, bool mask_only );
325 
326  bool loadClassColors( const char* filename );
327  bool loadClassLabels( const char* filename );
328  bool saveClassLegend( const char* filename );
329 
330  std::vector<std::string> mClassLabels;
331  std::string mClassPath;
332 
334  float4* mClassColors;
335  uint8_t* mClassMap;
338  uint32_t mLastInputWidth;
339  uint32_t mLastInputHeight;
341 };
342 
343 
344 #endif
345 
segNet::FILTER_LINEAR
@ FILTER_LINEAR
Bilinear filtering.
Definition: segNet.h:96
segNet::Overlay
bool Overlay(T *output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR)
Produce the segmentation overlay alpha blended on top of the original image.
Definition: segNet.h:229
segNet::~segNet
virtual ~segNet()
Destroy.
segNet::saveClassLegend
bool saveClassLegend(const char *filename)
color
uchar3 color
The RGB color of the point.
Definition: cudaPointCloud.h:11
segNet::GetGridHeight
uint32_t GetGridHeight() const
Retrieve the number of rows in the classification grid.
Definition: segNet.h:316
segNet::VisualizationFlagsFromStr
static uint32_t VisualizationFlagsFromStr(const char *str, uint32_t default_value=VISUALIZE_OVERLAY)
Parse a string of one of more VisualizationMode values.
segNet::mLastInputImg
void * mLastInputImg
last input image to be processed, stored for overlay
Definition: segNet.h:337
segNet::GetClassLabel
const char * GetClassLabel(uint32_t id) const
Retrieve the description of a particular class.
Definition: segNet.h:268
segNet::FindClassID
int FindClassID(const char *label_name)
Find the ID of a particular class (by label name).
segNet::mLastInputFormat
imageFormat mLastInputFormat
pixel format of last input image
Definition: segNet.h:340
segNet::GetClassPath
const char * GetClassPath() const
Retrieve the path to the file containing the class label descriptions.
Definition: segNet.h:304
segNet::overlayPoint
bool overlayPoint(void *input, uint32_t in_width, uint32_t in_height, imageFormat in_format, void *output, uint32_t out_width, uint32_t out_height, imageFormat out_format, bool mask_only)
DEVICE_GPU
@ DEVICE_GPU
GPU (if multiple GPUs are present, a specific GPU can be selected with cudaSetDevice()
Definition: tensorNet.h:131
DIMS_C
#define DIMS_C(x)
Definition: tensorNet.h:60
segNet::FilterModeFromStr
static FilterMode FilterModeFromStr(const char *str, FilterMode default_value=FILTER_LINEAR)
Parse a string from one of the FilterMode values.
SEGNET_DEFAULT_INPUT
#define SEGNET_DEFAULT_INPUT
Name of default input blob for segmentation model.
Definition: segNet.h:34
deviceType
deviceType
Enumeration for indicating the desired device that the network should run on, if available in hardwar...
Definition: tensorNet.h:129
segNet::FilterMode
FilterMode
Enumeration of mask/overlay filtering modes.
Definition: segNet.h:93
segNet::GetClassColor
float4 GetClassColor(uint32_t id) const
Retrieve the RGBA visualization color a particular class.
Definition: segNet.h:278
tensorNet.h
segNet::SetClassColor
void SetClassColor(uint32_t classIndex, const float4 &color)
Set the visualization color of a particular class of object.
DIMS_H
#define DIMS_H(x)
Definition: tensorNet.h:61
TYPE_FASTEST
@ TYPE_FASTEST
The fastest detected precision should be use (i.e.
Definition: tensorNet.h:105
segNet::mColorsAlphaSet
bool * mColorsAlphaSet
true if class color had been explicitly set from file or user
Definition: segNet.h:333
segNet::loadClassLabels
bool loadClassLabels(const char *filename)
segNet::Create
static segNet * Create(const char *network="fcn-resnet18-voc", uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE, precisionType precision=TYPE_FASTEST, deviceType device=DEVICE_GPU, bool allowGPUFallback=true)
Load a pre-trained model.
segNet::overlayLinear
bool overlayLinear(void *input, uint32_t in_width, uint32_t in_height, imageFormat in_format, void *output, uint32_t out_width, uint32_t out_height, imageFormat out_format, bool mask_only)
precisionType
precisionType
Enumeration for indicating the desired precision that the network should run in, if available in hard...
Definition: tensorNet.h:102
segNet::Process
bool Process(T *input, uint32_t width, uint32_t height, const char *ignore_class="void")
Perform the initial inferencing processing portion of the segmentation.
Definition: segNet.h:175
segNet
Image segmentation with FCN-Alexnet or custom models, using TensorRT.
Definition: segNet.h:87
segNet::FILTER_POINT
@ FILTER_POINT
Nearest point sampling.
Definition: segNet.h:95
segNet::segNet
segNet()
segNet::VISUALIZE_MASK
@ VISUALIZE_MASK
View just the colorized segmentation class mask.
Definition: segNet.h:105
SEGNET_USAGE_STRING
#define SEGNET_USAGE_STRING
Standard command-line options able to be passed to segNet::Create()
Definition: segNet.h:58
segNet::mClassMap
uint8_t * mClassMap
runtime buffer for the argmax-classified class index of each tile
Definition: segNet.h:335
tensorNet
Abstract class for loading a tensor network with TensorRT.
Definition: tensorNet.h:218
segNet::Mask
bool Mask(T *output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR)
Produce a colorized segmentation mask.
Definition: segNet.h:201
segNet::SetOverlayAlpha
void SetOverlayAlpha(float alpha, bool explicit_exempt=true)
Set overlay alpha blending value for all classes (between 0-255), (optionally except for those that h...
segNet::GetOverlayAlpha
float GetOverlayAlpha() const
Retrieve the overlay alpha blending value for classes that don't have it explicitly set.
DIMS_W
#define DIMS_W(x)
Definition: tensorNet.h:62
SEGNET_DEFAULT_OUTPUT
#define SEGNET_DEFAULT_OUTPUT
Name of default output blob for segmentation model.
Definition: segNet.h:40
segNet::VisualizationFlags
VisualizationFlags
Visualization flags.
Definition: segNet.h:102
segNet::Usage
static const char * Usage()
Usage string for command line arguments to Create()
Definition: segNet.h:160
segNet::mClassLabels
std::vector< std::string > mClassLabels
Definition: segNet.h:330
segNet::GetClassDesc
const char * GetClassDesc(uint32_t id) const
Retrieve the description of a particular class.
Definition: segNet.h:273
segNet::GetGridWidth
uint32_t GetGridWidth() const
Retrieve the number of columns in the classification grid.
Definition: segNet.h:310
segNet::mClassColors
float4 * mClassColors
array of overlay colors in shared CPU/GPU memory
Definition: segNet.h:334
segNet::VISUALIZE_OVERLAY
@ VISUALIZE_OVERLAY
Overlay the segmentation class colors with alpha blending.
Definition: segNet.h:104
segNet::mLastInputWidth
uint32_t mLastInputWidth
width in pixels of last input image to be processed
Definition: segNet.h:338
DEFAULT_MAX_BATCH_SIZE
#define DEFAULT_MAX_BATCH_SIZE
Default maximum batch size.
Definition: tensorNet.h:88
segNet::classify
bool classify(const char *ignore_class)
segNet::mLastInputHeight
uint32_t mLastInputHeight
height in pixels of last input image to be processed
Definition: segNet.h:339
commandLine
Command line parser for extracting flags, values, and strings.
Definition: commandLine.h:35
segNet::loadClassColors
bool loadClassColors(const char *filename)
imageFormat
imageFormat
The imageFormat enum is used to identify the pixel format and colorspace of an image.
Definition: imageFormat.h:49
segNet::mClassPath
std::string mClassPath
Definition: segNet.h:331
segNet::GetNumClasses
uint32_t GetNumClasses() const
Retrieve the number of object classes supported in the detector.
Definition: segNet.h:263
tensorNet::mOutputs
std::vector< layerInfo > mOutputs
Definition: tensorNet.h:819
alpha
__device__ cudaVectorTypeInfo< T >::Base alpha(T vec, typename cudaVectorTypeInfo< T >::Base default_alpha=255)
Definition: cudaVector.h:98