facebookarchive / caffe2

Caffe2 is a lightweight, modular, and scalable deep learning framework.
https://caffe2.ai
Apache License 2.0
8.42k stars 1.95k forks source link

EnforceNotMet when creating caffe2::Predictor #347

Closed mjohnst closed 7 years ago

mjohnst commented 7 years ago

I am implementing C++ API in Objective-C++. Here's a summarization of my logic

static caffe2::NetDef _initNet, _predictNet;
static caffe2::Predictor *_predictor;

void ReadProtoIntoNet(std::string fname, caffe2::NetDef* net) {
    int file = open(fname.c_str(), O_RDONLY);
    CAFFE_ENFORCE(net->ParseFromFileDescriptor(file));
    close(file);
}

std::string FilePathForResourceName(NSString* name, NSString* extension) {
    NSString* file_path = [[NSBundle mainBundle] pathForResource:name ofType:extension];
    if (file_path == NULL) {
        LOG(FATAL) << "Couldn't find '" << [name UTF8String] << "."
        << [extension UTF8String] << "' in bundle.";
        return nullptr;
    }
    return file_path.UTF8String;
}

ReadProtoIntoNet(FilePathForResourceName(@"Protobufs/Squeezenet/exec_net", @"pb"), &_initNet);
ReadProtoIntoNet(FilePathForResourceName(@"Protobufs/Squeezenet/predict_net", @"pb"), &_predictNet);

_predictNet.set_name("PredictNet");
_predictor = new caffe2::Predictor(_initNet, _predictNet);

Using Squeezenet Caffe2 Zoo pbs

This is my error when I hit the Predictor constructor:


[E operator.cc:72] Cannot find operator schema for GivenTensorFill. Will skip schema checking.
libc++abi.dylib: terminating with uncaught exception of type caffe2::EnforceNotMet: 
[enforce fail at operator.cc:108] op. Cannot create operator of type 'GivenTensorFill' on the device 'CPU'. Verify that implementation for the corresponding device exist. It might also happen if the binary is not linked with the operator implementation code. If Python frontend is used it might happen if dyndep.InitOpsLibrary call is missing. 
Operator def: output: "conv1_w" name: "" type: "GivenTensorFill" arg ......... (goes on to print all float values for shape)

Linked libs:

libCaffe2_CPU.a, libprotobuf-lite.a, libprotobuf.a, libprotoc.a, MobileCoreServices.framework

My lib search paths:

/Users/mjohnst/src/caffe2/install/lib

My header search paths:

/Users/mjohnst/src/caffe2/install/include, /Users/mjohnst/src/caffe2/third-party/eigen

I am able to compile & run on device with the header for GivenTensorFill imported too

#import "caffe2/operators/given_tensor_fill_op.h"
Yangqing commented 7 years ago

Ah, this happens when the libraries are not linked with whole static library flags. I assume that you are running it on Mac OS? The way to do it is to pass

-Wl,-force_load,libCaffe2_CPU.a

which ensures that Caffe2's operators are registered.

edit: fixed typo from force-load to force_load.

mjohnst commented 7 years ago

Thanks for the response @Yangqing, I am building it on Mac OS, I'll try passing those flags.

mjohnst commented 7 years ago

I think this works since it's uncovered new build issues with OpenCV (don't have the framework added yet). The flag has a typo in your post for anyone else referencing: it's -force_load instead of -force-load I'll post back if the error continues after I resolve OpenCV issues.

Yangqing commented 7 years ago

Sounds perfect. You can optionally do -DUSE_OPENCV=OFF to turn off opencv.

RobertBiehl commented 7 years ago

@mjohnst Please let me know if you've been successful. I cannot quite solve this yet.

jquave commented 7 years ago

@mjohnst I was able to get this working taking a similar approach: Caffe2 iOS Tutorial

mjohnst commented 7 years ago

I haven't had success yet. I'm attempting to re-build without OpenCV, but am getting some new errors I didn't have last time around.

First modified Makefile and added -DUSE_OPENCV=OFF -DUSE_CUDA=OFF ran make from root directory and sudo make install from build directory (building for macOS). Second I modified scripts/build_ios.sh and added -DUSE_OPENCV=OFF and ran it.

Upon running sudo make install from build_ios I encountered:

CMake Error at third_party/protobuf/cmake/cmake_install.cmake:47 (file):
  file INSTALL cannot find
  "/Users/mjohnst/src/caffe2/build_ios/third_party/protobuf/cmake/libprotoc.a".
Call Stack (most recent call first):
  cmake_install.cmake:32 (include)

Looking into fixing these errors, then I should hopefully make some more progress :)

@jquave I'll give that tutorial a shot, thanks for the link!

mjohnst commented 7 years ago

@jquave tutorial works great, no issues anymore

ghost commented 7 years ago

@mjohnst I met the same problem. [E operator.cc:72] Cannot find operator schema for GivenTensorFill. Will skip schema checking. I tried to build caffe2 on Android by eclipse. I think @Yangqing is right. This problem happens when the libraries are not linked with whole static library. In Android.mk, I need to set LOCAL_WHOLE_STATIC_LIBRARIES := $(LOCAL_MODULE for libcaffe2.a) To build on iOS, I think you need to add -Wl, -force_load flags.