Implementation:Tencent Ncnn YOLOv3 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 YOLOv3 with a MobileNetV2-YOLO backbone and ncnn.
Description
This example demonstrates YOLOv3 object detection using a MobileNetV2 backbone (MobileNetV2-YOLO) with the ncnn framework. It loads the pretrained model, resizes input images to 352x352, and applies mean subtraction (127.5) followed by normalization (0.007843 = 1/127.5). The model extracts the "detection_out" layer and parses detection results in the standard (label, prob, x1, y1, x2, y2) format for the 20 PASCAL VOC classes. Unlike the YOLOv2 example, this uses a single-step normalization call rather than the two-step Caffe-YOLOv2 approach.
Usage
Use this example for YOLOv3-based object detection when a lightweight MobileNetV2 backbone is preferred. It offers improved accuracy over the YOLOv2 MobileNet variant while maintaining efficient inference on mobile hardware.
Code Reference
Source Location
- Repository: Tencent_Ncnn
- File: examples/yolov3.cpp
- Lines: 1-144
Signature
static int detect_yolov3(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 352x352 |
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 |
|---|---|
| mobilenetv2_yolov3.param | MobileNetV2-YOLOv3 network parameter file |
| mobilenetv2_yolov3.bin | MobileNetV2-YOLOv3 network weight file |
Preprocessing
- Resize: Input resized to 352x352 using
ncnn::Mat::from_pixels_resizewith PIXEL_BGR - Mean values: [127.5, 127.5, 127.5]
- Norm values: [0.007843, 0.007843, 0.007843] (i.e., 1/127.5)
- Effect: Normalizes pixel values to the range [-1, 1]
Usage Examples
Running the Example
./yolov3 image.jpg
Key Code Pattern
ncnn::Net yolov3;
yolov3.opt.use_vulkan_compute = true;
yolov3.load_param("mobilenetv2_yolov3.param");
yolov3.load_model("mobilenetv2_yolov3.bin");
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR,
bgr.cols, bgr.rows, 352, 352);
const float mean_vals[3] = {127.5f, 127.5f, 127.5f};
const float norm_vals[3] = {0.007843f, 0.007843f, 0.007843f};
in.substract_mean_normalize(mean_vals, norm_vals);
ncnn::Extractor ex = yolov3.create_extractor();
ex.input("data", in);
ncnn::Mat out;
ex.extract("detection_out", out);
for (int i = 0; i < out.h; i++)
{
const float* values = out.row(i);
// values[0]=label, values[1]=prob, values[2..5]=box coords
}