forked from kevinlin311tw/Caffe-DeepBinaryCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_data_layer.cpp
More file actions
104 lines (93 loc) · 3.36 KB
/
base_data_layer.cpp
File metadata and controls
104 lines (93 loc) · 3.36 KB
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
#include <string>
#include <vector>
#include "caffe/data_layers.hpp"
#include "caffe/util/io.hpp"
namespace caffe {
template <typename Dtype>
BaseDataLayer<Dtype>::BaseDataLayer(const LayerParameter& param)
: Layer<Dtype>(param),
transform_param_(param.transform_param()),
data_transformer_(transform_param_) {
}
template <typename Dtype>
void BaseDataLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
vector<Blob<Dtype>*>* top) {
if (top->size() == 1) {
output_labels_ = false;
} else {
output_labels_ = true;
}
DataLayerSetUp(bottom, top);
// The subclasses should setup the datum channels, height and width
CHECK_GT(datum_channels_, 0);
CHECK_GT(datum_height_, 0);
CHECK_GT(datum_width_, 0);
if (transform_param_.crop_size() > 0) {
CHECK_GE(datum_height_, transform_param_.crop_size());
CHECK_GE(datum_width_, transform_param_.crop_size());
}
// check if we want to have mean
if (transform_param_.has_mean_file()) {
const string& mean_file = transform_param_.mean_file();
LOG(INFO) << "Loading mean file from" << mean_file;
BlobProto blob_proto;
ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
data_mean_.FromProto(blob_proto);
CHECK_GE(data_mean_.num(), 1);
CHECK_GE(data_mean_.channels(), datum_channels_);
CHECK_GE(data_mean_.height(), datum_height_);
CHECK_GE(data_mean_.width(), datum_width_);
} else {
// Simply initialize an all-empty mean.
data_mean_.Reshape(1, datum_channels_, datum_height_, datum_width_);
}
mean_ = data_mean_.cpu_data();
data_transformer_.InitRand();
}
template <typename Dtype>
void BasePrefetchingDataLayer<Dtype>::LayerSetUp(
const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) {
BaseDataLayer<Dtype>::LayerSetUp(bottom, top);
// Now, start the prefetch thread. Before calling prefetch, we make two
// cpu_data calls so that the prefetch thread does not accidentally make
// simultaneous cudaMalloc calls when the main thread is running. In some
// GPUs this seems to cause failures if we do not so.
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 BasePrefetchingDataLayer<Dtype>::CreatePrefetchThread() {
this->phase_ = Caffe::phase();
this->data_transformer_.InitRand();
CHECK(StartInternalThread()) << "Thread execution failed";
}
template <typename Dtype>
void BasePrefetchingDataLayer<Dtype>::JoinPrefetchThread() {
CHECK(WaitForInternalThreadToExit()) << "Thread joining failed";
}
template <typename Dtype>
void BasePrefetchingDataLayer<Dtype>::Forward_cpu(
const vector<Blob<Dtype>*>& bottom, vector<Blob<Dtype>*>* top) {
// First, join the thread
JoinPrefetchThread();
// Copy the data
caffe_copy(prefetch_data_.count(), prefetch_data_.cpu_data(),
(*top)[0]->mutable_cpu_data());
if (this->output_labels_) {
caffe_copy(prefetch_label_.count(), prefetch_label_.cpu_data(),
(*top)[1]->mutable_cpu_data());
}
// Start a new prefetch thread
CreatePrefetchThread();
}
#ifdef CPU_ONLY
STUB_GPU_FORWARD(BasePrefetchingDataLayer, Forward);
#endif
INSTANTIATE_CLASS(BaseDataLayer);
INSTANTIATE_CLASS(BasePrefetchingDataLayer);
} // namespace caffe