From 148535e6481b19876498ad160d9a18c04e1ba983 Mon Sep 17 00:00:00 2001 From: Yangqing Jia Date: Tue, 24 Sep 2013 20:08:56 -0700 Subject: [PATCH] data layer modification, and net header --- src/caffe/layers/data_layer.cpp | 15 ++++++----- src/caffe/net.hpp | 53 ++++++++++++++++++++++++++++++++++++++ src/caffe/proto/caffe.proto | 8 ++++-- src/caffe/test/test_data_layer.cpp | 16 +++++++----- 4 files changed, 77 insertions(+), 15 deletions(-) create mode 100644 src/caffe/net.hpp diff --git a/src/caffe/layers/data_layer.cpp b/src/caffe/layers/data_layer.cpp index 3cd76d1..c4bb8cc 100644 --- a/src/caffe/layers/data_layer.cpp +++ b/src/caffe/layers/data_layer.cpp @@ -1,5 +1,7 @@ // Copyright 2013 Yangqing Jia +#include +#include #include #include @@ -7,6 +9,8 @@ #include "caffe/layer.hpp" #include "caffe/vision_layers.hpp" +using std::string; + namespace caffe { template @@ -28,15 +32,14 @@ void DataLayer::SetUp(const vector*>& bottom, // Read a data point, and use it to initialize the top blob. Datum datum; datum.ParseFromString(iter_->value().ToString()); - const BlobProto& blob = datum.blob(); // image (*top)[0]->Reshape( - this->layer_param_.batchsize(), blob.channels(), blob.height(), - blob.width()); + this->layer_param_.batchsize(), datum.channels(), datum.height(), + datum.width()); // label (*top)[1]->Reshape(this->layer_param_.batchsize(), 1, 1, 1); // datum size - datum_size_ = blob.data_size(); + datum_size_ = datum.data().size(); } template @@ -48,9 +51,9 @@ void DataLayer::Forward_cpu(const vector*>& bottom, for (int i = 0; i < this->layer_param_.batchsize(); ++i) { // get a blob datum.ParseFromString(iter_->value().ToString()); - const BlobProto& blob = datum.blob(); + const string& data = datum.data(); for (int j = 0; j < datum_size_; ++j) { - top_data[i * datum_size_ + j] = blob.data(j); + top_data[i * datum_size_ + j] = (uint8_t)data[j]; } top_label[i] = datum.label(); // go to the next iter diff --git a/src/caffe/net.hpp b/src/caffe/net.hpp new file mode 100644 index 0000000..c8426ea --- /dev/null +++ b/src/caffe/net.hpp @@ -0,0 +1,53 @@ +// Copyright 2013 Yangqing Jia + +#ifndef CAFFE_LAYER_H_ +#define CAFFE_LAYER_H_ + +#include +#include +#include + +#include "caffe/blob.hpp" +#include "caffe/common.hpp" +#include "caffe/proto/caffe.pb.h" + +using std::map; +using std::vector; +using std::string; + +namespace caffe { + +template +class Net { + public: + explicit Net(const NetParameter& param); + ~Net(); + void Forward(const vector> & bottom, + vector* top); + Dtype Backward(const vector> & bottom, + vector* top); + + // For an already initialized net, CopyTrainedLayersFrom() copies the already + // trained layers from another net parameter instance. + void CopyTrainedLayersFrom(const NetParameter& param); + // Writes the net to a proto. + void ToProto(NetParameter* param, bool write_diff = false); + + protected: + // Individual layers in the net + vector > > layers_; + vector > > layer_names_; + // bottom_vecs stores the vectors containing the input for each layer + vector*> > bottom_vecs_; + // top_vecs stores the vectors containing the output for each layer + vector* > top_vecs_; + // blobs stores the blobs that store intermediate results between the + // layers. + vector > blobs_; + vector > blob_names_; +}; + + +} // namespace caffe + +#endif // CAFFE_LAYER_H_ diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index cb8c7c4..8f7e0c3 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -12,8 +12,12 @@ message BlobProto { } message Datum { - optional BlobProto blob = 1; - optional int32 label = 2; + optional int32 channels = 1; + optional int32 height = 2; + optional int32 width = 3; + // the actual image data, in bytes + optional bytes data = 4; + optional int32 label = 5; } message FillerParameter { diff --git a/src/caffe/test/test_data_layer.cpp b/src/caffe/test/test_data_layer.cpp index a080bc2..ce0d24e 100644 --- a/src/caffe/test/test_data_layer.cpp +++ b/src/caffe/test/test_data_layer.cpp @@ -1,9 +1,10 @@ // Copyright 2013 Yangqing Jia -#include #include #include +#include + #include "gtest/gtest.h" #include "caffe/blob.hpp" #include "caffe/common.hpp" @@ -12,6 +13,8 @@ #include "caffe/proto/caffe.pb.h" #include "caffe/test/test_caffe_main.hpp" +using std::string; + namespace caffe { extern cudaDeviceProp CAFFE_TEST_CUDA_PROP; @@ -38,13 +41,12 @@ class DataLayerTest : public ::testing::Test { for (int i = 0; i < 5; ++i) { Datum datum; datum.set_label(i); - BlobProto* blob = datum.mutable_blob(); - blob->set_num(1); - blob->set_channels(2); - blob->set_height(3); - blob->set_width(4); + datum.set_channels(2); + datum.set_height(3); + datum.set_width(4); + std::string* data = datum.mutable_data(); for (int j = 0; j < 24; ++j) { - blob->add_data(i); + data->push_back((uint8_t)i); } stringstream ss; ss << i; -- 2.7.4