-
Notifications
You must be signed in to change notification settings - Fork 49
/
compact_data_layer.cpp
304 lines (282 loc) · 10.7 KB
/
compact_data_layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <leveldb/db.h>
#include <stdint.h>
#include <string>
#include <vector>
#include <opencv2/core/core_c.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include "caffe/common.hpp"
#include "caffe/data_layers.hpp"
#include "caffe/layer.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"
#include "caffe/util/math_functions.hpp"
#include "caffe/util/rng.hpp"
using namespace cv;
namespace caffe {
template <typename Dtype>
CompactDataLayer<Dtype>::~CompactDataLayer<Dtype>() {
this->JoinPrefetchThread();
// clean up the database resources
switch (this->layer_param_.data_param().backend()) {
case DataParameter_DB_LEVELDB:
break; // do nothing
case DataParameter_DB_LMDB:
mdb_cursor_close(mdb_cursor_);
//mdb_close(mdb_env_, mdb_dbi_);
mdb_dbi_close(mdb_env_, mdb_dbi_);
mdb_txn_abort(mdb_txn_);
mdb_env_close(mdb_env_);
break;
default:
LOG(FATAL) << "Unknown database backend";
}
}
template <typename Dtype>
void CompactDataLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
if (top->size() == 1) {
this->output_labels_ = false;
} else {
this->output_labels_ = true;
}
DataLayerSetUp(bottom, top);
// The subclasses should setup the datum channels, height and width
CHECK_GT(this->datum_channels_, 0);
CHECK_GT(this->datum_height_, 0);
CHECK_GT(this->datum_width_, 0);
CHECK(this->transform_param_.crop_size() > 0);
CHECK_GE(this->datum_height_, this->transform_param_.crop_size());
CHECK_GE(this->datum_width_, this->transform_param_.crop_size());
int crop_size = this->transform_param_.crop_size();
// check if we want to have mean
if (transform_param_.has_mean_file()) {
//CHECK(this->transform_param_.has_mean_file());
this->data_mean_.Reshape(1, this->datum_channels_, crop_size, crop_size);
const string& mean_file = this->transform_param_.mean_file();
LOG(INFO) << "Loading mean file from" << mean_file;
BlobProto blob_proto;
ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
this->data_mean_.FromProto(blob_proto);
Blob<Dtype> tmp;
tmp.FromProto(blob_proto);
const Dtype* src_data = tmp.cpu_data();
Dtype* dst_data = this->data_mean_.mutable_cpu_data();
CHECK_EQ(tmp.num(), 1);
CHECK_EQ(tmp.channels(), this->datum_channels_);
CHECK_GE(tmp.height(), crop_size);
CHECK_GE(tmp.width(), crop_size);
int w_off = (tmp.width() - crop_size) / 2;
int h_off = (tmp.height() - crop_size) / 2;
for (int c = 0; c < this->datum_channels_; c++) {
for (int h = 0; h < crop_size; h++) {
for (int w = 0; w < crop_size; w++) {
int src_idx = (c * tmp.height() + h + h_off) * tmp.width() + w + w_off;
int dst_idx = (c * crop_size + h) * crop_size + w;
dst_data[dst_idx] = src_data[src_idx];
}
}
}
} else {
// Simply initialize an all-empty mean.
this->data_mean_.Reshape(1, this->datum_channels_, crop_size, crop_size);
}
this->mean_ = this->data_mean_.cpu_data();
this->data_transformer_.InitRand();
this->prefetch_data_.mutable_cpu_data();
if (this->output_labels_) {
this->prefetch_label_.mutable_cpu_data();
}
DLOG(INFO) << "Initializing prefetch";
this->CreatePrefetchThread();
DLOG(INFO) << "Prefetch initialized.";
}
template <typename Dtype>
void CompactDataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
// Initialize DB
switch (this->layer_param_.data_param().backend()) {
case DataParameter_DB_LEVELDB:
{
leveldb::DB* db_temp;
leveldb::Options options = GetLevelDBOptions();
options.create_if_missing = false;
LOG(INFO) << "Opening leveldb " << this->layer_param_.data_param().source();
leveldb::Status status = leveldb::DB::Open(
options, this->layer_param_.data_param().source(), &db_temp);
CHECK(status.ok()) << "Failed to open leveldb "
<< this->layer_param_.data_param().source() << std::endl
<< status.ToString();
db_.reset(db_temp);
iter_.reset(db_->NewIterator(leveldb::ReadOptions()));
iter_->SeekToFirst();
}
break;
case DataParameter_DB_LMDB:
CHECK_EQ(mdb_env_create(&mdb_env_), MDB_SUCCESS) << "mdb_env_create failed";
CHECK_EQ(mdb_env_set_mapsize(mdb_env_, 1099511627776), MDB_SUCCESS); // 1TB
CHECK_EQ(mdb_env_open(mdb_env_,
this->layer_param_.data_param().source().c_str(),
MDB_RDONLY|MDB_NOTLS, 0664), MDB_SUCCESS) << "mdb_env_open failed";
CHECK_EQ(mdb_txn_begin(mdb_env_, NULL, MDB_RDONLY, &mdb_txn_), MDB_SUCCESS)
<< "mdb_txn_begin failed";
CHECK_EQ(mdb_dbi_open(mdb_txn_, NULL, 0, &mdb_dbi_), MDB_SUCCESS)
<< "mdb_open failed";
CHECK_EQ(mdb_cursor_open(mdb_txn_, mdb_dbi_, &mdb_cursor_), MDB_SUCCESS)
<< "mdb_cursor_open failed";
LOG(INFO) << "Opening lmdb " << this->layer_param_.data_param().source();
CHECK_EQ(mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_, MDB_FIRST),
MDB_SUCCESS) << "mdb_cursor_get failed";
break;
default:
LOG(FATAL) << "Unknown database backend";
}
// Check if we would need to randomly skip a few data points
if (this->layer_param_.data_param().rand_skip()) {
unsigned int skip = caffe_rng_rand() %
this->layer_param_.data_param().rand_skip();
LOG(INFO) << "Skipping first " << skip << " data points.";
while (skip-- > 0) {
switch (this->layer_param_.data_param().backend()) {
case DataParameter_DB_LEVELDB:
iter_->Next();
if (!iter_->Valid()) {
iter_->SeekToFirst();
}
break;
case DataParameter_DB_LMDB:
if (mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_, MDB_NEXT)
!= MDB_SUCCESS) {
CHECK_EQ(mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_,
MDB_FIRST), MDB_SUCCESS);
}
break;
default:
LOG(FATAL) << "Unknown database backend";
}
}
}
// Read a data point, and use it to initialize the top blob.
Datum datum;
string value;
CvMat mat;
IplImage *img = NULL;
switch (this->layer_param_.data_param().backend()) {
case DataParameter_DB_LEVELDB:
value = this->iter_->value().ToString();
mat = cvMat(1, 1000 * 1000 * 3, CV_8UC1, const_cast<char *>(value.data()) + sizeof(int));
//datum.ParseFromString(iter_->value().ToString());
break;
case DataParameter_DB_LMDB:
mat = cvMat(1, 1000 * 1000 * 3, CV_8UC1, (char *)(mdb_value_.mv_data) + sizeof(int));
//datum.ParseFromArray(mdb_value_.mv_data, mdb_value_.mv_size);
break;
default:
LOG(FATAL) << "Unknown database backend";
}
img = cvDecodeImage(&mat, 0);
// datum size
this->datum_channels_ = img->nChannels;
cvReleaseImage(&img);
// image
int crop_size = this->layer_param_.transform_param().crop_size();
CHECK_GT(crop_size, 0) << "crop size must be greater than 0";
(*top)[0]->Reshape(this->layer_param_.data_param().batch_size(),
this->datum_channels_ , crop_size, crop_size);
this->prefetch_data_.Reshape(this->layer_param_.data_param().batch_size(),
this->datum_channels_ , crop_size, crop_size);
LOG(INFO) << "output data size: " << (*top)[0]->num() << ","
<< (*top)[0]->channels() << "," << (*top)[0]->height() << ","
<< (*top)[0]->width();
// label
if (this->output_labels_) {
(*top)[1]->Reshape(this->layer_param_.data_param().batch_size(), 1, 1, 1);
this->prefetch_label_.Reshape(this->layer_param_.data_param().batch_size(),
1, 1, 1);
}
this->datum_height_ = crop_size;
this->datum_width_ = crop_size;
this->datum_size_ = this->datum_channels_ * this->datum_height_ * this->datum_width_;
}
// This function is used to create a thread that prefetches the data.
template <typename Dtype>
void CompactDataLayer<Dtype>::InternalThreadEntry() {
Datum datum;
string value;
CvMat mat;
IplImage *img = NULL;
CHECK(this->prefetch_data_.count());
Dtype* top_data = this->prefetch_data_.mutable_cpu_data();
Dtype* top_label = NULL; // suppress warnings about uninitialized variables
if (this->output_labels_) {
top_label = this->prefetch_label_.mutable_cpu_data();
}
const int batch_size = this->layer_param_.data_param().batch_size();
for (int item_id = 0; item_id < batch_size; ++item_id) {
// get a blob
switch (this->layer_param_.data_param().backend()) {
case DataParameter_DB_LEVELDB:
CHECK(iter_);
CHECK(iter_->Valid());
value = iter_->value().ToString();
mat = cvMat(1, 1000 * 1000, CV_8UC1, const_cast<char *>(value.data()) + sizeof(int));
// datum.ParseFromString(iter_->value().ToString());
break;
case DataParameter_DB_LMDB:
//LOG(FATAL) << "LMDB is not supported at present";
CHECK_EQ(mdb_cursor_get(mdb_cursor_, &mdb_key_,
&mdb_value_, MDB_GET_CURRENT), MDB_SUCCESS);
mat = cvMat(1, 1000 * 1000 * 3, CV_8UC1, (char *)(mdb_value_.mv_data) + sizeof(int));
// datum.ParseFromArray(mdb_value_.mv_data,
// mdb_value_.mv_size);
break;
default:
LOG(FATAL) << "Unknown database backend";
}
img = cvDecodeImage(&mat, 0);
// Apply data transformations (mirror, scale, crop...)
this->data_transformer_.Transform(item_id, img, this->mean_, top_data);
cvReleaseImage(&img); // release current image
if (this->output_labels_) {
//top_label[item_id] = datum.label();
switch(this->layer_param_.data_param().backend()) {
case DataParameter_DB_LEVELDB:
top_label[item_id] = *((int *)const_cast<char *>(value.data()));
break;
case DataParameter_DB_LMDB:
top_label[item_id] = *((int *)mdb_value_.mv_data);
break;
default:
LOG(FATAL) << "Unkown database backend";
}
// LOG(INFO) << "label: " << top_label[item_id];
}
// go to the next iter
switch (this->layer_param_.data_param().backend()) {
case DataParameter_DB_LEVELDB:
iter_->Next();
if (!iter_->Valid()) {
// We have reached the end. Restart from the first.
DLOG(INFO) << "Restarting data prefetching from start.";
iter_->SeekToFirst();
}
break;
case DataParameter_DB_LMDB:
if (mdb_cursor_get(mdb_cursor_, &mdb_key_,
&mdb_value_, MDB_NEXT) != MDB_SUCCESS) {
// We have reached the end. Restart from the first.
DLOG(INFO) << "Restarting data prefetching from start.";
CHECK_EQ(mdb_cursor_get(mdb_cursor_, &mdb_key_,
&mdb_value_, MDB_FIRST), MDB_SUCCESS);
}
break;
default:
LOG(FATAL) << "Unknown database backend";
}
}
}
INSTANTIATE_CLASS(CompactDataLayer);
} // namespace caffe