Skip to content

AnomalyDetection.h

Go to the source code of this file.

Anomaly Detection is a function that learns only normal images and detects abnormal images and their areas.

SAIGE VISION provides the Anomaly Detection function that detects abnormal images and their areas by learning only normal images. The Anomaly Detection function detects the abnormal area as a SaigeSegmentedObject structure, which is a Contour. Therefore, it is similar to the Segmentation function, and additionally provides whether the image is abnormal, and the overall abnormal score of the image.

Inference Options

You can set whether to calculate the Mask, Object, and Heatmap, which are the inference results of Anomaly Detection. The Scoremap is calculated by default and cannot be set not to be calculated.

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// ExampleIADRuntime.cpp:145

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

In Anomaly Detection, the Scoremap can be colored as a Heatmap. The Score in Anomaly Detection is expressed as a real number greater than or equal to 0, unlike other functions. Therefore, you can set the maximum and minimum Score values corresponding to the color area. The trained model has a suitable Score range calculated and set, but you can adjust the range if it is not clearly visible.

182
183
184
185
186
187
188
189
190
191
192
193
// ExampleIADRuntime.cpp:184

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;
}
  • Before

Image

  • After

Image

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

Inference Results

You can get the basic inference results of Anomaly Detection, isNg, anomalyScore, scoreMin, and scoreMax.

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// ExampleIADRuntime.cpp:290

// 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

The Scoremap, which is the inference result of Anomaly Detection, is represented as a float array to express a real number greater than or equal to 0.

333
334
335
336
337
338
339
340
341
342
343
344
345
346
// ExampleIADRuntime.cpp:335

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);
}

For other results such as Mask, Object, Heatmap, please refer to the example code and API documentation.