mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Save model/Ndarray in C++

Open zhubuntu opened this issue 10 years ago • 6 comments

I'm training a model in C++ .But I can't find how to save my model/Ndarray to a binary file, so I can load them if I want to use the trained model.

zhubuntu avatar Mar 11 '16 06:03 zhubuntu

Now I know how to save the Ndarray,but still don't know how to save a trained model.

zhubuntu avatar Mar 11 '16 09:03 zhubuntu

Use https://github.com/dmlc/mxnet/blob/master/src/ndarray/ndarray.cc#L573 And save it like

save_dict = {('arg:%s' % k) : v for k, v in arg_params.items()}
save_dict.update({('aux:%s' % k) : v for k, v in aux_params.items()})
param_name = '%s-%04d.params' % (prefix, epoch)
nd.save(param_name, save_dict)

as in model.py:save_checkpoint

piiswrong avatar Mar 11 '16 19:03 piiswrong

@piiswrong Thanks,But when i use dmlc::Stream::Create,it will report unresolved external symbol,I think this is same with #967 ,So I just convert the NDarray to vector and save the vector to the file. And can you tell me how to save the whole trained model,I think this will be very convenient.

zhubuntu avatar Mar 14 '16 07:03 zhubuntu

The whole trained model is just a dictionary of array name to ndarrays. arguments starts with 'arg:' and auxiliary states starts with 'aux:' We can make a c_api for this. You are welcome to do it if you are interested.

piiswrong avatar Mar 14 '16 07:03 piiswrong

@piiswrong I see,Thanks for your help.I'm very glad to help c_api ,but I don't know how to write this and i was very busy with my course,fell very sorry about this.

zhubuntu avatar Mar 14 '16 07:03 zhubuntu

Hi @zhubuntu @piiswrong I am facing issue with dmlc::Stream::Create

Below is what i am getting on running below code in predictor.cc class

const char* fPath = env->GetStringUTFChars(filePath, 0); const char* fFlag = env->GetStringUTFChars(fileFlag, 0); dmlc::Stream *fStream = dmlc::Stream::Create(fPath, fFlag); Issue generated is as below

java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_ZN4dmlc6Stream6CreateEPKcS2_b" referenced by "/data/app/com.happen.it.make.whatsthis-2/lib/arm/libmxnet_predict.so"... at java.lang.Runtime.loadLibrary(Runtime.java:372) at java.lang.System.loadLibrary(System.java:1076) at org.dmlc.mxnet.Predictor.(Predictor.java:5)

Could you please help. Thanks.

gaush123 avatar Aug 04 '16 11:08 gaush123