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  " --profile enable layer profiling in TensorRT\n\n"
66 
67 
72 class imageNet : public tensorNet
73 {
74 public:
79  {
91  };
92 
98  static NetworkType NetworkTypeFromStr( const char* model_name );
99 
103  static const char* NetworkTypeToStr( NetworkType network );
104 
108  static imageNet* Create( NetworkType networkType=GOOGLENET, uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
109  precisionType precision=TYPE_FASTEST,
110  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
111 
122  static imageNet* Create( const char* prototxt_path, const char* model_path,
123  const char* mean_binary, const char* class_labels,
124  const char* input=IMAGENET_DEFAULT_INPUT,
125  const char* output=IMAGENET_DEFAULT_OUTPUT,
126  uint32_t maxBatchSize=DEFAULT_MAX_BATCH_SIZE,
127  precisionType precision=TYPE_FASTEST,
128  deviceType device=DEVICE_GPU, bool allowGPUFallback=true );
129 
133  static imageNet* Create( int argc, char** argv );
134 
138  static imageNet* Create( const commandLine& cmdLine );
139 
143  static inline const char* Usage() { return IMAGENET_USAGE_STRING; }
144 
148  virtual ~imageNet();
149 
159  template<typename T> int Classify( T* image, uint32_t width, uint32_t height, float* confidence=NULL ) { return Classify((void*)image, width, height, imageFormatFromType<T>(), confidence); }
160 
170  int Classify( void* image, uint32_t width, uint32_t height, imageFormat format, float* confidence=NULL );
171 
182  int Classify( float* rgba, uint32_t width, uint32_t height, float* confidence=NULL, imageFormat format=IMAGE_RGBA32F );
183 
187  inline uint32_t GetNumClasses() const { return mOutputClasses; }
188 
192  inline const char* GetClassDesc( uint32_t index ) const { return mClassDesc[index].c_str(); }
193 
197  inline const char* GetClassSynset( uint32_t index ) const { return mClassSynset[index].c_str(); }
198 
202  inline const char* GetClassPath() const { return mClassPath.c_str(); }
203 
207  inline NetworkType GetNetworkType() const { return mNetworkType; }
208 
212  inline const char* GetNetworkName() const { return NetworkTypeToStr(mNetworkType); }
213 
217  static bool LoadClassInfo( const char* filename, std::vector<std::string>& descriptions, int expectedClasses=-1 );
218 
222  static bool LoadClassInfo( const char* filename, std::vector<std::string>& descriptions, std::vector<std::string>& synsets, int expectedClasses=-1 );
223 
224 protected:
225  imageNet();
226 
227  int Classify( float* confidence=NULL );
228  bool PreProcess( void* image, uint32_t width, uint32_t height, imageFormat format );
229  bool Process();
230 
231  bool init( NetworkType networkType, uint32_t maxBatchSize, precisionType precision, deviceType device, bool allowGPUFallback );
232  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 );
233  bool loadClassInfo( const char* filename, int expectedClasses=-1 );
234 
235  uint32_t mOutputClasses;
236 
237  std::vector<std::string> mClassSynset; // 1000 class ID's (ie n01580077, n04325704)
238  std::vector<std::string> mClassDesc;
239 
240  std::string mClassPath;
242 };
243 
244 
245 #endif
precisionType
Enumeration for indicating the desired precision that the network should run in, if available in hard...
Definition: tensorNet.h:95
const char * GetClassPath() const
Retrieve the path to the file containing the class descriptions.
Definition: imageNet.h:202
GPU (if multiple GPUs are present, a specific GPU can be selected with cudaSetDevice() ...
Definition: tensorNet.h:124
const char * GetClassSynset(uint32_t index) const
Retrieve the class synset category of a particular class.
Definition: imageNet.h:197
#define IMAGENET_DEFAULT_INPUT
Name of default input blob for imageNet model.
Definition: imageNet.h:34
int Classify(T *image, uint32_t width, uint32_t height, float *confidence=NULL)
Determine the maximum likelihood image class.
Definition: imageNet.h:159
bool init(NetworkType networkType, uint32_t maxBatchSize, precisionType precision, deviceType device, bool allowGPUFallback)
GoogleNet trained 1000-class ILSVRC12.
Definition: imageNet.h:82
uint32_t GetNumClasses() const
Retrieve the number of image recognition classes (typically 1000)
Definition: imageNet.h:187
const char * GetClassDesc(uint32_t index) const
Retrieve the description of a particular class.
Definition: imageNet.h:192
deviceType
Enumeration for indicating the desired device that the network should run on, if available in hardwar...
Definition: tensorNet.h:122
#define IMAGENET_DEFAULT_OUTPUT
Name of default output confidence values for imageNet model.
Definition: imageNet.h:40
static bool LoadClassInfo(const char *filename, std::vector< std::string > &descriptions, int expectedClasses=-1)
Load class descriptions from a label file.
The fastest detected precision should be use (i.e.
Definition: tensorNet.h:98
static const char * NetworkTypeToStr(NetworkType network)
Convert a NetworkType enum to a string.
bool loadClassInfo(const char *filename, int expectedClasses=-1)
Custom model provided by the user.
Definition: imageNet.h:80
const char * GetNetworkName() const
Retrieve a string describing the network name.
Definition: imageNet.h:212
VGG-16 trained on 1000-class ILSVRC14.
Definition: imageNet.h:88
ResNet-50 trained on 1000-class ILSVRC15.
Definition: imageNet.h:85
bool Process()
Command line parser for extracting flags, values, and strings.
Definition: commandLine.h:35
NetworkType
Network choice enumeration.
Definition: imageNet.h:78
NetworkType mNetworkType
Definition: imageNet.h:241
ResNet-50 trained on 1000-class ILSVRC15.
Definition: imageNet.h:87
Image recognition with classification networks, using TensorRT.
Definition: imageNet.h:72
bool PreProcess(void *image, uint32_t width, uint32_t height, imageFormat format)
#define DEFAULT_MAX_BATCH_SIZE
Default maximum batch size.
Definition: tensorNet.h:81
Abstract class for loading a tensor network with TensorRT.
Definition: tensorNet.h:211
std::vector< std::string > mClassSynset
Definition: imageNet.h:237
ResNet-101 trained on 1000-class ILSVRC15.
Definition: imageNet.h:86
std::vector< std::string > mClassDesc
Definition: imageNet.h:238
AlexNet trained on 1000-class ILSVRC12.
Definition: imageNet.h:81
GoogleNet trained on 12-class subset of ImageNet ILSVRC12 from the tutorial.
Definition: imageNet.h:83
float4 RGBA32F (&#39;rgba32f&#39;)
Definition: imageFormat.h:55
std::string mClassPath
Definition: imageNet.h:240
static NetworkType NetworkTypeFromStr(const char *model_name)
Parse a string to one of the built-in pretrained models.
#define IMAGENET_USAGE_STRING
Standard command-line options able to be passed to imageNet::Create()
Definition: imageNet.h:47
ResNet-18 trained on 1000-class ILSVRC15.
Definition: imageNet.h:84
Inception-v4 trained on 1000-class ILSVRC12.
Definition: imageNet.h:90
uint32_t mOutputClasses
Definition: imageNet.h:235
NetworkType GetNetworkType() const
Retrieve the network type (alexnet or googlenet)
Definition: imageNet.h:207
virtual ~imageNet()
Destroy.
VGG-19 trained on 1000-class ILSVRC14.
Definition: imageNet.h:89
static const char * Usage()
Usage string for command line arguments to Create()
Definition: imageNet.h:143
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.
imageFormat
The imageFormat enum is used to identify the pixel format and colorspace of an image.
Definition: imageFormat.h:49