Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct caffe.Net object using NetParameter

From the documentation I thought there was a constructor taking a NetParameter argument,

explicit Net(const NetParameter& param);

but when I try to use it like this:

    import caffe
    from caffe import layers as L
    from google.protobuf import text_format

def logreg(hdf5, batch_size):
    # logistic regression: data, matrix multiplication, and 2-class softmax loss
    n = caffe.NetSpec()
    n.data, n.label = L.HDF5Data(batch_size=batch_size, source=hdf5, ntop=2)
    n.ip1 = L.InnerProduct(n.data, num_output=2, weight_filler=dict(type='xavier'))
    n.accuracy = L.Accuracy(n.ip1, n.label)
    n.loss = L.SoftmaxWithLoss(n.ip1, n.label)
    return n.to_proto()

logreg_str = str(logreg('examples/hdf5_classification/data/test.txt', 10))

net_param = caffe.proto.caffe_pb2.NetParameter()
_ = text_format.Merge(logreg_str, net_param)

print type(net_param);
caffe.Net(net_param, caffe.TEST)

The below error occurs in ipython

<class 'caffe.proto.caffe_pb2.NetParameter'>

--------------------------------------------------------------------------- 
ArgumentError                         Traceback (most recent call last)
<ipython-input-20-edce76ff13a1> in <module>()
     14 
     15 print type(net_param);
---> 16 caffe.Net(net_param, caffe.TEST)

ArgumentError: Python argument types in
    Net.__init__(Net, NetParameter, int) did not match C++ signature:
    __init__(boost::python::api::object, std::string, std::string, int)
    __init__(boost::python::api::object, std::string, int)

So what am I doing wrong here? How do I use this constructor?

Note: I know how use the "read file from disk constructor" already, I want to use the NetParameter one / or understand why it doesn't work.

Edit after Shai's comment:

I acquired caffe using this command on Jul 26, 2015: git clone https://github.com/BVLC/caffe.git

Here's the file on my disk:

~/caffe/src/caffe$ grep NetParameter net.cpp | head -1
Net<Dtype>::Net(const NetParameter& param) {
~/caffe/src/caffe$ ~/caffe/build/tools/caffe -version
caffe

The -version switch appears to do nothing. I grepped through the source and was unable to find a version number.

like image 598
Andrew Avatar asked Aug 11 '15 07:08

Andrew


2 Answers

Nothing wrong with your code. Indeed there's an overloaded constructor of the Net class in C++, but it's currently not exposed by the python interface. The python interface is limited to the constructor with the file param.

I'm not sure if simply exposing it in python/caffe/_caffe.cpp is the only thing keeping us from constructing a python Net object with NetParameter or if more elaborate changes are needed.

like image 135
ypx Avatar answered Oct 12 '22 06:10

ypx


I came across the same problem and I get a solution on google user group, which explains that your c++ boost lib is too old, you may need to update it.

like image 20
yunfeng Avatar answered Oct 12 '22 07:10

yunfeng