Implementation:Tencent Ncnn Fasterrcnn Example
| Knowledge Sources | |
|---|---|
| Domains | Vision, Object_Detection |
| Last Updated | 2026-02-09 19:00 GMT |
Overview
Concrete tool for object detection inference using Faster R-CNN (ZF backbone) with ncnn.
Description
This example demonstrates the classic Faster R-CNN two-stage object detection pipeline on the PASCAL VOC 20-class dataset. It loads a ZF (Zeiler-Fergus) Faster R-CNN model converted from the py-faster-rcnn project. The image is resized so the shorter side is 600 pixels while preserving aspect ratio, then preprocessed with ImageNet BGR mean subtraction (102.9801, 115.9465, 122.7717). The network takes both the image data and an im_info tensor containing height, width, and scale. Inference runs in two steps: step one extracts the shared convolutional features (conv5_relu5) and all region proposals (rois); step two iterates over each ROI, feeding it along with the shared features to extract per-class bounding box regression (bbox_pred) and classification probabilities (cls_prob). Post-processing applies per-class NMS with OMP-parallelized quicksort and limits detections to 100 per image. Results are visualized with labeled bounding boxes drawn on the image.
Usage
Use this example to perform multi-class object detection on a single image using the two-stage Faster R-CNN architecture. Detects 20 VOC classes: aeroplane, bicycle, bird, boat, bottle, bus, car, cat, chair, cow, diningtable, dog, horse, motorbike, person, pottedplant, sheep, sofa, train, tvmonitor.
Code Reference
Source Location
- Repository: Tencent_Ncnn
- File: examples/fasterrcnn.cpp
- Lines: 1-352
Signature
static int detect_fasterrcnn(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"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| imagepath | const char* (argv[1]) | Yes | Path to input image |
Outputs
| Name | Type | Description |
|---|---|---|
| objects | std::vector<Object> | Detected objects with bounding box (Rect_<float>), class label (int), and probability (float) |
| visualization | cv::Mat | Image with drawn bounding boxes and class labels displayed via cv::imshow |
Usage Examples
Running the Example
./fasterrcnn image.jpg
Key Code Pattern
ncnn::Net fasterrcnn;
fasterrcnn.opt.use_vulkan_compute = true;
fasterrcnn.load_param("ZF_faster_rcnn_final.param");
fasterrcnn.load_model("ZF_faster_rcnn_final.bin");
ncnn::Mat in = ncnn::Mat::from_pixels_resize(bgr.data, ncnn::Mat::PIXEL_BGR, bgr.cols, bgr.rows, w, h);
const float mean_vals[3] = {102.9801f, 115.9465f, 122.7717f};
in.substract_mean_normalize(mean_vals, 0);
ncnn::Mat im_info(3);
im_info[0] = h; im_info[1] = w; im_info[2] = scale;
// Step 1: extract shared features and region proposals
ncnn::Extractor ex1 = fasterrcnn.create_extractor();
ex1.input("data", in);
ex1.input("im_info", im_info);
ncnn::Mat conv5_relu5, rois;
ex1.extract("conv5_relu5", conv5_relu5);
ex1.extract("rois", rois);
// Step 2: per-ROI classification and bbox regression
for (int i = 0; i < rois.c; i++)
{
ncnn::Extractor ex2 = fasterrcnn.create_extractor();
ncnn::Mat roi = rois.channel(i);
ex2.input("conv5_relu5", conv5_relu5);
ex2.input("rois", roi);
ncnn::Mat bbox_pred, cls_prob;
ex2.extract("bbox_pred", bbox_pred);
ex2.extract("cls_prob", cls_prob);
// ... decode and apply bbox regression
}