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 SqueezeNet C API Example

From Leeroopedia


Knowledge Sources
Domains Vision, Image Classification
Last Updated 2026-02-09 19:00 GMT

Overview

Concrete tool for image classification using SqueezeNet v1.1 via the ncnn C API instead of the C++ API, with explicit resource management.

Description

This example demonstrates SqueezeNet v1.1 image classification using ncnn's C-style API functions rather than the C++ API. It performs the same classification task as the standard squeezenet.cpp example but uses C API calls such as ncnn_net_create, ncnn_net_load_param, ncnn_mat_from_pixels_resize, and ncnn_extractor_create. The example resizes input to 227x227, applies mean subtraction (104, 117, 123), and extracts the "prob" layer to produce class probability scores. The top-3 class indices and scores are printed via partial sort. All created objects (mat, extractor, option, net) are explicitly destroyed to demonstrate proper C API resource lifecycle management.

Usage

Use this example as a reference for integrating ncnn into C programs or C-compatible language bindings (e.g., from Objective-C, or via FFI). It demonstrates the complete lifecycle of C API objects from creation through destruction, which is essential when the C++ RAII pattern is not available.

Code Reference

Source Location

Signature

static int detect_squeezenet(const cv::Mat& bgr, std::vector<float>& cls_scores);
static int print_topk(const std::vector<float>& cls_scores, int topk);
int main(int argc, char** argv);

Import

#include "c_api.h"

I/O Contract

Inputs

Name Type Required Description
image_path const char* Yes Path to input image (passed as command-line argument)
bgr const cv::Mat& Yes BGR image loaded by OpenCV, resized internally to 227x227

Outputs

Name Type Description
cls_scores std::vector<float> Classification probability scores for all ImageNet classes
Console output stderr Top-3 class indices and their confidence scores

Model Files

File Description
squeezenet_v1.1.param SqueezeNet v1.1 parameter file
squeezenet_v1.1.bin SqueezeNet v1.1 weight file

Preprocessing

  • Resize: Input resized to 227x227 using ncnn_mat_from_pixels_resize
  • Pixel format: NCNN_MAT_PIXEL_BGR
  • Mean values: [104.0, 117.0, 123.0]
  • Norm values: None (passed as 0)

C API Resource Lifecycle

The example demonstrates the full create-use-destroy pattern for all ncnn C API objects:

// Creation
ncnn_net_t squeezenet = ncnn_net_create();
ncnn_option_t opt = ncnn_option_create();
ncnn_mat_t in = ncnn_mat_from_pixels_resize(...);
ncnn_extractor_t ex = ncnn_extractor_create(squeezenet);

// ... use objects ...

// Destruction (reverse order)
ncnn_mat_destroy(in);
ncnn_mat_destroy(out);
ncnn_extractor_destroy(ex);
ncnn_option_destroy(opt);
ncnn_net_destroy(squeezenet);

Usage Examples

Running the Example

./squeezenet_c_api image.jpg

Key Code Pattern

ncnn_net_t squeezenet = ncnn_net_create();

ncnn_option_t opt = ncnn_option_create();
ncnn_option_set_use_vulkan_compute(opt, 1);
ncnn_net_set_option(squeezenet, opt);

ncnn_net_load_param(squeezenet, "squeezenet_v1.1.param");
ncnn_net_load_model(squeezenet, "squeezenet_v1.1.bin");

ncnn_mat_t in = ncnn_mat_from_pixels_resize(bgr.data, NCNN_MAT_PIXEL_BGR,
    bgr.cols, bgr.rows, bgr.cols * 3, 227, 227, NULL);

const float mean_vals[3] = {104.f, 117.f, 123.f};
ncnn_mat_substract_mean_normalize(in, mean_vals, 0);

ncnn_extractor_t ex = ncnn_extractor_create(squeezenet);
ncnn_extractor_input(ex, "data", in);

ncnn_mat_t out;
ncnn_extractor_extract(ex, "prob", &out);

Related Pages

Page Connections

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