NVIDIA DeepStream SDK API Reference

7.0 Release
yoloPlugins.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, NVIDIA CORPORATION. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #ifndef __YOLO_PLUGINS__
14 #define __YOLO_PLUGINS__
15 
16 #include <cassert>
17 #include <cstring>
18 #include <cuda_runtime_api.h>
19 #include <iostream>
20 #include <memory>
21 
22 #include "NvInferPlugin.h"
23 
24 #define CHECK(status) \
25  { \
26  if (status != 0) \
27  { \
28  std::cout << "Cuda failure: " << cudaGetErrorString(status) << " in file " << __FILE__ \
29  << " at line " << __LINE__ << std::endl; \
30  abort(); \
31  } \
32  }
33 
34 namespace
35 {
36 const char* YOLOV3LAYER_PLUGIN_VERSION {"1"};
37 const char* YOLOV3LAYER_PLUGIN_NAME {"YoloLayerV3_TRT"};
38 } // namespace
39 
40 class YoloLayerV3 : public nvinfer1::IPluginV2
41 {
42 public:
43  YoloLayerV3 (const void* data, size_t length);
44  YoloLayerV3 (const uint& numBoxes, const uint& numClasses, const uint& gridSize);
45  const char* getPluginType () const noexcept override { return YOLOV3LAYER_PLUGIN_NAME; }
46  const char* getPluginVersion () const noexcept override { return YOLOV3LAYER_PLUGIN_VERSION; }
47  int getNbOutputs () const noexcept override { return 1; }
48 
49  nvinfer1::Dims getOutputDimensions (
50  int index, const nvinfer1::Dims* inputs,
51  int nbInputDims) noexcept override;
52 
53  bool supportsFormat (
54  nvinfer1::DataType type, nvinfer1::PluginFormat format) const noexcept override;
55 
56  void configureWithFormat (
57  const nvinfer1::Dims* inputDims, int nbInputs,
58  const nvinfer1::Dims* outputDims, int nbOutputs,
59  nvinfer1::DataType type, nvinfer1::PluginFormat format, int maxBatchSize) noexcept override;
60 
61  int initialize () noexcept override { return 0; }
62  void terminate () noexcept override {}
63  size_t getWorkspaceSize (int maxBatchSize) const noexcept override { return 0; }
64  int32_t enqueue (
65  int32_t batchSize, void const* const* inputs, void* const* outputs,
66  void* workspace, cudaStream_t stream) noexcept override;
67  size_t getSerializationSize() const noexcept override;
68  void serialize (void* buffer) const noexcept override;
69  void destroy () noexcept override { delete this; }
70  nvinfer1::IPluginV2* clone() const noexcept override;
71 
72  void setPluginNamespace (const char* pluginNamespace) noexcept override {
73  m_Namespace = pluginNamespace;
74  }
75  virtual const char* getPluginNamespace () const noexcept override {
76  return m_Namespace.c_str();
77  }
78 
79 private:
80  uint m_NumBoxes {0};
81  uint m_NumClasses {0};
82  uint m_GridSize {0};
83  uint64_t m_OutputSize {0};
84  std::string m_Namespace {""};
85 };
86 
87 class YoloLayerV3PluginCreator : public nvinfer1::IPluginCreator
88 {
89 public:
92 
93  const char* getPluginName () const noexcept override { return YOLOV3LAYER_PLUGIN_NAME; }
94  const char* getPluginVersion () const noexcept override { return YOLOV3LAYER_PLUGIN_VERSION; }
95 
96  const nvinfer1::PluginFieldCollection* getFieldNames() noexcept override {
97  std::cerr<< "YoloLayerV3PluginCreator::getFieldNames is not implemented" << std::endl;
98  return nullptr;
99  }
100 
101  nvinfer1::IPluginV2* createPlugin (
102  const char* name, const nvinfer1::PluginFieldCollection* fc) noexcept override
103  {
104  std::cerr<< "YoloLayerV3PluginCreator::getFieldNames is not implemented.\n";
105  return nullptr;
106  }
107 
108  nvinfer1::IPluginV2* deserializePlugin (
109  const char* name, const void* serialData, size_t serialLength) noexcept override
110  {
111  std::cout << "Deserialize yoloLayerV3 plugin: " << name << std::endl;
112  return new YoloLayerV3(serialData, serialLength);
113  }
114 
115  void setPluginNamespace(const char* libNamespace) noexcept override {
116  m_Namespace = libNamespace;
117  }
118  const char* getPluginNamespace() const noexcept override {
119  return m_Namespace.c_str();
120  }
121 
122 private:
123  std::string m_Namespace {""};
124 };
125 
126 #endif // __YOLO_PLUGINS__
YoloLayerV3::initialize
int initialize() noexcept override
Definition: yoloPlugins.h:61
YoloLayerV3::clone
nvinfer1::IPluginV2 * clone() const noexcept override
YoloLayerV3::getPluginNamespace
virtual const char * getPluginNamespace() const noexcept override
Definition: yoloPlugins.h:75
YoloLayerV3::getWorkspaceSize
size_t getWorkspaceSize(int maxBatchSize) const noexcept override
Definition: yoloPlugins.h:63
ds3d::DataType
DataType
Definition: idatatype.h:77
YoloLayerV3::getPluginVersion
const char * getPluginVersion() const noexcept override
Definition: yoloPlugins.h:46
YoloLayerV3PluginCreator::getFieldNames
const nvinfer1::PluginFieldCollection * getFieldNames() noexcept override
Definition: yoloPlugins.h:96
YoloLayerV3::supportsFormat
bool supportsFormat(nvinfer1::DataType type, nvinfer1::PluginFormat format) const noexcept override
YoloLayerV3PluginCreator::getPluginName
const char * getPluginName() const noexcept override
Definition: yoloPlugins.h:93
YoloLayerV3::YoloLayerV3
YoloLayerV3(const void *data, size_t length)
inputDims
NvDsInferDimsCHW inputDims
Holds the input dimensions for the model.
Definition: nvdsinfer_context.h:270
YoloLayerV3PluginCreator::deserializePlugin
nvinfer1::IPluginV2 * deserializePlugin(const char *name, const void *serialData, size_t serialLength) noexcept override
Definition: yoloPlugins.h:108
YoloLayerV3::destroy
void destroy() noexcept override
Definition: yoloPlugins.h:69
YoloLayerV3::configureWithFormat
void configureWithFormat(const nvinfer1::Dims *inputDims, int nbInputs, const nvinfer1::Dims *outputDims, int nbOutputs, nvinfer1::DataType type, nvinfer1::PluginFormat format, int maxBatchSize) noexcept override
YoloLayerV3
Definition: yoloPlugins.h:40
YoloLayerV3::getNbOutputs
int getNbOutputs() const noexcept override
Definition: yoloPlugins.h:47
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: nvbufsurftransform.h:34
YoloLayerV3::terminate
void terminate() noexcept override
Definition: yoloPlugins.h:62
YoloLayerV3PluginCreator::setPluginNamespace
void setPluginNamespace(const char *libNamespace) noexcept override
Definition: yoloPlugins.h:115
YoloLayerV3::getPluginType
const char * getPluginType() const noexcept override
Definition: yoloPlugins.h:45
YoloLayerV3PluginCreator::~YoloLayerV3PluginCreator
~YoloLayerV3PluginCreator()
Definition: yoloPlugins.h:91
YoloLayerV3::getOutputDimensions
nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs, int nbInputDims) noexcept override
YoloLayerV3::setPluginNamespace
void setPluginNamespace(const char *pluginNamespace) noexcept override
Definition: yoloPlugins.h:72
YoloLayerV3PluginCreator::createPlugin
nvinfer1::IPluginV2 * createPlugin(const char *name, const nvinfer1::PluginFieldCollection *fc) noexcept override
Definition: yoloPlugins.h:101
YoloLayerV3::getSerializationSize
size_t getSerializationSize() const noexcept override
YoloLayerV3::serialize
void serialize(void *buffer) const noexcept override
YoloLayerV3PluginCreator::getPluginVersion
const char * getPluginVersion() const noexcept override
Definition: yoloPlugins.h:94
YoloLayerV3::enqueue
int32_t enqueue(int32_t batchSize, void const *const *inputs, void *const *outputs, void *workspace, cudaStream_t stream) noexcept override
YoloLayerV3PluginCreator::getPluginNamespace
const char * getPluginNamespace() const noexcept override
Definition: yoloPlugins.h:118
YoloLayerV3PluginCreator::YoloLayerV3PluginCreator
YoloLayerV3PluginCreator()
Definition: yoloPlugins.h:90
YoloLayerV3PluginCreator
Definition: yoloPlugins.h:87