Skip to content
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

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

Closed
suruoxi opened this issue Oct 20, 2015 · 21 comments

Comments

@suruoxi
Copy link

suruoxi commented Oct 20, 2015

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

@suruoxi
Copy link
Author

suruoxi commented Oct 21, 2015

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.

@suruoxi suruoxi changed the title Error in python interface. Error in python interface. " Net.__init__(Net, str, str, int) did not match C++ signature" Oct 21, 2015
@suruoxi suruoxi closed this as completed Oct 21, 2015
@ry
Copy link

ry commented Jan 21, 2016

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

@codegeniur
Copy link

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<char, std::__1::char_traits, std::__1::allocator >, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, int)
__init
(boost::python::api::object, std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator >, int)`

@Mithul
Copy link

Mithul commented Apr 30, 2016

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

@lukeyeager
Copy link
Contributor

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.

@kscottz
Copy link

kscottz commented Jun 27, 2016

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)

@ajtulloch
Copy link
Contributor

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

@groakat
Copy link

groakat commented Aug 19, 2016

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

@kravchyuriy
Copy link

kravchyuriy commented Jan 4, 2017

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

@isn4
Copy link

isn4 commented Apr 2, 2017

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

@DaliaMarzouk
Copy link

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

@atakemura
Copy link

atakemura commented Jun 14, 2017

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.

@aniket03
Copy link

aniket03 commented Jun 22, 2017

@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

@flyinskyin2013
Copy link

flyinskyin2013 commented May 25, 2018

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

@kr11
Copy link

kr11 commented Sep 20, 2018

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.

@xiaxinkai
Copy link

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.

@prabodh23
Copy link

prabodh23 commented Dec 13, 2019

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

@prabodh23
Copy link

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 ..!

@yezhengli-Mr9
Copy link

yezhengli-Mr9 commented Jan 7, 2021

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#

@winterswee
Copy link

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#

Hi, I have the same problem, have you solved it?

@NCUTzyx
Copy link

NCUTzyx commented May 25, 2022

Boost.Python.ArgumentError: Python argument types in
rectangle.init(rectangle, numpy.int32, numpy.int32, numpy.int32, numpy.int32)
did not match C++ signature:
init(struct _object * __ptr64, long left, long top, long right, long bottom)
init(struct _object * __ptr64)
博主这个是知道怎末弄吗?球球

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests