wkcn / MobulaOP

A Simple & Flexible Cross Framework Operators Toolkit
MIT License
164 stars 21 forks source link

CustomOp in python and C++ for prediction #3

Closed nuaawzl closed 6 years ago

nuaawzl commented 6 years ago

Hello, it's very useful! I have a problem, I define a custom operator in mxnet(python) and get a model. Now I want to load the model(.json & .params ) by mxnet(C++). Can you give me some advice? thanks.

wkcn commented 6 years ago

Thank you:) Do you want to load the parameters in C++? I think you can try the function

  static void Load(const std::string &file_name,
                   std::vector<NDArray> *array_list = nullptr,
                   std::map<std::string, NDArray> *array_map = nullptr);

or

MXNET_DLL int MXNDArrayLoad(const char* fname,
                            mx_uint *out_size,
                            NDArrayHandle** out_arr,
                            mx_uint *out_name_size,
                            const char*** out_names);

in $MXNET_HOME/cpp-package/include/mxnet-cpp/ndarray.h and $MXNET_HOME/include/mxnet/c_api.h.

And there are some examples to use the functions.(https://github.com/apache/incubator-mxnet/blob/master/cpp-package/example/feature_extract/feature_extract.cpp

https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/ndarray/utils.py)

nuaawzl commented 6 years ago

Thanks, I want to load the symbol and parameters in C++. But the model use a custom operator. MXNet C++ has no the operator, so I can't load the json file.

wkcn commented 6 years ago

@nuaawzl I found MXSymbolCreateFromFile is used to load all symbols, including Custom Operator. And the custom operator is registered in python/mxnet/operator.py.

I think it may be a solution.

  1. get creator_func of the custom operator in Python. Cython may be useful.
  2. use MXCustomOpRegister to register the custom operator in C++
  3. use MXSymbolCreateFromFile to load all symbols in C++
nuaawzl commented 6 years ago

Thank you very much. I coded the new operator and recompiled mxnet. I'll try your advice.