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

Apply Caffe2 lib in C++ application #585

Open honamida opened 7 years ago

honamida commented 7 years ago

Hi, I'm trying to apply some C++ API into my application. First I use some toy program which is modified according to some issues and codes here (many thanks!) to test the API on both Linux/Windows.

In Linux it works well that I only need to configure CMake to link libCaffe2_CPU/GPU.so, where I put it in /usr/local/lib, and its library.

In Windows ( There may be some unpredictable problem since I'm win7 user instead of win10), after following the instruction, I can build caffe2 successfully and generate Caffe2_CPU/GPU.lib under build/Release. Unfortunately when I want to build the same toy program by VS2015, It cannot link the Caffe2 library with LNK2001

image

Have anyone tried in the same way and got the solution?

honamida commented 7 years ago

After some trials I compiled my program in Windows, but failed to run it with the following error message:

image

honamida commented 7 years ago

Here are more information from my Visual Studio 2015, After I use /WHOLEARCHIVE flag to force VS link all the library

image

Also I found that there're some similar problem appears in Tensorflow GPU mode

GreedyZach commented 7 years ago

I encountered the same problem.

I was writing a simple program to read the squeeze net on my Windows machine using Visual Studio 2015. The program crashed when I constructed a predictor from the net (caffe2::Predictor predictor(initNet, predNet);).

Under Debug mode, I found the crash happened in StaticLinkingProtector within operator.h:

struct StaticLinkingProtector { StaticLinkingProtector() { const int registered_ops = CPUOperatorRegistry()->Keys().size(); // Note: this is a check failure instead of an exception, because if // the linking is wrong, Caffe2 won't be able to run properly anyway, // so it's better to fail loud. // If Caffe2 is properly linked with whole archive, there should be more // than zero registered ops. if (registered_ops == 0) { LOG(FATAL) << "You might have made a build error: the Caffe2 library does not seem " "to be linked with whole-static library option. To do so, use " "-Wl,-force_load (clang) or -Wl,--whole-archive (gcc) to link the " "Caffe2 library."; } }

I followed the suggestion and used /WHOLEARCHIVE for my project, then I got the same error saying that my linker crashed.

I did a little bit search online and found a similar issue people had encountered with TensorFlow: https://groups.google.com/forum/#!topic/bazel-discuss/tDGrTAtaxYw

Hopefully this issue can be resolved soon :)

gnobbi commented 7 years ago

is there any update?

Update: found a solution my self add -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE in build_windows.bat and it will export all symbols in library

z6491679 commented 7 years ago

@gnobbi I have added the struction and rebuild the caffe2 but the problem still exists,sign.

heurainbow commented 7 years ago

Same problem with win7 vs2015. Any solution?

fengshikun commented 7 years ago

Any solutions now, same error to me...

th1761 commented 7 years ago

Has anyone gotten Caffe2 to install and build on Windows 7 for VS2015 c++??

Shuwrood-SSW commented 6 years ago

Did you successfully train caffe2 models with vs, if you have done, can you share me how?

tblattner commented 6 years ago

Same here. I've been trying for weeks to deploy inferencing with Caffe2 on Windows. Have the same "Caffe2 library does not seem to be linked with whole-static", and am now at a loss as to how to get things working.

Tempted to just wait for TensorRT on Windows and give up on using Caffe2 on Windows.

Edit: Okay I managed to get things inferencing (currently with CPU only, will attempt GPU next).

Trick was: 1) Add -WHOLEARCHIVE:caffe2.lib 2) link to caffe2_protos.lib, nomnigraph.lib, onnx.lib, onnx_proto.lib, libprotoc.lib, and cpuinfo.lib

Once this is done, I was able to run inferencing (finally!). Hopefully adding GPU linking will require about the same, I will update once I get things working on this end.

JisongXie commented 6 years ago

I want to know how to add -WHOLEARCHIVE:caffe2.lib in Visual Studio, just in the extra dependency items like we add libs generally?@tblattner Thank you~ image But I get error message when I attempt to build solution if I try this. unresolved external symbol:onnxifi_load Maybe it cannot be tried as this.

tblattner commented 6 years ago

I've been using CMake to generate the VS project files, but peaking into the linker options in the generated VS I see the following:

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64\cudart_static.lib
-WHOLEARCHIVE:caffe2.lib
C:\Program Files\Caffe2\lib\caffe2.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64\cuda.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64\cudnn.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64\cublas.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64\cublas_device.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64\curand.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\lib\x64\nvrtc.lib
-WHOLEARCHIVE:caffe2_gpu.lib
C:\Program Files\Caffe2\lib\caffe2_gpu.lib
C:\Program Files\Caffe2\lib\libprotobuf.lib
C:\path\to\pytorch\build\lib\Release\caffe2_protos.lib
C:\path\to\pytorch\build\lib\Release\nomnigraph.lib
C:\path\to\pytorch\build\lib\Release\onnx.lib
C:\path\to\pytorch\build\lib\Release\onnx_proto.lib
C:\path\to\pytorch\build\lib\Release\libprotoc.lib
C:\path\to\pytorch\build\lib\Release\cpuinfo.lib
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
comdlg32.lib
advapi32.lib

Granted this was on a version a few months back, so things may have changed ...

I think the error you are getting has to do with missing onnx.lib and onnx_proto.lib

Note that is this is the version using CUDA, which I am happy to say is working! I had to make one modifications to 'caffe2/image/image_input_op.h', which was having some errors compiling on Windows. This may have been fixed in the latest version.

