docs/html/segNet_8h_source.html
| | 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:
93enum FilterMode
94 {
95FILTER_POINT = 0,
97 };
98
103 {
104VISUALIZE_OVERLAY = (1 << 0),
105VISUALIZE_MASK = (1 << 1),
106 };
107
112static uint32_t VisualizationFlagsFromStr( const char* str, uint32_t default_value=VISUALIZE_OVERLAY );
113
119static FilterMode FilterModeFromStr( const char* str, FilterMode default_value=FILTER_LINEAR );
120
125static segNet* Create( const char* network="fcn-resnet18-voc", uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
126precisionType precision=TYPE_FASTEST, deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
127
138static segNet* Create( const char* prototxt_path, const char* model_path,
139const char* class_labels, const char* class_colors=NULL,
140const char* input = SEGNET_DEFAULT_INPUT,
141const char* output = SEGNET_DEFAULT_OUTPUT,
142 uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
143precisionType precision=TYPE_FASTEST,
144deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
145
146
150static segNet* Create( int argc, char** argv );
151
155static segNet* Create( const commandLine& cmdLine );
156
160static inline const char* Usage() { return SEGNET_USAGE_STRING; }
161
165virtual ~segNet();
166
175template<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
185bool Process( void* input, uint32_t width, uint32_t height, imageFormat format, const char* ignore_class="void" );
186
196bool Process( float* input, uint32_t width, uint32_t height, const char* ignore_class="void" );
197
201template<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
206bool Mask( void* output, uint32_t width, uint32_t height, imageFormat format, FilterMode filter=FILTER_LINEAR );
207
212bool Mask( float* output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR );
213
218bool Mask( uint8_t* output, uint32_t width, uint32_t height );
219
229template<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
240bool Overlay( void* output, uint32_t width, uint32_t height, imageFormat format, FilterMode filter=FILTER_LINEAR );
241
253bool Overlay( float* output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR );
254
258int FindClassID( const char* label_name );
259
263inline uint32_t GetNumClasses() const { return DIMS_C(mOutputs[0].dims); }
264
268inline const char* GetClassLabel( uint32_t id ) const { return GetClassDesc(id); }
269
273inline const char* GetClassDesc( uint32_t id ) const { return id < mClassLabels.size() ? mClassLabels[id].c_str() : NULL; }
274
278inline float4 GetClassColor( uint32_t id ) const { return mClassColors[id]; }
279
283void SetClassColor( uint32_t classIndex, const float4& color );
284
288void SetClassColor( uint32_t classIndex, float r, float g, float b, float a=255.0f );
289
293float GetOverlayAlpha() const;
294
299void SetOverlayAlpha( float alpha, bool explicit_exempt=true );
300
304inline const char* GetClassPath() const { return mClassPath.c_str(); }
305
310inline uint32_t GetGridWidth() const { return DIMS_W(mOutputs[0].dims); }
311
316inline uint32_t GetGridHeight() const { return DIMS_H(mOutputs[0].dims); }
317
318 protected:
319segNet();
320
321bool classify( const char* ignore_class );
322
323bool 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 );
324bool 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
326bool loadClassColors( const char* filename );
327bool loadClassLabels( const char* filename );
328bool saveClassLegend( const char* filename );
329
330 std::vector<std::string> mClassLabels;
331 std::string mClassPath;
332
333bool* mColorsAlphaSet;
334 float4* mClassColors;
337void* mLastInputImg;
338 uint32_t mLastInputWidth;
339 uint32_t mLastInputHeight;
340imageFormat mLastInputFormat;
341 };
342
343
344 #endif
345
@ FILTER_LINEAR
Bilinear filtering.
Definition: segNet.h:96
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
virtual ~segNet()
Destroy.
bool saveClassLegend(const char *filename)
uchar3 color
The RGB color of the point.
Definition: cudaPointCloud.h:11
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.
void * mLastInputImg
last input image to be processed, stored for overlay
Definition: segNet.h:337
const char * GetClassLabel(uint32_t id) const
Retrieve the description of a particular class.
Definition: segNet.h:268
int FindClassID(const char *label_name)
Find the ID of a particular class (by label name).
imageFormat mLastInputFormat
pixel format of last input image
Definition: segNet.h:340
const char * GetClassPath() const
Retrieve the path to the file containing the class label descriptions.
Definition: segNet.h:304
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
GPU (if multiple GPUs are present, a specific GPU can be selected with cudaSetDevice()
Definition: tensorNet.h:131
#define DIMS_C(x)
Definition: tensorNet.h:60
static FilterMode FilterModeFromStr(const char *str, FilterMode default_value=FILTER_LINEAR)
Parse a string from one of the FilterMode values.
#define SEGNET_DEFAULT_INPUT
Name of default input blob for segmentation model.
Definition: segNet.h:34
deviceType
Enumeration for indicating the desired device that the network should run on, if available in hardwar...
Definition: tensorNet.h:129
FilterMode
Enumeration of mask/overlay filtering modes.
Definition: segNet.h:93
float4 GetClassColor(uint32_t id) const
Retrieve the RGBA visualization color a particular class.
Definition: segNet.h:278
void SetClassColor(uint32_t classIndex, const float4 &color)
Set the visualization color of a particular class of object.
#define DIMS_H(x)
Definition: tensorNet.h:61
@ TYPE_FASTEST
The fastest detected precision should be use (i.e.
Definition: tensorNet.h:105
bool * mColorsAlphaSet
true if class color had been explicitly set from file or user
Definition: segNet.h:333
bool loadClassLabels(const char *filename)
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.
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
Enumeration for indicating the desired precision that the network should run in, if available in hard...
Definition: tensorNet.h:102
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
Image segmentation with FCN-Alexnet or custom models, using TensorRT.
Definition: segNet.h:87
@ FILTER_POINT
Nearest point sampling.
Definition: segNet.h:95
segNet()
@ VISUALIZE_MASK
View just the colorized segmentation class mask.
Definition: segNet.h:105
#define SEGNET_USAGE_STRING
Standard command-line options able to be passed to segNet::Create()
Definition: segNet.h:58
uint8_t * mClassMap
runtime buffer for the argmax-classified class index of each tile
Definition: segNet.h:335
Abstract class for loading a tensor network with TensorRT.
Definition: tensorNet.h:218
bool Mask(T *output, uint32_t width, uint32_t height, FilterMode filter=FILTER_LINEAR)
Produce a colorized segmentation mask.
Definition: segNet.h:201
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...
float GetOverlayAlpha() const
Retrieve the overlay alpha blending value for classes that don't have it explicitly set.
#define DIMS_W(x)
Definition: tensorNet.h:62
#define SEGNET_DEFAULT_OUTPUT
Name of default output blob for segmentation model.
Definition: segNet.h:40
VisualizationFlags
Visualization flags.
Definition: segNet.h:102
static const char * Usage()
Usage string for command line arguments to Create()
Definition: segNet.h:160
std::vector< std::string > mClassLabels
Definition: segNet.h:330
const char * GetClassDesc(uint32_t id) const
Retrieve the description of a particular class.
Definition: segNet.h:273
uint32_t GetGridWidth() const
Retrieve the number of columns in the classification grid.
Definition: segNet.h:310
float4 * mClassColors
array of overlay colors in shared CPU/GPU memory
Definition: segNet.h:334
@ VISUALIZE_OVERLAY
Overlay the segmentation class colors with alpha blending.
Definition: segNet.h:104
uint32_t mLastInputWidth
width in pixels of last input image to be processed
Definition: segNet.h:338
#define DEFAULT_MAX_BATCH_SIZE
Default maximum batch size.
Definition: tensorNet.h:88
bool classify(const char *ignore_class)
uint32_t mLastInputHeight
height in pixels of last input image to be processed
Definition: segNet.h:339
Command line parser for extracting flags, values, and strings.
Definition: commandLine.h:35
bool loadClassColors(const char *filename)
imageFormat
The imageFormat enum is used to identify the pixel format and colorspace of an image.
Definition: imageFormat.h:49
std::string mClassPath
Definition: segNet.h:331
uint32_t GetNumClasses() const
Retrieve the number of object classes supported in the detector.
Definition: segNet.h:263
std::vector< layerInfo > mOutputs
Definition: tensorNet.h:819
__device__ cudaVectorTypeInfo< T >::Base alpha(T vec, typename cudaVectorTypeInfo< T >::Base default_alpha=255)
Definition: cudaVector.h:98