Jetson Inference
DNN Vision Library
imageNet.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 __IMAGE_NET_H__
24 #define __IMAGE_NET_H__
25 
26 
27 #include "tensorNet.h"
28 
29 
34 #define IMAGENET_DEFAULT_INPUT "data"
35 
40 #define IMAGENET_DEFAULT_OUTPUT "prob"
41 
42 
47 #define IMAGENET_USAGE_STRING "imageNet arguments: \n" \
48  " --network NETWORK pre-trained model to load, one of the following:\n" \
49  " * alexnet\n" \
50  " * googlenet (default)\n" \
51  " * googlenet-12\n" \
52  " * resnet-18\n" \
53  " * resnet-50\n" \
54  " * resnet-101\n" \
55  " * resnet-152\n" \
56  " * vgg-16\n" \
57  " * vgg-19\n" \
58  " * inception-v4\n" \
59  " --model MODEL path to custom model to load (.caffemodel, .uff, or .onnx)\n" \
60  " --prototxt PROTOTXT path to custom prototxt to load (for .caffemodel only)\n" \
61  " --labels LABELS path to text file containing the labels for each class\n" \
62  " --input_blob INPUT name of the input layer (default is '" IMAGENET_DEFAULT_INPUT "')\n" \
63  " --output_blob OUTPUT name of the output layer (default is '" IMAGENET_DEFAULT_OUTPUT "')\n" \
64  " --batch_size BATCH maximum batch size (default is 1)\n"
65 
66 
71 class imageNet : public tensorNet
72 {
73 public:
78  {
90  };
91 
97  static NetworkType NetworkTypeFromStr( const char* model_name );
98 
102  static const char* NetworkTypeToStr( NetworkType network );
103 
107  static imageNet* Create( NetworkType networkType=GOOGLENET, uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
108  precisionType precision=TYPE_FASTEST,
109  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
110 
121  static imageNet* Create( const char* prototxt_path, const char* model_path,
122  const char* mean_binary, const char* class_labels,
123  const char* input=IMAGENET_DEFAULT_INPUT,
124  const char* output=IMAGENET_DEFAULT_OUTPUT,
125  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
126  precisionType precision=TYPE_FASTEST,
127  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
128 
132  static imageNet* Create( int argc, char** argv );
133 
137  static inline const char* Usage() { return IMAGENET_USAGE_STRING; }
138 
142  virtual ~imageNet();
143 
153  int Classify( float* rgba, uint32_t width, uint32_t height, float* confidence=NULL );
154 
161  int Classify( float* confidence=NULL );
162 
168  bool PreProcess( float* rgba, uint32_t width, uint32_t height );
169 
174  bool Process();
175 
179  inline uint32_t GetNumClasses() const { return mOutputClasses; }
180 
184  inline const char* GetClassDesc( uint32_t index ) const { return mClassDesc[index].c_str(); }
185 
189  inline const char* GetClassSynset( uint32_t index ) const { return mClassSynset[index].c_str(); }
190 
194  inline const char* GetClassPath() const { return mClassPath.c_str(); }
195 
199  inline NetworkType GetNetworkType() const { return mNetworkType; }
200 
204  inline const char* GetNetworkName() const { NetworkTypeToStr(mNetworkType); }
205 
206 protected:
207  imageNet();
208 
209  bool init( NetworkType networkType, uint32_t maxBatchSize, precisionType precision, deviceType device, bool allowGPUFallback );
210  bool init(const char* prototxt_path, const char* model_path, const char* mean_binary, const char* class_path, const char* input, const char* output, uint32_t maxBatchSize, precisionType precision, deviceType device, bool allowGPUFallback );
211  bool loadClassInfo( const char* filename );
212 
213  uint32_t mCustomClasses;
214  uint32_t mOutputClasses;
215 
216  std::vector<std::string> mClassSynset; // 1000 class ID's (ie n01580077, n04325704)
217  std::vector<std::string> mClassDesc;
218 
219  std::string mClassPath;
221 };
222 
223 
224 #endif
precisionType
Enumeration for indicating the desired precision that the network should run in, if available in hard...
Definition: tensorNet.h:79
const char * GetClassPath() const
Retrieve the path to the file containing the class descriptions.
Definition: imageNet.h:194
GPU (if multiple GPUs are present, a specific GPU can be selected with cudaSetDevice() ...
Definition: tensorNet.h:108
const char * GetClassSynset(uint32_t index) const
Retrieve the class synset category of a particular class.
Definition: imageNet.h:189
#define IMAGENET_DEFAULT_INPUT
Name of default input blob for imageNet model.
Definition: imageNet.h:34
uint32_t mCustomClasses
Definition: imageNet.h:213
bool init(NetworkType networkType, uint32_t maxBatchSize, precisionType precision, deviceType device, bool allowGPUFallback)
GoogleNet trained 1000-class ILSVRC12.
Definition: imageNet.h:81
uint32_t GetNumClasses() const
Retrieve the number of image recognition classes (typically 1000)
Definition: imageNet.h:179
const char * GetClassDesc(uint32_t index) const
Retrieve the description of a particular class.
Definition: imageNet.h:184
bool loadClassInfo(const char *filename)
deviceType
Enumeration for indicating the desired device that the network should run on, if available in hardwar...
Definition: tensorNet.h:106
#define IMAGENET_DEFAULT_OUTPUT
Name of default output confidence values for imageNet model.
Definition: imageNet.h:40
The fastest detected precision should be use (i.e.
Definition: tensorNet.h:82
static const char * NetworkTypeToStr(NetworkType network)
Convert a NetworkType enum to a string.
Custom model provided by the user.
Definition: imageNet.h:79
const char * GetNetworkName() const
Retrieve a string describing the network name.
Definition: imageNet.h:204
VGG-16 trained on 1000-class ILSVRC14.
Definition: imageNet.h:87
bool PreProcess(float *rgba, uint32_t width, uint32_t height)
Perform pre-processing on the image to apply mean-value subtraction and to organize the data into NCH...
ResNet-50 trained on 1000-class ILSVRC15.
Definition: imageNet.h:84
bool Process()
Process the network, without determining the classification argmax.
NetworkType
Network choice enumeration.
Definition: imageNet.h:77
NetworkType mNetworkType
Definition: imageNet.h:220
ResNet-50 trained on 1000-class ILSVRC15.
Definition: imageNet.h:86
Image recognition with classification networks, using TensorRT.
Definition: imageNet.h:71
int Classify(float *rgba, uint32_t width, uint32_t height, float *confidence=NULL)
Determine the maximum likelihood image class.
#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
std::vector< std::string > mClassSynset
Definition: imageNet.h:216
ResNet-101 trained on 1000-class ILSVRC15.
Definition: imageNet.h:85
std::vector< std::string > mClassDesc
Definition: imageNet.h:217
AlexNet trained on 1000-class ILSVRC12.
Definition: imageNet.h:80
GoogleNet trained on 12-class subset of ImageNet ILSVRC12 from the tutorial.
Definition: imageNet.h:82
std::string mClassPath
Definition: imageNet.h:219
static NetworkType NetworkTypeFromStr(const char *model_name)
Parse a string to one of the built-in pretrained models.
#define IMAGENET_USAGE_STRING
Command-line options able to be passed to imageNet::Create()
Definition: imageNet.h:47
ResNet-18 trained on 1000-class ILSVRC15.
Definition: imageNet.h:83
Inception-v4 trained on 1000-class ILSVRC12.
Definition: imageNet.h:89
uint32_t mOutputClasses
Definition: imageNet.h:214
NetworkType GetNetworkType() const
Retrieve the network type (alexnet or googlenet)
Definition: imageNet.h:199
virtual ~imageNet()
Destroy.
VGG-19 trained on 1000-class ILSVRC14.
Definition: imageNet.h:88
static const char * Usage()
Usage string for command line arguments to Create()
Definition: imageNet.h:137
static imageNet * Create(NetworkType networkType=GOOGLENET, uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE, precisionType precision=TYPE_FASTEST, deviceType device=DEVICE_GPU, bool allowGPUFallback=true)
Load a new network instance.