diff --git a/caffe2/image/image_input_op.h b/caffe2/image/image_input_op.h
index 8633bb8c9..4bc2c94cc 100644
--- a/caffe2/image/image_input_op.h
+++ b/caffe2/image/image_input_op.h
@@ -7,6 +7,7 @@
 #include <iostream>
 #include <algorithm>

+#include "caffe2/core/common.h"
 #include "caffe/proto/caffe.pb.h"
 #include "caffe2/core/db.h"
 #include "caffe2/utils/cast.h"
@@ -242,13 +243,13 @@ ImageInputOp<Context>::ImageInputOp(

   // hard-coded PCA eigenvectors and eigenvalues, based on RBG channel order
   color_lighting_eigvecs_.push_back(
-    std::vector<float>{-144.7125, 183.396, 102.2295});
+    std::vector<float>{-144.7125f, 183.396f, 102.2295f});
   color_lighting_eigvecs_.push_back(
-    std::vector<float>{-148.104, -1.1475, -207.57});
+    std::vector<float>{-148.104f, -1.1475f, -207.57f});
   color_lighting_eigvecs_.push_back(
-    std::vector<float>{-148.818, -177.174, 107.1765});
+    std::vector<float>{-148.818f, -177.174f, 107.1765f});

-  color_lighting_eigvals_ = std::vector<float>{0.2175, 0.0188, 0.0045};
+  color_lighting_eigvals_ = std::vector<float>{0.2175f, 0.0188f, 0.0045f};

   CAFFE_ENFORCE_GT(batch_size_, 0, "Batch size should be nonnegative.");
   if (use_caffe_datum_) {

Goodluck!

JisongXie commented 6 years ago

Great thanks to you, @tblattner. I finally find that the predictor_verifier project in Caffe2.sln has provided a good solution. I right click it to look at its properties, and set many of my projects's properties like it. image Then it works! It looks like yours, almost the same as your answer. image Very kind of you! Thanks.

CarlosYeverino commented 6 years ago

Hello @tblattner and @JisongXie ,

I successfully built Caffe2 from source on windows 10. Now, I would like to use caffe2 libraries in Visual Studio 2015 to write C++ programs. Do you know, how should I add the libraries and from what path in order to be able to include the libraries as below?

#include "caffe2/core/common.h"
#include "caffe2/utils/proto_utils.h"
#include "caffe2/core/workspace.h"
#include "caffe2/core/tensor.h"
#include "caffe2/core/init.h"

Should I add only these libraries?

image

I followed this guide https://caffe2.ai/docs/cplusplus_tutorial.html . I added the following directories in Visual Studio: D:\Yeverino\git_projects\pytorch\build\lib\Release D:\Yeverino\git_projects\pytorch\build\caffe2\proto\Caffe2_PROTO.dir\Release D:\Yeverino\git_projects\pytorch\caffe2\cuda_rtc\core D:\Yeverino\git_projects\pytorch\caffe2\proto D:\Yeverino\git_projects\pytorch\build D:\Yeverino\git_projects\pytorch\build\caffe2

image

However, I got errors like below when I tried to include #include in a project: Error C1083 Cannot open include file: 'caffe2/core/common.h': No such file or directory ConsoleApplication1 D:\Yeverino\git_projects\pytorch\caffe2\cuda_rtc\core\blob.h 11

Once I changed #include "caffe2/core/blob_serializer_base.h" to #include "blob_serializer_base.h", error was fixed and other similar errors were detected due to the prefix "caffe2/$package/" in the includes. I really want to avoid renaming all includes.

JisongXie commented 6 years ago

Well, @CarlosYeverino, I don't know what's your purpose to use caffe2 with C++. Is it the same as me to use it just to predict with pretrained models, or to train and test your model? My circumstance is the former. As I mentioned above, I use the setting the same as the _predictorverifier project in the Caffe2.sln, and it works fine. I suggest you first to see whether the "common.h" exists in that path, and then go into the blob.h to see how "common.h" is included. Maybe you need to add such item into your include directory of visual studio project : _D:\Yeverino\gitprojects\pytorch

Then maybe it could successfully find the following header files as you mentioned:

include "caffe2/core/common.h"

include "caffe2/utils/proto_utils.h"

include "caffe2/core/workspace.h"

include "caffe2/core/tensor.h"

include "caffe2/core/init.h"

CarlosYeverino commented 6 years ago

Hi @JisongXie ,

thanks for your answer. I would like to deploy my nets trained in python in C++. So, it would be a predictor only.

I found the Caffe2.sln in path D:\Yeverino\git_projects\pytorch\build. What properties did you copy from the predictor_verifier project? Just the additional include directories in section C/C++ > General?

JisongXie commented 6 years ago

Well, not only those. @CarolsYeverino I set almost all properties of my new projects the same as the predictor_verifier project.

brantPTS commented 6 years ago

Anyone have advice for the process of building pytorch / caffe2 for c++ inference on Windows? Right now I'm trying to build but C:\local\PyTorch\pytorch\aten\src\ATen\cudnn\Descriptors.h seems to be referencing types only defined in some really old cuda.

orionr commented 6 years ago

@brantPTS I would checkout the thread at https://github.com/pytorch/pytorch/issues/12001

We've done some serious work on Windows recently, so see if the Windows CI scripts referenced on there help you.

brantPTS commented 6 years ago

@orionr Thanks! I will check out the script.