Skip to content

Commit f3a933a

Browse files
committed
Separate IO dependencies
OpenCV, LMDB, LevelDB and Snappy are made optional via switches (USE_OPENCV, USE_LMDB, USE_LEVELDB) available for Make and CMake builds. Since Snappy is a LevelDB dependency, its use is determined by USE_LEVELDB. HDF5 is left bundled because it is used for serializing weights and solverstates.
1 parent 71e0587 commit f3a933a

40 files changed

+264
-60
lines changed

.travis.yml

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
# one using CMake, and one using make.
33
env:
44
matrix:
5-
- WITH_CUDA=false WITH_CMAKE=false
6-
- WITH_CUDA=false WITH_CMAKE=true
7-
- WITH_CUDA=true WITH_CMAKE=false
8-
- WITH_CUDA=true WITH_CMAKE=true
9-
- WITH_CUDA=false WITH_CMAKE=true PYTHON_VERSION=3
5+
- WITH_CUDA=false WITH_CMAKE=false WITH_IO=true
6+
- WITH_CUDA=false WITH_CMAKE=true WITH_IO=true PYTHON_VERSION=3
7+
- WITH_CUDA=true WITH_CMAKE=false WITH_IO=true
8+
- WITH_CUDA=true WITH_CMAKE=true WITH_IO=true
9+
- WITH_CUDA=false WITH_CMAKE=false WITH_IO=false
10+
- WITH_CUDA=false WITH_CMAKE=true WITH_IO=false PYTHON_VERSION=3
1011

1112
language: cpp
1213

CMakeLists.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ include(cmake/ConfigGen.cmake)
1616

1717
# ---[ Options
1818
caffe_option(CPU_ONLY "Build Caffe without CUDA support" OFF) # TODO: rename to USE_CUDA
19-
caffe_option(USE_CUDNN "Build Caffe with cuDNN libary support" ON IF NOT CPU_ONLY)
19+
caffe_option(USE_CUDNN "Build Caffe with cuDNN library support" ON IF NOT CPU_ONLY)
2020
caffe_option(BUILD_SHARED_LIBS "Build shared libraries" ON)
2121
caffe_option(BUILD_python "Build Python wrapper" ON)
2222
set(python_version "2" CACHE STRING "Specify which python version to use")
2323
caffe_option(BUILD_matlab "Build Matlab wrapper" OFF IF UNIX OR APPLE)
2424
caffe_option(BUILD_docs "Build documentation" ON IF UNIX OR APPLE)
25-
caffe_option(BUILD_python_layer "Build the Caffe python layer" ON)
25+
caffe_option(BUILD_python_layer "Build the caffe python layer" ON)
26+
caffe_option(USE_LMDB "Build with lmdb" ON)
27+
caffe_option(USE_LEVELDB "Build with levelDB" ON)
28+
caffe_option(USE_OPENCV "Build with OpenCV support" ON)
2629

2730
# ---[ Dependencies
2831
include(cmake/Dependencies.cmake)

Makefile

+24-4
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,18 @@ ifneq ($(CPU_ONLY), 1)
169169
LIBRARY_DIRS += $(CUDA_LIB_DIR)
170170
LIBRARIES := cudart cublas curand
171171
endif
172-
LIBRARIES += glog gflags protobuf leveldb snappy \
173-
lmdb boost_system hdf5_hl hdf5 m \
174-
opencv_core opencv_highgui opencv_imgproc
172+
173+
LIBRARIES += glog gflags protobuf boost_system m hdf5_hl hdf5
174+
175+
ifeq ($(USE_LEVELDB), 1)
176+
LIBRARIES += leveldb snappy
177+
endif
178+
ifeq ($(USE_LMDB), 1)
179+
LIBRARIES += lmdb
180+
endif
181+
ifeq ($(USE_OPENCV), 1)
182+
LIBRARIES += opencv_core opencv_highgui opencv_imgproc
183+
endif
175184
PYTHON_LIBRARIES := boost_python python2.7
176185
WARNINGS := -Wall -Wno-sign-compare
177186

