Ask Your Question
0

load/save neural net from xml file

asked 2018-04-26 02:24:18 -0600

Shivanshu gravatar image

updated 2018-04-26 02:27:00 -0600

i have developed neural network for my facial recognition application ..first i saved my network ..after training using (let say my network object is "neuron") neuron->save("neuron.xml");...further i modified my prgram so that is ignores training program and directly load pre-trained neural config.form "neuron.xml"...

I did:

Ptr<ANN_MLP>neuron=ANN_MLP::loadFromString<ANN_MLP>("neuron.xml");

which encounters ther pharasing error as follows::

OpenCV Error: Parsing error ((null)(1): Valid XML should start with '<?xml ...?>') in icvXMLParse, file C:\builds\master_PackSlave-win64-vc11-shared\opencv\modules\core\src\persistence.cpp, line 2220

i checked the xml file.It started with <?xml version="1.0"?> what could be the mess?? Thank you

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-04-26 02:32:44 -0600

berak gravatar image

Algorithm::loadFromString

loads data from memory , not from a file on disk. it expects a string containing the data, not a filename.

this is useful, if your model comes e.g. from a database, a socket, or is even hardcoded in your program.

to load a model from disk, use:

Ptr<ANN_MLP> neuron = ANN_MLP::load<ANN_MLP>("neuron.xml");
edit flag offensive delete link more

Comments

its still giving error: `OpenCV Error: Unspecified error (The node is neither a map nor an empty collection) in cvGetFileNodeByName, file C:\builds\master_PackSlave-win64-vc11-shared\opencv\modules\core\src\persistence.cpp, line 739

what kind of "node" it is talking about..?

Shivanshu gravatar imageShivanshu ( 2018-04-26 03:01:31 -0600 )edit

can you put the xml on some gist/pastebin ?

which opencv version is it, exactly ?

i tried to reproduce your problem, saving the model from your python code (other question), and loading it back with the line above in c++, -- no problem here.

i can only guess, but the only node in there, which does a "collection" are the weights

berak gravatar imageberak ( 2018-04-26 03:13:59 -0600 )edit

I didnt used gist or pasetbin before....

Shivanshu gravatar imageShivanshu ( 2018-04-26 03:43:24 -0600 )edit

just put it here: https://bpaste.net/

berak gravatar imageberak ( 2018-04-26 03:55:23 -0600 )edit
1

hmm, mine looks like this:

<?xml version="1.0"?>
<opencv_storage>
<opencv_ml_ann_mlp>     <-- there is an additional "top-level" node, for the whole model

are you using different opencv versions to save/load it ?

version, again ?

berak gravatar imageberak ( 2018-04-26 04:06:59 -0600 )edit

opencv 2.4 msvc

Shivanshu gravatar imageShivanshu ( 2018-04-26 04:12:15 -0600 )edit
1

really ? 2.4 does not have ANN_MLP or a loadFromString() function

or did you mean, you saved it from 2.4, and try to load it using 3.x ? that would somehow explain the error, your xml is missing the <opencv_ml_ann_mlp> tag

berak gravatar imageberak ( 2018-04-26 04:25:22 -0600 )edit
1

you could work around it like this (using the read() method instead):

FileStorage fs("neuron.xml",0);
Ptr<ml::ANN_MLP> neuron = Algorithm::read<ml::ANN_MLP>(fs.root());
berak gravatar imageberak ( 2018-04-26 04:27:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-26 02:24:18 -0600

Seen: 554 times

Last updated: Apr 26 '18