-
Notifications
You must be signed in to change notification settings - Fork 200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Save/Load a NeuralNet via python? #84
Comments
I tried to hack code to save a net, when I call
Alright I found that not all layer have its weights ....... |
When I try to call a RandomTranslation Layer's getOutputCubeSize(), Python crashed immediately, this function defined in Layer Class as base, and try.....except cannot catch this error @hughperkins |
Where are you seeing RandomTranslationLayer?
|
well
|
Oh, maybe that's the problem, the fact that it isnt defined perhaps? |
Oh, I see the issue I think. But... how are you creating a random translations layer in order to test this? |
issue about outputcubsize addressed in 5a53d11 Test output:
|
getweights() issue addressed in 14716af Also, made it return a numpy tensor (unfortunately 1d for now, but at least its a numpy tensor) Test output:
|
I think pickling/dumping the entire network will take a bit of work. Let's simply save the weights for now, just as you are attempting? |
The main problem I met is how.can I get the layer's info and reuse it, should I analyze the output of asstring()? Sounds .... uh....complicated meanwhile, I had not found a wrapper of predict, am I missing something? |
Bunch of changes in 8fac057 test_setweights test created, and passes:
|
Hmmm, well, the following methods exist:
Which concrete specific information do you need? |
You can do
There's an example of using this in python/test_lowlevel.py |
the crash code is something like the net created in test_deepcl_numpy.py, after created a net call Otherwise, what I want to find is a way that I can get all layer's info(necessary info to rebuild a same net) . |
(for the net weights, there's a c++ class, src/weights/WeightsPersister.h . I can probably wrap that actually. That would allow you to read/write the weights of an entire network. You'd need to know the definiton of that network though) |
I think this is fixed already, in 5a53d11 I guess I should create a new binary release probably right? |
Alright I will try it later , thanks a lot |
( v10.0.0 is building now, shoudl be ready in ~15 minutes ) |
Hmmmm, so, I made it so that getOutputCubeSize() throws a normal python exception. But I guess that you probably want it to return the actual output cube size in fact? :-D |
randomtranslationslayer.getOutputCubeSize fixed in 7e62cb2
|
(v10.0.1 building) |
actually I don't know how does this net works, any suggestion about what should I know when I rebuild a net? I am setting a new mobile for my mom so I cannot test anything now..... |
Well... I guess I'm imagining that you could define the network in a python script, like: network.py
Then, when you train, you'll do something like:
At prediction:
How does this sound? |
(v10.0.1 built https://pypi.python.org/pypi/DeepCL/10.0.1 ) |
(note that you'll need to reinstall the native library too, for 10.0.1. This is beause the fix for getOutputCubeSize is actualy in the native underlying library) |
(the above methodology for save/load network is how it works in torch by the way, eg: |
Yeah, I know that......I plan to write a class to handle that, thank you for your tips about it |
Ok, uh..... I tried to make a class to create network and everything seems great. However, I think that maybe DeepCL can provide an inverse Operation of NetdefToNet.createNetFromNetdef, that is, to generate a netdef string according to a net, so we can just save the InputLayer , the Netdef String and the weights, these can be serialization easier. But there is a question ..... can each property be represented by Netdef string? I am not sure about it. |
Uh....... I can not call |
Hmmmm, you're right, there's a duplicate Actually, you can simply drop the |
Yeah, it is. |
It doesnt mean anything, you can ignore it. Well... actually... so what it was is, I was using it to predict the next-move in Go, 围棋 , so that would be one of 19x19 output positions. I could represent that as 19x19 = 361 neurons, but I decided it felt more natural to rearrange those into a 19x19 grid. So, a fully connected layer, it's fully-connected, but you can imagine the outputs as a tower, where the height of the tower is the number of neurons, and each plane in the tower is 1x1. Or you can rearrange that tower into a square grid, and make there be only one such square. On the whole, unless you have a good reason, you'd probably better keep imageSize to 1, in fully connected layers :-) |
I think I get the point~ :-P |
Ok I try to write a script to rebuild a net and it seems work, However, when I try to use this net to predict somthing, it crashed without any error output run make_pkl.py to build a seriallized net and run crash_code.py to reproduce the crash, DeepCL_sl.py is the module I write, running in python 2.7 Another question is .... what is your numpy version? maybe it is the reason that you can not use numpy.core.multiarray in pickle/cPickle |
I think I had made a same network as the previous one (at least they look same in asString() ) and loaded their weights by using setWeights() function. Technically it can work out a same result as the previous one. |
When I run
|
So, I changed line 23 and 30, to have
|
but right, after making that change, it crashes as you are say. |
Ok, I managed to get this down to the minimum test case to reproduce: test.py:
call_test.py:
Result:
|
Ok, moving the
call_test.py:
result:
(the |
so it means I should pass a cl object into my function instead of create in it? |
That will get your code working now, yes.
Yes, I assume so. Whether I can, eg assign the |
Addressed in 450dba1 I'll create a new binary soonish (I'll probably wait till the next hour, to start the ec2 build instances). |
I try to return cl object with net object and it solved the crash too , therefore it must be a gc-caused problem. Easy to cause a bug, I think. |
Oh I have another request, can I disable the output of cl infos? something like
|
You mean, disable all the spammy stuff, that you've shown in the codeblock? |
yeah, is it possible to easily disable them? Though it is harmless but it is not useful for using an network, either. |
just like the verbose option when booting the hackintosh, if it does not work ,we need it, but they were hidden in default :-P |
Hmmm, seesm like configurable logging will need a bit more work than exposing one switch. Will ponder... |
Seems like this would need some kind of configurable logging framework. I'm not really up on which logging frameworks work well in c++. thoughts? |
Uh...no idea about that...cpp is unfamiliar for me... seems most of cpp program do not need a logging framework... |
Not sure. Basically, in our case we want to be able to turn logging on/off at runtime. So, at the very least would need to create some methods like |
how about google glog |
Hi @hughperkins, I'm looking for a way to save and load the trained net in Python 2.7. Looking at the changes made by @NKUCodingCat seems logical. However I'm unable to find any of his save and load functionality available in the examples provided by DeepCL. By any chance if you have plan to provide public Python API for saving and loading the net in Python 2.7? |
I do not intend to do that. But just through lack of time, rather than any fundamental objection. Is this something you might consider contributing to? |
Noted with thanks. Let me see how I could contribute. |
I read the interface declared in net/NeuralNet.h and NeuralNet.pyx, seems there is not a function provided to dump an entire NeuralNet with trained weight. However, mentioned weight.dat was not found when I use Python Interface to train network. data from getOutput() in NeuralNet.pyx seems very hard to understand. So is it possible to add such a function which can Save/Load entire CNN ( No matter how it works ) to let a trained network can be used in other place? In a word, how to save and reuse a trained net in Python?
Meanwhile, I simply tried cPickle to dump it but it failed.
The text was updated successfully, but these errors were encountered: