| 24 | |
| 25 | template <typename Dtype> |
| 26 | void ImageDataLayer<Dtype>::DataLayerSetUp(const vector<Blob<Dtype>*>& bottom, |
| 27 | const vector<Blob<Dtype>*>& top) { |
| 28 | const int new_height = this->layer_param_.image_data_param().new_height(); |
| 29 | const int new_width = this->layer_param_.image_data_param().new_width(); |
| 30 | const bool is_color = this->layer_param_.image_data_param().is_color(); |
| 31 | string root_folder = this->layer_param_.image_data_param().root_folder(); |
| 32 | |
| 33 | CHECK((new_height == 0 && new_width == 0) || |
| 34 | (new_height > 0 && new_width > 0)) << "Current implementation requires " |
| 35 | "new_height and new_width to be set at the same time."; |
| 36 | // Read the file with filenames and labels |
| 37 | const string& source = this->layer_param_.image_data_param().source(); |
| 38 | LOG(INFO) << "Opening file " << source; |
| 39 | std::ifstream infile(source.c_str()); |
| 40 | string line; |
| 41 | size_t pos; |
| 42 | int label; |
| 43 | while (std::getline(infile, line)) { |
| 44 | pos = line.find_last_of(' '); |
| 45 | label = atoi(line.substr(pos + 1).c_str()); |
| 46 | lines_.push_back(std::make_pair(line.substr(0, pos), label)); |
| 47 | } |
| 48 | |
| 49 | CHECK(!lines_.empty()) << "File is empty"; |
| 50 | |
| 51 | if (this->layer_param_.image_data_param().shuffle()) { |
| 52 | // randomly shuffle data |
| 53 | LOG(INFO) << "Shuffling data"; |
| 54 | const unsigned int prefetch_rng_seed = caffe_rng_rand(); |
| 55 | prefetch_rng_.reset(new Caffe::RNG(prefetch_rng_seed)); |
| 56 | ShuffleImages(); |
| 57 | } else { |
| 58 | if (this->phase_ == TRAIN && Caffe::solver_rank() > 0 && |
| 59 | this->layer_param_.image_data_param().rand_skip() == 0) { |
| 60 | LOG(WARNING) << "Shuffling or skipping recommended for multi-GPU"; |
| 61 | } |
| 62 | } |
| 63 | LOG(INFO) << "A total of " << lines_.size() << " images."; |
| 64 | |
| 65 | lines_id_ = 0; |
| 66 | // Check if we would need to randomly skip a few data points |
| 67 | if (this->layer_param_.image_data_param().rand_skip()) { |
| 68 | unsigned int skip = caffe_rng_rand() % |
| 69 | this->layer_param_.image_data_param().rand_skip(); |
| 70 | LOG(INFO) << "Skipping first " << skip << " data points."; |
| 71 | CHECK_GT(lines_.size(), skip) << "Not enough points to skip"; |
| 72 | lines_id_ = skip; |
| 73 | } |
| 74 | // Read an image, and use it to initialize the top blob. |
| 75 | cv::Mat cv_img = ReadImageToCVMat(root_folder + lines_[lines_id_].first, |
| 76 | new_height, new_width, is_color); |
| 77 | CHECK(cv_img.data) << "Could not load " << lines_[lines_id_].first; |
| 78 | // Use data_transformer to infer the expected blob shape from a cv_image. |
| 79 | vector<int> top_shape = this->data_transformer_->InferBlobShape(cv_img); |
| 80 | this->transformed_data_.Reshape(top_shape); |
| 81 | // Reshape prefetch_data and top[0] according to the batch_size. |
| 82 | const int batch_size = this->layer_param_.image_data_param().batch_size(); |
| 83 | CHECK_GT(batch_size, 0) << "Positive batch size required"; |
nothing calls this directly
no test coverage detected