@@ -290,6 +299,17 @@ ifeq ($(USE_CUDNN), 1)
290299
COMMON_FLAGS += -DUSE_CUDNN
291300
endif
292301

302+
# i/o libraries configuration
303+
ifeq ($(USE_OPENCV), 1)
304+
COMMON_FLAGS += -DUSE_OPENCV
305+
endif
306+
ifeq ($(USE_LEVELDB), 1)
307+
COMMON_FLAGS += -DUSE_LEVELDB
308+
endif
309+
ifeq ($(USE_LMDB), 1)
310+
COMMON_FLAGS += -DUSE_LMDB
311+
endif
312+
293313
# CPU-only configuration
294314
ifeq ($(CPU_ONLY), 1)
295315
OBJS := $(PROTO_OBJS) $(CXX_OBJS)
@@ -472,7 +492,7 @@ runtest: $(TEST_ALL_BIN)
472492

473493
pytest: py
474494
cd python; python -m unittest discover -s caffe/test
475-
495+
476496
mattest: mat
477497
cd matlab; $(MATLAB_DIR)/bin/matlab -nodisplay -r 'caffe.run_tests(), exit()'
478498

Makefile.config.example

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
# CPU-only switch (uncomment to build without GPU support).
88
# CPU_ONLY := 1
99

10+
# comment out to disable IO dependencies
11+
USE_LEVELDB := 1
12+
USE_LMDB := 1
13+
USE_OPENCV := 1
14+
1015
# To customize your choice of compiler, uncomment and set the following.
1116
# N.B. the default for Linux is g++ and the default for OSX is clang++
1217
# CUSTOM_CXX := g++

cmake/ConfigGen.cmake

+12
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ function(caffe_generate_export_configs)
5656
list(APPEND Caffe_DEFINITIONS -DCPU_ONLY)
5757
endif()
5858

59+
if(USE_OPENCV)
60+
list(APPEND Caffe_DEFINITIONS -DUSE_OPENCV)
61+
endif()
62+
63+
if(USE_LMDB)
64+
list(APPEND Caffe_DEFINITIONS -DUSE_LMDB)
65+
endif()
66+
67+
if(USE_LEVELDB)
68+
list(APPEND Caffe_DEFINITIONS -DUSE_LEVELDB)
69+
endif()
70+
5971
if(NOT HAVE_CUDNN)
6072
set(HAVE_CUDNN FALSE)
6173
else()

cmake/Dependencies.cmake

+26-15
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,27 @@ include_directories(SYSTEM ${HDF5_INCLUDE_DIRS} ${HDF5_HL_INCLUDE_DIR})
2929
list(APPEND Caffe_LINKER_LIBS ${HDF5_LIBRARIES})
3030

3131
# ---[ LMDB
32-
find_package(LMDB REQUIRED)
33-
include_directories(SYSTEM ${LMDB_INCLUDE_DIR})
34-
list(APPEND Caffe_LINKER_LIBS ${LMDB_LIBRARIES})
32+
if(USE_LMDB)
33+
find_package(LMDB REQUIRED)
34+
include_directories(SYSTEM ${LMDB_INCLUDE_DIR})
35+
list(APPEND Caffe_LINKER_LIBS ${LMDB_LIBRARIES})
36+
add_definitions(-DUSE_LMDB)
37+
endif()
3538

3639
# ---[ LevelDB
37-
find_package(LevelDB REQUIRED)
38-
include_directories(SYSTEM ${LevelDB_INCLUDE})
39-
list(APPEND Caffe_LINKER_LIBS ${LevelDB_LIBRARIES})
40+
if(USE_LEVELDB)
41+
find_package(LevelDB REQUIRED)
42+
include_directories(SYSTEM ${LevelDB_INCLUDE})
43+
list(APPEND Caffe_LINKER_LIBS ${LevelDB_LIBRARIES})
44+
add_definitions(-DUSE_LEVELDB)
45+
endif()
4046

