Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Tencent Ncnn MobileNetV2 SSDLite Example

From Leeroopedia


Knowledge Sources
Domains Vision, Object Detection
Last Updated 2026-02-09 19:00 GMT

Overview

Concrete tool for object detection on PASCAL VOC classes using MobileNetV2 SSD-Lite with ncnn.

Description

This example demonstrates MobileNetV2 SSD-Lite object detection using the ncnn framework. It loads a pretrained MobileNetV2-SSDLite model (originally from Caffe), resizes input images to 300x300 with mean/norm preprocessing, and extracts detection results for the 20 PASCAL VOC classes. A custom Noop layer is registered as a stub for the "Silence" layer from the original Caffe model that is not natively supported by ncnn. Detected objects are drawn with bounding boxes and class labels using OpenCV.

Usage

Use this example when you need to perform multi-class object detection on images using a lightweight MobileNetV2-based SSD-Lite model. It is suitable for mobile and edge deployment scenarios where detection of common VOC categories (person, car, dog, etc.) is required. It also serves as a reference for registering custom layer stubs when converting models with unsupported layers.

Code Reference

Source Location

Signature

static int detect_mobilenetv2(const cv::Mat& bgr, std::vector<Object>& objects);
static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects);
int main(int argc, char** argv);

Import

#include "net.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 300x300

Outputs

Name Type Description
objects std::vector<Object> Detected objects, each with label (int), prob (float), and rect (cv::Rect_<float>)
Visual output cv::imshow window Displayed image with bounding boxes and class labels

Model Files

File Description
mobilenetv2_ssdlite_voc.param Network parameter file
mobilenetv2_ssdlite_voc.bin Network weight file

Preprocessing

  • Resize: Input resized to 300x300 using ncnn::Mat::from_pixels_resize with PIXEL_BGR
  • Mean values: [127.5, 127.5, 127.5]
  • Norm values: [1/127.5, 1/127.5, 1/127.5]
  • Custom layer: Registers "Silence" layer mapped to a Noop stub via DEFINE_LAYER_CREATOR(Noop)

Usage Examples

Running the Example

./mobilenetv2ssdlite image.jpg

Key Code Pattern

ncnn::Net mobilenetv2;
mobilenetv2.opt.use_vulkan_compute = true;
mobilenetv2.register_custom_layer("Silence", Noop_layer_creator);

mobilenetv2.load_param("mobilenetv2_ssdlite_voc.param");
mobilenetv2.load_model("mobilenetv2_ssdlite_voc.bin");

ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR,
    bgr.cols, bgr.rows, 300, 300);

const float mean_vals[3] = {127.5f, 127.5f, 127.5f};
const float norm_vals[3] = {1.0 / 127.5, 1.0 / 127.5, 1.0 / 127.5};
in.substract_mean_normalize(mean_vals, norm_vals);

ncnn::Extractor ex = mobilenetv2.create_extractor();
ex.input("data", in);

ncnn::Mat out;
ex.extract("detection_out", out);

Related Pages

Page Connections

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