Caffe: Error in python interface. " Net.__init__(Net, str, str, int) did not match C++ signature"

Created on 20 Oct 2015  路  19Comments  路  Source: BVLC/caffe

I tried to init a net with the python code:

net = caffe.Net('models/bvlc_reference_caffenet/deploy.prototxt',
                              'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',
                              caffe.TEST)

The following error is reported:

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

I think the python interface is compatible with C++ signature. Then why is this error reported?

OpenSuse 13.02 64bit

Most helpful comment

Well, I solved this problem.
The reason is python str can not convert to c++ std::string .
I change the following lines in $CAFFE_ROOT/python/caffe/_caffe.cpp

shared_ptr<Net<Dtype> > Net_Init( string param_file, int phase) ...
shared_ptr<Net<Dtype> > Net_Init( string param_file, string pretrained_param_file, int phase) ...    

to

shared_ptr<Net<Dtype> > Net_Init( char * param_file, int phase) ...
shared_ptr<Net<Dtype> > Net_Init( char * param_file, char * pretrained_param_file, int phase) ...    

And it's normal now.

All 19 comments

Well, I solved this problem.
The reason is python str can not convert to c++ std::string .
I change the following lines in $CAFFE_ROOT/python/caffe/_caffe.cpp

shared_ptr<Net<Dtype> > Net_Init( string param_file, int phase) ...
shared_ptr<Net<Dtype> > Net_Init( string param_file, string pretrained_param_file, int phase) ...    

to

shared_ptr<Net<Dtype> > Net_Init( char * param_file, int phase) ...
shared_ptr<Net<Dtype> > Net_Init( char * param_file, char * pretrained_param_file, int phase) ...    

And it's normal now.

I hit this same error in Jan 2016. @suruoxi's fix seems to work.

