Skip to content

Classification.h

Go to the source code of this file.

Classification classifies images into several classes determined by the user.

The image is classified into the class with the highest score, which is calculated as the probability of belonging to each class.

Inference Options

See also: SaigeInferenceOption

Class Activation Map

In the Classification function, you can calculate the Class Activation Map. This provides distribution information on the degree of class activation for each pixel in the image. To use this feature, set the calcClassActivationMap value to true and set the flag to SAIGE_SET_CALC_CLASS_ACTIVATION_MAP.

Warning

It cannot be calculated when the optimization feature is applied. In this example, the Class Activation Map is set to be calculated only when the optimize option value is false.

174
175
176
177
178
179
180
181
182
183
184
185
186
// ExampleClsRuntime.cpp:176

if (verbosity >= 3 && optimize == false) {
    options->calcClassActivationMap = true;
    retval = SaigeSetInferenceOption(handle, options, SAIGE_SET_CALC_CLASS_ACTIVATION_MAP);
    if (retval < 0) {
        std::cerr << "SaigeSetInferenceOption() failed - " << SaigeStrError(retval)
                  << std::endl;
        SaigeDestroyInferenceHandle(handle);
        SaigeDestroyInferenceResult(result);
        return retval;
    }
}

Inspection Results

In the Classification function, you can get the Score values per class, the class ID of the classified image, and the inference time.

Additional Scores

You can set the additional Scores per class, SaigeInferenceOption::additionalScores. This allows you to give weight to specific classes. To get the result with this option applied, you must pass true as the second argument to the SaigeGetInferenceResultScore function.

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// ExampleClsRuntime.cpp:256

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