summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTea <tea.desouza@gmail.com>2015-06-27 11:44:56 +0800
committerTea <tea.desouza@gmail.com>2015-09-17 15:08:29 +0800
commitf3a933a620b8b089a7fe29ba76ec27f5408ff68d (patch)
tree6c33b48d78564bdc0721dc31d1ccb915be888571 /src
parent71e05876f644a08af4cb1c955d01c5a272539e96 (diff)
downloadcaffeonacl-f3a933a620b8b089a7fe29ba76ec27f5408ff68d.tar.gz
caffeonacl-f3a933a620b8b089a7fe29ba76ec27f5408ff68d.tar.bz2
caffeonacl-f3a933a620b8b089a7fe29ba76ec27f5408ff68d.zip
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.
Diffstat (limited to 'src')
-rw-r--r--src/caffe/data_transformer.cpp16
-rw-r--r--src/caffe/layers/data_layer.cpp3
-rw-r--r--src/caffe/layers/image_data_layer.cpp2
-rw-r--r--src/caffe/layers/memory_data_layer.cpp4
-rw-r--r--src/caffe/layers/window_data_layer.cpp2
-rw-r--r--src/caffe/test/test_data_layer.cpp6
-rw-r--r--src/caffe/test/test_data_transformer.cpp2
-rw-r--r--src/caffe/test/test_db.cpp2
-rw-r--r--src/caffe/test/test_image_data_layer.cpp2
-rw-r--r--src/caffe/test/test_io.cpp2
-rw-r--r--src/caffe/test/test_layer_factory.cpp4
-rw-r--r--src/caffe/test/test_memory_data_layer.cpp5
-rw-r--r--src/caffe/test/test_upgrade_proto.cpp12
-rw-r--r--src/caffe/util/db.cpp14
-rw-r--r--src/caffe/util/db_leveldb.cpp2
-rw-r--r--src/caffe/util/db_lmdb.cpp2
-rw-r--r--src/caffe/util/io.cpp10
17 files changed, 81 insertions, 9 deletions
diff --git a/src/caffe/data_transformer.cpp b/src/caffe/data_transformer.cpp
index 4666d9bd..7189d67e 100644
--- a/src/caffe/data_transformer.cpp
+++ b/src/caffe/data_transformer.cpp
@@ -1,4 +1,6 @@
+#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
+#endif // USE_OPENCV
#include <string>
#include <vector>
@@ -124,11 +126,13 @@ void DataTransformer<Dtype>::Transform(const Datum& datum,
}
}
+
template<typename Dtype>
void DataTransformer<Dtype>::Transform(const Datum& datum,
Blob<Dtype>* transformed_blob) {
// If datum is encoded, decoded and transform the cv::image.
if (datum.encoded()) {
+#ifdef USE_OPENCV
CHECK(!(param_.force_color() && param_.force_gray()))
<< "cannot set both force_color and force_gray";
cv::Mat cv_img;
@@ -140,6 +144,9 @@ void DataTransformer<Dtype>::Transform(const Datum& datum,
}
// Transform the cv::image into blob.
return Transform(cv_img, transformed_blob);
+#else
+ LOG(FATAL) << "Encoded datum requires OpenCV; compile with USE_OPENCV.";
+#endif // USE_OPENCV
} else {
if (param_.force_color() || param_.force_gray()) {
LOG(ERROR) << "force_color and force_gray only for encoded datum";
@@ -194,6 +201,7 @@ void DataTransformer<Dtype>::Transform(const vector<Datum> & datum_vector,
}
}
+#ifdef USE_OPENCV
template<typename Dtype>
void DataTransformer<Dtype>::Transform(const vector<cv::Mat> & mat_vector,
Blob<Dtype>* transformed_blob) {
@@ -315,6 +323,7 @@ void DataTransformer<Dtype>::Transform(const cv::Mat& cv_img,
}
}
}
+#endif // USE_OPENCV
template<typename Dtype>
void DataTransformer<Dtype>::Transform(Blob<Dtype>* input_blob,
@@ -432,6 +441,7 @@ void DataTransformer<Dtype>::Transform(Blob<Dtype>* input_blob,
template<typename Dtype>
vector<int> DataTransformer<Dtype>::InferBlobShape(const Datum& datum) {
if (datum.encoded()) {
+#ifdef USE_OPENCV
CHECK(!(param_.force_color() && param_.force_gray()))
<< "cannot set both force_color and force_gray";
cv::Mat cv_img;
@@ -443,8 +453,10 @@ vector<int> DataTransformer<Dtype>::InferBlobShape(const Datum& datum) {
}
// InferBlobShape using the cv::image.
return InferBlobShape(cv_img);
+#else
+ LOG(FATAL) << "Encoded datum requires OpenCV; compile with USE_OPENCV.";
+#endif // USE_OPENCV
}
-
const int crop_size = param_.crop_size();
const int datum_channels = datum.channels();
const int datum_height = datum.height();
@@ -474,6 +486,7 @@ vector<int> DataTransformer<Dtype>::InferBlobShape(
return shape;
}
+#ifdef USE_OPENCV
template<typename Dtype>
vector<int> DataTransformer<Dtype>::InferBlobShape(const cv::Mat& cv_img) {
const int crop_size = param_.crop_size();
@@ -504,6 +517,7 @@ vector<int> DataTransformer<Dtype>::InferBlobShape(
shape[0] = num;
return shape;
}
+#endif // USE_OPENCV
template <typename Dtype>
void DataTransformer<Dtype>::InitRand() {
diff --git a/src/caffe/layers/data_layer.cpp b/src/caffe/layers/data_layer.cpp
index 0932d9fe..71f8cb09 100644
--- a/src/caffe/layers/data_layer.cpp
+++ b/src/caffe/layers/data_layer.cpp
@@ -1,5 +1,6 @@
+#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
-
+#endif // USE_OPENCV
#include <stdint.h>
#include <string>
diff --git a/src/caffe/layers/image_data_layer.cpp b/src/caffe/layers/image_data_layer.cpp
index 223ba3a7..3d2190f8 100644
--- a/src/caffe/layers/image_data_layer.cpp
+++ b/src/caffe/layers/image_data_layer.cpp
@@ -1,3 +1,4 @@
+#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <fstream> // NOLINT(readability/streams)
@@ -164,3 +165,4 @@ INSTANTIATE_CLASS(ImageDataLayer);
REGISTER_LAYER_CLASS(ImageData);
} // namespace caffe
+#endif // USE_OPENCV
diff --git a/src/caffe/layers/memory_data_layer.cpp b/src/caffe/layers/memory_data_layer.cpp
index 42de4198..2370aa04 100644
--- a/src/caffe/layers/memory_data_layer.cpp
+++ b/src/caffe/layers/memory_data_layer.cpp
@@ -1,4 +1,6 @@
+#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
+#endif // USE_OPENCV
#include <vector>
@@ -53,6 +55,7 @@ void MemoryDataLayer<Dtype>::AddDatumVector(const vector<Datum>& datum_vector) {
has_new_data_ = true;
}
+#ifdef USE_OPENCV
template <typename Dtype>
void MemoryDataLayer<Dtype>::AddMatVector(const vector<cv::Mat>& mat_vector,
const vector<int>& labels) {
@@ -76,6 +79,7 @@ void MemoryDataLayer<Dtype>::AddMatVector(const vector<cv::Mat>& mat_vector,
Reset(top_data, top_label, num);
has_new_data_ = true;
}
+#endif // USE_OPENCV
template <typename Dtype>
void MemoryDataLayer<Dtype>::Reset(Dtype* data, Dtype* labels, int n) {
diff --git a/src/caffe/layers/window_data_layer.cpp b/src/caffe/layers/window_data_layer.cpp
index f637f2ec..f8db61c9 100644
--- a/src/caffe/layers/window_data_layer.cpp
+++ b/src/caffe/layers/window_data_layer.cpp
@@ -1,3 +1,4 @@
+#ifdef USE_OPENCV
#include <opencv2/highgui/highgui_c.h>
#include <stdint.h>
@@ -468,3 +469,4 @@ INSTANTIATE_CLASS(WindowDataLayer);
REGISTER_LAYER_CLASS(WindowData);
} // namespace caffe
+#endif // USE_OPENCV
diff --git a/src/caffe/test/test_data_layer.cpp b/src/caffe/test/test_data_layer.cpp
index afe2a40d..9e03954a 100644
--- a/src/caffe/test/test_data_layer.cpp
+++ b/src/caffe/test/test_data_layer.cpp
@@ -1,3 +1,4 @@
+#ifdef USE_OPENCV
#include <string>
#include <vector>
@@ -348,6 +349,7 @@ class DataLayerTest : public MultiDeviceTest<TypeParam> {
TYPED_TEST_CASE(DataLayerTest, TestDtypesAndDevices);
+#ifdef USE_LEVELDB
TYPED_TEST(DataLayerTest, TestReadLevelDB) {
const bool unique_pixels = false; // all pixels the same; images different
this->Fill(unique_pixels, DataParameter_DB_LEVELDB);
@@ -385,7 +387,9 @@ TYPED_TEST(DataLayerTest, TestReadCropTestLevelDB) {
this->Fill(unique_pixels, DataParameter_DB_LEVELDB);
this->TestReadCrop(TEST);
}
+#endif // USE_LEVELDB
+#ifdef USE_LMDB
TYPED_TEST(DataLayerTest, TestReadLMDB) {
const bool unique_pixels = false; // all pixels the same; images different
this->Fill(unique_pixels, DataParameter_DB_LMDB);
@@ -424,4 +428,6 @@ TYPED_TEST(DataLayerTest, TestReadCropTestLMDB) {
this->TestReadCrop(TEST);
}
+#endif // USE_LMDB
} // namespace caffe
+#endif // USE_OPENCV
diff --git a/src/caffe/test/test_data_transformer.cpp b/src/caffe/test/test_data_transformer.cpp
index 16570e20..8a101374 100644
--- a/src/caffe/test/test_data_transformer.cpp
+++ b/src/caffe/test/test_data_transformer.cpp
@@ -1,3 +1,4 @@
+#ifdef USE_OPENCV
#include <string>
#include <vector>
@@ -353,3 +354,4 @@ TYPED_TEST(DataTransformTest, TestMeanFile) {
}
} // namespace caffe
+#endif // USE_OPENCV
diff --git a/src/caffe/test/test_db.cpp b/src/caffe/test/test_db.cpp
index 5b2ac230..1b487b14 100644
--- a/src/caffe/test/test_db.cpp
+++ b/src/caffe/test/test_db.cpp
@@ -1,3 +1,4 @@
+#if defined(USE_LEVELDB) && defined(USE_LMDB) && defined(USE_OPENCV)
#include <string>
#include "boost/scoped_ptr.hpp"
@@ -132,3 +133,4 @@ TYPED_TEST(DBTest, TestWrite) {
}
} // namespace caffe
+#endif // USE_LEVELDB, USE_LMDB and USE_OPENCV
diff --git a/src/caffe/test/test_image_data_layer.cpp b/src/caffe/test/test_image_data_layer.cpp
index 931a5ebf..481fcef7 100644
--- a/src/caffe/test/test_image_data_layer.cpp
+++ b/src/caffe/test/test_image_data_layer.cpp
@@ -1,3 +1,4 @@
+#ifdef USE_OPENCV
#include <map>
#include <string>
#include <vector>
@@ -177,3 +178,4 @@ TYPED_TEST(ImageDataLayerTest, TestShuffle) {
}
} // namespace caffe
+#endif // USE_OPENCV
diff --git a/src/caffe/test/test_io.cpp b/src/caffe/test/test_io.cpp
index 4ab96311..c2c919e9 100644
--- a/src/caffe/test/test_io.cpp
+++ b/src/caffe/test/test_io.cpp
@@ -1,3 +1,4 @@
+#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
@@ -420,3 +421,4 @@ TEST_F(IOTest, TestDecodeDatumToCVMatContentNative) {
}
} // namespace caffe
+#endif // USE_OPENCV
diff --git a/src/caffe/test/test_layer_factory.cpp b/src/caffe/test/test_layer_factory.cpp
index c86fafd0..7d5d39d8 100644
--- a/src/caffe/test/test_layer_factory.cpp
+++ b/src/caffe/test/test_layer_factory.cpp
@@ -31,12 +31,16 @@ TYPED_TEST(LayerFactoryTest, TestCreateLayer) {
LayerParameter layer_param;
// Data layers expect a DB
if (iter->first == "Data") {
+#ifdef USE_LEVELDB
string tmp;
MakeTempDir(&tmp);
boost::scoped_ptr<db::DB> db(db::GetDB(DataParameter_DB_LEVELDB));
db->Open(tmp, db::NEW);
db->Close();
layer_param.mutable_data_param()->set_source(tmp);
+#else
+ continue;
+#endif // USE_LEVELDB
}
layer_param.set_type(iter->first);
layer = LayerRegistry<Dtype>::CreateLayer(layer_param);
diff --git a/src/caffe/test/test_memory_data_layer.cpp b/src/caffe/test/test_memory_data_layer.cpp
index a79033f5..7269a4d4 100644
--- a/src/caffe/test/test_memory_data_layer.cpp
+++ b/src/caffe/test/test_memory_data_layer.cpp
@@ -1,4 +1,6 @@
+#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
+#endif // USE_OPENCV
#include <string>
#include <vector>
@@ -113,6 +115,7 @@ TYPED_TEST(MemoryDataLayerTest, TestForward) {
}
}
+#ifdef USE_OPENCV
TYPED_TEST(MemoryDataLayerTest, AddDatumVectorDefaultTransform) {
typedef typename TypeParam::Dtype Dtype;
@@ -292,5 +295,5 @@ TYPED_TEST(MemoryDataLayerTest, TestSetBatchSize) {
}
}
}
-
+#endif // USE_OPENCV
} // namespace caffe
diff --git a/src/caffe/test/test_upgrade_proto.cpp b/src/caffe/test/test_upgrade_proto.cpp
index 00672023..ee05b151 100644
--- a/src/caffe/test/test_upgrade_proto.cpp
+++ b/src/caffe/test/test_upgrade_proto.cpp
@@ -2892,6 +2892,7 @@ TEST_F(NetUpgradeTest, TestImageNet) {
this->RunV1UpgradeTest(expected_v1_proto, expected_v2_proto);
} // NOLINT(readability/fn_size)
+#ifdef USE_OPENCV
TEST_F(NetUpgradeTest, TestUpgradeV1LayerType) {
LayerParameter layer_param;
shared_ptr<Layer<float> > layer;
@@ -2906,16 +2907,25 @@ TEST_F(NetUpgradeTest, TestUpgradeV1LayerType) {
layer_param.set_type(v2_layer_type);
// Data layers expect a DB
if (v2_layer_type == "Data") {
+ #ifdef USE_LEVELDB
string tmp;
MakeTempDir(&tmp);
boost::scoped_ptr<db::DB> db(db::GetDB(DataParameter_DB_LEVELDB));
db->Open(tmp, db::NEW);
db->Close();
layer_param.mutable_data_param()->set_source(tmp);
+ #else
+ continue;
+ #endif // USE_LEVELDB
}
+ #ifndef USE_OPENCV
+ if (v2_layer_type == "ImageData" || v2_layer_type == "WindowData") {
+ continue;
+ }
+ #endif // !USE_OPENCV
layer = LayerRegistry<float>::CreateLayer(layer_param);
EXPECT_EQ(v2_layer_type, layer->type());
}
}
-
+#endif // USE_OPENCV
} // NOLINT(readability/fn_size) // namespace caffe
diff --git a/src/caffe/util/db.cpp b/src/caffe/util/db.cpp
index f55420e9..ccda054d 100644
--- a/src/caffe/util/db.cpp
+++ b/src/caffe/util/db.cpp
@@ -8,23 +8,31 @@ namespace caffe { namespace db {
DB* GetDB(DataParameter::DB backend) {
switch (backend) {
+#ifdef USE_LEVELDB
case DataParameter_DB_LEVELDB:
return new LevelDB();
+#endif // USE_LEVELDB
+#ifdef USE_LMDB
case DataParameter_DB_LMDB:
return new LMDB();
+#endif // USE_LMDB
default:
LOG(FATAL) << "Unknown database backend";
}
}
DB* GetDB(const string& backend) {
+#ifdef USE_LEVELDB
if (backend == "leveldb") {
return new LevelDB();
- } else if (backend == "lmdb") {
+ }
+#endif // USE_LEVELDB
+#ifdef USE_LMDB
+ if (backend == "lmdb") {
return new LMDB();
- } else {
- LOG(FATAL) << "Unknown database backend";
}
+#endif // USE_LMDB
+ LOG(FATAL) << "Unknown database backend";
}
} // namespace db
diff --git a/src/caffe/util/db_leveldb.cpp b/src/caffe/util/db_leveldb.cpp
index 06c46627..f5c4d8a6 100644
--- a/src/caffe/util/db_leveldb.cpp
+++ b/src/caffe/util/db_leveldb.cpp
@@ -1,3 +1,4 @@
+#ifdef USE_LEVELDB
#include "caffe/util/db_leveldb.hpp"
#include <string>
@@ -19,3 +20,4 @@ void LevelDB::Open(const string& source, Mode mode) {
} // namespace db
} // namespace caffe
+#endif // USE_LEVELDB
diff --git a/src/caffe/util/db_lmdb.cpp b/src/caffe/util/db_lmdb.cpp
index a054b796..78dd880a 100644
--- a/src/caffe/util/db_lmdb.cpp
+++ b/src/caffe/util/db_lmdb.cpp
@@ -1,3 +1,4 @@
+#ifdef USE_LMDB
#include "caffe/util/db_lmdb.hpp"
#include <sys/stat.h>
@@ -49,3 +50,4 @@ void LMDBTransaction::Put(const string& key, const string& value) {
} // namespace db
} // namespace caffe
+#endif // USE_LMDB
diff --git a/src/caffe/util/io.cpp b/src/caffe/util/io.cpp
index 6f033142..f2b1dd98 100644
--- a/src/caffe/util/io.cpp
+++ b/src/caffe/util/io.cpp
@@ -3,9 +3,11 @@
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <opencv2/core/core.hpp>
+#ifdef USE_OPENCV
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc.hpp>
+#endif // USE_OPENCV
#include <stdint.h>
#include <algorithm>
@@ -67,6 +69,7 @@ void WriteProtoToBinaryFile(const Message& proto, const char* filename) {
CHECK(proto.SerializeToOstream(&output));
}
+#ifdef USE_OPENCV
cv::Mat ReadImageToCVMat(const string& filename,
const int height, const int width, const bool is_color) {
cv::Mat cv_img;
@@ -98,6 +101,7 @@ cv::Mat ReadImageToCVMat(const string& filename,
cv::Mat ReadImageToCVMat(const string& filename) {
return ReadImageToCVMat(filename, 0, 0, true);
}
+
// Do the file extension and encoding match?
static bool matchExt(const std::string & fn,
std::string en) {
@@ -111,6 +115,7 @@ static bool matchExt(const std::string & fn,
return true;
return false;
}
+
bool ReadImageToDatum(const string& filename, const int label,
const int height, const int width, const bool is_color,
const std::string & encoding, Datum* datum) {
@@ -135,6 +140,7 @@ bool ReadImageToDatum(const string& filename, const int label,
return false;
}
}
+#endif // USE_OPENCV
bool ReadFileToDatum(const string& filename, const int label,
Datum* datum) {
@@ -156,6 +162,7 @@ bool ReadFileToDatum(const string& filename, const int label,
}
}
+#ifdef USE_OPENCV
cv::Mat DecodeDatumToCVMatNative(const Datum& datum) {
cv::Mat cv_img;
CHECK(datum.encoded()) << "Datum not encoded";
@@ -227,6 +234,5 @@ void CVMatToDatum(const cv::Mat& cv_img, Datum* datum) {
}
datum->set_data(buffer);
}
-
-
+#endif // USE_OPENCV
} // namespace caffe