4147
# ---[ Snappy
42-
find_package(Snappy REQUIRED)
43-
include_directories(SYSTEM ${Snappy_INCLUDE_DIR})
44-
list(APPEND Caffe_LINKER_LIBS ${Snappy_LIBRARIES})
48+
if(USE_LEVELDB)
49+
find_package(Snappy REQUIRED)
50+
include_directories(SYSTEM ${Snappy_INCLUDE_DIR})
51+
list(APPEND Caffe_LINKER_LIBS ${Snappy_LIBRARIES})
52+
endif()
4553

4654
# ---[ CUDA
4755
include(cmake/Cuda.cmake)
@@ -57,13 +65,16 @@ if(NOT HAVE_CUDA)
5765
endif()
5866

5967
# ---[ OpenCV
60-
find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs)
61-
if(NOT OpenCV_FOUND) # if not OpenCV 3.x, then imgcodecs are not found
62-
find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc)
68+
if(USE_OPENCV)
69+
find_package(OpenCV QUIET COMPONENTS core highgui imgproc imgcodecs)
70+
if(NOT OpenCV_FOUND) # if not OpenCV 3.x, then imgcodecs are not found
71+
find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc)
72+
endif()
73+
include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS})
74+
list(APPEND Caffe_LINKER_LIBS ${OpenCV_LIBS})
75+
message(STATUS "OpenCV found (${OpenCV_CONFIG_PATH})")
76+
add_definitions(-DUSE_OPENCV)
6377
endif()
64-
include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS})
65-
list(APPEND Caffe_LINKER_LIBS ${OpenCV_LIBS})
66-
message(STATUS "OpenCV found (${OpenCV_CONFIG_PATH})")
6778

6879
# ---[ BLAS
6980
if(NOT APPLE)

cmake/Summary.cmake

+13-5
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,26 @@ function(caffe_print_configuration_summary)
114114
caffe_status(" BUILD_matlab : ${BUILD_matlab}")
115115
caffe_status(" BUILD_docs : ${BUILD_docs}")
116116
caffe_status(" CPU_ONLY : ${CPU_ONLY}")
117+
caffe_status(" USE_LMDB : ${USE_LMDB}")
118+
caffe_status(" USE_LEVELDB : ${USE_LEVELDB}")
119+
caffe_status(" USE_OPENCV : ${USE_OPENCV}")
117120
caffe_status("")
118121
caffe_status("Dependencies:")
119122
caffe_status(" BLAS : " APPLE THEN "Yes (vecLib)" ELSE "Yes (${BLAS})")
120123
caffe_status(" Boost : Yes (ver. ${Boost_MAJOR_VERSION}.${Boost_MINOR_VERSION})")
121124
caffe_status(" glog : Yes")
122125
caffe_status(" gflags : Yes")
123126
caffe_status(" protobuf : " PROTOBUF_FOUND THEN "Yes (ver. ${PROTOBUF_VERSION})" ELSE "No" )
124-
caffe_status(" lmdb : " LMDB_FOUND THEN "Yes (ver. ${LMDB_VERSION})" ELSE "No")
125-
caffe_status(" Snappy : " SNAPPY_FOUND THEN "Yes (ver. ${Snappy_VERSION})" ELSE "No" )
126-
caffe_status(" LevelDB : " LEVELDB_FOUND THEN "Yes (ver. ${LEVELDB_VERSION})" ELSE "No")
127-
caffe_status(" OpenCV : Yes (ver. ${OpenCV_VERSION})")
127+
if(USE_LMDB)
128+
caffe_status(" lmdb : " LMDB_FOUND THEN "Yes (ver. ${LMDB_VERSION})" ELSE "No")
129+
endif()
130+
if(USE_LEVELDB)
131+
caffe_status(" LevelDB : " LEVELDB_FOUND THEN "Yes (ver. ${LEVELDB_VERSION})" ELSE "No")
132+
caffe_status(" Snappy : " SNAPPY_FOUND THEN "Yes (ver. ${Snappy_VERSION})" ELSE "No" )
133+
endif()
134+
if(USE_OPENCV)
135+
caffe_status(" OpenCV : Yes (ver. ${OpenCV_VERSION})")
136+
endif()
128137
caffe_status(" CUDA : " HAVE_CUDA THEN "Yes (ver. ${CUDA_VERSION})" ELSE "No" )
129138
caffe_status("")
130139
if(HAVE_CUDA)
@@ -165,4 +174,3 @@ function(caffe_print_configuration_summary)
165174
caffe_status(" Install path : ${CMAKE_INSTALL_PREFIX}")
166175
caffe_status("")
167176
endfunction()
168-

cmake/Templates/CaffeConfig.cmake.in

+14-12
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,24 @@
1717
# Caffe_HAVE_CUDNN - signals about cuDNN support
1818

1919

20-
# OpenCV dependency
20+
# OpenCV dependency (optional)
2121

22-
if(NOT OpenCV_FOUND)
23-
set(Caffe_OpenCV_CONFIG_PATH "@OpenCV_CONFIG_PATH@")
24-
if(Caffe_OpenCV_CONFIG_PATH)
25-
get_filename_component(Caffe_OpenCV_CONFIG_PATH ${Caffe_OpenCV_CONFIG_PATH} ABSOLUTE)
22+
if(@USE_OPENCV@)
23+
if(NOT OpenCV_FOUND)
24+
set(Caffe_OpenCV_CONFIG_PATH "@OpenCV_CONFIG_PATH@")
25+
if(Caffe_OpenCV_CONFIG_PATH)
26+
get_filename_component(Caffe_OpenCV_CONFIG_PATH ${Caffe_OpenCV_CONFIG_PATH} ABSOLUTE)
2627

27-
if(EXISTS ${Caffe_OpenCV_CONFIG_PATH} AND NOT TARGET opencv_core)
28-
message(STATUS "Caffe: using OpenCV config from ${Caffe_OpenCV_CONFIG_PATH}")
29-
include(${Caffe_OpenCV_CONFIG_PATH}/OpenCVModules.cmake)
30-
endif()
28+
if(EXISTS ${Caffe_OpenCV_CONFIG_PATH} AND NOT TARGET opencv_core)
29+
message(STATUS "Caffe: using OpenCV config from ${Caffe_OpenCV_CONFIG_PATH}")
30+
include(${Caffe_OpenCV_CONFIG_PATH}/OpenCVModules.cmake)
31+
endif()
3132

32-
else()
33-
find_package(OpenCV REQUIRED)
33+
else()
34+
find_package(OpenCV REQUIRED)
35+
endif()
36+
unset(Caffe_OpenCV_CONFIG_PATH)
3437
endif()
35-
unset(Caffe_OpenCV_CONFIG_PATH)
3638
endif()
3739

3840
# Compute paths

cmake/Templates/caffe_config.h.in

+5
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@
3030

3131
/* Matlab */
3232
#cmakedefine HAVE_MATLAB
33+
34+
/* IO libraries */
35+
#cmakedefine USE_OPENCV
36+
#cmakedefine USE_LMDB
37+
#cmakedefine USE_LEVELDB

docs/installation.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ When updating Caffe, it's best to `make clean` before re-compiling.
1717

1818
## Prerequisites
1919

20-
Caffe has several dependencies.
20+
Caffe has several dependencies:
2121

2222
* [CUDA](https://developer.nvidia.com/cuda-zone) is required for GPU mode.
2323
* library version 7.0 and the latest driver version are recommended, but 6.* is fine too
2424
* 5.5, and 5.0 are compatible but considered legacy
2525
* [BLAS](http://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms) via ATLAS, MKL, or OpenBLAS.
2626
* [Boost](http://www.boost.org/) >= 1.55
27+
* `protobuf`, `glog`, `gflags`, `hdf5`
28+
29+
Optional dependencies:
30+
2731
* [OpenCV](http://opencv.org/) >= 2.4 including 3.0
28-
* `protobuf`, `glog`, `gflags`
29-
* IO libraries `hdf5`, `leveldb`, `snappy`, `lmdb`
32+
* IO libraries: `lmdb`, `leveldb` (note: leveldb requires `snappy`)
3033

3134
Pycaffe and Matcaffe interfaces have their own natural needs.
3235

examples/cpp_classification/classification.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
#include <caffe/caffe.hpp>
2+
#ifdef USE_OPENCV
23
#include <opencv2/core/core.hpp>
34
#include <opencv2/highgui/highgui.hpp>
45
#include <opencv2/imgproc/imgproc.hpp>
6+
#endif // USE_OPENCV
57
#include <algorithm>
68
#include <iosfwd>
79
#include <memory>
810
#include <string>
911
#include <utility>
1012
#include <vector>
1113

14+
#ifdef USE_OPENCV
1215
using namespace caffe; // NOLINT(build/namespaces)
1316
using std::string;
1417

@@ -255,3 +258,8 @@ int main(int argc, char** argv) {
255258
<< p.first << "\"" << std::endl;
256259
}
257260
}
261+
#else
262+
int main(int argc, char** argv) {
263+
LOG(FATAL) << "This example requires OpenCV; compile with USE_OPENCV.";
264+
}
265+
#endif // USE_OPENCV

examples/mnist/convert_mnist_data.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@
99
#include <gflags/gflags.h>
1010
#include <glog/logging.h>
1111
#include <google/protobuf/text_format.h>
12+
13+
#if defined(USE_LEVELDB) && defined(USE_LMDB)
1214
#include <leveldb/db.h>
1315
#include <leveldb/write_batch.h>
1416
#include <lmdb.h>
17+
#endif
18+
1519
#include <stdint.h>
1620
#include <sys/stat.h>
1721

@@ -20,6 +24,8 @@
2024

2125
#include "caffe/proto/caffe.pb.h"
2226

27+
#if defined(USE_LEVELDB) && defined(USE_LMDB)
28+
2329
using namespace caffe; // NOLINT(build/namespaces)
2430
using std::string;
2531

@@ -196,3 +202,9 @@ int main(int argc, char** argv) {
196202
}
197203
return 0;
198204
}
205+
#else
206+
int main(int argc, char** argv) {
207+
LOG(FATAL) << "This example requires LevelDB and LMDB; " <<
208+
"compile with USE_LEVELDB and USE_LMDB.";
209+
}
210+
#endif // USE_LEVELDB and USE_LMDB

examples/siamese/convert_mnist_siamese_data.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
#include "glog/logging.h"
1212
#include "google/protobuf/text_format.h"
13-
#include "leveldb/db.h"
1413
#include "stdint.h"
1514

1615
#include "caffe/proto/caffe.pb.h"
1716
#include "caffe/util/math_functions.hpp"
1817

18+
#ifdef USE_LEVELDB
19+
#include "leveldb/db.h"
20+
1921
uint32_t swap_endian(uint32_t val) {
2022
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF);
2123
return (val << 16) | (val >> 16);
@@ -121,3 +123,8 @@ int main(int argc, char** argv) {
121123
}
122124
return 0;
123125
}
126+
#else
127+
int main(int argc, char** argv) {
128+
LOG(FATAL) << "This example requires LevelDB; compile with USE_LEVELDB.";
129+
}
130+
#endif // USE_LEVELDB

include/caffe/data_layers.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <string>
55
#include <utility>
66
#include <vector>
7-
87
#include "hdf5.h"
98

109
#include "caffe/blob.hpp"
@@ -275,8 +274,10 @@ class MemoryDataLayer : public BaseDataLayer<Dtype> {
275274
virtual inline int ExactNumTopBlobs() const { return 2; }
276275

277276
virtual void AddDatumVector(const vector<Datum>& datum_vector);
277+
#ifdef USE_OPENCV
278278
virtual void AddMatVector(const vector<cv::Mat>& mat_vector,
279279
const vector<int>& labels);
280+
#endif // USE_OPENCV
280281

281282
// Reset should accept const pointers, but can't, because the memory
282283
// will be given to Blob, which is mutable

0 commit comments

Comments
 (0)