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.

164
165
166
167
168
169
170
171
172
173
174
175
176
// ExampleClsRuntime.cpp:166

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.

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