HDlModel.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /***********************************************************
  2. * File generated by the HALCON-Compiler hcomp version 22.05
  3. * Usage: Interface to C++
  4. *
  5. * Software by: MVTec Software GmbH, www.mvtec.com
  6. ***********************************************************/
  7. #ifndef HCPP_HDLMODEL
  8. #define HCPP_HDLMODEL
  9. namespace HalconCpp
  10. {
  11. // Represents an instance of a Deep Neural Network.
  12. class LIntExport HDlModel : public HHandle
  13. {
  14. public:
  15. // Create an uninitialized instance
  16. HDlModel():HHandle() {}
  17. // Copy constructor
  18. HDlModel(const HDlModel& source) : HHandle(source) {}
  19. // Copy constructor
  20. HDlModel(const HHandle& handle);
  21. // Create HDlModel from handle, taking ownership
  22. explicit HDlModel(Hlong handle);
  23. bool operator==(const HHandle& obj) const
  24. {
  25. return HHandleBase::operator==(obj);
  26. }
  27. bool operator!=(const HHandle& obj) const
  28. {
  29. return HHandleBase::operator!=(obj);
  30. }
  31. protected:
  32. // Verify matching semantic type ('dl_model')!
  33. virtual void AssertType(Hphandle handle) const;
  34. public:
  35. // Deep copy of all data represented by this object instance
  36. HDlModel Clone() const;
  37. /*****************************************************************************
  38. * Operator-based class constructors
  39. *****************************************************************************/
  40. // create_dl_model_detection: Create a deep learning network for object detection or instance segmentation.
  41. explicit HDlModel(const HString& Backbone, Hlong NumClasses, const HDict& DLModelDetectionParam);
  42. // create_dl_model_detection: Create a deep learning network for object detection or instance segmentation.
  43. explicit HDlModel(const char* Backbone, Hlong NumClasses, const HDict& DLModelDetectionParam);
  44. #ifdef _WIN32
  45. // create_dl_model_detection: Create a deep learning network for object detection or instance segmentation.
  46. explicit HDlModel(const wchar_t* Backbone, Hlong NumClasses, const HDict& DLModelDetectionParam);
  47. #endif
  48. // read_dl_model: Read a deep learning model from a file.
  49. explicit HDlModel(const HString& FileName);
  50. // read_dl_model: Read a deep learning model from a file.
  51. explicit HDlModel(const char* FileName);
  52. #ifdef _WIN32
  53. // read_dl_model: Read a deep learning model from a file.
  54. explicit HDlModel(const wchar_t* FileName);
  55. #endif
  56. // create_dl_model: Create a deep learning model.
  57. explicit HDlModel(const HDlLayerArray& OutputLayers);
  58. // create_dl_model: Create a deep learning model.
  59. explicit HDlModel(const HDlLayer& OutputLayers);
  60. /***************************************************************************
  61. * Operators *
  62. ***************************************************************************/
  63. // Apply a deep-learning-based network on a set of images for inference.
  64. HDictArray ApplyDlModel(const HDictArray& DLSampleBatch, const HTuple& Outputs) const;
  65. // Clear a deep learning model.
  66. static void ClearDlModel(const HDlModelArray& DLModelHandle);
  67. // Clear a deep learning model.
  68. void ClearDlModel() const;
  69. // Create a deep learning network for object detection or instance segmentation.
  70. void CreateDlModelDetection(const HString& Backbone, Hlong NumClasses, const HDict& DLModelDetectionParam);
  71. // Create a deep learning network for object detection or instance segmentation.
  72. void CreateDlModelDetection(const char* Backbone, Hlong NumClasses, const HDict& DLModelDetectionParam);
  73. #ifdef _WIN32
  74. // Create a deep learning network for object detection or instance segmentation.
  75. void CreateDlModelDetection(const wchar_t* Backbone, Hlong NumClasses, const HDict& DLModelDetectionParam);
  76. #endif
  77. // Deserialize a deep learning model.
  78. void DeserializeDlModel(const HSerializedItem& SerializedItemHandle);
  79. // Return the parameters of a deep learning model.
  80. HTuple GetDlModelParam(const HString& GenParamName) const;
  81. // Return the parameters of a deep learning model.
  82. HTuple GetDlModelParam(const char* GenParamName) const;
  83. #ifdef _WIN32
  84. // Return the parameters of a deep learning model.
  85. HTuple GetDlModelParam(const wchar_t* GenParamName) const;
  86. #endif
  87. // Read a deep learning model from a file.
  88. void ReadDlModel(const HString& FileName);
  89. // Read a deep learning model from a file.
  90. void ReadDlModel(const char* FileName);
  91. #ifdef _WIN32
  92. // Read a deep learning model from a file.
  93. void ReadDlModel(const wchar_t* FileName);
  94. #endif
  95. // Serialize a deep learning model.
  96. HSerializedItem SerializeDlModel() const;
  97. // Set the parameters of a deep learning model.
  98. void SetDlModelParam(const HString& GenParamName, const HTuple& GenParamValue) const;
  99. // Set the parameters of a deep learning model.
  100. void SetDlModelParam(const HString& GenParamName, Hlong GenParamValue) const;
  101. // Set the parameters of a deep learning model.
  102. void SetDlModelParam(const char* GenParamName, Hlong GenParamValue) const;
  103. #ifdef _WIN32
  104. // Set the parameters of a deep learning model.
  105. void SetDlModelParam(const wchar_t* GenParamName, Hlong GenParamValue) const;
  106. #endif
  107. // Train a deep learning model.
  108. HDict TrainDlModelBatch(const HDictArray& DLSampleBatch) const;
  109. // Write a deep learning model in a file.
  110. void WriteDlModel(const HString& FileName) const;
  111. // Write a deep learning model in a file.
  112. void WriteDlModel(const char* FileName) const;
  113. #ifdef _WIN32
  114. // Write a deep learning model in a file.
  115. void WriteDlModel(const wchar_t* FileName) const;
  116. #endif
  117. // Infer the sample and generate a heatmap.
  118. HDictArray GenDlModelHeatmap(const HDictArray& DLSample, const HString& HeatmapMethod, const HTuple& TargetClasses, const HDict& GenParam) const;
  119. // Infer the sample and generate a heatmap.
  120. HDictArray GenDlModelHeatmap(const HDictArray& DLSample, const char* HeatmapMethod, const HTuple& TargetClasses, const HDict& GenParam) const;
  121. #ifdef _WIN32
  122. // Infer the sample and generate a heatmap.
  123. HDictArray GenDlModelHeatmap(const HDictArray& DLSample, const wchar_t* HeatmapMethod, const HTuple& TargetClasses, const HDict& GenParam) const;
  124. #endif
  125. // Train a deep learning model for anomaly detection.
  126. HDict TrainDlModelAnomalyDataset(const HDictArray& DLSamples, const HDict& DLTrainParam) const;
  127. // Calculate scores to prune a deep learning model.
  128. void AddDlPruningBatch(const HDlPrune& DLPruningHandle, const HDictArray& DLSampleBatch) const;
  129. // Create a pruning data handle.
  130. HDlPrune CreateDlPruning(const HString& Mode, const HDict& GenParam) const;
  131. // Create a pruning data handle.
  132. HDlPrune CreateDlPruning(const char* Mode, const HDict& GenParam) const;
  133. #ifdef _WIN32
  134. // Create a pruning data handle.
  135. HDlPrune CreateDlPruning(const wchar_t* Mode, const HDict& GenParam) const;
  136. #endif
  137. // Prune a deep learning model.
  138. HDlModel GenDlPrunedModel(const HDlPrune& DLPruningHandle) const;
  139. // Optimize a model for inference on a device via the -interface.
  140. HDlModel OptimizeDlModelForInference(const HDlDeviceArray& DLDeviceHandle, const HString& Precision, const HDictArray& DLSamples, const HDict& GenParam, HDict* ConversionReport) const;
  141. // Optimize a model for inference on a device via the -interface.
  142. HDlModel OptimizeDlModelForInference(const HDlDevice& DLDeviceHandle, const HString& Precision, const HDictArray& DLSamples, const HDict& GenParam, HDict* ConversionReport) const;
  143. // Optimize a model for inference on a device via the -interface.
  144. HDlModel OptimizeDlModelForInference(const HDlDevice& DLDeviceHandle, const char* Precision, const HDictArray& DLSamples, const HDict& GenParam, HDict* ConversionReport) const;
  145. #ifdef _WIN32
  146. // Optimize a model for inference on a device via the -interface.
  147. HDlModel OptimizeDlModelForInference(const HDlDevice& DLDeviceHandle, const wchar_t* Precision, const HDictArray& DLSamples, const HDict& GenParam, HDict* ConversionReport) const;
  148. #endif
  149. // Create a deep learning model.
  150. void CreateDlModel(const HDlLayerArray& OutputLayers);
  151. // Create a deep learning model.
  152. void CreateDlModel(const HDlLayer& OutputLayers);
  153. // Create a deep copy of the layers and all of their graph ancestors in a given deep learning model.
  154. HDlLayerArray GetDlModelLayer(const HTuple& LayerNames) const;
  155. // Create a deep copy of the layers and all of their graph ancestors in a given deep learning model.
  156. HDlLayer GetDlModelLayer(const HString& LayerNames) const;
  157. // Create a deep copy of the layers and all of their graph ancestors in a given deep learning model.
  158. HDlLayer GetDlModelLayer(const char* LayerNames) const;
  159. #ifdef _WIN32
  160. // Create a deep copy of the layers and all of their graph ancestors in a given deep learning model.
  161. HDlLayer GetDlModelLayer(const wchar_t* LayerNames) const;
  162. #endif
  163. // Get the activations of a Deep Learning model layer.
  164. HImage GetDlModelLayerActivations(const HString& LayerName) const;
  165. // Get the activations of a Deep Learning model layer.
  166. HImage GetDlModelLayerActivations(const char* LayerName) const;
  167. #ifdef _WIN32
  168. // Get the activations of a Deep Learning model layer.
  169. HImage GetDlModelLayerActivations(const wchar_t* LayerName) const;
  170. #endif
  171. // Get the gradients of a Deep Learning model layer.
  172. HImage GetDlModelLayerGradients(const HString& LayerName) const;
  173. // Get the gradients of a Deep Learning model layer.
  174. HImage GetDlModelLayerGradients(const char* LayerName) const;
  175. #ifdef _WIN32
  176. // Get the gradients of a Deep Learning model layer.
  177. HImage GetDlModelLayerGradients(const wchar_t* LayerName) const;
  178. #endif
  179. // Get the weights (or values) of a Deep Learning model layer.
  180. HImage GetDlModelLayerWeights(const HString& LayerName, const HString& WeightsType) const;
  181. // Get the weights (or values) of a Deep Learning model layer.
  182. HImage GetDlModelLayerWeights(const char* LayerName, const char* WeightsType) const;
  183. #ifdef _WIN32
  184. // Get the weights (or values) of a Deep Learning model layer.
  185. HImage GetDlModelLayerWeights(const wchar_t* LayerName, const wchar_t* WeightsType) const;
  186. #endif
  187. // Load the weights of a source model into a target model.
  188. HTuple LoadDlModelWeights(const HDlModel& DLModelHandleTarget) const;
  189. // Set the weights (or values) of a Deep Learning model layer.
  190. void SetDlModelLayerWeights(const HImage& Weights, const HString& LayerName, const HString& WeightsType) const;
  191. // Set the weights (or values) of a Deep Learning model layer.
  192. void SetDlModelLayerWeights(const HImage& Weights, const char* LayerName, const char* WeightsType) const;
  193. #ifdef _WIN32
  194. // Set the weights (or values) of a Deep Learning model layer.
  195. void SetDlModelLayerWeights(const HImage& Weights, const wchar_t* LayerName, const wchar_t* WeightsType) const;
  196. #endif
  197. };
  198. // forward declarations and types for internal array implementation
  199. template<class T> class HSmartPtr;
  200. template<class T> class HHandleBaseArrayRef;
  201. typedef HHandleBaseArrayRef<HDlModel> HDlModelArrayRef;
  202. typedef HSmartPtr< HDlModelArrayRef > HDlModelArrayPtr;
  203. // Represents multiple tool instances
  204. class LIntExport HDlModelArray : public HHandleBaseArray
  205. {
  206. public:
  207. // Create empty array
  208. HDlModelArray();
  209. // Create array from native array of tool instances
  210. HDlModelArray(const HDlModel* classes, Hlong length);
  211. // Copy constructor
  212. HDlModelArray(const HDlModelArray &tool_array);
  213. // Destructor
  214. virtual ~HDlModelArray();
  215. // Assignment operator
  216. HDlModelArray &operator=(const HDlModelArray &tool_array);
  217. // Clears array and all tool instances
  218. virtual void Clear();
  219. // Get array of native tool instances
  220. const HDlModel* Tools() const;
  221. // Get number of tools
  222. virtual Hlong Length() const;
  223. // Create tool array from tuple of handles
  224. virtual void SetFromTuple(const HTuple& handles);
  225. // Get tuple of handles for tool array
  226. virtual HTuple ConvertToTuple() const;
  227. protected:
  228. // Smart pointer to internal data container
  229. HDlModelArrayPtr *mArrayPtr;
  230. };
  231. }
  232. #endif