MCPcopy Create free account
hub / github.com/OpenPPL/ppl.nn / GetClassificationResult

Function GetClassificationResult

samples/cpp/run_model/classification.cpp:75–95  ·  view source on GitHub ↗

get classification result from network output

Source from the content-addressed store, hash-verified

73
74// get classification result from network output
75int32_t GetClassificationResult(const float* scores, const int32_t size) {
76 vector<pair<float, int>> pairs(size);
77 for (int32_t i = 0; i < size; i++) {
78 pairs[i] = make_pair(scores[i], i);
79 }
80
81 auto cmp_func = [](const pair<float, int>& p0, const pair<float, int>& p1) -> bool {
82 return p0.first > p1.first;
83 };
84
85 const int32_t top_k = 5;
86 nth_element(pairs.begin(), pairs.begin() + top_k, pairs.end(), cmp_func); // get top K results & sort
87 sort(pairs.begin(), pairs.begin() + top_k, cmp_func);
88
89 printf("top %d results:\n", top_k);
90 for (int32_t i = 0; i < top_k; ++i) {
91 printf("%dth: %-10f %-10d %s\n", i + 1, pairs[i].first, pairs[i].second, imagenet_labels_tab[pairs[i].second]);
92 }
93
94 return 0;
95}
96
97// run classification model
98int RunClassificationModel(const Mat& src_img, const char* onnx_model_path) {

Callers 1

RunClassificationModelFunction · 0.85

Calls 2

beginMethod · 0.60
endMethod · 0.60

Tested by

no test coverage detected