]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - jacinto-ai/caffe-jacinto.git/blob - src/caffe/layers/data_layer.cpp
a few updates
[jacinto-ai/caffe-jacinto.git] / src / caffe / layers / data_layer.cpp
1 // Copyright 2013 Yangqing Jia
3 #include <vector>
5 #include <leveldb/db.h>
7 #include "caffe/layer.hpp"
8 #include "caffe/vision_layers.hpp"
10 namespace caffe {
12 template <typename Dtype>
13 void DataLayer<Dtype>::SetUp(const vector<Blob<Dtype>*>& bottom,
14       vector<Blob<Dtype>*>* top) {
15   CHECK_EQ(bottom.size(), 0) << "Neuron Layer takes no input blobs.";
16   CHECK_EQ(top->size(), 2) << "Neuron Layer takes two blobs as output.";
17   // Initialize the leveldb
18   leveldb::DB* db_temp;
19   leveldb::Options options;
20   options.create_if_missing = false;
21   leveldb::Status status = leveldb::DB::Open(
22       options, this->layer_param_.source(), &db_temp);
23   CHECK(status.ok());
24   db_.reset(db_temp);
25   iter_.reset(db_->NewIterator(leveldb::ReadOptions()));
26   // Read a data point, and use it to initialize the top blob.
27   Datum datum;
28   datum.ParseFromString(iter_->value().ToString());
29   const BlobProto& blob = datum.blob();
30   // image
31   (*top)[0]->Reshape(
32       this->layer_param_.batchsize(), blob.channels(), blob.height(),
33       blob.width());
34   // label
35   (*top)[1]->Reshape(this->layer_param_.batchsize(), 1, 1, 1);
36   // datum size
37   datum_size_ = blob.data_size();
38 }
40 template <typename Dtype>
41 void DataLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
42       vector<Blob<Dtype>*>* top) {
43   Datum datum;
44   Dtype* top_data = (*top)[0]->mutable_cpu_data();
45   Dtype* top_label = (*top)[0]->mutable_cpu_diff();
46   for (int i = 0; i < this->layer_param_.batchsize(); ++i) {
47     // get a blob
48     datum.ParseFromString(iter_->value().ToString());
49     const BlobProto& blob = datum.blob();
50     for (int j = 0; j < datum_size_; ++j) {
51       top_data[i * datum_size_ + j] = blob.data(j);
52     }
53     top_label[i] = datum.label();
54     // go to the next iter
55     iter_->Next();
56     if (!iter_->Valid()) {
57       // We have reached the end. Restart from the first.
58       LOG(INFO) << "Restarting data read from start.";
59       iter_.reset(db_->NewIterator(leveldb::ReadOptions()));
60     }
61   }
62 }
64 template <typename Dtype>
65 void DataLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
66       vector<Blob<Dtype>*>* top) {
67   Forward_cpu(bottom, top);
68   // explicitly copy data to gpu - this is achieved by simply calling gpu_data
69   // functions.
70   // TODO(Yangqing): maybe we don't need this since data synchronization is
71   // simply done under the hood?
72   (*top)[0]->gpu_data();
73   (*top)[1]->gpu_data();
74 }
76 // The backward operations are dummy - they do not carry any computation.
77 template <typename Dtype>
78 Dtype DataLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
79       const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
80   return Dtype(0.);
81 }
83 template <typename Dtype>
84 Dtype DataLayer<Dtype>::Backward_gpu(const vector<Blob<Dtype>*>& top,
85       const bool propagate_down, vector<Blob<Dtype>*>* bottom) {
86   return Dtype(0.);
87 }
89 INSTANTIATE_CLASS(DataLayer);
91 }  // namespace caffe