DaoAI World C++ SDK INDUSTRIAL 2024.8.0
Loading...
Searching...
No Matches
model.h
Go to the documentation of this file.
1// Copyright (c) 2024, DaoAI Robotics. All rights reserved.
2// Version: 0.0.0
3
4#pragma once
5#include "API_EXPORT.h"
6#include <string>
7#include <map>
8#include <filesystem>
9#include "common.h"
10#include "prediction.h"
11#include "config.h"
12#include "component_memory.h"
13namespace DaoAI
14{
15
16 namespace DeepLearning
17 {
26
27 // Initialize DaoAI Deep Learning SDK
28 DAOAI_API void initialize(const bool& use_shared_memory = false, const size_t& reserved_vram_size = 500000000);
29
31
37
38 struct ModelInfo
39 {
42 std::vector<std::string> class_labels;
43 std::vector<int> num_keypoints;
44 std::vector<std::string> keypoint_labels;
45 std::string extra_infos = "";
46 };
47
48 class ModelInstance;
49
50 class Model
51 {
52 public:
53 Model() = default;
54 Model(const std::filesystem::path& model_config_path, const std::filesystem::path& model_weights_path, const DaoAI::DeepLearning::DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
55 Model(const std::filesystem::path& model_path, const DaoAI::DeepLearning::DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
56
57 DAOAI_API virtual ~Model();
58
60
61 /*
62 * @brief Set the batch size for inference
63 * @param batch_size: the batch size for inference
64 */
65 DAOAI_API void setBatchSize(const size_t& batch_size);
66
67 /*
68 * @brief Get the batch size for inference
69 */
70 DAOAI_API size_t getBatchSize() const;
71
72 protected:
73 std::shared_ptr<ModelInstance> model_instance_ = nullptr;
74 };
75
76 class UnsupervisedDefectSegmentationImpl;
77
78 namespace Vision
79 {
80
81
82 class Detection : public Model
83 {
84 public:
85 using Model::Model;
86
87 /*
88 * @brief Set the threshold for how confident the model should be in its predictions
89 * @param threshold: the threshold for how confident the model should be in its predictions
90 */
91 DAOAI_API void setConfidenceThreshold(const float& threshold);
92
93 /*
94 * @brief Get the threshold for how confident the model should be in its predictions
95 */
97
98 /*
99 * @brief Set the threshold for the IOU between the predicted bounding box and the ground truth bounding box
100 * @param threshold: the threshold for the IOU between the predicted bounding box and the ground truth bounding box
101 */
102 DAOAI_API void setIOUThreshold(const float& threshold);
103
104 /*
105 * @brief Get the threshold for the IOU between the predicted bounding box and the ground truth bounding box
106 */
108 };
109
110 class ObjectDetection final : public Detection
111 {
112 public:
113 DAOAI_API ObjectDetection(const std::filesystem::path& model_file, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
114 DAOAI_API ObjectDetection(const std::filesystem::path& model_config_path, const std::filesystem::path& model_weights_path, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
116 DAOAI_API std::vector<ObjectDetectionResult> inference(const std::vector<Image>& images);
117 };
118 class InstanceSegmentation final : public Detection
119 {
120 public:
121 DAOAI_API InstanceSegmentation(const std::filesystem::path& model_file, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
122 DAOAI_API InstanceSegmentation(const std::filesystem::path& model_config_path, const std::filesystem::path& model_weights_path, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
124 DAOAI_API std::vector<InstanceSegmentationResult> inference(const std::vector<Image>& images);
125 };
126 class KeypointDetection final : public Detection
127 {
128 public:
129 DAOAI_API KeypointDetection(const std::filesystem::path& model_file, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
130 DAOAI_API KeypointDetection(const std::filesystem::path& model_config_path, const std::filesystem::path& model_weights_path, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
132 DAOAI_API std::vector<KeypointDetectionResult> inference(const std::vector<Image>& images);
133 };
134 class OCR final : public Model
135 {
136 public:
137 DAOAI_API OCR(const std::filesystem::path& model_file, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
138 DAOAI_API OCR(const std::filesystem::path& model_config_path, const std::filesystem::path& model_weights_path, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
140 DAOAI_API std::vector<OCRResult> inference(const std::vector<Image>& images);
141 };
142 class Classification final : public Model
143 {
144 public:
145 DAOAI_API Classification(const std::filesystem::path& model_file, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
146 DAOAI_API Classification(const std::filesystem::path& model_config_path, const std::filesystem::path& model_weights_path, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
148 DAOAI_API std::vector<ClassificationResult> inference(const std::vector<Image>& images);
149 };
151 {
152 public:
153 DAOAI_API SupervisedDefectSegmentation(const std::filesystem::path& model_file, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
154 DAOAI_API SupervisedDefectSegmentation(const std::filesystem::path& model_config_path, const std::filesystem::path& model_weights_path, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
156 DAOAI_API std::vector<SupervisedDefectSegmentationResult> inference(const std::vector<Image>& images);
157 };
158 class AutoSegmentation final : public Model
159 {
160 public:
161 DAOAI_API AutoSegmentation(const std::filesystem::path& model_file, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
162 DAOAI_API std::vector<ImageEmbedding> generateImageEmbeddings(const std::vector<Image>& images);
164 DAOAI_API std::vector<AutoSegmentationResult> inference(const std::vector<ImageEmbedding>& embeddings, const std::vector<std::vector<Box>>& boxes, const std::vector<std::vector<Point>>& points = std::vector<std::vector<Point>>());
165 DAOAI_API std::vector<AutoSegmentationResult> inference(const std::vector<ImageEmbedding>& embeddings, const std::vector<std::vector<Point>>& points);
166 DAOAI_API AutoSegmentationResult inference(const ImageEmbedding& embedding, const std::vector<Box>& boxes, const std::vector<Point>& points = std::vector<Point>());
167 DAOAI_API AutoSegmentationResult inference(const ImageEmbedding& embedding, const std::vector<Point>& points);
168 };
169
170#ifdef INDUSTRIAL
171 class PresenceChecking final : public Detection
172 {
173 public:
174 DAOAI_API PresenceChecking(const std::filesystem::path& model_file, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
175 DAOAI_API PresenceChecking(const std::filesystem::path& model_config_path, const std::filesystem::path& model_weights_path, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
177 DAOAI_API std::vector<PresenceCheckingResult> inference(const std::vector<Image>& images);
178 };
179
180 class Positioning final : public Detection
181 {
182 public:
183 DAOAI_API Positioning(const std::filesystem::path& model_file, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
184 DAOAI_API Positioning(const std::filesystem::path& model_config_path, const std::filesystem::path& model_weights_path, const DeviceType& device = DeviceType::GPU, const int& device_idx = -1);
186 DAOAI_API std::vector<PositioningResult> inference(const std::vector<Image>& images);
187 };
188
189
191 {
192 public:
197
202
211 const Image& image, const std::string& component_name, float sensitivity);
212
220 DAOAI_API std::vector<UnsupervisedDefectSegmentationResult> inference(
221 const std::vector<Image>& images,
222 const std::vector<std::string>& component_names,
223 const std::vector<float>& sensitivities);
224
225 DAOAI_API std::vector<UnsupervisedDefectSegmentationResult> inference(const std::vector<Image>& images);
226
228
229 DAOAI_API void setBatchSize(const size_t& batch_size);
230
231 DAOAI_API size_t getBatchSize() const;
232
238 DAOAI_API void addComponentMemory(const std::string& component_name, const std::filesystem::path& component_memory_file_path);
239
245 DAOAI_API void addComponentMemory(const std::string& component_name, const ComponentMemory& component_memory);
246
251 DAOAI_API void addComponentArchive(const std::string& archive_path);
252
257 DAOAI_API void removeComponentMemory(const std::string& component_name);
258
263 DAOAI_API std::unordered_map<std::string, ComponentMemory> listComponentMemory();
264
270
277 DAOAI_API ComponentMemory createComponentMemory(const std::string& component_name, const std::vector<Image>& good_images, const std::vector<Image>& bad_images = {}, const std::vector<Image>& masks = {}, const bool& append_memory = false);
278
279 private:
280 std::shared_ptr<UnsupervisedDefectSegmentationImpl> impl_ = nullptr;
281 };
282#endif // INDUSTRIAL
283 }
284 }
285}
#define DAOAI_API
Definition API_EXPORT.h:13
Definition component_memory.h:14
Definition common.h:186
Definition model.h:51
DAOAI_API ModelInfo getModelInfo()
virtual DAOAI_API ~Model()
std::shared_ptr< ModelInstance > model_instance_
Definition model.h:73
DAOAI_API void setBatchSize(const size_t &batch_size)
DAOAI_API size_t getBatchSize() const
Model(const std::filesystem::path &model_config_path, const std::filesystem::path &model_weights_path, const DaoAI::DeepLearning::DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
Model(const std::filesystem::path &model_path, const DaoAI::DeepLearning::DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API std::vector< ImageEmbedding > generateImageEmbeddings(const std::vector< Image > &images)
DAOAI_API AutoSegmentationResult inference(const ImageEmbedding &embedding, const std::vector< Point > &points)
DAOAI_API std::vector< AutoSegmentationResult > inference(const std::vector< ImageEmbedding > &embeddings, const std::vector< std::vector< Point > > &points)
DAOAI_API AutoSegmentation(const std::filesystem::path &model_file, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API ImageEmbedding generateImageEmbeddings(const Image &image)
DAOAI_API std::vector< AutoSegmentationResult > inference(const std::vector< ImageEmbedding > &embeddings, const std::vector< std::vector< Box > > &boxes, const std::vector< std::vector< Point > > &points=std::vector< std::vector< Point > >())
DAOAI_API AutoSegmentationResult inference(const ImageEmbedding &embedding, const std::vector< Box > &boxes, const std::vector< Point > &points=std::vector< Point >())
DAOAI_API std::vector< ClassificationResult > inference(const std::vector< Image > &images)
DAOAI_API ClassificationResult inference(const Image &image)
DAOAI_API Classification(const std::filesystem::path &model_file, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API Classification(const std::filesystem::path &model_config_path, const std::filesystem::path &model_weights_path, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API void setIOUThreshold(const float &threshold)
DAOAI_API void setConfidenceThreshold(const float &threshold)
DAOAI_API float getConfidenceThreshold() const
DAOAI_API float getIOUThreshold() const
DAOAI_API InstanceSegmentation(const std::filesystem::path &model_file, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API InstanceSegmentation(const std::filesystem::path &model_config_path, const std::filesystem::path &model_weights_path, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API std::vector< InstanceSegmentationResult > inference(const std::vector< Image > &images)
DAOAI_API InstanceSegmentationResult inference(const Image &image)
DAOAI_API std::vector< KeypointDetectionResult > inference(const std::vector< Image > &images)
DAOAI_API KeypointDetection(const std::filesystem::path &model_config_path, const std::filesystem::path &model_weights_path, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API KeypointDetectionResult inference(const Image &image)
DAOAI_API KeypointDetection(const std::filesystem::path &model_file, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
Definition model.h:135
DAOAI_API OCRResult inference(const Image &image)
DAOAI_API OCR(const std::filesystem::path &model_file, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API std::vector< OCRResult > inference(const std::vector< Image > &images)
DAOAI_API OCR(const std::filesystem::path &model_config_path, const std::filesystem::path &model_weights_path, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
Definition prediction.h:97
DAOAI_API ObjectDetection(const std::filesystem::path &model_file, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API ObjectDetectionResult inference(const Image &image)
DAOAI_API std::vector< ObjectDetectionResult > inference(const std::vector< Image > &images)
DAOAI_API ObjectDetection(const std::filesystem::path &model_config_path, const std::filesystem::path &model_weights_path, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API Positioning(const std::filesystem::path &model_config_path, const std::filesystem::path &model_weights_path, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API Positioning(const std::filesystem::path &model_file, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API std::vector< PositioningResult > inference(const std::vector< Image > &images)
DAOAI_API PositioningResult inference(const Image &image)
DAOAI_API PresenceChecking(const std::filesystem::path &model_config_path, const std::filesystem::path &model_weights_path, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API PresenceChecking(const std::filesystem::path &model_file, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API PresenceCheckingResult inference(const Image &image)
DAOAI_API std::vector< PresenceCheckingResult > inference(const std::vector< Image > &images)
DAOAI_API std::vector< SupervisedDefectSegmentationResult > inference(const std::vector< Image > &images)
DAOAI_API SupervisedDefectSegmentation(const std::filesystem::path &model_file, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API SupervisedDefectSegmentation(const std::filesystem::path &model_config_path, const std::filesystem::path &model_weights_path, const DeviceType &device=DeviceType::GPU, const int &device_idx=-1)
DAOAI_API SupervisedDefectSegmentationResult inference(const Image &image)
DAOAI_API void addComponentMemory(const std::string &component_name, const ComponentMemory &component_memory)
Add a component memory to the component memory list.
DAOAI_API UnsupervisedDefectSegmentationResult inference(const Image &image, const std::string &component_name, float sensitivity)
Inference function for unsupervised defect segmentation.
DAOAI_API void removeComponentMemory(const std::string &component_name)
Remove a component memory from the component memory list.
DAOAI_API std::vector< UnsupervisedDefectSegmentationResult > inference(const std::vector< Image > &images)
DAOAI_API UnsupervisedDefectSegmentation(const DeviceType &device)
Constructor.
DAOAI_API void addComponentMemory(const std::string &component_name, const std::filesystem::path &component_memory_file_path)
Add a component memory to the component memory list.
DAOAI_API void setBatchSize(const size_t &batch_size)
DAOAI_API void setDetectionLevel(const DetectionLevel &value)
Set the detection level for the base model, whether image-level or pixel-level.
DAOAI_API UnsupervisedDefectSegmentationResult inference(const Image &image)
DAOAI_API std::unordered_map< std::string, ComponentMemory > listComponentMemory()
Get the list of component memory objects.
DAOAI_API void addComponentArchive(const std::string &archive_path)
Add a component memory archive to the component memory list.
DAOAI_API std::vector< UnsupervisedDefectSegmentationResult > inference(const std::vector< Image > &images, const std::vector< std::string > &component_names, const std::vector< float > &sensitivities)
Inference function for unsupervised defect segmentation.
DAOAI_API ComponentMemory createComponentMemory(const std::string &component_name, const std::vector< Image > &good_images, const std::vector< Image > &bad_images={}, const std::vector< Image > &masks={}, const bool &append_memory=false)
Train a component memory.
DAOAI_API int getNumCUDADevices()
DeviceType
Definition common.h:18
ModelType
Definition model.h:19
DAOAI_API void clearGPUMemory()
DAOAI_API void initialize(const bool &use_shared_memory=false, const size_t &reserved_vram_size=500000000)
DetectionLevel
Definition common.h:23
Definition common.h:14
Definition model.h:39
std::vector< std::string > keypoint_labels
Definition model.h:44
std::string extra_infos
Definition model.h:45
std::vector< std::string > class_labels
Definition model.h:42
std::vector< int > num_keypoints
Definition model.h:43
ModelType model_type
Definition model.h:40
DeviceType device
Definition model.h:41