From 77d570093e8a3340a929633f0d54f9c5810bd45e Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sun, 8 Jun 2014 14:58:53 -0700 Subject: [PATCH 1/2] add DummyDataLayer --- include/caffe/data_layers.hpp | 27 +++++++ src/caffe/layer_factory.cpp | 2 + src/caffe/layers/dummy_data_layer.cpp | 100 ++++++++++++++++++++++++++ src/caffe/proto/caffe.proto | 26 +++++-- 4 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 src/caffe/layers/dummy_data_layer.cpp diff --git a/include/caffe/data_layers.hpp b/include/caffe/data_layers.hpp index 87be0f8e742..3d3ce94bc88 100644 --- a/include/caffe/data_layers.hpp +++ b/include/caffe/data_layers.hpp @@ -14,6 +14,7 @@ #include "caffe/blob.hpp" #include "caffe/common.hpp" +#include "caffe/filler.hpp" #include "caffe/layer.hpp" #include "caffe/proto/caffe.pb.h" @@ -146,6 +147,32 @@ class DataLayer : public Layer { Caffe::Phase phase_; }; +template +class DummyDataLayer : public Layer { + public: + explicit DummyDataLayer(const LayerParameter& param) + : Layer(param) {} + virtual void SetUp(const vector*>& bottom, + vector*>* top); + + virtual inline LayerParameter_LayerType type() const { + return LayerParameter_LayerType_DUMMY_DATA; + } + virtual inline int ExactNumBottomBlobs() const { return 0; } + virtual inline int MinTopBlobs() const { return 1; } + + protected: + virtual Dtype Forward_cpu(const vector*>& bottom, + vector*>* top); + virtual void Backward_cpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { return; } + virtual void Backward_gpu(const vector*>& top, + const bool propagate_down, vector*>* bottom) { return; } + + vector > > fillers_; + vector refill_; +}; + // This function is used to create a pthread that prefetches the data. template void* ImageDataLayerPrefetch(void* layer_pointer); diff --git a/src/caffe/layer_factory.cpp b/src/caffe/layer_factory.cpp index 58c20b1f430..d6e506dfb9f 100644 --- a/src/caffe/layer_factory.cpp +++ b/src/caffe/layer_factory.cpp @@ -36,6 +36,8 @@ Layer* GetLayer(const LayerParameter& param) { return new DataLayer(param); case LayerParameter_LayerType_DROPOUT: return new DropoutLayer(param); + case LayerParameter_LayerType_DUMMY_DATA: + return new DummyDataLayer(param); case LayerParameter_LayerType_EUCLIDEAN_LOSS: return new EuclideanLossLayer(param); case LayerParameter_LayerType_ELTWISE: diff --git a/src/caffe/layers/dummy_data_layer.cpp b/src/caffe/layers/dummy_data_layer.cpp new file mode 100644 index 00000000000..38568580485 --- /dev/null +++ b/src/caffe/layers/dummy_data_layer.cpp @@ -0,0 +1,100 @@ +// Copyright 2014 BVLC and contributors. + +#include + +#include "caffe/filler.hpp" +#include "caffe/layer.hpp" +#include "caffe/vision_layers.hpp" + +namespace caffe { + +template +void DummyDataLayer::SetUp(const vector*>& bottom, + vector*>* top) { + const int num_top = top->size(); + const DummyDataParameter& param = this->layer_param_.dummy_data_param(); + const int num_data_filler = param.data_filler_size(); + CHECK(num_data_filler == 0 || num_data_filler == 1 || + num_data_filler == num_top) + << "Number of data fillers must be 0, 1 or equal to the number of tops: " + << num_top << "; you specified " << num_data_filler << " data fillers."; + CHECK(param.num_size() == 1 || param.num_size() == num_top) + << "Must specify either a single (1) 'num' or one for each top blob (" + << num_top << "); you specified " << param.num_size() << "."; + CHECK(param.channels_size() == 1 || param.channels_size() == num_top) + << "Must specify either a single (1) 'channels' or one for each top blob (" + << num_top << "); you specified " << param.channels_size() << "."; + CHECK(param.height_size() == 1 || param.height_size() == num_top) + << "Must specify either a single (1) 'height' or one for each top blob (" + << num_top << "); you specified " << param.height_size() << "."; + CHECK(param.width_size() == 1 || param.width_size() == num_top) + << "Must specify either a single (1) 'width' or one for each top blob (" + << num_top << "); you specified " << param.width_size() << "."; + // refill_[i] tells Forward i whether or not to actually refill top Blob i. + // If refill_[i] is false, Forward does nothing for Blob i. We use this to + // avoid wastefully refilling "constant" Blobs in every forward pass. + // We first fill refill_ in with the INVERSE of its final values. + // The first time we run Forward from the SetUp method, we'll fill only the + // Blobs for which refill_ is normally false. These Blobs will never be + // filled again. + refill_.clear(); + fillers_.clear(); + if (num_data_filler <= 1) { + FillerParameter filler_param; + if (num_data_filler == 0) { + filler_param.set_type("constant"); + filler_param.set_value(0); + } else { + filler_param.CopyFrom(param.data_filler(0)); + } + // Refill on each iteration iff not using a constant filler, + // but use the inverse of this rule for the first run. + refill_.resize(1); + refill_[0] = (strcmp(filler_param.type().c_str(), "constant") == 0); + fillers_.resize(1); + fillers_[0].reset(GetFiller(filler_param)); + } else { + refill_.resize(num_top); + fillers_.resize(num_top); + for (int i = 0; i < num_top; ++i) { + fillers_[i].reset(GetFiller(param.data_filler(i))); + // Refill on each iteration iff not using a constant filler, + // but use the inverse of this rule for the first run. + refill_[i] = + (strcmp(param.data_filler(i).type().c_str(), "constant") == 0); + } + } + for (int i = 0; i < num_top; ++i) { + const int num = (param.num_size() == 1) ? param.num(0) : param.num(i); + const int channels = + (param.channels_size() == 1) ? param.channels(0) : param.channels(i); + const int height = + (param.height_size() == 1) ? param.height(0) : param.height(i); + const int width = + (param.width_size() == 1) ? param.width(0) : param.width(i); + (*top)[i]->Reshape(num, channels, height, width); + } + // Run Forward once, with refill_ inverted, to fill the constant Blobs. + Forward(bottom, top); + // Invert the inverted refill_ values to refill the desired (non-constant) + // Blobs in every usual forward pass. + for (int i = 0; i < refill_.size(); ++i) { + refill_[i] = !refill_[i]; + } +} + +template +Dtype DummyDataLayer::Forward_cpu(const vector*>& bottom, + vector*>* top) { + for (int i = 0; i < top->size(); ++i) { + const int filler_id = (fillers_.size() > 1) ? i : 0; + if (refill_[filler_id]) { + fillers_[filler_id]->Fill((*top)[i]); + } + } + return Dtype(0.); +} + +INSTANTIATE_CLASS(DummyDataLayer); + +} // namespace caffe diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index e540a95a921..60c7daa9031 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -115,7 +115,7 @@ message SolverState { // NOTE // Update the next available ID when you add a new LayerParameter field. // -// LayerParameter next available ID: 25 (last added: eltwise_param) +// LayerParameter next available ID: 27 (last added: dummy_data_param) message LayerParameter { repeated string bottom = 2; // the name of the bottom blobs repeated string top = 3; // the name of the top blobs @@ -127,7 +127,7 @@ message LayerParameter { // line above the enum. Update the next available ID when you add a new // LayerType. // - // LayerType next available ID: 32 (last added: THRESHOLD) + // LayerType next available ID: 33 (last added: DUMMY_DATA) enum LayerType { // "NONE" layer type is 0th enum element so that we don't cause confusion // by defaulting to an existent LayerType (instead, should usually error if @@ -140,6 +140,7 @@ message LayerParameter { CONVOLUTION = 4; DATA = 5; DROPOUT = 6; + DUMMY_DATA = 32; EUCLIDEAN_LOSS = 7; ELTWISE = 25; FLATTEN = 8; @@ -175,13 +176,12 @@ message LayerParameter { // The weight decay that is multiplied on the global weight decay. repeated float weight_decay = 8; - // Parameters for particular layer types. - // Parameters next available ID: 26 (last added: ThresholdParameter) optional ArgMaxParameter argmax_param = 23; optional ConcatParameter concat_param = 9; optional ConvolutionParameter convolution_param = 10; optional DataParameter data_param = 11; optional DropoutParameter dropout_param = 12; + optional DummyDataParameter dummy_data_param = 26; optional EltwiseParameter eltwise_param = 24; optional HDF5DataParameter hdf5_data_param = 13; optional HDF5OutputParameter hdf5_output_param = 14; @@ -254,6 +254,24 @@ message DropoutParameter { optional float dropout_ratio = 1 [default = 0.5]; // dropout ratio } +// Message that stores parameters used by DummyDataLayer. +// DummyDataLayer fills any number of arbitrarily shaped blobs with random +// (or constant) data generated by "Fillers" (see "message FillerParameter"). +message DummyDataParameter { + // This layer produces N >= 1 top blobs. DummyDataParameter must specify 1 or N + // num, N channels, N height, and N width fields, and must specify 0, 1 or N + // data_fillers. + // + // If 0 data_fillers are specified, ConstantFiller with a value of 0 is used. + // If 1 data_filler is specified, it is applied to all top blobs. If N are + // specified, the ith is applied to the ith top blob. + repeated FillerParameter data_filler = 1; + repeated uint32 num = 2; + repeated uint32 channels = 3; + repeated uint32 height = 4; + repeated uint32 width = 5; +} + // Message that stores parameters used by EltwiseLayer message EltwiseParameter { enum EltwiseOp { From 30fc73a175622a64feae29d535514bd973f24cbf Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Sun, 8 Jun 2014 16:04:41 -0700 Subject: [PATCH 2/2] add DummyDataLayer tests --- src/caffe/test/test_dummy_data_layer.cpp | 202 +++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 src/caffe/test/test_dummy_data_layer.cpp diff --git a/src/caffe/test/test_dummy_data_layer.cpp b/src/caffe/test/test_dummy_data_layer.cpp new file mode 100644 index 00000000000..7d9287e86ab --- /dev/null +++ b/src/caffe/test/test_dummy_data_layer.cpp @@ -0,0 +1,202 @@ +// Copyright 2014 BVLC and contributors. + +#include +#include + +#include "gtest/gtest.h" +#include "caffe/blob.hpp" +#include "caffe/common.hpp" +#include "caffe/vision_layers.hpp" +#include "caffe/proto/caffe.pb.h" +#include "caffe/test/test_caffe_main.hpp" + +using std::string; +using std::stringstream; + +namespace caffe { + +extern cudaDeviceProp CAFFE_TEST_CUDA_PROP; + +template +class DummyDataLayerTest : public ::testing::Test { + protected: + DummyDataLayerTest() + : blob_top_a_(new Blob()), + blob_top_b_(new Blob()), + blob_top_c_(new Blob()) {} + + virtual void SetUp() { + blob_bottom_vec_.clear(); + blob_top_vec_.clear(); + blob_top_vec_.push_back(blob_top_a_); + blob_top_vec_.push_back(blob_top_b_); + blob_top_vec_.push_back(blob_top_c_); + } + + virtual ~DummyDataLayerTest() { + delete blob_top_a_; + delete blob_top_b_; + delete blob_top_c_; + } + + Blob* const blob_top_a_; + Blob* const blob_top_b_; + Blob* const blob_top_c_; + vector*> blob_bottom_vec_; + vector*> blob_top_vec_; +}; + +typedef ::testing::Types Dtypes; +TYPED_TEST_CASE(DummyDataLayerTest, Dtypes); + +TYPED_TEST(DummyDataLayerTest, TestOneTopConstant) { + Caffe::set_mode(Caffe::CPU); + LayerParameter param; + DummyDataParameter* dummy_data_param = param.mutable_dummy_data_param(); + dummy_data_param->add_num(5); + dummy_data_param->add_channels(3); + dummy_data_param->add_height(2); + dummy_data_param->add_width(4); + this->blob_top_vec_.resize(1); + DummyDataLayer layer(param); + layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); + EXPECT_EQ(this->blob_top_a_->num(), 5); + EXPECT_EQ(this->blob_top_a_->channels(), 3); + EXPECT_EQ(this->blob_top_a_->height(), 2); + EXPECT_EQ(this->blob_top_a_->width(), 4); + EXPECT_EQ(this->blob_top_b_->count(), 0); + EXPECT_EQ(this->blob_top_c_->count(), 0); + for (int i = 0; i < this->blob_top_vec_.size(); ++i) { + for (int j = 0; j < this->blob_top_vec_[i]->count(); ++j) { + EXPECT_EQ(0, this->blob_top_vec_[i]->cpu_data()[j]); + } + } + layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_); + for (int i = 0; i < this->blob_top_vec_.size(); ++i) { + for (int j = 0; j < this->blob_top_vec_[i]->count(); ++j) { + EXPECT_EQ(0, this->blob_top_vec_[i]->cpu_data()[j]); + } + } +} + +TYPED_TEST(DummyDataLayerTest, TestTwoTopConstant) { + Caffe::set_mode(Caffe::CPU); + LayerParameter param; + DummyDataParameter* dummy_data_param = param.mutable_dummy_data_param(); + dummy_data_param->add_num(5); + dummy_data_param->add_channels(3); + dummy_data_param->add_height(2); + dummy_data_param->add_width(4); + dummy_data_param->add_num(5); + // Don't explicitly set number of channels or height for 2nd top blob; should + // default to first channels and height (as we check later). + dummy_data_param->add_height(1); + FillerParameter* data_filler_param = dummy_data_param->add_data_filler(); + data_filler_param->set_value(7); + this->blob_top_vec_.resize(2); + DummyDataLayer layer(param); + layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); + EXPECT_EQ(this->blob_top_a_->num(), 5); + EXPECT_EQ(this->blob_top_a_->channels(), 3); + EXPECT_EQ(this->blob_top_a_->height(), 2); + EXPECT_EQ(this->blob_top_a_->width(), 4); + EXPECT_EQ(this->blob_top_b_->num(), 5); + EXPECT_EQ(this->blob_top_b_->channels(), 3); + EXPECT_EQ(this->blob_top_b_->height(), 1); + EXPECT_EQ(this->blob_top_b_->width(), 4); + EXPECT_EQ(this->blob_top_c_->count(), 0); + for (int i = 0; i < this->blob_top_vec_.size(); ++i) { + for (int j = 0; j < this->blob_top_vec_[i]->count(); ++j) { + EXPECT_EQ(7, this->blob_top_vec_[i]->cpu_data()[j]); + } + } + layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_); + for (int i = 0; i < this->blob_top_vec_.size(); ++i) { + for (int j = 0; j < this->blob_top_vec_[i]->count(); ++j) { + EXPECT_EQ(7, this->blob_top_vec_[i]->cpu_data()[j]); + } + } +} + +TYPED_TEST(DummyDataLayerTest, TestThreeTopConstantGaussianConstant) { + Caffe::set_mode(Caffe::CPU); + LayerParameter param; + DummyDataParameter* dummy_data_param = param.mutable_dummy_data_param(); + dummy_data_param->add_num(5); + dummy_data_param->add_channels(3); + dummy_data_param->add_height(2); + dummy_data_param->add_width(4); + FillerParameter* data_filler_param_a = dummy_data_param->add_data_filler(); + data_filler_param_a->set_value(7); + FillerParameter* data_filler_param_b = dummy_data_param->add_data_filler(); + data_filler_param_b->set_type("gaussian"); + TypeParam gaussian_mean = 3.0; + TypeParam gaussian_std = 0.01; + data_filler_param_b->set_mean(gaussian_mean); + data_filler_param_b->set_std(gaussian_std); + FillerParameter* data_filler_param_c = dummy_data_param->add_data_filler(); + data_filler_param_c->set_value(9); + DummyDataLayer layer(param); + layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); + EXPECT_EQ(this->blob_top_a_->num(), 5); + EXPECT_EQ(this->blob_top_a_->channels(), 3); + EXPECT_EQ(this->blob_top_a_->height(), 2); + EXPECT_EQ(this->blob_top_a_->width(), 4); + EXPECT_EQ(this->blob_top_b_->num(), 5); + EXPECT_EQ(this->blob_top_b_->channels(), 3); + EXPECT_EQ(this->blob_top_b_->height(), 2); + EXPECT_EQ(this->blob_top_b_->width(), 4); + EXPECT_EQ(this->blob_top_c_->num(), 5); + EXPECT_EQ(this->blob_top_c_->channels(), 3); + EXPECT_EQ(this->blob_top_c_->height(), 2); + EXPECT_EQ(this->blob_top_c_->width(), 4); + for (int i = 0; i < this->blob_top_a_->count(); ++i) { + EXPECT_EQ(7, this->blob_top_a_->cpu_data()[i]); + } + // Blob b uses a Gaussian filler, so SetUp should not have initialized it. + // Blob b's data should therefore be the default Blob data value: 0. + for (int i = 0; i < this->blob_top_b_->count(); ++i) { + EXPECT_EQ(0, this->blob_top_b_->cpu_data()[i]); + } + for (int i = 0; i < this->blob_top_c_->count(); ++i) { + EXPECT_EQ(9, this->blob_top_c_->cpu_data()[i]); + } + + // Do a Forward pass to fill in Blob b with Gaussian data. + layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_); + for (int i = 0; i < this->blob_top_a_->count(); ++i) { + EXPECT_EQ(7, this->blob_top_a_->cpu_data()[i]); + } + // Check that the Gaussian's data has been filled in with values within + // 10 standard deviations of the mean. Record the first and last sample. + // to check that they're different after the next Forward pass. + for (int i = 0; i < this->blob_top_b_->count(); ++i) { + EXPECT_NEAR(gaussian_mean, this->blob_top_b_->cpu_data()[i], + gaussian_std * 10); + } + const TypeParam first_gaussian_sample = this->blob_top_b_->cpu_data()[0]; + const TypeParam last_gaussian_sample = + this->blob_top_b_->cpu_data()[this->blob_top_b_->count() - 1]; + for (int i = 0; i < this->blob_top_c_->count(); ++i) { + EXPECT_EQ(9, this->blob_top_c_->cpu_data()[i]); + } + + // Do another Forward pass to fill in Blob b with Gaussian data again, + // checking that we get different values. + layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_); + for (int i = 0; i < this->blob_top_a_->count(); ++i) { + EXPECT_EQ(7, this->blob_top_a_->cpu_data()[i]); + } + for (int i = 0; i < this->blob_top_b_->count(); ++i) { + EXPECT_NEAR(gaussian_mean, this->blob_top_b_->cpu_data()[i], + gaussian_std * 10); + } + EXPECT_NE(first_gaussian_sample, this->blob_top_b_->cpu_data()[0]); + EXPECT_NE(last_gaussian_sample, + this->blob_top_b_->cpu_data()[this->blob_top_b_->count() - 1]); + for (int i = 0; i < this->blob_top_c_->count(); ++i) { + EXPECT_EQ(9, this->blob_top_c_->cpu_data()[i]); + } +} + +} // namespace caffe