Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tencent Ncnn Mxnet2ncnn

From Leeroopedia


Knowledge Sources
Domains Model_Conversion, MXNet_Framework
Last Updated 2026-02-09 19:00 GMT

Overview

Command-line tool that converts MXNet model files (JSON symbol graph and binary params) into ncnn's native .param and .bin format.

Description

mxnet2ncnn parses MXNet's JSON graph into MXNetNode objects and loads weight parameters into MXNetParam structures, then maps each MXNet operator to equivalent ncnn layer definitions while writing the converted weights.

The MXNetNode class provides a type-safe attribute access system through the AttrProxy inner class, which supports implicit conversion to int, float, std::string, std::vector<int>, and std::vector<float> types. Attribute access methods include attr_i (integer), attr_f (float), attr_s (string), attr_ai (integer array), and attr_af (float array). Each node stores its operator name, node name, attribute map, input connections, and weight references.

The MXNetParam class stores named weight tensors as std::vector<float> data with an optional initialization string.

The main function calls read_mxnet_json to parse the JSON symbol graph and read_mxnet_param to load the binary parameter file, then iterates over all nodes to perform the conversion. MXNet operators (convolution, pooling, batch normalization, activation, fully connected, etc.) are mapped to their ncnn equivalents with appropriate parameter translation.

The converter handles the complete range of MXNet operators including Convolution, Deconvolution, FullyConnected, Pooling, BatchNorm, various activations (ReLU, LeakyReLU, PReLU, ELU, Sigmoid, TanH, Softmax), Concat, ElementWiseSum, SliceChannel, LRN, Flatten, Reshape, Dropout, SoftmaxOutput, and detection-specific layers.

Usage

Use this tool when you need to deploy models trained with Apache MXNet (formerly known as MXNet/Gluon) on mobile and embedded devices via ncnn.

Code Reference

Source Location

Signature

class MXNetNode {
public:
    bool has_attr(const char* key) const;
    bool is_attr_scalar(const char* key) const;

    class AttrProxy {
    public:
        operator int() const;
        operator float() const;
        operator std::string() const;
        operator std::vector<int>() const;
        operator std::vector<float>() const;
    };

    AttrProxy attr(const char* key) const;
    int attr_i(const char* key) const;
    float attr_f(const char* key) const;
    std::string attr_s(const char* key) const;
    std::vector<int> attr_ai(const char* key) const;
    std::vector<float> attr_af(const char* key) const;

    bool is_weight() const;
    bool has_weight(int i) const;
    std::vector<float> weight(int i, int init_len = 0) const;

    std::string op;
    std::string name;
    int output_size;
    std::map<std::string, std::string> attrs;
    std::vector<int> inputs;
};

class MXNetParam {
public:
    std::string name;
    std::vector<float> data;
    std::string init;
};

int main(int argc, char** argv);
// Usage: mxnet2ncnn [mxnetjson] [mxnetparam] [ncnnparam] [ncnnbin]

Import

// CLI tool - no import needed
// Usage: ./mxnet2ncnn [mxnetjson] [mxnetparam] [ncnnparam] [ncnnbin]

I/O Contract

Inputs

Name Type Required Description
mxnetjson file path (string) Yes Path to the MXNet JSON symbol graph file (e.g., model-symbol.json)
mxnetparam file path (string) Yes Path to the MXNet binary parameter file (e.g., model-0000.params)
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 and layer definitions
ncnn.bin binary file ncnn binary weight file containing converted float weight data

Usage Examples

Basic Conversion

./mxnet2ncnn model-symbol.json model-0000.params model.param model.bin

Default Output Names

./mxnet2ncnn model-symbol.json model-0000.params
# Produces ncnn.param and ncnn.bin in the current directory

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment