yolo.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2019-2020, 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. #ifndef _YOLO_H_
  23. #define _YOLO_H_
  24. #include <stdint.h>
  25. #include <string>
  26. #include <vector>
  27. #include <memory>
  28. #include "NvInfer.h"
  29. #include "trt_utils.h"
  30. #include "yololayer.h"
  31. #include "mish.h"
  32. typedef enum {
  33. /** NvDsInferContext operation succeeded. */
  34. NVDSINFER_SUCCESS = 0,
  35. /** Failed to configure the NvDsInferContext instance possibly due to an
  36. * erroneous initialization property. */
  37. NVDSINFER_CONFIG_FAILED,
  38. /** Custom Library interface implementation failed. */
  39. NVDSINFER_CUSTOM_LIB_FAILED,
  40. /** Invalid parameters were supplied. */
  41. NVDSINFER_INVALID_PARAMS,
  42. /** Output parsing failed. */
  43. NVDSINFER_OUTPUT_PARSING_FAILED,
  44. /** CUDA error was encountered. */
  45. NVDSINFER_CUDA_ERROR,
  46. /** TensorRT interface failed. */
  47. NVDSINFER_TENSORRT_ERROR,
  48. /** Resource error was encountered. */
  49. NVDSINFER_RESOURCE_ERROR,
  50. /** TRT-IS error was encountered. */
  51. NVDSINFER_TRTIS_ERROR,
  52. /** Unknown error was encountered. */
  53. NVDSINFER_UNKNOWN_ERROR
  54. } NvDsInferStatus;
  55. class IModelParser
  56. {
  57. public:
  58. IModelParser() = default;
  59. /**
  60. * Destructor, make sure all external resource would be released here. */
  61. virtual ~IModelParser() = default;
  62. /**
  63. * Function interface for parsing custom model and building tensorrt
  64. * network.
  65. *
  66. * @param[in, out] network NvDsInfer will create the @a network and
  67. * implementation can setup this network layer by layer.
  68. * @return NvDsInferStatus indicating if model parsing was sucessful.
  69. */
  70. virtual NvDsInferStatus parseModel(
  71. nvinfer1::INetworkDefinition& network) = 0;
  72. /**
  73. * Function interface to check if parser can support full-dimensions.
  74. */
  75. virtual bool hasFullDimsSupported() const = 0;
  76. /**
  77. * Function interface to get the new model name which is to be used for
  78. * constructing the serialized engine file path.
  79. */
  80. virtual const char* getModelName() const = 0;
  81. };
  82. /**
  83. * Holds all the file paths required to build a network.
  84. */
  85. struct NetworkInfo
  86. {
  87. std::string networkType;
  88. std::string configFilePath;
  89. std::string wtsFilePath;
  90. std::string deviceType;
  91. std::string inputBlobName;
  92. uint maxbatchSize;
  93. };
  94. /**
  95. * Holds information about an output tensor of the yolo network.
  96. */
  97. struct TensorInfo
  98. {
  99. std::string blobName;
  100. uint stride{0};
  101. uint gridSize{0};
  102. uint numClasses{0};
  103. uint numBBoxes{0};
  104. uint64_t volume{0};
  105. std::vector<uint> masks;
  106. std::vector<float> anchors;
  107. int bindingIndex{-1};
  108. float* hostBuffer{nullptr};
  109. };
  110. class Yolo : public IModelParser {
  111. public:
  112. Yolo(const NetworkInfo& networkInfo);
  113. ~Yolo() override;
  114. bool hasFullDimsSupported() const override { return false; }
  115. const char* getModelName() const override {
  116. return m_ConfigFilePath.empty() ? m_NetworkType.c_str()
  117. : m_ConfigFilePath.c_str();
  118. }
  119. NvDsInferStatus parseModel(nvinfer1::INetworkDefinition& network) override;
  120. nvinfer1::ICudaEngine *createEngine (nvinfer1::IBuilder* builder);
  121. protected:
  122. const std::string m_NetworkType;
  123. const std::string m_ConfigFilePath;
  124. const std::string m_WtsFilePath;
  125. const std::string m_DeviceType;
  126. const std::string m_InputBlobName;
  127. const std::string m_OutputBlobName;
  128. std::vector<TensorInfo> m_OutputTensors;
  129. std::vector<std::map<std::string, std::string>> m_ConfigBlocks;
  130. uint m_InputH;
  131. uint m_InputW;
  132. uint m_InputC;
  133. uint64_t m_InputSize;
  134. uint m_MaxBatchSize;
  135. // TRT specific members
  136. std::vector<nvinfer1::Weights> m_TrtWeights;
  137. std::vector<nvinfer1::ITensor*> m_YoloTensor;
  138. std::vector<YoloKernel> m_YoloKernel;
  139. private:
  140. NvDsInferStatus buildYoloNetwork(
  141. std::vector<float>& weights, nvinfer1::INetworkDefinition& network);
  142. std::vector<std::map<std::string, std::string>> parseConfigFile(
  143. const std::string cfgFilePath);
  144. void parseConfigBlocks();
  145. void destroyNetworkUtils();
  146. };
  147. #endif // _YOLO_H_