跳转至

AnomalyDetection.h

Go to the source code of this file.

异常检测是一种仅学习正常图像并检测异常图像及其区域的功能。

SAIGE VISION提供了仅通过学习正常图像来检测异常图像及其区域的异常检测功能。 异常检测功能将异常区域检测为一个 SaigeSegmentedObject 结构,即一个轮廓。 因此,它类似于分割功能,并额外提供图像是否异常以及图像的整体异常分数。

推理选项

您可以设置是否计算异常检测的推理结果的Mask、Object和Heatmap。 Scoremap默认情况下会被计算,不能设置不计算。

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// ExampleIADRuntime.cpp:155

options->calcScoremap = true;
options->calcHeatmap = true;
options->calcMask = true;
options->calcObject = true;
options->calcObjectAreaAndApplyThreshold = true;
options->calcObjectScoreAndApplyThreshold = true;
unsigned int flags = SAIGE_SET_CALC_SCOREMAP | SAIGE_SET_CALC_HEATMAP |
                     SAIGE_SET_CALC_MASK | SAIGE_SET_CALC_OBJECT |
                     SAIGE_SET_CALC_OBJECT_AREA_AND_APPLY_THRESHOLD |
                     SAIGE_SET_CALC_OBJECT_SCORE_AND_APPLY_THRESHOLD;
retval = SaigeSetInferenceOption(handle, options, flags);
if (retval < 0) {
    std::cerr << "SaigeSetInferenceOption() failed - " << SaigeStrError(retval)
              << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}

See also:

Heatmap

在异常检测中,Scoremap可以被着色为Heatmap。异常检测中的Score以大于或等于0的实数表示,与其他功能不同。 因此,您可以设置与颜色区域对应的最大和最小Score值。训练模型已经计算并设置了适当的Score范围,但如果不清晰可见,您可以调整范围。

192
193
194
195
196
197
198
199
200
201
202
203
// ExampleIADRuntime.cpp:194

options->heatmapColorRangeMin = 1.0;
options->heatmapColorRangeMax = 2.5;
retval = SaigeSetInferenceOption(handle, options, SAIGE_SET_HEATMAP_COLOR_RANGE);
if (retval < 0) {
    std::cerr << "SaigeSetInferenceOption() failed - " << SaigeStrError(retval)
              << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}
  • 更改前

Image

  • 更改后

Image

See also: SaigeInferenceOption::heatmapColorRangeMin, SaigeInferenceOption::heatmapColorRangeMax

推理结果

您可以获得异常检测的基本推理结果,isNg、anomalyScore、scoreMin和scoreMax。

298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// ExampleIADRuntime.cpp:300

// DEFAULT
retval = SaigeGetInferenceResultAnomalyScores(result, &anomalyScore, &scoreMin, &scoreMax);
if (retval < 0) {
    std::cerr << "SaigeGetInferenceResultAnomalyScores() failed - " << SaigeStrError(retval)
              << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}
retval = SaigeGetInferenceResultIsNg(result, &isNg);
if (retval < 0) {
    std::cerr << "SaigeGetInferenceResultIsNg() failed - " << SaigeStrError(retval)
              << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}

See also: SaigeGetInferenceResultAnomalyScores, SaigeGetInferenceResultIsNg

ScoremapIAD

异常检测的推理结果Scoremap表示为float数组,以表示大于或等于0的实数。

343
344
345
346
347
348
349
350
351
352
353
354
355
356
// ExampleIADRuntime.cpp:345

retval = SaigeGetInferenceResultScoremapIAD(result, &scoremap);
if (retval < 0) {
    std::cerr << "SaigeGetInferenceResultScoremapIAD() failed - "
              << SaigeStrError(retval) << std::endl;
    SaigeDestroyInferenceHandle(handle);
    SaigeDestroyInferenceResult(result);
    return retval;
}
if (verbosity >= 4) {
    SaigeShowImage(scoremap, w, h, 1, SaigeImageType::SV_F32, false, false,
                   1 / scoreMax, waitkeyMilliSecond);
}

对于Mask、Object、Heatmap等其他结果,请参考示例代码和API文档。