Implementation:Tencent Ncnn YOLOv2 Example
| 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 YOLOv2 with a MobileNet-YOLO backbone and ncnn.
Description
This example demonstrates YOLOv2 object detection using a MobileNet backbone (MobileNet-YOLO) with the ncnn framework. It loads the pretrained model from the MobileNet-YOLO project, resizes input images to 416x416, and applies Caffe-YOLOv2-style normalization: first scaling by 0.007843 (1/127.5) and then subtracting a mean of 1.0. This two-step preprocessing is distinct from other SSD examples. The model extracts the "detection_out" layer and parses results in the standard (label, prob, x1, y1, x2, y2) format for the 20 PASCAL VOC classes.
Usage
Use this example for YOLO-based object detection when a lightweight MobileNet backbone is preferred over the original Darknet architecture. It is an early YOLO-series example suitable for understanding the YOLOv2 detection pipeline with ncnn and for comparing against the SSD-based detection examples.
Code Reference
Source Location
- Repository: Tencent_Ncnn
- File: examples/yolov2.cpp
- Lines: 1-147
Signature
static int detect_yolov2(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 416x416 |
Outputs
| Name | Type | Description |
|---|---|---|
| objects | std::vector<Object> | Detected objects with label, prob, and rect for VOC classes |
| Visual output | cv::imshow window | Displayed image with bounding boxes and class labels |
Model Files
| File | Description |
|---|---|
| mobilenet_yolo.param | MobileNet-YOLO network parameter file |
| mobilenet_yolo.bin | MobileNet-YOLO network weight file |
Preprocessing
- Resize: Input resized to 416x416 using
ncnn::Mat::from_pixels_resizewith PIXEL_BGR - Two-step normalization (Caffe-YOLOv2 style):
- Step 1: Scale by norm values [0.007843, 0.007843, 0.007843] (i.e., 1/127.5)
- Step 2: Subtract mean values [1.0, 1.0, 1.0]
- This is equivalent to:
X' = X * 0.007843 - 1.0
Usage Examples
Running the Example
./yolov2 image.jpg
Key Code Pattern
ncnn::Net yolov2;
yolov2.opt.use_vulkan_compute = true;
yolov2.load_param("mobilenet_yolo.param");
yolov2.load_model("mobilenet_yolo.bin");
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR,
bgr.cols, bgr.rows, 416, 416);
// Caffe-YOLOv2-Windows style: X' = X * scale - mean
const float mean_vals[3] = {1.0f, 1.0f, 1.0f};
const float norm_vals[3] = {0.007843f, 0.007843f, 0.007843f};
in.substract_mean_normalize(0, norm_vals);
in.substract_mean_normalize(mean_vals, 0);
ncnn::Extractor ex = yolov2.create_extractor();
ex.input("data", in);
ncnn::Mat out;
ex.extract("detection_out", out);