void load_batch(int batch_size, int crop_w, int crop_h, Image<float> &data, Image<int> &labels, db::Cursor* cur) { assert(data.extent(0) == crop_w); assert(data.extent(1) == crop_h); assert(data.extent(3) == batch_size); assert(labels.extent(0) == batch_size); for (int n = 0; n < batch_size; n++) { Datum datum; datum.ParseFromString(cur->value()); cv::Mat img; img = DatumToCVMat(datum); assert(crop_w <= img.rows); assert(crop_h <= img.cols); labels(n) = datum.label(); for (int h = 0; h < crop_h; h++) for (int w = 0; w < crop_w; w++) for (int c = 0; c < 3; c++) data(w, h, c, n) = (float)img.at<cv::Vec3b>(h, w)[c]; cur->Next(); } }
void ImageDataSLLayer<Dtype>::InternalThreadEntry() { Datum datum; CHECK(this->prefetch_data_.count()); Dtype* top_data = this->prefetch_data_.mutable_cpu_data(); Dtype* top_label = this->prefetch_label_.mutable_cpu_data(); ImageDataParameter image_data_param = this->layer_param_.image_data_param(); const int batch_size = image_data_param.batch_size(); const int new_height = image_data_param.new_height(); const int new_width = image_data_param.new_width(); // datum scales const int lines_size = lines_.size(); for (int item_id = 0; item_id < batch_size; ++item_id) { // get a blob CHECK_GT(lines_size, lines_id_); if (!ReadImageToDatumSL(lines_[lines_id_].first, lines_[lines_id_].second, new_height, new_width, &datum)) { continue; } // Apply transformations (mirror, crop...) to the data this->data_transformer_.Transform(item_id, datum, this->mean_, top_data); for(int i = 0; i < 128 * 128; i++){ //LOG(INFO) << datum.label(i); top_label[item_id * 128 * 128 + i] = datum.label(i); } #if 0 if (count_4 % 100 == 0) { const int img_size = 256; cv::Mat img__(img_size, img_size, CV_8UC1); //cv::resize(cv_img_2, img__, cv::Size(64, 64)); int i__ = 0; for (int h = 0; h < img_size; h++) { for (int w = 0; w < img_size; w++) { img__.at<unsigned char>(h, w) = top_data[256 * 256 * 3 + i__]; i__++; } } cv::imshow("seg", img__); cv::waitKey(0); } count_4 = (count_4 + 1) % 10000; #endif // go to the next iter lines_id_++; if (lines_id_ >= lines_size) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; lines_id_ = 0; if (this->layer_param_.image_data_param().shuffle()) { ShuffleImages(); } } } }
TEST_F(IOTest, TestReadFileToDatum) { string filename = EXAMPLES_SOURCE_DIR "images/cat.jpg"; Datum datum; EXPECT_TRUE(ReadFileToDatum(filename, &datum)); EXPECT_TRUE(datum.encoded()); EXPECT_EQ(datum.label(), -1); EXPECT_EQ(datum.data().size(), 140391); }
void DataLayer<Dtype>::InternalThreadEntry() { CPUTimer batch_timer; batch_timer.Start(); double read_time = 0; double trans_time = 0; CPUTimer timer; CHECK(this->prefetch_data_.count()); CHECK(this->transformed_data_.count()); // Reshape according to the first datum of each batch // on single input batches allows for inputs of varying dimension. const int batch_size = this->layer_param_.data_param().batch_size(); Datum datum; datum.ParseFromString(cursor_->value()); // Use data_transformer to infer the expected blob shape from datum. vector<int> top_shape = this->data_transformer_->InferBlobShape(datum); this->transformed_data_.Reshape(top_shape); // Reshape prefetch_data according to the batch_size. top_shape[0] = batch_size; this->prefetch_data_.Reshape(top_shape); 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(); } timer.Start(); for (int item_id = 0; item_id < batch_size; ++item_id) { // get a datum Datum datum; datum.ParseFromString(cursor_->value()); read_time += timer.MicroSeconds(); timer.Start(); // Apply data transformations (mirror, scale, crop...) int offset = this->prefetch_data_.offset(item_id); this->transformed_data_.set_cpu_data(top_data + offset); this->data_transformer_->Transform(datum, &(this->transformed_data_)); // Copy label. if (this->output_labels_) { top_label[item_id] = datum.label(); } trans_time += timer.MicroSeconds(); timer.Start(); // go to the next item. cursor_->Next(); if (!cursor_->valid()) { DLOG(INFO) << "Restarting data prefetching from start."; cursor_->SeekToFirst(); } } timer.Stop(); batch_timer.Stop(); DLOG(INFO) << "Prefetch batch: " << batch_timer.MilliSeconds() << " ms."; DLOG(INFO) << " Read time: " << read_time / 1000 << " ms."; DLOG(INFO) << "Transform time: " << trans_time / 1000 << " ms."; }
void GetLabelsKeysMap(map<int, vector<MDB_val> >* labels_keys, vector<int>* labels) { LMDBCursor::SeekToFirst(); labels_keys->clear(); while (valid()) { Datum datum; datum.ParseFromString(LMDBCursor::value()); int label = datum.label(); (*labels_keys)[label].push_back(mdb_key_); LMDBCursor::Next(); } for(map<int,vector<MDB_val> >::iterator it = labels_keys->begin(); it != labels_keys->end(); ++it) { labels->push_back(it->first); } }
bool MostCV::LevelDBReader::GetNextEntry(string &key, vector<double> &retVec, int &label) { if (!database_iter_->Valid()) return false; Datum datum; datum.clear_float_data(); datum.clear_data(); datum.ParseFromString(database_iter_->value().ToString()); key = database_iter_->key().ToString(); label = datum.label(); int expected_data_size = std::max<int>(datum.data().size(), datum.float_data_size()); const int datum_volume_size = datum.channels() * datum.height() * datum.width(); if (expected_data_size != datum_volume_size) { cout << "Something wrong in saved data."; assert(false); } retVec.resize(datum_volume_size); const string& data = datum.data(); if (data.size() != 0) { // Data stored in string, e.g. just pixel values of 196608 = 256 * 256 * 3 for (int i = 0; i < datum_volume_size; ++i) retVec[i] = data[i]; } else { // Data stored in real feature vector such as 4096 from feature extraction for (int i = 0; i < datum_volume_size; ++i) retVec[i] = datum.float_data(i); } database_iter_->Next(); ++record_idx_; return true; }
void DataLayer<Dtype>::InternalThreadEntry() { CPUTimer batch_timer; batch_timer.Start(); double read_time = 0; double trans_time = 0; CPUTimer timer; CHECK(this->prefetch_data_.count()); CHECK(this->transformed_data_.count()); // Reshape on single input batches for inputs of varying dimension. const int batch_size = this->layer_param_.data_param().batch_size(); const int crop_size = this->layer_param_.transform_param().crop_size(); if (batch_size == 1 && crop_size == 0) { Datum datum; datum.ParseFromString(cursor_->value()); this->prefetch_data_.Reshape(1, datum.channels(), datum.height(), datum.width()); this->transformed_data_.Reshape(1, datum.channels(), datum.height(), datum.width()); } 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(); } bool force_color = this->layer_param_.data_param().force_encoded_color(); for (int item_id = 0; item_id < batch_size; ++item_id) { timer.Start(); // get a blob Datum datum; datum.ParseFromString(cursor_->value()); cv::Mat cv_img; if (datum.encoded()) { if (force_color) { cv_img = DecodeDatumToCVMat(datum, true); } else { cv_img = DecodeDatumToCVMatNative(datum); } if (cv_img.channels() != this->transformed_data_.channels()) { LOG(WARNING) << "Your dataset contains encoded images with mixed " << "channel sizes. Consider adding a 'force_color' flag to the " << "model definition, or rebuild your dataset using " << "convert_imageset."; } } read_time += timer.MicroSeconds(); timer.Start(); // Apply data transformations (mirror, scale, crop...) int offset = this->prefetch_data_.offset(item_id); this->transformed_data_.set_cpu_data(top_data + offset); if (datum.encoded()) { this->data_transformer_->Transform(cv_img, &(this->transformed_data_)); } else { this->data_transformer_->Transform(datum, &(this->transformed_data_)); } if (this->output_labels_) { for (int label_i = 0; label_i < datum.label_size(); ++label_i){ top_label[item_id * datum.label_size() + label_i] = datum.label(label_i); } //top_label[item_id] = datum.label(); } trans_time += timer.MicroSeconds(); // go to the next iter cursor_->Next(); if (!cursor_->valid()) { DLOG(INFO) << "Restarting data prefetching from start."; cursor_->SeekToFirst(); } } batch_timer.Stop(); DLOG(INFO) << "Prefetch batch: " << batch_timer.MilliSeconds() << " ms."; DLOG(INFO) << " Read time: " << read_time / 1000 << " ms."; DLOG(INFO) << "Transform time: " << trans_time / 1000 << " ms."; }
void MyImageDataLayer<Dtype>::fetchData() { Datum datum; CHECK(prefetch_data_.count()); Dtype* top_data = prefetch_data_.mutable_cpu_data(); Dtype* top_label = prefetch_label_.mutable_cpu_data(); ImageDataParameter image_data_param = this->layer_param_.image_data_param(); const Dtype scale = image_data_param.scale();//image_data_layer相关参数 const int batch_size = 1;//image_data_param.batch_size(); 这里我们只需要一张图片 const int crop_size = image_data_param.crop_size(); const bool mirror = image_data_param.mirror(); const int new_height = image_data_param.new_height(); const int new_width = image_data_param.new_width(); if (mirror && crop_size == 0) { LOG(FATAL) << "Current implementation requires mirror and crop_size to be " << "set at the same time."; } // datum scales const int channels = datum_channels_; const int height = datum_height_; const int width = datum_width_; const int size = datum_size_; const int lines_size = lines_.size(); const Dtype* mean = data_mean_.cpu_data(); for (int item_id = 0; item_id < batch_size; ++item_id) {//读取一图片 // get a blob CHECK_GT(lines_size, lines_id_); if (!ReadImageToDatum(lines_[lines_id_].first, lines_[lines_id_].second, new_height, new_width, &datum)) { continue; } const string& data = datum.data(); if (crop_size) { CHECK(data.size()) << "Image cropping only support uint8 data"; int h_off, w_off; // We only do random crop when we do training. h_off = (height - crop_size) / 2; w_off = (width - crop_size) / 2; // Normal copy 正常读取,把裁剪后的图片数据读给top_data for (int c = 0; c < channels; ++c) { for (int h = 0; h < crop_size; ++h) { for (int w = 0; w < crop_size; ++w) { int top_index = ((item_id * channels + c) * crop_size + h) * crop_size + w; int data_index = (c * height + h + h_off) * width + w + w_off; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); top_data[top_index] = (datum_element - mean[data_index]) * scale; } } } } else { // Just copy the whole data 正常读取,把图片数据读给top_data if (data.size()) { for (int j = 0; j < size; ++j) { Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[j])); top_data[item_id * size + j] = (datum_element - mean[j]) * scale; } } else { for (int j = 0; j < size; ++j) { top_data[item_id * size + j] = (datum.float_data(j) - mean[j]) * scale; } } } top_label[item_id] = datum.label();//读取该图片的标签 } }
void DataLayer<Dtype>::InternalThreadEntry() { Datum datum; 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()); datum.ParseFromString(iter_->value().ToString()); break; case DataParameter_DB_LMDB: CHECK_EQ(mdb_cursor_get(mdb_cursor_, &mdb_key_, &mdb_value_, MDB_GET_CURRENT), MDB_SUCCESS); datum.ParseFromArray(mdb_value_.mv_data, mdb_value_.mv_size); break; default: LOG(FATAL) << "Unknown database backend"; } // Apply data transformations (mirror, scale, crop...) this->data_transformer_.Transform(item_id, datum, this->mean_, top_data); if (this->output_labels_) { // liu // top_label[item_id] = datum.label(); // LOG(ERROR) << "label size " << datum.label_size() << " " << datum.label(0) \ << " " << datum.label(1) << " " << datum.label(2) << " " << datum.label(3); for(int label_i=0; label_i < datum.label_size(); label_i++){ top_label[item_id * datum.label_size() + label_i] = datum.label(label_i); } } // 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"; } } }
void* ImageDataLayerPrefetch(void* layer_pointer) { CHECK(layer_pointer); ImageDataLayer<Dtype>* layer = reinterpret_cast<ImageDataLayer<Dtype>*>(layer_pointer); CHECK(layer); Datum datum; CHECK(layer->prefetch_data_); Dtype* top_data = layer->prefetch_data_->mutable_cpu_data(); Dtype* top_label = layer->prefetch_label_->mutable_cpu_data(); ImageDataParameter image_data_param = layer->layer_param_.image_data_param(); const Dtype scale = image_data_param.scale(); const int batch_size = image_data_param.batch_size(); const int crop_size = image_data_param.crop_size(); const bool mirror = image_data_param.mirror(); const int new_height = image_data_param.new_height(); const int new_width = image_data_param.new_width(); if (mirror && crop_size == 0) { LOG(FATAL) << "Current implementation requires mirror and crop_size to be " << "set at the same time."; } // datum scales const int channels = layer->datum_channels_; const int height = layer->datum_height_; const int width = layer->datum_width_; const int size = layer->datum_size_; const int lines_size = layer->shuffle_index_.size(); const Dtype* mean = layer->data_mean_.cpu_data(); for (int item_id = 0; item_id < batch_size; ++item_id) { // get a blob CHECK_GT(lines_size, layer->lines_id_); int id = layer->shuffle_index_[layer->lines_id_]; if (!ReadImageToDatum(layer->fn_list_[id], layer->label_list_[id], new_height, new_width, &datum)) { continue; } const string& data = datum.data(); if (crop_size) { CHECK(data.size()) << "Image cropping only support uint8 data"; int h_off, w_off; // We only do random crop when we do training. if (layer->phase_ == Caffe::TRAIN) { h_off = layer->PrefetchRand() % (height - crop_size); w_off = layer->PrefetchRand() % (width - crop_size); } else { h_off = (height - crop_size) / 2; w_off = (width - crop_size) / 2; } if (mirror && layer->PrefetchRand() % 2) { // Copy mirrored version for (int c = 0; c < channels; ++c) { for (int h = 0; h < crop_size; ++h) { for (int w = 0; w < crop_size; ++w) { int top_index = ((item_id * channels + c) * crop_size + h) * crop_size + (crop_size - 1 - w); int data_index = (c * height + h + h_off) * width + w + w_off; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); top_data[top_index] = (datum_element - mean[data_index]) * scale; } } } } else { // Normal copy for (int c = 0; c < channels; ++c) { for (int h = 0; h < crop_size; ++h) { for (int w = 0; w < crop_size; ++w) { int top_index = ((item_id * channels + c) * crop_size + h) * crop_size + w; int data_index = (c * height + h + h_off) * width + w + w_off; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); top_data[top_index] = (datum_element - mean[data_index]) * scale; } } } } } else { // Just copy the whole data if (data.size()) { for (int j = 0; j < size; ++j) { Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[j])); top_data[item_id * size + j] = (datum_element - mean[j]) * scale; } } else { for (int j = 0; j < size; ++j) { top_data[item_id * size + j] = (datum.float_data(j) - mean[j]) * scale; } } } top_label[item_id] = datum.label(); // go to the next iter layer->lines_id_++; if (layer->lines_id_ >= lines_size) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; layer->lines_id_ = 0; if (layer->layer_param_.image_data_param().shuffle()) { layer->ShuffleImages(); } } } return reinterpret_cast<void*>(NULL); }
void* DataLayerPrefetch(void* layer_pointer) { CHECK(layer_pointer); DataLayer<Dtype>* layer = reinterpret_cast<DataLayer<Dtype>*>(layer_pointer); CHECK(layer); Datum datum; CHECK(layer->prefetch_data_); Dtype* top_data = layer->prefetch_data_->mutable_cpu_data(); Dtype* top_label = layer->prefetch_label_->mutable_cpu_data(); const Dtype scale = layer->layer_param_.scale(); const int batchsize = layer->layer_param_.batchsize(); const int cropsize = layer->layer_param_.cropsize(); const bool mirror = layer->layer_param_.mirror(); if (mirror && cropsize == 0) { LOG(FATAL) << "Current implementation requires mirror and cropsize to be " << "set at the same time."; } // datum scales const int channels = layer->datum_channels_; const int height = layer->datum_height_; const int width = layer->datum_width_; const int size = layer->datum_size_; const Dtype* mean = layer->data_mean_.cpu_data(); for (int itemid = 0; itemid < batchsize; ++itemid) { // get a blob CHECK(layer->iter_); CHECK(layer->iter_->Valid()); datum.ParseFromString(layer->iter_->value().ToString()); const string& data = datum.data(); if (cropsize) { //CHECK(data.size()) << "Image cropping only support uint8 data"; int h_off, w_off; // We only do random crop when we do training. if (Caffe::phase() == Caffe::TRAIN) { // NOLINT_NEXT_LINE(runtime/threadsafe_fn) h_off = rand() % (height - cropsize); // NOLINT_NEXT_LINE(runtime/threadsafe_fn) w_off = rand() % (width - cropsize); } else { h_off = (height - cropsize) / 2; w_off = (width - cropsize) / 2; } // NOLINT_NEXT_LINE(runtime/threadsafe_fn) if (mirror && rand() % 2) { // Copy mirrored version for (int c = 0; c < channels; ++c) { for (int h = 0; h < cropsize; ++h) { for (int w = 0; w < cropsize; ++w) { top_data[((itemid * channels + c) * cropsize + h) * cropsize + cropsize - 1 - w] = (static_cast<Dtype>( (float)datum.float_data((c * height + h + h_off) * width + w + w_off)) - mean[(c * height + h + h_off) * width + w + w_off]) * scale; } } } } else { // Normal copy for (int c = 0; c < channels; ++c) { for (int h = 0; h < cropsize; ++h) { for (int w = 0; w < cropsize; ++w) { top_data[((itemid * channels + c) * cropsize + h) * cropsize + w] = (static_cast<Dtype>( (float)datum.float_data((c * height + h + h_off) * width + w + w_off)) - mean[(c * height + h + h_off) * width + w + w_off]) * scale; } } } } } else { // we will prefer to use data() first, and then try float_data() if (data.size()) { //cout << "unint8 data!!!!" << endl; for (int j = 0; j < size; ++j) { //cout << "datum.int_data " << j << "of size: " << size << static_cast<Dtype>((uint8_t)data[j]) << " mean: " << mean[j] << endl; top_data[itemid * size + j] = (static_cast<Dtype>((uint8_t)data[j]) - mean[j]) * scale; } } else { //cout << "float data !!!!!!!!!!!" << endl; for (int j = 0; j < size; ++j) { //cout << "item: " << itemid <<" datum.float_data " << j << "of size: " << size << endl; //cout << datum.float_data(j) << " mean: " << mean[j] << endl; top_data[itemid * size + j] = (datum.float_data(j) - mean[j]) * scale; } } } top_label[itemid] = datum.label(); // go to the next iter layer->iter_->Next(); if (!layer->iter_->Valid()) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; layer->iter_->SeekToFirst(); } } return reinterpret_cast<void*>(NULL); }
void* DataLayerPrefetch(void* layer_pointer) { CHECK(layer_pointer); DataLayer<Dtype>* layer = static_cast<DataLayer<Dtype>*>(layer_pointer); CHECK(layer); Datum datum; CHECK(layer->prefetch_data_); Dtype* top_data = layer->prefetch_data_->mutable_cpu_data(); Dtype* top_label; if (layer->output_labels_) { top_label = layer->prefetch_label_->mutable_cpu_data(); } const Dtype scale = layer->layer_param_.data_param().scale(); const int batch_size = layer->layer_param_.data_param().batch_size(); const int crop_size = layer->layer_param_.data_param().crop_size(); const bool mirror = layer->layer_param_.data_param().mirror(); if (mirror && crop_size == 0) { LOG(FATAL) << "Current implementation requires mirror and crop_size to be " << "set at the same time."; } // datum scales const int channels = layer->datum_channels_; const int height = layer->datum_height_; const int width = layer->datum_width_; const int size = layer->datum_size_; const Dtype* mean = layer->data_mean_.cpu_data(); for (int item_id = 0; item_id < batch_size; ++item_id) { // get a blob CHECK(layer->iter_); CHECK(layer->iter_->Valid()); datum.ParseFromString(layer->iter_->value().ToString()); const string& data = datum.data(); if (crop_size) { CHECK(data.size()) << "Image cropping only support uint8 data"; int h_off, w_off; // We only do random crop when we do training. if (layer->phase_ == Caffe::TRAIN) { h_off = layer->PrefetchRand() % (height - crop_size); w_off = layer->PrefetchRand() % (width - crop_size); } else { h_off = (height - crop_size) / 2; w_off = (width - crop_size) / 2; } if (mirror && layer->PrefetchRand() % 2) { // Copy mirrored version for (int c = 0; c < channels; ++c) { for (int h = 0; h < crop_size; ++h) { for (int w = 0; w < crop_size; ++w) { int top_index = ((item_id * channels + c) * crop_size + h) * crop_size + (crop_size - 1 - w); int data_index = (c * height + h + h_off) * width + w + w_off; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); top_data[top_index] = (datum_element - mean[data_index]) * scale; } } } } else { // Normal copy for (int c = 0; c < channels; ++c) { for (int h = 0; h < crop_size; ++h) { for (int w = 0; w < crop_size; ++w) { int top_index = ((item_id * channels + c) * crop_size + h) * crop_size + w; int data_index = (c * height + h + h_off) * width + w + w_off; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); top_data[top_index] = (datum_element - mean[data_index]) * scale; } } } } } else { // we will prefer to use data() first, and then try float_data() if (data.size()) { for (int j = 0; j < size; ++j) { Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[j])); top_data[item_id * size + j] = (datum_element - mean[j]) * scale; } } else { for (int j = 0; j < size; ++j) { top_data[item_id * size + j] = (datum.float_data(j) - mean[j]) * scale; } } } if (layer->output_labels_) { top_label[item_id] = datum.label(); } // go to the next iter layer->iter_->Next(); if (!layer->iter_->Valid()) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; layer->iter_->SeekToFirst(); } } return static_cast<void*>(NULL); }
void DataLayer<Dtype>::load_batch(Batch<Dtype>* batch) { CPUTimer batch_timer; batch_timer.Start(); double read_time = 0; double trans_time = 0; CPUTimer timer; // LOG(INFO)<<"load_batch"; CHECK(batch->data_.count()); CHECK(this->transformed_data_.count()); // Reshape according to the first datum of each batch // on single input batches allows for inputs of varying dimension. const int batch_size = this->layer_param_.data_param().batch_size(); // Datum& datum = *(reader_.full().peek()); // // Use data_transformer to infer the expected blob shape from datum. Datum datum; datum.ParseFromString(cursor_->value()); // Use data_transformer to infer the expected blob shape from datum. vector<int> top_shape = this->data_transformer_->InferBlobShape(datum); this->transformed_data_.Reshape(top_shape); // Reshape batch according to the batch_size. top_shape[0] = batch_size; batch->data_.Reshape(top_shape); Dtype* top_data = batch->data_.mutable_cpu_data(); // Dtype* top_data = this->prefetch_data_.mutable_cpu_data(); Dtype* top_label = NULL; // suppress warnings about uninitialized variables // LOG(INFO)<<" output_labels_:"<<this->output_labels_; if (this->output_labels_) { top_label = batch->label_.mutable_cpu_data(); // top_label = this->prefetch_label_.mutable_cpu_data(); } Dtype* use_data=this->use_data_.mutable_cpu_data(); // LOG(INFO)<<" use_data[0]:"<<use_data[0]; if (use_data[0]==0.0){ // LOG(INFO)<<"visit in order"; for (int item_id = 0; item_id < batch_size; item_id++) { Datum datum; datum.ParseFromString(cursor_->value()); // Apply data transformations (mirror, scale, crop...) // LOG(INFO)<<"jq enter data_layers"<< item_id; int offset = batch->data_.offset(item_id); // LOG(INFO)<<"jq enter data_layers"; this->transformed_data_.set_cpu_data(top_data + offset); this->data_transformer_->Transform(datum, &(this->transformed_data_)); // Copy label. if (this->output_labels_) { top_label[item_id] = datum.label(); // std::cout<<" cursor_:"<<datum.label(); } // use_data[item_id +5] = start; // trans_time += timer.MicroSeconds(); cursor_->Next(); // start +=1.0; // std::cout<<" output_labels_:"<<this->output_labels_; if (!cursor_->valid()) { DLOG(INFO) << "Restarting data prefetching from start."; cursor_->SeekToFirst(); } // reader_.free().push(const_cast<Datum*>(&datum)); } }else if (use_data[0]!=0.0){ // forward-backward using semi supervised with false label // 0, sami-super-unsuper, 1, label_kinds, 2, step over, // 3, datanum, 4, start index // LOG(INFO)<<"visit in Key/value"; // LOG(INFO)<<"this->PREFETCH_COUNT:"<<this->PREFETCH_COUNT; int step_over = batch_size+1; // std::cout<<std::endl; scoped_ptr<db::Transaction> txn(db_->NewTransaction()); // std::cout<<"key:"; int kCIFARImageNBytes=3072; for (int item_id = 0; item_id < batch_size; item_id++) { char str_buffer[kCIFARImageNBytes]; int id= static_cast<int>(use_data[item_id+ 1]); // std::cout<<" "<<id<<":"; int length = snprintf(str_buffer, kCIFARImageNBytes, "%05d", id); string value; string str=string(str_buffer, length); txn->Get(str, value); Datum datum; datum.ParseFromString(value); int offset = batch->data_.offset(item_id); // LOG(INFO)<<"jq enter data_layers"; this->transformed_data_.set_cpu_data(top_data + offset); this->data_transformer_->Transform(datum, &(this->transformed_data_)); // std::cout<<" output_labels_:"<<this->output_labels_; if (this->output_labels_) { // top_label[item_id] = datum.label(); top_label[item_id] = use_data[item_id+ step_over]; // std::cout<<" KV:"<<datum.label(); // top_label[item_id]= static_cast<int>(use_data[item_id + batch_size +3]); } if( use_data[item_id+ step_over]!=(datum.label()%1000)) LOG(INFO)<<"image id:"<<id<<" not correctly fetch: "<<datum.label() <<" vs "<<use_data[item_id+ step_over]; // std::cout<<top_label[item_id]; // std::cout<<" key:"<<id; } // std::cout<<std::endl; // for (int item_id = 0; item_id < 50000; item_id++) { // char str_buffer[kCIFARImageNBytes]; // // int id= static_cast<int>(use_data[item_id+ 1]); // int length = snprintf(str_buffer, kCIFARImageNBytes, "%05d", item_id); // string value; // string str=string(str_buffer, length); // txn->Get(str, value); // // Datum datum; // // datum.ParseFromString(value); // // int offset = batch->data_.offset(item_id); // // // LOG(INFO)<<"jq enter data_layers"; // // this->transformed_data_.set_cpu_data(top_data + offset); // // this->data_transformer_->Transform(datum, &(this->transformed_data_)); // // if (this->output_labels_) { // // top_label[item_id] = datum.label(); // // // top_label[item_id]= static_cast<int>(use_data[item_id + batch_size +3]); // // } // // std::cout<<" "<<item_id; // } // std::cout<<std::endl; txn->Commit(); } timer.Stop(); batch_timer.Stop(); DLOG(INFO) << "Prefetch batch: " << batch_timer.MilliSeconds() << " ms."; DLOG(INFO) << " Read time: " << read_time / 1000 << " ms."; DLOG(INFO) << "Transform time: " << trans_time / 1000 << " ms."; }
void* PoseImageDataLayerPrefetch(void* layer_pointer) { CHECK(layer_pointer); PoseImageDataLayer<Dtype>* layer = reinterpret_cast<PoseImageDataLayer<Dtype>*>(layer_pointer); CHECK(layer); Datum datum; CHECK(layer->prefetch_data_); Dtype* top_data = layer->prefetch_data_->mutable_cpu_data(); Dtype* top_label = layer->prefetch_label_->mutable_cpu_data(); PoseImageDataParameter pose_image_data_param = layer->layer_param_.pose_image_data_param(); const Dtype scale = pose_image_data_param.scale(); const int batch_size = pose_image_data_param.batch_size(); const int crop_size = pose_image_data_param.crop_size(); const bool mirror = pose_image_data_param.mirror(); const int new_height = pose_image_data_param.new_height(); const int new_width = pose_image_data_param.new_width(); const int out_height = pose_image_data_param.out_height(); const int out_width = pose_image_data_param.out_width(); const int key_point_range = pose_image_data_param.key_point_range(); const float scale_lower_bound = pose_image_data_param.scale_lower_bound(); const float scale_upper_bound = pose_image_data_param.scale_upper_bound(); const int key_point_num = pose_image_data_param.key_point_num(); const int randmargin = pose_image_data_param.randmargin(); const int use_mode = pose_image_data_param.use_mode(); const float torso_ratio = pose_image_data_param.torso_ratio(); const int mx1 = pose_image_data_param.mx1(); const int mx2 = pose_image_data_param.mx2(); const int my1 = pose_image_data_param.my1(); const int my2 = pose_image_data_param.my2(); const bool color_aug = pose_image_data_param.color_aug(); if (mirror && crop_size == 0) { LOG(FATAL) << "Current implementation requires mirror and crop_size to be " << "set at the same time."; } // datum scales const int channels = layer->datum_channels_; const int height = layer->datum_height_; const int width = layer->datum_width_; const int size = layer->datum_size_; const int lines_size = layer->lines_.size(); const Dtype* mean = layer->data_mean_.cpu_data(); int * was = new int[out_height * out_width]; for (int item_id = 0; item_id < batch_size; ++item_id) { char ss1[1010],ss2[1010]; sprintf(ss1,"/home/dragon123/cnncode/showimg/%d.jpg",item_id); //sprintf(ss2,"/home/dragon123/cnncode/showimg/%d_gt.jpg",item_id); // get a blob float nowscale = 1; if (layer->phase_ == Caffe::TRAIN) nowscale = random(scale_lower_bound, scale_upper_bound); CHECK_GT(1.55, nowscale); CHECK_GT(nowscale, 0.95); CHECK_GT(lines_size, layer->lines_id_); if (use_mode == 1) { bool temp = PoseReadImageToDatum_mode1(layer->lines_[layer->lines_id_].first, layer->lines_[layer->lines_id_].second, new_height, new_width, &datum, nowscale, torso_ratio, mx1, mx2, my1, my2, randmargin); if (temp == false) continue; } else { bool temp = PoseReadImageToDatum_mode2(layer->lines_[layer->lines_id_].first, layer->lines_[layer->lines_id_].second, new_height, new_width, &datum, nowscale, torso_ratio, mx1, mx2, my1, my2, randmargin); if (temp == false) continue; } const string& data = datum.data(); if (new_height > 0 && new_width > 0) { CHECK(data.size()) << "Image cropping only support uint8 data"; int h_off, w_off; // We only do random crop when we do training. h_off = 0; w_off = 0; if (mirror && layer->PrefetchRand() % 2) { // Copy mirrored version for (int c = 0; c < channels; ++c) { float thisRand = 1; if(color_aug) { thisRand = random(0.8,1.2); } for (int h = 0; h < new_height; ++h) { for (int w = 0; w < new_width; ++w) { int top_index = ((item_id * channels + c) * new_height + h) * new_width + (new_width - 1 - w); int data_index = (c * height + h + h_off) * width + w + w_off; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); top_data[top_index] = (datum_element - mean[data_index]) * scale; top_data[top_index] = min(top_data[top_index] * thisRand, (Dtype)(255.0)); } } } } else { // Normal copy //Mat img(Size(new_width,new_height), CV_8UC3); for (int c = 0; c < channels; ++c) { float thisRand = 1; if(color_aug) { thisRand = random(0.8,1.2); } for (int h = 0; h < new_height; ++h) { for (int w = 0; w < new_width; ++w) { int top_index = ((item_id * channels + c) * new_height + h) * new_width + w; int data_index = (c * height + h + h_off) * width + w + w_off; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); top_data[top_index] = (datum_element - mean[data_index]) * scale; //img.at<cv::Vec3b>(h, w)[c] = (uchar)(datum_element * scale) * thisRand; top_data[top_index] = min(top_data[top_index] * thisRand, (Dtype)(255.0)); } } } //imwrite(ss1, img); } } else { // Just copy the whole data if (data.size()) { for (int j = 0; j < size; ++j) { Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[j])); top_data[item_id * size + j] = (datum_element - mean[j]) * scale; } } else { for (int j = 0; j < size; ++j) { top_data[item_id * size + j] = (datum.float_data(j) - mean[j]) * scale; } } } float lblratio = new_height / out_height; vector<int> pts; for (int label_i = 0; label_i < datum.label_size(); label_i++) { pts.push_back( datum.label(label_i) / lblratio ); } int lblLen = key_point_num * out_height * out_width; PoseReadLabel(pts, was, top_label + item_id * lblLen, out_height, out_width); /*for(int ci = 0; ci < key_point_num; ci ++) { Mat img(Size(out_height, out_width), CV_8UC3); sprintf(ss2,"/home/dragon123/cnncode/showimg/%d_%d_gt.jpg",item_id, ci); for(int h = 0; h < out_height; h ++) for(int w = 0; w < out_width; w ++) { int clr = top_label[item_id * lblLen + ci * out_height * out_width + h * out_width + w]; if(clr <= 0) { if(clr == 0) for(int c = 0; c < 3; c ++) img.at<cv::Vec3b>(h, w)[c] = 0; if(clr < 0) for(int c = 0; c < 3; c ++) img.at<cv::Vec3b>(h, w)[c] = 128; } else { for(int c = 0; c < 3; c ++) img.at<cv::Vec3b>(h, w)[c] = 255; } } imwrite(ss2, img); }*/ // go to the next iter layer->lines_id_++; if (layer->lines_id_ >= lines_size) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; layer->lines_id_ = 0; if (layer->layer_param_.pose_image_data_param().shuffle()) { layer->ShuffleImages(); } } } delete was; return reinterpret_cast<void*>(NULL); }
void* DataLayerPrefetch(void* layer_pointer) { CHECK(layer_pointer); DataLayer<Dtype>* layer = static_cast<DataLayer<Dtype>*>(layer_pointer); CHECK(layer); Datum datum; CHECK(layer->prefetch_data_); Dtype* top_data = layer->prefetch_data_->mutable_cpu_data(); //数据 Dtype* top_label; //标签 if (layer->output_labels_) { top_label = layer->prefetch_label_->mutable_cpu_data(); } const Dtype scale = layer->layer_param_.data_param().scale(); const int batch_size = layer->layer_param_.data_param().batch_size(); const int crop_size = layer->layer_param_.data_param().crop_size(); const bool mirror = layer->layer_param_.data_param().mirror(); if (mirror && crop_size == 0) {//当前实现需要同时设置mirror和cropsize LOG(FATAL) << "Current implementation requires mirror and crop_size to be " << "set at the same time."; } // datum scales const int channels = layer->datum_channels_; const int height = layer->datum_height_; const int width = layer->datum_width_; const int size = layer->datum_size_; const Dtype* mean = layer->data_mean_.cpu_data(); for (int item_id = 0; item_id < batch_size; ++item_id) { //每一批数据的数量是batchsize,一个循环拉取一张 // get a blob CHECK(layer->iter_); CHECK(layer->iter_->Valid()); datum.ParseFromString(layer->iter_->value().ToString());//利用迭代器拉取下一批数据 const string& data = datum.data(); int label_blob_channels = layer->prefetch_label_->channels(); int label_data_dim = datum.label_size(); CHECK_EQ(layer->prefetch_label_->channels(), datum.label_size()) << "label size is NOT the same."; if (crop_size) {//如果需要裁剪 CHECK(data.size()) << "Image cropping only support uint8 data"; int h_off, w_off; // We only do random crop when we do training. //只是在训练阶段做随机裁剪 if (layer->phase_ == Caffe::TRAIN) { h_off = layer->PrefetchRand() % (height - crop_size); w_off = layer->PrefetchRand() % (width - crop_size); } else {//测试阶段固定裁剪 h_off = (height - crop_size) / 2; w_off = (width - crop_size) / 2; } //怎么感觉下面两种情况的代码是一样的? if (mirror && layer->PrefetchRand() % 2) { // Copy mirrored version for (int c = 0; c < channels; ++c) { for (int h = 0; h < crop_size; ++h) { for (int w = 0; w < crop_size; ++w) { int top_index = ((item_id * channels + c) * crop_size + h) * crop_size + (crop_size - 1 - w); int data_index = (c * height + h + h_off) * width + w + w_off; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); top_data[top_index] = (datum_element - mean[data_index]) * scale; } } } } else {//如果不需要裁剪 // Normal copy //我们优先考虑data(),然后float_data() for (int c = 0; c < channels; ++c) { for (int h = 0; h < crop_size; ++h) { for (int w = 0; w < crop_size; ++w) { int top_index = ((item_id * channels + c) * crop_size + h) * crop_size + w; int data_index = (c * height + h + h_off) * width + w + w_off; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); top_data[top_index] = (datum_element - mean[data_index]) * scale; } } } } } else { // we will prefer to use data() first, and then try float_data() if (data.size()) { for (int j = 0; j < size; ++j) { Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[j])); top_data[item_id * size + j] = (datum_element - mean[j]) * scale; } } else { for (int j = 0; j < size; ++j) { top_data[item_id * size + j] = (datum.float_data(j) - mean[j]) * scale; } } } if (g_item_id++ < 5) { int label_size = datum.label_size(); int image_label = 0; for (int j = 0; j < label_size; ++j) { if (datum.label(j) == 1) { image_label = j; break; } } char strImgRawDataFile[255] = ""; sprintf(strImgRawDataFile, "caffe_%s_%05d_%d%s", "train", item_id, image_label, ".txt"); ofstream fout_image_raw_data(strImgRawDataFile); for (int h = 0; h < height; ++h) { for (int w = 0; w < width; ++w) { int pixel_index = h * height + w; Dtype datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[pixel_index])); char strHexByte[3] = ""; sprintf(strHexByte, "%02X", (unsigned char)datum_element); fout_image_raw_data<<" "<<strHexByte; } fout_image_raw_data<<endl; } fout_image_raw_data<<endl; for (int j = 0; j < label_size; ++j) { fout_image_raw_data<<datum.label(j); } fout_image_raw_data.close(); } if (layer->output_labels_) { int label_size = datum.label_size(); for (int j = 0; j < label_size; ++j) { top_label[item_id * label_size + j] = datum.label(j); } //top_label[item_id] = datum.label(); } // go to the next iter layer->iter_->Next(); if (!layer->iter_->Valid()) { // We have reached the end. Restart from the first. DLOG(INFO) << "Restarting data prefetching from start."; layer->iter_->SeekToFirst(); } } return static_cast<void*>(NULL); }