Implementation:Tencent Ncnn Darknet2ncnn
| Knowledge Sources | |
|---|---|
| Domains | Model_Conversion, Darknet_Framework |
| Last Updated | 2026-02-09 19:00 GMT |
Overview
Command-line tool that converts Darknet deep learning models (.cfg configuration and .weights binary files) into ncnn's native .param and .bin format, with particular focus on YOLO object detection architectures.
Description
darknet2ncnn operates in three phases: config parsing, weight loading, and model writing.
The load_cfg function reads the Darknet .cfg file line by line, parsing INI-style sections (e.g., [convolutional], [shortcut], [route], [yolo], [maxpool], [upsample]) into Section structs. Each section stores layer-specific fields (filters, size, stride, padding, batch_normalize, activation, anchors, masks, classes, etc.) using a field descriptor table that maps string keys to struct member offsets via the update_field function, supporting INT, FLOAT, IARRAY, and STRING field types.
The parse_cfg function translates these Darknet sections into ncnn layer definitions. Convolutional layers are mapped to ncnn Convolution or ConvolutionDepthWise (when groups > 1), with batch normalization parameters folded into separate BatchNorm layers. Activation functions are mapped to ncnn equivalents: leaky to ReLU with slope, mish to Mish, swish/silu to Swish, logistic to Sigmoid, and linear is a no-op. Route layers (concatenation of layer outputs) map to Concat, shortcut layers (element-wise addition) map to Eltwise, maxpool maps to Pooling, and upsample maps to Interp with resize_type=1 (nearest neighbor).
YOLO detection layers are converted to ncnn's Yolov3DetectionOutput with anchor boxes, masks, class counts, confidence thresholds, and scale_x_y parameters. When merge_output is enabled (default), all YOLO output layers across scales are merged into a single detection output layer.
The load_weights function reads Darknet's binary weight file, handling version-dependent header formats (major/minor/revision plus a seen counter that varies between uint32 and uint64 based on version), and loads per-layer weights in Darknet's order: bias, then optionally batch normalization parameters (scales, rolling_mean, rolling_variance), then convolution kernel weights.
Usage
Use this tool when you need to deploy YOLO models (YOLOv3, YOLOv4, and their tiny variants) from the Darknet framework to ncnn. It is a standalone tool with no external dependencies, making it easy to build and use.
Code Reference
Source Location
- Repository: Tencent_Ncnn
- File: tools/darknet/darknet2ncnn.cpp
- Lines: 1-954
Signature
typedef struct Section {
std::string name;
int line_number;
int original_layer_count;
std::unordered_map<std::string, std::string> options;
int w, h, c, inputs, letter_box;
int batch_normalize, filters, size, groups, stride, padding, pad, dilation;
std::string activation;
// ... weights, bias, scales, rolling_mean, rolling_variance
std::string layer_type, layer_name;
std::vector<std::string> input_blobs, output_blobs;
std::vector<std::string> param;
} Section;
void file_error(const char* s);
void fread_or_error(void* buffer, size_t size, size_t count, FILE* fp, const char* s);
int main(int argc, char** argv);
// Usage: darknet2ncnn [darknetcfg] [darknetweights] [ncnnparam] [ncnnbin] [merge_output]
Import
// CLI tool - no import needed
// Usage: ./darknet2ncnn [darknetcfg] [darknetweights] [ncnnparam] [ncnnbin] [merge_output]
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| darknetcfg | file path (string) | Yes | Path to the Darknet .cfg configuration file |
| darknetweights | file path (string) | Yes | Path to the Darknet .weights binary file |
| ncnnparam | file path (string) | No | Output .param file path (defaults to "ncnn.param") |
| ncnnbin | file path (string) | No | Output .bin file path (defaults to "ncnn.bin") |
| merge_output | integer (0 or 1) | No | Merge all YOLO output layers into one (default: 1 = enabled) |
Outputs
| Name | Type | Description |
|---|---|---|
| ncnn.param | text file | ncnn parameter file with magic number 7767517, layer/blob counts, and layer definitions |
| ncnn.bin | binary file | ncnn binary weight file with 4-byte alignment padding per layer |
Usage Examples
Basic Conversion
./darknet2ncnn yolov3.cfg yolov3.weights yolov3.param yolov3.bin
Conversion with Separate YOLO Outputs
./darknet2ncnn yolov4-tiny.cfg yolov4-tiny.weights yolov4-tiny.param yolov4-tiny.bin 0
Default Output Names
./darknet2ncnn yolov3.cfg yolov3.weights
# Produces ncnn.param and ncnn.bin in the current directory