Common.h
Go to the source code of this file.
说明检查程序的一般过程。
以下是分类示例代码的一部分。
入门
首先,设置要使用的图像数据和模型文件的路径。在此示例中,我们使用了安装在 SAIGE_HOME 路径中的示例数据和模型文件。
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 | // ExampleClsRuntime.cpp:21
std::string dataDir =
get_env_var("SAIGE_HOME") + "/" + get_runtime_base() + "/engine/Examples/data/sfaw2023/images/test/";
std::string trainedModelPath = dataDir + "../../models/cls_example.saigecls";
// image paths to classify
// used filesystem library of c++17 standard.
// u8string means 8byte character string so, it doesn't support unicode system of filesystem.
std::vector<std::string> imagePaths;
for (const auto& file : std::filesystem::recursive_directory_iterator(dataDir)) {
if (file.is_regular_file() && file.path().extension() == ".png") {
imagePaths.push_back(std::string(file.path().u8string()));
}
}
|
创建句柄
接下来,为检查句柄和检查结果创建句柄。使用 SaigeHandleConfig 结构在创建句柄时设置的选项。 此外,为了应用设置的选项值,必须适当设置相关标志。 如果要使用优化选项,则添加 SAIGE_OPTIMIZE_HANDLE 标志。
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 | // ExampleClsRuntime.cpp:50
SaigeInferenceHandle handle = nullptr;
SaigeHandleConfig config;
config.batchSize = 1;
unsigned int handleFlags = SAIGE_SET_BATCH_SIZE;
if (optimize) {
handleFlags |= SAIGE_OPTIMIZE_HANDLE;
}
retval = SaigeCreateInferenceHandle(&handle, kProjectTypeCLS, trainedModelPath.c_str(),
deviceIndex, &config, handleFlags);
if (retval < 0) {
std::cerr << "SaigeCreateInferenceHandle() failed - " << SaigeStrError(retval) << std::endl;
return retval;
}
SaigeInferenceResult result = nullptr;
retval = SaigeCreateInferenceResult(&result);
if (retval < 0) {
std::cerr << "SaigeCreateInferenceResult() failed - " << SaigeStrError(retval) << std::endl;
SaigeDestroyInferenceHandle(handle);
return retval;
}
|
使用结果句柄
从结果句柄获取的推断结果在销毁结果句柄或用于下一个检查时会自动释放。 在 SAIGE VISION 中,数组形式的推断结果数据的内存管理由结果句柄自动执行。 当结果句柄用于下一个检查或调用 SaigeDestroyInferenceResult 函数时分配的内存被释放。
获取模型信息
使用 SaigeModelInfo 结构检索模型信息。
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 | // ExampleClsRuntime.cpp:77
std::cout << SaigeGetModuleVersion() << std::endl;
SaigeModelInfo* modelInfo = new SaigeModelInfo;
retval = SaigeGetModelInfo(handle, modelInfo);
if (retval < 0) {
std::cerr << "SaigeGetModelInfo() failed - " << SaigeStrError(retval) << std::endl;
} else if (verbosity >= 1) {
std::cout << "[Classification Model Info]" << std::endl;
std::cout << "Network Type: " << modelInfo->networkType << std::endl;
std::cout << "Number of Classes: " << modelInfo->numOfClasses << std::endl;
for (auto i = 0; i < modelInfo->numOfClasses; ++i) {
uint8_t red, green, blue;
SaigeColorCodeToRGB(modelInfo->classColors[i], &red, &green, &blue);
std::cout << modelInfo->classNames[i] << ": color RGB(" << (int)red << ", " << (int)green
<< ", " << (int)blue << ") - " << (modelInfo->classIsNg[i] ? "NG" : "OK")
<< std::endl;
}
std::cout << "Trained Developer Version: " << modelInfo->developerVersion << std::endl;
std::cout << "Server Version: " << modelInfo->serverVersion << std::endl;
std::cout << "Module Version: " << modelInfo->moduleVersion << std::endl;
std::cout << "TrainingTimeStarted: " << modelInfo->trainingTimeStarted << std::endl;
std::cout << "TrainingTimeCompleted: " << modelInfo->trainingTimeCompleted << std::endl;
std::cout << "TrainingTimeElapsed: " << modelInfo->trainingTimeElapsed << std::endl;
std::cout << "UsedGpuName: " << modelInfo->usedGpuName << std::endl;
std::cout << "Number of Train Images: " << modelInfo->numOfTrainImages << std::endl;
std::cout << "Number of Validation Images: " << modelInfo->numOfValidationImages
<< std::endl;
std::cout << "Number of Iterations: " << modelInfo->numOfIterations << std::endl;
std::cout << "ROI Info" << std::endl
<< "- ROI Mode: " << modelInfo->roiInfo.roiMode << std::endl
<< "- ROI LTRB Ratio: " << modelInfo->roiInfo.left << ", " << modelInfo->roiInfo.top
<< ", " << modelInfo->roiInfo.right << ", " << modelInfo->roiInfo.bottom
<< std::endl;
std::cout << "Image Resize Width, Height: " << modelInfo->imageResizeWidth << ", "
<< modelInfo->imageResizeHeight << std::endl;
if (modelInfo->roiInfo.blindMask != nullptr) {
// show blind mask
SaigeShowImage(modelInfo->roiInfo.blindMask, modelInfo->roiInfo.blindMaskWidth,
modelInfo->roiInfo.blindMaskHeight, 1, SaigeImageType::SV_U8, false, true,
255.0, 0);
}
}
|
设置推断选项
使用 SaigeInferenceOption 结构和 Get、Set 函数设置推断期间要使用的选项。
126
127
128
129
130
131
132
133
134
135 | // ExampleClsRuntime.cpp:128
SaigeInferenceOption* options = new SaigeInferenceOption;
retval = SaigeGetInferenceOption(handle, options);
if (retval < 0) {
std::cerr << "SaigeGetInferenceOption() failed - " << SaigeStrError(retval) << std::endl;
SaigeDestroyInferenceHandle(handle);
SaigeDestroyInferenceResult(result);
return retval;
}
|
根据程序设置,可以设置其他选项。在这里,使用 bool detailedTime 值设置 SaigeInferenceOption::calcTime 值。在这种情况下,必须将指定要设置的选项值的标志作为 SaigeSetInferenceOption 函数的第三个参数传递。
137
138
139
140
141
142
143
144
145
146
147
148
149 | // ExampleClsRuntime.cpp:139
if (detailedTime) {
options->calcTime = true;
retval = SaigeSetInferenceOption(handle, options, SAIGE_SET_CALC_TIME);
if (retval < 0) {
std::cerr << "SaigeSetInferenceOption() failed - " << SaigeStrError(retval)
<< std::endl;
SaigeDestroyInferenceHandle(handle);
SaigeDestroyInferenceResult(result);
return retval;
}
}
|
检查
在这里,将图像文件加载到数组中并进行检查。要加载图像数据,请使用 SaigeGetImageData 函数。在这种情况下,您将接收加载的图像数据的宽度、高度、通道和类型。
216
217
218
219
220
221
222
223 | // ExampleClsRuntime.cpp:218
// 이미지 데이터를 로드합니다.
ucImageData = (unsigned char*)SaigeGetImageData(img.c_str(), &w, &h, &c, &type);
if (ucImageData == nullptr) {
std::cerr << "SaigeGetImageData() failed - " << img << std::endl;
continue;
}
|
imageFlag 值设置图像数据是 8 位还是 16 位、通道的顺序等。对于使用 SaigeGetImageData 函数加载的图像,可以使用 SaigeConvertImageType 函数设置 imageFlag 值。 然后,使用 SaigeInferFromByteArray 函数执行推断。
232
233
234
235
236
237
238
239
240 | // ExampleClsRuntime.cpp:234
unsigned int imageFlag = SaigeConvertImageType(c, type);
retval = SaigeInferFromByteArray(handle, ucImageData, w, h, imageFlag, result);
if (retval < 0) {
std::cerr << "SaigeInferFromByteArray() failed - " << SaigeStrError(retval)
<< std::endl;
continue;
}
|
要获取推断结果,请使用适合项目类型的 Get Result 函数。
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275 | // ExampleClsRuntime.cpp:246
std::vector<float> score;
int classId;
score.resize(modelInfo->numOfClasses);
retval = SaigeGetInferenceResultScore(result, score.data(), true);
if (retval < 0) {
std::cerr << "SaigeGetInferenceResultScore() failed - " << SaigeStrError(retval)
<< std::endl;
SaigeDestroyInferenceHandle(handle);
SaigeDestroyInferenceResult(result);
return retval;
}
retval = SaigeGetInferenceResultClass(result, &classId);
if (retval < 0) {
std::cerr << "SaigeGetInferenceResultClass() failed - " << SaigeStrError(retval)
<< std::endl;
SaigeDestroyInferenceHandle(handle);
SaigeDestroyInferenceResult(result);
return retval;
}
retval = SaigeGetInferenceResultInspectionTime(result, &imreadTime, &inferenceTime,
&postProcessingTime);
if (retval < 0) {
std::cerr << "SaigeGetInferenceResultClass() failed - " << SaigeStrError(retval)
<< std::endl;
SaigeDestroyInferenceHandle(handle);
SaigeDestroyInferenceResult(result);
return retval;
}
|
See also: SaigeGetInferenceResultClass, SaigeGetInferenceResultScore, SaigeGetInferenceResultInspectionTime
清理
如果为模型信息和推断选项等结构分配了内存,则必须释放内存。
333
334
335
336
337
338
339
340
341
342
343 | // ExampleClsRuntime.cpp:335
if (ucImageData != nullptr) {
retval = SaigeFreeImageData(ucImageData);
if (retval < 0) {
std::cerr << "SaigeFreeImageData() Failed - " << SaigeStrError(retval) << std::endl;
}
ucImageData = nullptr;
}
delete modelInfo;
delete options;
|
最后,销毁检查句柄和结果句柄。
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362 | // ExampleClsRuntime.cpp:350
retval = SaigeDestroyInferenceResult(result);
if (retval < 0) {
std::cerr << "SaigeDestroyInferenceResult() failed - " << SaigeStrError(retval)
<< std::endl;
return retval;
}
retval = SaigeDestroyInferenceHandle(handle);
if (retval < 0) {
std::cerr << "SaigeDestroyInferenceHandle() failed - " << SaigeStrError(retval)
<< std::endl;
return retval;
}
|