diff --git a/python/caffe/_caffe.cpp b/python/caffe/_caffe.cpp
index 4ea2ec6..4c558cb 100644
--- a/python/caffe/_caffe.cpp
+++ b/python/caffe/_caffe.cpp
@@ -75,7 +75,7 @@ void CheckContiguousArray(PyArrayObject* arr, string name,

 // Net constructor for passing phase as int
 shared_ptr<Net<Dtype> > Net_Init(
-    string param_file, int phase) {
+    char* param_file, int phase) {
   CheckFile(param_file);

   shared_ptr<Net<Dtype> > net(new Net<Dtype>(param_file,
@@ -85,7 +85,7 @@ shared_ptr<Net<Dtype> > Net_Init(

 // Net construct-and-load convenience constructor
 shared_ptr<Net<Dtype> > Net_Init_Load(
-    string param_file, string pretrained_param_file, int phase) {
+    char* param_file, char* pretrained_param_file, int phase) {
   CheckFile(param_file);
   CheckFile(pretrained_param_file);

Maybe has something to do with differing versions of Boost? Please reopen this issue

Hi, I've the same error too and did the above but it didn't work.
How to make this work?

ArgumentError Traceback (most recent call last)
in ()
10 model="numbers_deploy.prototxt"
11 weights="numbers_iter_5000.caffemodel"
---> 12 net = caffe.Net(model,weights)
13 net.set_phase_test()
14 net.set_raw_scale('data', 255.0)

ArgumentError: Python argument types in
Net.init(Net, str, str)
did not match C++ signature:
init(boost::python::api::object, std::1::basic_string, std::__1::allocator >, std::__1::basic_string, std::__1::allocator >, int)
__init
(boost::python::api::object, std::__1::basic_string, std::__1::allocator >, int)`

Running Ubuntu 15.10 and receive the same error on the ipython example 00

net = caffe.Net(model_def,      # defines the structure of the model
                model_weights,  # contains the trained weights
                caffe.TEST)     # use test mode (e.g., don't perform dropout)
Boost.Python.ArgumentError: Python argument types in
Net.__init__(Net, str, str, int)
did not match C++ signature:
 __init__(boost::python::api::object, std::string, std::string, int)
__init__(boost::python::api::object, std::string, int)

the above fix does not work for me

If you take a careful look at the error message, the constructor is expecting strings for the first two arguments. What are the types of model_def and model_weights? They should be filenames.

I ran into this issue recent. I found that just wrapping the input args with str() seemed to get the function signature to match. I.e.

net = caffe.Net(str(model_def),      # defines the structure of the model
                str(model_weights),  # contains the trained weights
                caffe.TEST)     # use test mode (e.g., don't perform dropout)

You may be using Python 3/from future import unicode_literals, in which case wrapping with str() works.

I had this problem, because I had boost installed in anaconda when compiling caffe. Compiling caffe without boost solves this problem.

Reinstalling latest version of Boost from sources resolved that problem for me (Ubuntu 16).

Since this seems to be a boost error, what version of boost is everyone running?

Any one could solve this problem. I am facing the same error and tried many solutions without a luck

Encountered this error today, solved by installing the latest boost from source.

Ubuntu 16.04, anaconda3, Python 3.6, CUDA 8, Boost 1.64

  1. Removed boost from conda (i.e. conda uninstall boost)
  2. Tell b2 to use the stuff that came with conda when building Boost
    $ ./b2 install -j8 cxxflags="-std=c++11 -I<your conda dir>/include/python3.6m"
  3. Make sure conda lib/include are included in Makefile.config
  4. $ make all -j8

Additionally, to get $ make runtest to pass,

  1. Add export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<your local boost root>/boost_1_64_0/lib:<your conda loot>/lib to your .bashrc and refresh

I encountered this error in examples/00-classification.ipynb, where it tried to load trained net/weight with caffe.Net(...).
Initially I had even weirder errors because the default boost on this Ubuntu is 1.58 and conda boost was 1.61, and there was no compatible boost on conda repo for py36, so if it's using 1.61 to build it will later complain it can't find 1.61 in ldconfig and vice versa.

@suruoxi I also faced the same issue and tried the solution you have stated but it didn't work out for me. Do we also need to compile _caffe.cpp again here. I can see a file _caffe.so thought it might be related.

^ Sorted just had to run make pycaffe for it to be compiled again. Thanks for the solution though

I use above way to slove my same problem.
When I create Net, the weight-model path must be string-type. So,If you can not ensure the parameter's type,Please do use str() to convert it to string-type

as @suruoxi said, in the newest version of _caffe.cpp, I replace all parameters with the type of string in Net_Init-like functions with char *. After changing, don't forget re-run make pycaffe.

thanks! I met the same issue, and solved it.

as @kr11 said, in the newest version of _caffe.cpp, I replace all parameters with the type of string in Net_Init-like functions with char *. After changing, don't forget re-run make pycaffe.

Hi All, I am getting same error and i have tried all the solutions which mention above,
I have updated my boost with python3 boost(boost.python3) as well but still its throwing error.
In official documentation they mentioned:
For Python Caffe: Python 2.7 or Python 3.3+, numpy (>= 1.7), boost-provided boost.python

So is the boost.python is dependency or it will work for boost.python3 as well..?

Please guide me if i am losing the track or missing anything??

Installation Info:
Using pip environment, opencv-3.4.3, numpy==1.14, ubuntu-16.04, python-3.5.2

Hi All,
Thanks to him: https://stackoverflow.com/questions/54664703/error-parsing-text-format-caffe-netparameter-5417-message-type-caffe-convolu
which says that: Your version of caffe does not have sparse_ratio as part of its inner_product_param or its convolution_param (see these definitions in caffe.proto).
It seems like the specific code you are trying to run requires its own version of caffe that has a slightly different caffe.proto definitions.

So the caffe model which i am trying to load its belongs to other community name is "Deeplab_v1" which needs its own caffe to build which has pyhton2 version. But i want build that caffe into python3 and i am trying to build and load using official document which is wrong so i make few changes in the files of "Deeplab_v1" and make it compatible with python3. And thats how i solved the problem.

Thanks ..!

Well, I solved this problem.
The reason is python str can not convert to c++ std::string .
I change the following lines in $CAFFE_ROOT/python/caffe/_caffe.cpp

shared_ptr<Net<Dtype> > Net_Init( string param_file, int phase) ...
shared_ptr<Net<Dtype> > Net_Init( string param_file, string pretrained_param_file, int phase) ...    

to

shared_ptr<Net<Dtype> > Net_Init( char * param_file, int phase) ...
shared_ptr<Net<Dtype> > Net_Init( char * param_file, char * pretrained_param_file, int phase) ...    

And it's normal now.

Seems hopeful I made corresponding caffe/python/caffe/_caffe.cpp adjustment:

// Net constructor for passing phase as int
shared_ptr<Net<Dtype> > Net_Init(
-- //    string param_file, int phase) {
++ // https://github.com/BVLC/caffe/issues/3220
++ char *param_file, int phase) {
  CheckFile(param_file);

  shared_ptr<Net<Dtype> > net(new Net<Dtype>(param_file,
      static_cast<Phase>(phase)));
  return net;
}

// Net construct-and-load convenience constructor
shared_ptr<Net<Dtype> > Net_Init_Load(
--//    string param_file, string pretrained_param_file, int phase) {
++// https://github.com/BVLC/caffe/issues/3220
++char * param_file, char * pretrained_param_file, int phase) {
  CheckFile(param_file);
  CheckFile(pretrained_param_file);

from caffe-fast-rcnn but still gets following (although it seems better than this lxmert's cpu-only caffe python27 installation):

Called with args:
Namespace(caffemodel='./resnet101_faster_rcnn_final_iter_320000.caffemodel', cfg_file='/opt/butd/experiments/cfgs/faster_rcnn_end2end_resnet.yml', imgroot='/workspace/images/', outfile='test_obj36.tsv', prototxt='/opt/butd/models/vg/ResNet-101/faster_rcnn_end2end_final/test.prototxt', set_cfgs=None, split='test')
/opt/butd//tools/../lib/fast_rcnn/config.py:288: YAMLLoadWarning: calling yaml.load() without Loader=... is deprecated, as the default Loader is unsafe. Please read https://msg.pyyaml.org/load for full details.
  yaml_cfg = edict(yaml.load(f))
Using config:
{'DATA_DIR': '/opt/butd/data',
 'DEDUP_BOXES': 0.0625,
 'EPS': 1e-14,
 'EXP_DIR': 'faster_rcnn_resnet',
 'GPU_ID': 0,
 'MATLAB': 'matlab',
 'MODELS_DIR': '/opt/butd/models/pascal_voc',
 'PIXEL_MEANS': array([[[102.9801, 115.9465, 122.7717]]]),
 'RNG_SEED': 3,
 'ROOT_DIR': '/opt/butd',
 'TEST': {'AGNOSTIC': False,
          'BBOX_REG': True,
          'HAS_ATTRIBUTES': True,
          'HAS_RELATIONS': False,
          'HAS_RPN': True,
          'MAX_SIZE': 1000,
          'NMS': 0.3,
          'PROPOSAL_METHOD': 'selective_search',
          'RPN_MIN_SIZE': 16,
          'RPN_NMS_THRESH': 0.7,
          'RPN_POST_NMS_TOP_N': 300,
          'RPN_PRE_NMS_TOP_N': 6000,
          'SCALES': [600],
          'SOFT_NMS': 0,
          'SVM': False},
 'TRAIN': {'AGNOSTIC': False,
           'ASPECT_GROUPING': True,
           'BATCH_SIZE': 64,
           'BBOX_INSIDE_WEIGHTS': [1.0, 1.0, 1.0, 1.0],
           'BBOX_NORMALIZE_MEANS': [0.0, 0.0, 0.0, 0.0],
           'BBOX_NORMALIZE_STDS': [0.1, 0.1, 0.2, 0.2],
           'BBOX_NORMALIZE_TARGETS': True,
           'BBOX_NORMALIZE_TARGETS_PRECOMPUTED': True,
           'BBOX_REG': True,
           'BBOX_THRESH': 0.5,
           'BG_THRESH_HI': 0.5,
           'BG_THRESH_LO': 0.0,
           'FG_FRACTION': 0.5,
           'FG_THRESH': 0.5,
           'HAS_ATTRIBUTES': True,
           'HAS_RELATIONS': False,
           'HAS_RPN': True,
           'IMS_PER_BATCH': 1,
           'MAX_SIZE': 1000,
           'MIN_RELATION_FRACTION': 0.25,
           'PROPOSAL_METHOD': 'gt',
           'RPN_BATCHSIZE': 64,
           'RPN_BBOX_INSIDE_WEIGHTS': [1.0, 1.0, 1.0, 1.0],
           'RPN_CLOBBER_POSITIVES': False,
           'RPN_FG_FRACTION': 0.5,
           'RPN_MIN_SIZE': 16,
           'RPN_NEGATIVE_OVERLAP': 0.3,
           'RPN_NMS_THRESH': 0.7,
           'RPN_NORMALIZE_MEANS': [0.0, 0.0, 0.0, 0.0],
           'RPN_NORMALIZE_STDS': [0.1, 0.1, 0.2, 0.2],
           'RPN_NORMALIZE_TARGETS': False,
           'RPN_POSITIVE_OVERLAP': 0.7,
           'RPN_POSITIVE_WEIGHT': -1.0,
           'RPN_POST_NMS_TOP_N': 2000,
           'RPN_PRE_NMS_TOP_N': 12000,
           'SCALES': [600],
           'SNAPSHOT_INFIX': '',
           'SNAPSHOT_ITERS': 10000,
           'USE_FLIPPED': True,
           'USE_PREFETCH': False},
 'USE_GPU_NMS': False}
missing 9/9
START caffe.set_mode_cpu()
Traceback (most recent call last):
  File "extract_nlvr2_image.py", line 203, in <module>
    generate_tsv(args.prototxt, args.caffemodel, image_ids, args.outfile)
  File "extract_nlvr2_image.py", line 90, in generate_tsv
    net = caffe.Net(prototxt, caffe.TEST, weights=weights)
Boost.Python.ArgumentError: Python argument types in
    Net.__init__(Net, str, int)
did not match C++ signature:
    __init__(boost::python::api::object, char*, char*, int)
    __init__(boost::python::api::object, char*, int)
root@2c3a1f2f867c:/workspace/features#
Was this page helpful?
0 / 5 - 0 ratings

Related issues

vladislavdonchev picture vladislavdonchev  路  3Comments

prathmeshrmadhu picture prathmeshrmadhu  路  3Comments

serimp picture serimp  路  3Comments

inferrna picture inferrna  路  3Comments

sdemyanov picture sdemyanov  路  3Comments