forked from dusty-nv/jetson-inference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backgroundNet.h
167 lines (143 loc) · 7.11 KB
/
backgroundNet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef __BACKGROUND_NET_H__
#define __BACKGROUND_NET_H__
#include "tensorNet.h"
#include <jetson-utils/cudaFilterMode.h>
/**
* Name of default input layer for backgroundNet model.
* @ingroup backgroundNet
*/
#define BACKGROUNDNET_DEFAULT_INPUT "input_0"
/**
* Name of default output layer for backgroundNet model.
* @ingroup backgroundNet
*/
#define BACKGROUNDNET_DEFAULT_OUTPUT "output_0"
/**
* The model type for backgroundNet in data/networks/models.json
* @ingroup backgroundNet
*/
#define BACKGROUNDNET_MODEL_TYPE "background"
/**
* Standard command-line options able to be passed to backgroundNet::Create()
* @ingroup backgroundNet
*/
#define BACKGROUNDNET_USAGE_STRING "backgroundNet arguments: \n" \
" --network=NETWORK pre-trained model to load, one of the following:\n" \
" * u2net (default)\n" \
" --model=MODEL path to custom model to load (caffemodel, uff, or onnx)\n" \
" --input-blob=INPUT name of the input layer (default is '" BACKGROUNDNET_DEFAULT_INPUT "')\n" \
" --output-blob=OUTPUT name of the output layer (default is '" BACKGROUNDNET_DEFAULT_OUTPUT "')\n" \
" --profile enable layer profiling in TensorRT\n\n"
/**
* Background subtraction/removal with DNNs, using TensorRT.
* @ingroup backgroundNet
*/
class backgroundNet : public tensorNet
{
public:
/**
* Load a pre-trained model.
*/
static backgroundNet* Create( const char* network="u2net", uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
precisionType precision=TYPE_FASTEST, deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
/**
* Load a new network instance
* @param model_path File path to the caffemodel
* @param input Name of the input layer blob.
* @param output Name of the output layer blob.
* @param maxBatchSize The maximum batch size that the network will support and be optimized for.
*/
static backgroundNet* Create( const char* model_path,
const char* input=BACKGROUNDNET_DEFAULT_INPUT,
const char* output=BACKGROUNDNET_DEFAULT_OUTPUT,
uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
precisionType precision=TYPE_FASTEST,
deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
/**
* Load a new network instance by parsing the command line.
*/
static backgroundNet* Create( int argc, char** argv );
/**
* Load a new network instance by parsing the command line.
*/
static backgroundNet* Create( const commandLine& cmdLine );
/**
* Usage string for command line arguments to Create()
*/
static inline const char* Usage() { return BACKGROUNDNET_USAGE_STRING; }
/**
* Destroy
*/
virtual ~backgroundNet();
/**
* Perform background subtraction/removal on the image (in-place).
* @param image input/output image in CUDA device memory.
* @param width width of the image in pixels.
* @param height height of the output image in pixels.
* @param filter the upsampling mode used to resize the DNN mask (FILTER_LINEAR or FILTER_POINT)
* @param maskAlpha if true (default), the mask will be applied to the alpha channel in addition to the color channels.
* @returns true on success and false if an error occurred.
*/
template<typename T> int Process( T* image, uint32_t width, uint32_t height,
cudaFilterMode filter=FILTER_LINEAR, bool maskAlpha=true ) { return Process((void*)image, width, height, imageFormatFromType<T>(), filter, maskAlpha); }
/**
* Perform background subtraction/removal on the image.
* @param input input image in CUDA device memory.
* @param output output image in CUDA device memory.
* @param width width of the image in pixels.
* @param height height of the output image in pixels.
* @param filter the upsampling mode used to resize the DNN mask (FILTER_LINEAR or FILTER_POINT)
* @param maskAlpha if true (default), the mask will be applied to the alpha channel in addition to the color channels.
* @returns true on success and false if an error occurred.
*/
template<typename T> int Process( T* input, T* output, uint32_t width, uint32_t height,
cudaFilterMode filter=FILTER_LINEAR, bool maskAlpha=true ) { return Process((void*)input, (void*)output, width, height, imageFormatFromType<T>(), filter, maskAlpha); }
/**
* Perform background subtraction/removal on the image (in-place).
* @param image input/output image in CUDA device memory.
* @param width width of the image in pixels.
* @param height height of the output image in pixels.
* @param filter the upsampling mode used to resize the DNN mask (FILTER_LINEAR or FILTER_POINT)
* @param maskAlpha if true (default), the mask will be applied to the alpha channel as well.
* @returns true on success and false if an error occurred.
*/
inline bool Process( void* image, uint32_t width, uint32_t height, imageFormat format,
cudaFilterMode filter=FILTER_LINEAR, bool maskAlpha=true ) { return Process(image, image, width, height, format, filter, maskAlpha); }
/**
* Perform background subtraction/removal on the image.
* @param input input image in CUDA device memory.
* @param output output image in CUDA device memory.
* @param width width of the image in pixels.
* @param height height of the output image in pixels.
* @param filter the upsampling mode used to resize the DNN mask (FILTER_LINEAR or FILTER_POINT)
* @param maskAlpha if true (default), the mask will be applied to the alpha channel as well.
* @returns true on success and false if an error occurred.
*/
bool Process( void* input, void* output, uint32_t width, uint32_t height, imageFormat format,
cudaFilterMode filter=FILTER_LINEAR, bool maskAlpha=true );
protected:
backgroundNet();
bool init(const char* model_path, const char* input, const char* output, uint32_t maxBatchSize, precisionType precision, deviceType device, bool allowGPUFallback );
};
#endif