Skip to content

Commit

Permalink
Data_layer (#1933) update and URI sources
Browse files Browse the repository at this point in the history
  • Loading branch information
cypof committed Apr 22, 2015
1 parent c6414ea commit 60d2cb0
Show file tree
Hide file tree
Showing 38 changed files with 1,349 additions and 232 deletions.
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PROJECT := caffe

CONFIG_FILE := Makefile.config
CONFIG_FILE ?= Makefile.config
include $(CONFIG_FILE)

BUILD_DIR_LINK := $(BUILD_DIR)
Expand Down Expand Up @@ -161,8 +161,11 @@ ifneq ($(CPU_ONLY), 1)
LIBRARY_DIRS += $(CUDA_LIB_DIR)
LIBRARIES := cudart cublas curand
endif
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 m \
ifneq ($(NO_IO_DEPENDENCIES), 1)
LIBRARIES += leveldb lmdb snappy hdf5_hl hdf5 # TODO opencv
endif
LIBRARIES += glog gflags protobuf m \
boost_system \
opencv_core opencv_highgui opencv_imgproc
PYTHON_LIBRARIES := boost_python python2.7
WARNINGS := -Wall -Wno-sign-compare
Expand Down Expand Up @@ -271,6 +274,8 @@ endif
# Debugging
ifeq ($(DEBUG), 1)
COMMON_FLAGS += -DDEBUG -g -O0
# Compile issue in DEBUG on MAC (https://svn.boost.org/trac/boost/ticket/9392)
COMMON_FLAGS += -DBOOST_NOINLINE='__attribute__ ((noinline))'
NVCCFLAGS += -G
else
COMMON_FLAGS += -DNDEBUG -O2
Expand All @@ -288,10 +293,15 @@ ifeq ($(CPU_ONLY), 1)
TEST_OBJS := $(TEST_CXX_OBJS)
TEST_BINS := $(TEST_CXX_BINS)
ALL_WARNS := $(ALL_CXX_WARNS)
TEST_FILTER := --gtest_filter="-*GPU*"
TEST_FILTER += --gtest_filter="-*GPU*"
COMMON_FLAGS += -DCPU_ONLY
endif

# No IO dependencies
ifeq ($(NO_IO_DEPENDENCIES), 1)
COMMON_FLAGS += -DNO_IO_DEPENDENCIES
endif

# Python layer support
ifeq ($(WITH_PYTHON_LAYER), 1)
COMMON_FLAGS += -DWITH_PYTHON_LAYER
Expand Down
3 changes: 3 additions & 0 deletions Makefile.config.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

# Removes IO dependencies and related code: lmdb, leveldb, snappy, hdf5
# NO_IO_DEPENDENCIES := 1

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
Expand Down
9 changes: 9 additions & 0 deletions examples/mnist/convert_mnist_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// The MNIST dataset could be downloaded at
// http://yann.lecun.com/exdb/mnist/

#ifndef NO_IO_DEPENDENCIES

#include <gflags/gflags.h>
#include <glog/logging.h>
#include <google/protobuf/text_format.h>
Expand Down Expand Up @@ -196,3 +198,10 @@ int main(int argc, char** argv) {
}
return 0;
}

#else
#include <stdio.h>
int main(int argc, char** argv) {
fprintf(stderr, "NO_IO_DEPENDENCIES false");
}
#endif
10 changes: 10 additions & 0 deletions examples/siamese/convert_mnist_siamese_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
// convert_mnist_data input_image_file input_label_file output_db_file
// The MNIST dataset could be downloaded at
// http://yann.lecun.com/exdb/mnist/

#ifndef NO_IO_DEPENDENCIES

#include <fstream> // NOLINT(readability/streams)
#include <string>

Expand Down Expand Up @@ -121,3 +124,10 @@ int main(int argc, char** argv) {
}
return 0;
}

