MCPcopy Create free account
hub / github.com/BVLC/caffe / load_batch

Method load_batch

src/caffe/layers/image_data_layer.cpp:110–173  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

108// This function is called on prefetch thread
109template <typename Dtype>
110void ImageDataLayer<Dtype>::load_batch(Batch<Dtype>* batch) {
111 CPUTimer batch_timer;
112 batch_timer.Start();
113 double read_time = 0;
114 double trans_time = 0;
115 CPUTimer timer;
116 CHECK(batch->data_.count());
117 CHECK(this->transformed_data_.count());
118 ImageDataParameter image_data_param = this->layer_param_.image_data_param();
119 const int batch_size = image_data_param.batch_size();
120 const int new_height = image_data_param.new_height();
121 const int new_width = image_data_param.new_width();
122 const bool is_color = image_data_param.is_color();
123 string root_folder = image_data_param.root_folder();
124
125 // Reshape according to the first image of each batch
126 // on single input batches allows for inputs of varying dimension.
127 cv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first,
128 new_height, new_width, is_color);
129 CHECK(cv_img.data) << "Could not load " << lines_[lines_id_].first;
130 // Use data_transformer to infer the expected blob shape from a cv_img.
131 vector<int> top_shape = this->data_transformer_->InferBlobShape(cv_img);
132 this->transformed_data_.Reshape(top_shape);
133 // Reshape batch according to the batch_size.
134 top_shape[0] = batch_size;
135 batch->data_.Reshape(top_shape);
136
137 Dtype* prefetch_data = batch->data_.mutable_cpu_data();
138 Dtype* prefetch_label = batch->label_.mutable_cpu_data();
139
140 // datum scales
141 const int lines_size = lines_.size();
142 for (int item_id = 0; item_id < batch_size; ++item_id) {
143 // get a blob
144 timer.Start();
145 CHECK_GT(lines_size, lines_id_);
146 cv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first,
147 new_height, new_width, is_color);
148 CHECK(cv_img.data) << "Could not load " << lines_[lines_id_].first;
149 read_time += timer.MicroSeconds();
150 timer.Start();
151 // Apply transformations (mirror, crop...) to the image
152 int offset = batch->data_.offset(item_id);
153 this->transformed_data_.set_cpu_data(prefetch_data + offset);
154 this->data_transformer_->Transform(cv_img, &(this->transformed_data_));
155 trans_time += timer.MicroSeconds();
156
157 prefetch_label[item_id] = lines_[lines_id_].second;
158 // go to the next iter
159 lines_id_++;
160 if (lines_id_ >= lines_size) {
161 // We have reached the end. Restart from the first.
162 DLOG(INFO) << "Restarting data prefetching from start.";
163 lines_id_ = 0;
164 if (this->layer_param_.image_data_param().shuffle()) {
165 ShuffleImages();
166 }
167 }

Callers

nothing calls this directly

Calls 14

ReadImageToCVMatFunction · 0.85
StartMethod · 0.80
countMethod · 0.80
batch_sizeMethod · 0.80
InferBlobShapeMethod · 0.80
MicroSecondsMethod · 0.80
offsetMethod · 0.80
TransformMethod · 0.80
StopMethod · 0.80
MilliSecondsMethod · 0.80
ReshapeMethod · 0.45
mutable_cpu_dataMethod · 0.45

Tested by

no test coverage detected