Workflow:Tencent Ncnn Image Classification Inference
| Knowledge Sources | |
|---|---|
| Domains | Image_Classification, Inference, Computer_Vision |
| Last Updated | 2026-02-09 19:00 GMT |
Overview
End-to-end process for running image classification inference using ncnn with pre-trained models such as SqueezeNet, ShuffleNetV2, or any classification network.
Description
This workflow demonstrates the canonical ncnn inference pattern for image classification tasks. It loads a pre-converted ncnn model (.param and .bin files), reads an input image, converts it to the ncnn tensor format with appropriate preprocessing (resize, mean subtraction, normalization), runs the forward pass through the network, and interprets the output probability vector to produce top-K class predictions. This is the simplest and most fundamental ncnn usage pattern.
Key outcomes:
- Classification scores for all classes (e.g., 1000 ImageNet categories)
- Top-K predicted class indices with confidence scores
Usage
Execute this workflow when you have a classification model already converted to ncnn format and need to classify images on a device. This is the foundational inference pattern that all other ncnn workflows build upon.
Execution Steps
Step 1: Load the Classification Model
Create an ncnn::Net instance and load the model's .param (network graph) and .bin (weights) files. Optionally enable Vulkan GPU compute for accelerated inference on supported devices.
Key considerations:
- Check return values of load_param and load_model for errors
- Set net.opt.use_vulkan_compute = true before loading to enable GPU inference
- Models can be loaded from file paths, FILE pointers, or embedded memory arrays
Step 2: Read and Preprocess the Input Image
Load the input image using OpenCV (or ncnn's built-in simpleocv). Convert the raw pixel data to an ncnn::Mat using from_pixels_resize, which simultaneously converts the pixel format and resizes to the model's expected input dimensions. Apply mean subtraction and normalization to match the training preprocessing.
Key considerations:
- Match the input dimensions to what the model expects (e.g., 224x224 or 227x227)
- Use the correct pixel format conversion (e.g., PIXEL_BGR for models trained on BGR input)
- Mean values and normalization factors must match the training pipeline exactly
- substract_mean_normalize accepts separate mean and norm arrays per channel
Step 3: Run Forward Pass
Create an ncnn::Extractor from the Net object. Set the input blob with the preprocessed Mat, then extract the output blob containing classification probabilities. The output is typically a 1D Mat where each element corresponds to a class score.
Key considerations:
- Input and output blob names come from the .param file (e.g., "data" for input, "prob" for output)
- The Extractor handles all intermediate computation automatically
- For repeated inference, create a new Extractor each time (they are lightweight)
Step 4: Interpret Classification Results
Read the output Mat to extract per-class scores. Sort the scores to find the top-K predictions and map the indices to human-readable class labels (e.g., using the ImageNet synset_words.txt label file).
Key considerations:
- Output scores may be raw logits or softmax probabilities depending on the model
- Use partial sort for efficient top-K extraction
- Class indices correspond to the training label order