Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tencent Ncnn Top K Result Sorting

From Leeroopedia


Knowledge Sources
Domains Computer_Vision, Classification
Last Updated 2026-02-09 00:00 GMT

Overview

Pattern implementation for selecting top-K classification predictions using STL partial sort, as demonstrated in ncnn example code.

Description

This is a Pattern Doc — it documents a user-implemented pattern rather than a library API. The ncnn examples (squeezenet.cpp, shufflenetv2.cpp) implement top-K result selection using std::partial_sort with a std::greater comparator on score-index pairs. This pattern extracts the output ncnn::Mat (1D tensor of per-class scores) into a vector of (score, index) pairs, then partially sorts to find the K highest.

Class labels are typically loaded from a separate file (e.g., synset_words.txt for ImageNet) and indexed by the class index from the sorted results.

Usage

Implement this pattern as the post-processing step after classification network inference. Copy and adapt the print_topk function from the ncnn examples.

Code Reference

Source Location

  • Repository: ncnn
  • File: examples/squeezenet.cpp
  • Lines: L49-71 (print_topk function)

Signature

// Pattern: Top-K result extraction from ncnn classification output
// This is example code to adapt, not a library API

static int print_topk(const std::vector<float>& cls_scores, int topk)
{
    int size = cls_scores.size();
    std::vector<std::pair<float, int> > vec;
    vec.resize(size);
    for (int i = 0; i < size; i++)
    {
        vec[i] = std::make_pair(cls_scores[i], i);
    }

    std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
                      std::greater<std::pair<float, int> >());

    for (int i = 0; i < topk; i++)
    {
        float score = vec[i].first;
        int index = vec[i].second;
        fprintf(stderr, "%d = %f\n", index, score);
    }
    return 0;
}

Import

#include <algorithm>  // std::partial_sort
#include <vector>
#include <utility>    // std::pair

I/O Contract

Inputs

Name Type Required Description
cls_scores std::vector<float> Yes Per-class scores extracted from ncnn::Mat output
topk int Yes Number of top predictions to return (e.g., 3, 5)

Outputs

Name Type Description
sorted pairs std::vector<std::pair<float,int>> Top-K (score, class_index) pairs sorted by descending score

Usage Examples

Complete Classification Pipeline

#include "net.h"
#include <algorithm>
#include <vector>

// After inference: out is ncnn::Mat with w = num_classes
ncnn::Mat out;
ex.extract("prob", out);

// Extract scores
std::vector<float> cls_scores(out.w);
for (int j = 0; j < out.w; j++)
    cls_scores[j] = out[j];

// Top-5 selection
int topk = 5;
std::vector<std::pair<float, int> > vec(out.w);
for (int i = 0; i < out.w; i++)
    vec[i] = std::make_pair(cls_scores[i], i);

std::partial_sort(vec.begin(), vec.begin() + topk, vec.end(),
                  std::greater<std::pair<float, int> >());

for (int i = 0; i < topk; i++)
    printf("class %d: %.4f\n", vec[i].second, vec[i].first);

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment