Skip to content

Commit e15898a

Browse files
authored
Merge pull request BVLC#6346 from jerryz123/HDF5_config
Added USE_HDF5 flag to build
2 parents dc6d330 + 3318a46 commit e15898a

16 files changed

+79
-1
lines changed

CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ caffe_option(USE_LMDB "Build with lmdb" ON)
4242
caffe_option(ALLOW_LMDB_NOLOCK "Allow MDB_NOLOCK when reading LMDB files (only if necessary)" OFF)
4343
caffe_option(USE_OPENMP "Link with OpenMP (when your BLAS wants OpenMP and you get linker errors)" OFF)
4444

45+
# This code is taken from https://github.com/sh1r0/caffe-android-lib
46+
caffe_option(USE_HDF5 "Build with hdf5" ON)
47+
4548
# ---[ Dependencies
4649
include(cmake/Dependencies.cmake)
4750

Makefile

+11-1
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,13 @@ ifneq ($(CPU_ONLY), 1)
178178
LIBRARIES := cudart cublas curand
179179
endif
180180

181-
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
181+
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m
182182

183183
# handle IO dependencies
184184
USE_LEVELDB ?= 1
185185
USE_LMDB ?= 1
186+
# This code is taken from https://github.com/sh1r0/caffe-android-lib
187+
USE_HDF5 ?= 1
186188
USE_OPENCV ?= 1
187189

188190
ifeq ($(USE_LEVELDB), 1)
@@ -191,6 +193,10 @@ endif
191193
ifeq ($(USE_LMDB), 1)
192194
LIBRARIES += lmdb
193195
endif
196+
# This code is taken from https://github.com/sh1r0/caffe-android-lib
197+
ifeq ($(USE_HDF5), 1)
198+
LIBRARIES += hdf5_hl hdf5
199+
endif
194200
ifeq ($(USE_OPENCV), 1)
195201
LIBRARIES += opencv_core opencv_highgui opencv_imgproc
196202

@@ -347,6 +353,10 @@ ifeq ($(ALLOW_LMDB_NOLOCK), 1)
347353
COMMON_FLAGS += -DALLOW_LMDB_NOLOCK
348354
endif
349355
endif
356+
# This code is taken from https://github.com/sh1r0/caffe-android-lib
357+
ifeq ($(USE_HDF5), 1)
358+
COMMON_FLAGS += -DUSE_HDF5
359+
endif
350360

351361
# CPU-only configuration
352362
ifeq ($(CPU_ONLY), 1)

Makefile.config.example

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# USE_OPENCV := 0
1212
# USE_LEVELDB := 0
1313
# USE_LMDB := 0
14+
# This code is taken from https://github.com/sh1r0/caffe-android-lib
15+
# USE_HDF5 := 0
1416

1517
# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
1618
# You should not set this flag if you will be reading LMDBs with any

cmake/ConfigGen.cmake

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ function(caffe_generate_export_configs)
2424
set(HAVE_CUDA FALSE)
2525
endif()
2626

27+
set(HDF5_IMPORTED OFF)
28+
foreach(_lib ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES})
29+
if(TARGET ${_lib})
30+
set(HDF5_IMPORTED ON)
31+
endif()
32+
endforeach()
33+
34+
# This code is taken from https://github.com/sh1r0/caffe-android-lib
35+
if(USE_HDF5)
36+
list(APPEND Caffe_DEFINITIONS -DUSE_HDF5)
37+
endif()
38+
2739
if(NOT HAVE_CUDNN)
2840
set(HAVE_CUDNN FALSE)
2941
endif()

cmake/Dependencies.cmake

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ find_package(HDF5 COMPONENTS HL REQUIRED)
4747
list(APPEND Caffe_INCLUDE_DIRS PUBLIC ${HDF5_INCLUDE_DIRS})
4848
list(APPEND Caffe_LINKER_LIBS PUBLIC ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES})
4949

50+
# This code is taken from https://github.com/sh1r0/caffe-android-lib
51+
if(USE_HDF5)
52+
find_package(HDF5 COMPONENTS HL REQUIRED)
53+
include_directories(SYSTEM ${HDF5_INCLUDE_DIRS} ${HDF5_HL_INCLUDE_DIR})
54+
list(APPEND Caffe_LINKER_LIBS ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARIES})
55+
add_definitions(-DUSE_HDF5)
56+
endif()
57+
5058
# ---[ LMDB
5159
if(USE_LMDB)
5260
find_package(LMDB REQUIRED)

cmake/Summary.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ function(caffe_print_configuration_summary)
119119
caffe_status(" USE_LMDB : ${USE_LMDB}")
120120
caffe_status(" USE_NCCL : ${USE_NCCL}")
121121
caffe_status(" ALLOW_LMDB_NOLOCK : ${ALLOW_LMDB_NOLOCK}")
122+
# This code is taken from https://github.com/sh1r0/caffe-android-lib
123+
caffe_status(" USE_HDF5 : ${USE_HDF5}")
122124
caffe_status("")
123125
caffe_status("Dependencies:")
124126
caffe_status(" BLAS : " APPLE THEN "Yes (vecLib)" ELSE "Yes (${BLAS})")

include/caffe/util/hdf5.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef USE_HDF5
12
#ifndef CAFFE_UTIL_HDF5_H_
23
#define CAFFE_UTIL_HDF5_H_
34

@@ -37,3 +38,4 @@ string hdf5_get_name_by_idx(hid_t loc_id, int idx);
3738
} // namespace caffe
3839

3940
#endif // CAFFE_UTIL_HDF5_H_
41+
#endif // USE_HDF5

src/caffe/layers/hdf5_data_layer.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef USE_HDF5
12
/*
23
TODO:
34
- load file in a separate thread ("prefetch")
@@ -184,3 +185,4 @@ INSTANTIATE_CLASS(HDF5DataLayer);
184185
REGISTER_LAYER_CLASS(HDF5Data);
185186

186187
} // namespace caffe
188+
#endif // USE_HDF5

src/caffe/layers/hdf5_data_layer.cu

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef USE_HDF5
12
/*
23
TODO:
34
- only load parts of the file, in accordance with a prototxt param "max_mem"
@@ -34,3 +35,4 @@ void HDF5DataLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
3435
INSTANTIATE_LAYER_GPU_FUNCS(HDF5DataLayer);
3536

3637
} // namespace caffe
38+
#endif // USE_HDF5

src/caffe/layers/hdf5_output_layer.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef USE_HDF5
12
#include <vector>
23

34
#include "hdf5.h"
@@ -72,3 +73,4 @@ INSTANTIATE_CLASS(HDF5OutputLayer);
7273
REGISTER_LAYER_CLASS(HDF5Output);
7374

7475
} // namespace caffe
76+
#endif // USE_HDF5

src/caffe/layers/hdf5_output_layer.cu

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef USE_HDF5
12
#include <vector>
23

34
#include "hdf5.h"
@@ -37,3 +38,4 @@ void HDF5OutputLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
3738
INSTANTIATE_LAYER_GPU_FUNCS(HDF5OutputLayer);
3839

3940
} // namespace caffe
41+
#endif // USE_HDF5

src/caffe/net.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#include <utility>
66
#include <vector>
77

8+
#ifdef USE_HDF5
89
#include "hdf5.h"
10+
#endif // USE_HDF5
911

1012
#include "caffe/common.hpp"
1113
#include "caffe/layer.hpp"
@@ -786,6 +788,7 @@ void Net<Dtype>::CopyTrainedLayersFromBinaryProto(
786788

787789
template <typename Dtype>
788790
void Net<Dtype>::CopyTrainedLayersFromHDF5(const string trained_filename) {
791+
#ifdef USE_HDF5
789792
hid_t file_hid = H5Fopen(trained_filename.c_str(), H5F_ACC_RDONLY,
790793
H5P_DEFAULT);
791794
CHECK_GE(file_hid, 0) << "Couldn't open " << trained_filename;
@@ -832,6 +835,10 @@ void Net<Dtype>::CopyTrainedLayersFromHDF5(const string trained_filename) {
832835
}
833836
H5Gclose(data_hid);
834837
H5Fclose(file_hid);
838+
#else
839+
LOG(FATAL) << "CopyTrainedLayersFromHDF5 requires hdf5;"
840+
<< " compile with USE_HDF5.";
841+
#endif // USE_HDF5
835842
}
836843

837844
template <typename Dtype>
@@ -848,6 +855,8 @@ void Net<Dtype>::ToProto(NetParameter* param, bool write_diff) const {
848855

849856
template <typename Dtype>
850857
void Net<Dtype>::ToHDF5(const string& filename, bool write_diff) const {
858+
// This code is taken from https://github.com/sh1r0/caffe-android-lib
859+
#ifdef USE_HDF5
851860
hid_t file_hid = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
852861
H5P_DEFAULT);
853862
CHECK_GE(file_hid, 0)
@@ -901,6 +910,10 @@ void Net<Dtype>::ToHDF5(const string& filename, bool write_diff) const {
901910
H5Gclose(diff_hid);
902911
}
903912
H5Fclose(file_hid);
913+
// This code is taken from https://github.com/sh1r0/caffe-android-lib
914+
#else
915+
LOG(FATAL) << "ToHDF5 requires hdf5; compile with USE_HDF5.";
916+
#endif // USE_HDF5
904917
}
905918

906919
template <typename Dtype>

src/caffe/solvers/sgd_solver.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ void SGDSolver<Dtype>::SnapshotSolverStateToBinaryProto(
289289
template <typename Dtype>
290290
void SGDSolver<Dtype>::SnapshotSolverStateToHDF5(
291291
const string& model_filename) {
292+
// This code is taken from https://github.com/sh1r0/caffe-android-lib
293+
#ifdef USE_HDF5
292294
string snapshot_filename =
293295
Solver<Dtype>::SnapshotFilename(".solverstate.h5");
294296
LOG(INFO) << "Snapshotting solver state to HDF5 file " << snapshot_filename;
@@ -310,6 +312,11 @@ void SGDSolver<Dtype>::SnapshotSolverStateToHDF5(
310312
}
311313
H5Gclose(history_hid);
312314
H5Fclose(file_hid);
315+
// This code is taken from https://github.com/sh1r0/caffe-android-lib
316+
#else
317+
LOG(FATAL) << "SnapshotSolverStateToHDF5 requires hdf5;"
318+
<< " compile with USE_HDF5.";
319+
#endif // USE_HDF5
313320
}
314321

315322
template <typename Dtype>
@@ -334,6 +341,7 @@ void SGDSolver<Dtype>::RestoreSolverStateFromBinaryProto(
334341

335342
template <typename Dtype>
336343
void SGDSolver<Dtype>::RestoreSolverStateFromHDF5(const string& state_file) {
344+
#ifdef USE_HDF5
337345
hid_t file_hid = H5Fopen(state_file.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
338346
CHECK_GE(file_hid, 0) << "Couldn't open solver state file " << state_file;
339347
this->iter_ = hdf5_load_int(file_hid, "iter");
@@ -355,6 +363,10 @@ void SGDSolver<Dtype>::RestoreSolverStateFromHDF5(const string& state_file) {
355363
}
356364
H5Gclose(history_hid);
357365
H5Fclose(file_hid);
366+
#else
367+
LOG(FATAL) << "RestoreSolverStateFromHDF5 requires hdf5;"
368+
<< " compile with USE_HDF5.";
369+
#endif // USE_HDF5
358370
}
359371

360372
INSTANTIATE_CLASS(SGDSolver);

src/caffe/test/test_hdf5_output_layer.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef USE_HDF5
12
#include <string>
23
#include <vector>
34

@@ -120,3 +121,4 @@ TYPED_TEST(HDF5OutputLayerTest, TestForward) {
120121
}
121122

122123
} // namespace caffe
124+
#endif // USE_HDF5

src/caffe/test/test_hdf5data_layer.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef USE_HDF5
12
#include <string>
23
#include <vector>
34

@@ -163,3 +164,4 @@ TYPED_TEST(HDF5DataLayerTest, TestSkip) {
163164
}
164165

165166
} // namespace caffe
167+
#endif // USE_HDF5

src/caffe/util/hdf5.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifdef USE_HDF5
12
#include "caffe/util/hdf5.hpp"
23

34
#include <string>
@@ -207,3 +208,4 @@ string hdf5_get_name_by_idx(hid_t loc_id, int idx) {
207208
}
208209

209210
} // namespace caffe
211+
#endif // USE_HDF5

0 commit comments

Comments
 (0)