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 PeleeNetSSD Seg Example

From Leeroopedia


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

Overview

Concrete tool for combined object detection and semantic segmentation using PeleeNet-SSD with ncnn in a single forward pass.

Description

This example demonstrates a multi-task model that performs both object detection and semantic segmentation simultaneously. It loads a PeleeNet model (converted from MobileNet-YOLO), resizes input to 304x304, and extracts two outputs: the "detection_out" layer for bounding box detections and the "sigmoid" layer for a segmentation map. The segmentation map is resized back to original image dimensions using bilinear interpolation and overlaid as a colored mask. Detection classes include driving-related categories: person, rider, car, bus, truck, bike, motor, traffic light, traffic sign, and train. Segmentation pixels exceeding a 0.45 threshold are blended with the original image using color mapping.

Usage

Use this example for autonomous driving or traffic scene understanding where both object detection and road segmentation are needed from a single efficient model. It is suitable for scenarios requiring simultaneous perception of discrete objects and continuous regions.

Code Reference

Source Location

Signature

static int detect_peleenet(const cv::Mat& bgr, std::vector<Object>& objects, ncnn::Mat& resized);
static void draw_objects(const cv::Mat& bgr, const std::vector<Object>& objects, ncnn::Mat map);
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 304x304

Outputs

Name Type Description
objects std::vector<Object> Detected objects with label, prob, and rect for driving-related classes
resized ncnn::Mat Segmentation map resized to original image dimensions via bilinear interpolation
Visual output cv::imshow window Image with bounding boxes, class labels, and colored segmentation overlay

Model Files

File Description
pelee.param PeleeNet network parameter file
pelee.bin PeleeNet network weight file

Preprocessing

  • Resize: Input resized to 304x304 using ncnn::Mat::from_pixels_resize with PIXEL_BGR
  • Mean values: [103.9, 116.7, 123.6]
  • Norm values: [0.017, 0.017, 0.017]
  • Segmentation post-processing: Output from "sigmoid" layer resized via resize_bilinear to original image dimensions

Usage Examples

Running the Example

./peleenetssd_seg image.jpg

Key Code Pattern

ncnn::Net peleenet;
peleenet.opt.use_vulkan_compute = true;

peleenet.load_param("pelee.param");
peleenet.load_model("pelee.bin");

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

const float mean_vals[3] = {103.9f, 116.7f, 123.6f};
const float norm_vals[3] = {0.017f, 0.017f, 0.017f};
in.substract_mean_normalize(mean_vals, norm_vals);

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

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

ncnn::Mat seg_out;
ex.extract("sigmoid", seg_out);    // Segmentation map
resize_bilinear(seg_out, resized, img_w, img_h);

Related Pages

Page Connections

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