Implementation:Tencent Ncnn Caffe2ncnn
| Knowledge Sources | |
|---|---|
| Domains | Model_Conversion, Caffe_Framework |
| Last Updated | 2026-02-09 19:00 GMT |
Overview
Command-line tool that converts Caffe deep learning models (.prototxt network definition and .caffemodel trained weights) into ncnn's native .param and .bin format.
Description
caffe2ncnn is a model conversion tool that reads Caffe models using Google Protocol Buffers and translates them into ncnn's serialization format. The converter loads the Caffe model in two steps: read_proto_from_text parses the .prototxt network definition and read_proto_from_binary deserializes the .caffemodel weight file with a large byte limit set via SetTotalBytesLimit to handle large models.
The conversion proceeds in two phases. In the first phase, it analyzes the network topology: it counts layers and blob names, handles Caffe's in-place layer pattern (where bottom and top blob names are identical) by decorating blob names with the layer name suffix, and tracks blob reference counts to determine where Split layers need to be inserted. Unlike Caffe's implicit fan-out, ncnn requires explicit Split layers when a blob is consumed by multiple layers.
In the second phase, it iterates over each layer and performs type-specific conversion. Layer type mappings include: Caffe Convolution with groups maps to ncnn ConvolutionDepthWise, MemoryData maps to Input, Python ProposalLayer maps to Proposal, ReLU6 maps to Clip, and Silence maps to Noop. It supports a comprehensive set of Caffe layer types covering BatchNorm, BN/Scale, Concat, Convolution, ConvolutionDepthWise, Crop, Deconvolution, DetectionOutput, Dropout, Eltwise, ELU, Embed, Flatten, InnerProduct, Input, Interp, LRN, LSTM, Normalize, Padding, Permute, Pooling, Power, PReLU, PriorBox, Proposal, PSROIPooling, ReLU, Reshape, ROIPooling, Scale, Sigmoid, Slice, Softmax, TanH, Threshold, and YoloDetectionOutput.
Weight data is written as raw float arrays to the .bin file with appropriate handling for batch normalization scale factors and variance epsilon values.
Usage
Use this tool when you need to deploy a model originally trained in the Caffe framework on mobile or embedded devices via ncnn. Caffe was one of the earliest and most widely-used deep learning frameworks, and many classic architectures (classification, detection, segmentation) were published with Caffe model files.
Code Reference
Source Location
- Repository: Tencent_Ncnn
- File: tools/caffe/caffe2ncnn.cpp
- Lines: 1-1176
Signature
static bool read_proto_from_text(const char* filepath, google::protobuf::Message* message);
static bool read_proto_from_binary(const char* filepath, google::protobuf::Message* message);
int main(int argc, char** argv);
// Usage: caffe2ncnn [caffeproto] [caffemodel] [ncnnparam] [ncnnbin]
Import
// CLI tool - no import needed
// Usage: ./caffe2ncnn [caffeproto] [caffemodel] [ncnnparam] [ncnnbin]
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| caffeproto | file path (string) | Yes | Path to the Caffe .prototxt network definition file |
| caffemodel | file path (string) | Yes | Path to the Caffe .caffemodel binary weight 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") |
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 containing raw float weight data |
Usage Examples
Basic Conversion (default output names)
./caffe2ncnn deploy.prototxt model.caffemodel
# Produces ncnn.param and ncnn.bin in the current directory
Conversion with Custom Output Paths
./caffe2ncnn deploy.prototxt model.caffemodel output.param output.bin