Build a 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) 

Below is the error 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 to use this constructor?

Note. I know how to use the "read file from disk constructor" already, I want to use NetParameter alone or understand why it does not work.

Edit after Shai comment:

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

Here is 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 does not work. I grepped through the source and could not find the version number.

+6
source share
2 answers

There is nothing wrong with the code. There is actually an overloaded constructor of the Net class in C ++, but it is not currently displayed by the python interface. The python interface is limited to a constructor with a file parameter.

I'm not sure that just exposing it in python / caffe / _caffe.cpp is the only thing that keeps us from creating a Python Net object using NetParameter or if more complex changes are needed.

+2
source

I ran into the same problem and I got a solution in the google user group explaining that your C ++ boost lib is too old, you might need to update it.

+2
source

All Articles