#else
#include <stdio.h>
int main(int argc, char** argv) {
fprintf(stderr, "NO_IO_DEPENDENCIES false");
}
#endif
21 changes: 14 additions & 7 deletions include/caffe/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ void GlobalInit(int* pargc, char*** pargv);
class Caffe {
public:
~Caffe();
inline static Caffe& Get() {
if (!singleton_.get()) {
singleton_.reset(new Caffe());
}
return *singleton_;
}

// Thread local context for Caffe. Moved to common.cpp instead of
// including boost/thread.hpp to avoid a boost/NVCC issues (#1009, #1010)
// on OSX. Also fails on Linux with CUDA 7.0.18.
static Caffe& Get();

enum Brew { CPU, GPU };

// This random number generator facade hides boost and CUDA rng
Expand Down Expand Up @@ -149,6 +149,12 @@ class Caffe {
static void SetDevice(const int device_id);
// Prints the current GPU status.
static void DeviceQuery();
// Parallel training info
inline static int solver_count() { return Get().solver_count_; }
inline static void set_solver_count(int val) { Get().solver_count_ = val; }
inline static bool root_solver() { return Get().solver_index_ == 0; }
inline static int solver_index() { return Get().solver_index_; }
inline static void set_solver_index(int val) { Get().solver_index_ = val; }

protected:
#ifndef CPU_ONLY
Expand All @@ -158,7 +164,8 @@ class Caffe {
shared_ptr<RNG> random_generator_;

Brew mode_;
static shared_ptr<Caffe> singleton_;
int solver_count_;
int solver_index_;

private:
// The private constructor to avoid duplicate instantiation.
Expand Down
57 changes: 39 additions & 18 deletions include/caffe/data_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include <utility>
#include <vector>

#include "boost/scoped_ptr.hpp"
#include "boost/random/uniform_real.hpp"
#include "boost/random/variate_generator.hpp"
#ifndef NO_IO_DEPENDENCIES
#include "hdf5.h"

#endif
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/data_transformer.hpp"
Expand All @@ -16,7 +18,10 @@
#include "caffe/layer.hpp"
#include "caffe/net.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/scheme.hpp"
#include "caffe/util/blocking_queue.hpp"
#include "caffe/util/db.hpp"
#include "caffe/util/rng.hpp"

namespace caffe {

Expand Down Expand Up @@ -52,12 +57,17 @@ class BaseDataLayer : public Layer<Dtype> {
bool output_labels_;
};

template <typename Dtype>
class Batch {
public:
Blob<Dtype> data_, label_;
};

template <typename Dtype>
class BasePrefetchingDataLayer :
public BaseDataLayer<Dtype>, public InternalThread {
public:
explicit BasePrefetchingDataLayer(const LayerParameter& param)
: BaseDataLayer<Dtype>(param) {}
explicit BasePrefetchingDataLayer(const LayerParameter& param);
virtual ~BasePrefetchingDataLayer() {}
// LayerSetUp: implements common data layer setup functionality, and calls
// DataLayerSetUp to do special data layer setup for individual layer types.
Expand All @@ -70,22 +80,24 @@ class BasePrefetchingDataLayer :
virtual void Forward_gpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);

virtual void CreatePrefetchThread();
virtual void JoinPrefetchThread();
// The thread's function
virtual void InternalThreadEntry() {}
// Prefetches batches (asynchronously if to GPU memory)
static const int PREFETCH_COUNT = 3;

protected:
Blob<Dtype> prefetch_data_;
Blob<Dtype> prefetch_label_;
virtual void InternalThreadEntry();
virtual void load_batch(Batch<Dtype>* batch) = 0;

Batch<Dtype> prefetch_[PREFETCH_COUNT];
blocking_queue<Batch<Dtype>*> prefetch_free_;
blocking_queue<Batch<Dtype>*> prefetch_full_;

Blob<Dtype> transformed_data_;
};

template <typename Dtype>
class DataLayer : public BasePrefetchingDataLayer<Dtype> {
class DataLayer: public BasePrefetchingDataLayer<Dtype> {
public:
explicit DataLayer(const LayerParameter& param)
: BasePrefetchingDataLayer<Dtype>(param) {}
explicit DataLayer(const LayerParameter& param);
virtual ~DataLayer();
virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
Expand All @@ -96,10 +108,13 @@ class DataLayer : public BasePrefetchingDataLayer<Dtype> {
virtual inline int MaxTopBlobs() const { return 2; }

protected:
virtual void InternalThreadEntry();
virtual void load_batch(Batch<Dtype>* batch);
Reader* next_reader();

shared_ptr<db::DB> db_;
shared_ptr<db::Cursor> cursor_;
vector<shared_ptr<Reader> > readers_;
boost::uniform_real<float> random_distribution_;
shared_ptr<boost::variate_generator<rng_t*, boost::uniform_real<float> > >
variate_generator_;
};

/**
Expand All @@ -112,6 +127,7 @@ class DummyDataLayer : public Layer<Dtype> {
public:
explicit DummyDataLayer(const LayerParameter& param)
: Layer<Dtype>(param) {}
virtual ~DummyDataLayer() {}
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);
// Data layers have no bottoms, so reshaping is trivial.
Expand All @@ -134,6 +150,8 @@ class DummyDataLayer : public Layer<Dtype> {
vector<bool> refill_;
};

#ifndef NO_IO_DEPENDENCIES

/**
* @brief Provides data to the Net from HDF5 files.
*
Expand Down Expand Up @@ -217,6 +235,8 @@ class HDF5OutputLayer : public Layer<Dtype> {
Blob<Dtype> label_blob_;
};

#endif

/**
* @brief Provides data to the Net from image files.
*
Expand All @@ -238,7 +258,7 @@ class ImageDataLayer : public BasePrefetchingDataLayer<Dtype> {
protected:
shared_ptr<Caffe::RNG> prefetch_rng_;
virtual void ShuffleImages();
virtual void InternalThreadEntry();
virtual void load_batch(Batch<Dtype>* batch);

vector<std::pair<std::string, int> > lines_;
int lines_id_;
Expand All @@ -254,6 +274,7 @@ class MemoryDataLayer : public BaseDataLayer<Dtype> {
public:
explicit MemoryDataLayer(const LayerParameter& param)
: BaseDataLayer<Dtype>(param), has_new_data_(false) {}
virtual ~MemoryDataLayer() {}
virtual void DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top);

Expand Down Expand Up @@ -310,7 +331,7 @@ class WindowDataLayer : public BasePrefetchingDataLayer<Dtype> {

protected:
virtual unsigned int PrefetchRand();
virtual void InternalThreadEntry();
virtual void load_batch(Batch<Dtype>* batch);

shared_ptr<Caffe::RNG> prefetch_rng_;
vector<std::pair<std::string, vector<int> > > image_database_;
Expand Down
24 changes: 18 additions & 6 deletions include/caffe/internal_thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ namespace caffe {
/**
* Virtual class encapsulate boost::thread for use in base class
* The child class will acquire the ability to run a single thread,
* by reimplementing the virutal function InternalThreadEntry.
* by reimplementing the virtual function InternalThreadEntry.
*/
class InternalThread {
public:
InternalThread() : thread_() {}
InternalThread();
virtual ~InternalThread();

/** Returns true if the thread was successfully started. **/
bool StartInternalThread();
// Caffe's thread local state will be initialized using the current
// thread values, e.g. device id, solver index etc. The random seed
// is initialized using caffe_rng_rand.
void StartInternalThread();

/** Will not return until the internal thread has exited. */
bool WaitForInternalThreadToExit();
// Will not return until the thread has exited.
void StopInternalThread();

bool is_started() const;

Expand All @@ -34,7 +36,17 @@ class InternalThread {
with the code you want your thread to run. */
virtual void InternalThreadEntry() {}

bool must_stop();

private:
void entry();

shared_ptr<boost::thread> thread_;
int device_;
Caffe::Brew mode_;
int rand_seed_;
int solver_count_;
int solver_index_;
};

} // namespace caffe
Expand Down
Loading

0 comments on commit 60d2cb0

Please sign in to comment.