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

Successful build under windows but several improvement is needed #1046

Open jackyko1991 opened 7 years ago

jackyko1991 commented 7 years ago

I have successfully build using MSVC 2015 with caffe 2. The installation instruction asks users to build with the .bat file in caffe2/scripts. However, I found this is not a good practice as sometimes we need use external dependencies. Configuring CMake with the GUI version would be much convenient under windows usage.

In Caffe windows version it will automatically download all dependency required in forms of pre-built library. It would be good if users want to quickly deploy Caffe.

For whom want to build from source, or using specific version of third party library, the submodule file format in Caffe 2 will much do the favor. But problem comes in when in CMake configuration.

Here is a temporary solution to build with customized CMake:

  1. (Optional) Download prebuild libraries from previous version of Caffe

    1. git clone https://github.com/BVLC/caffe
    2. git checkout windows
    3. CMake, only need to configure
    4. All necessary binaries and libraries can found at C:\Users\<username>\.caffe
  2. (Can be ignored if you choose step 0) Build protobuf, either from source (caffe2/scripts/build_host_protoc.bat would be a fast build from source option, highly recommended.), or directly use the protobuf binary downloaded in step 0. If you need to build Caffe2 with shared library, you also need to build shared library in this step.

  3. CMake and specify Protobuf_DIR.

    1. For step 0 users, point to C:\Users\<username>\.caffe\dependencies\libraries_v140_x64_py27_1.1.0\libraries\cmake
    2. For step 1 users, point to /caffe2/build_host_protoc/cmake
    3. For custom build users, point to folder containing protobuf-config.cmake
    4. Official suggested CMake is with
    5. CMAKE_VERBOSE_MAKEFILE ON
    6. BUILD_TEST OFF
    7. CMAKE_BUILD_TYPE Release
    8. USE_CUDA OFF (turn ON if you want to build GPU version, need to install CUDA driver first)
    9. USE_NNPAC OFF
    10. USE_GLOG OFF
    11. USE_GFLAGS OFF
    12. USE_LMDB OFF
    13. USE_LEVELDB OFF
    14. USE_ROCKSDB OFF
    15. USE_OPENCV OFF (suggested to turn this ON, but need to build OpenCV on your own, tested with OpenCV 3, OpenCV 2.4.13 may have problem on linking cudartlib, earlier version of OpenCV (possibily 2.4.11 or earlier) maybe a solution to linkage problem. This also happens in Linux)
    16. BUILD_SHARED_LIBS OFF (this option is under test, will cause compilation error if set to ON)
    17. BUILD_PYTHON OFF (Highly recommended to turn this ON unless you want to solely use C++ caffe2 interface)
  4. Specify all required dependency paths, similar to step 2

  5. Configure and Generate

  6. (VERY IMPORTANT)Note that CMakeLists.txt in current release does not properly configure protoc.exe, open <caffe2-build>/CMakeCache.txt, add following line to the file near Protobuf_DIR:

    PROTOBUF_PROTOC_EXECUTABLE:FILEPATH=<protobuf-build>/bin/protoc.exe

  7. Open Caffe2.sln and build (Debug or Release)

  8. Build error would appear when building lenghts_top_k_op.cc. At line 9 the type of y dimension need to change to int from auto.

  9. Build again and enjoy Caffe2.

One thing I think the development group should notify is in operator_schema.h line 288, std::bind to std::function conversion. Though building is successful, but type conversion is not properly defined.

ugionet commented 7 years ago

building with Gflags, Glog, LevelDB and Snappy is rather easy as well, under Windows VS2015 ... But remember you have to build only with MT Code generation option for all the 3rd part dependencies. Also, in order to make it work under python you will have to build ProtoBuf python source and add it to your PYTHONPATH. https://github.com/google/protobuf/releases/download/v3.2.0/protobuf-python-3.2.0.zip.

Also, it is recommended to use MKL for blas (for CPU purposes at least).

I am having a hard time achieving Good run times when using CUDA and CUDNN, has anybody managed to achieve good run time in that ? See performance evaluation i did: https://github.com/caffe2/caffe2/issues/921

ctraina commented 6 years ago

@ugionet - how did you get gflags/glog/leveldb to build? Any time I deviate from the default settings (USE_GLOG OFF, USE_GFLAGS=OFF, USE_LEVELDB=OFF), I get thousands of linker errors. When you say "build only with MT Code generation" for all 3rd party dependencies, where do I specify that?

pranman commented 6 years ago

@ctraina I'm sure you've solved this problem by now, but you can configure options in VS.

In Solution Explorer, right click on project name and go to properties -> C/C++ -> Code Generation Change the option under 'Runtime Library'.

https://msdn.microsoft.com/en-us/library/2kzt1wy3.aspx