void DataTransformer<Dtype>::Transform(const Datum& datum, Dtype* transformed_data) { const string& data = datum.data(); const int datum_channels = datum.channels(); const int datum_height = datum.height(); const int datum_width = datum.width(); const int crop_size = param_.crop_size(); const Dtype scale = param_.scale(); bool do_mirror = param_.mirror() && Rand(2); //aki_update const bool has_mean_file = param_.has_mean_file(); const bool has_uint8 = data.size() > 0; const bool has_mean_values = mean_values_.size() > 0; CHECK_GT(datum_channels, 0); CHECK_GE(datum_height, crop_size); CHECK_GE(datum_width, crop_size); Dtype* mean = NULL; if (has_mean_file) { CHECK_EQ(datum_channels, data_mean_.channels()); CHECK_EQ(datum_height, data_mean_.height()); CHECK_EQ(datum_width, data_mean_.width()); mean = data_mean_.mutable_cpu_data(); } if (has_mean_values) { CHECK(mean_values_.size() == 1 || mean_values_.size() == datum_channels) << "Specify either 1 mean_value or as many as channels: " << datum_channels; if (datum_channels > 1 && mean_values_.size() == 1) { // Replicate the mean_value for simplicity for (int c = 1; c < datum_channels; ++c) { mean_values_.push_back(mean_values_[0]); } } } int height = datum_height; int width = datum_width; int h_off = 0; int w_off = 0; if (crop_size) { height = crop_size; width = crop_size; // We only do random crop when we do training. if (phase_ == TRAIN) { h_off = Rand(datum_height - crop_size + 1); w_off = Rand(datum_width - crop_size + 1); } else { h_off = (datum_height - crop_size) / 2; w_off = (datum_width - crop_size) / 2; } } //aki_update_start //use the multiview strategy in testing const bool use_multiview = param_.multi_view(); if (use_multiview) { std::ifstream in_stream(std::string("multiview_cache").c_str()); int view_type = 0; in_stream >> view_type; in_stream.close(); if (view_type > 5) { //it means we have to use mirror right here do_mirror = true; view_type-=5; } switch(view_type){ case 1: h_off = 0; w_off = 0; break; case 2: h_off = 0; w_off = datum_width - crop_size; break; case 3: h_off = datum_width - crop_size; w_off = 0; break; case 4: h_off = datum_width - crop_size; w_off = datum_width - crop_size; break; case 5: h_off = (datum_height - crop_size) / 2; w_off = (datum_width - crop_size) / 2; break; default: break; } } //aki_update_end Dtype datum_element; int top_index, data_index; for (int c = 0; c < datum_channels; ++c) { for (int h = 0; h < height; ++h) { for (int w = 0; w < width; ++w) { data_index = (c * datum_height + h_off + h) * datum_width + w_off + w; if (do_mirror) { top_index = (c * height + h) * width + (width - 1 - w); } else { top_index = (c * height + h) * width + w; } if (has_uint8) { datum_element = static_cast<Dtype>(static_cast<uint8_t>(data[data_index])); } else { datum_element = datum.float_data(data_index); } if (has_mean_file) { transformed_data[top_index] = (datum_element - mean[data_index]) * scale; } else { if (has_mean_values) { transformed_data[top_index] = (datum_element - mean_values_[c]) * scale; } else { transformed_data[top_index] = datum_element * scale; } } } } } }
void minotaur::compiler::compile(template_builder &builder) { boost::filesystem::path id(this->input_file); boost::filesystem::path od(this->output_file); std::string id_full_path = id.string(), parent_path; std::vector<std::string> current_stack; if (boost::filesystem::exists(id)) { if (boost::filesystem::is_directory(id)) { boost::filesystem::recursive_directory_iterator dir_end; for (boost::filesystem::recursive_directory_iterator d(id); d != dir_end; ++d) { std::cout << "*" << d->path().string(); if (boost::filesystem::is_regular(*d)) { std::string extension = d->path().extension().string(); parent_path = d->path().parent_path().string(); minotaur::file_info finfo; boost::filesystem::path fpath = (*d).path(); finfo.extension = fpath.extension().string(); std::string fname = fpath.filename().string(); unsigned long pos = fname.find('.'); if (pos != std::string::npos) { finfo.name = fname.substr(0, pos); finfo.subextension = fname.substr(pos, fname.length() - pos - finfo.extension.size()); } else { finfo.name = fpath.filename().string(); finfo.subextension = ""; } finfo.path = fpath.parent_path().string(); finfo.fullPath = fpath.string(); std::string relative_path; if (parent_path != id_full_path) relative_path = parent_path.substr(id_full_path.length()+1); boost::filesystem::path target_compiled_file( od.string() + boost::filesystem::path::preferred_separator + (relative_path) + boost::filesystem::path::preferred_separator + finfo.name + finfo.extension + ".hpp" ); if (this->needs_update(fpath, target_compiled_file)) { std::ifstream in_stream(fpath.string()); if (!in_stream) throw icarus::open_file("Cannot open input file."); minotaur::parser_file c(in_stream); boost::filesystem::create_directories(target_compiled_file.parent_path().string()); boost::split(finfo.package, relative_path, boost::is_any_of("\\/")); c.parse(finfo); std::ofstream ostream(target_compiled_file.string()); if (ostream) { minotaur::cpp_template_builder builder; builder.write(finfo, ostream); } else throw icarus::open_file("Cannot create file stream on " + target_compiled_file.string() + "."); std::cout << ". OK"; } else { std::cout << ". Skipped"; } } std::cout << std::endl; } } else if (boost::filesystem::is_regular_file(id)) { std::string extension = id.extension().string(); parent_path = id.parent_path().string(); std::ifstream in_stream(id.string()); if (!in_stream) throw icarus::open_file("Cannot open input file."); minotaur::file_info finfo; finfo.extension = id.extension().string(); std::string fname = id.filename().string(); unsigned long pos = fname.find('.'); if (pos != std::string::npos) { finfo.name = fname.substr(0, pos); finfo.subextension = fname.substr(pos, fname.length() - pos - finfo.extension.size()); } else { finfo.name = id.filename().string(); finfo.subextension = ""; } finfo.path = id.parent_path().string(); finfo.fullPath = id.string(); minotaur::parser_file c(in_stream); c.parse(finfo); if (this->needs_update(id, od)) { std::ofstream ostream(od.string()); if (ostream) { minotaur::cpp_template_builder builder; builder.write(finfo, ostream); } else { // TODO: specialize exception std::cerr << "Cannot create file stream on " << od.string() << "." << std::endl; throw std::exception(); } } } else { // TODO: Throw an exception. } } else { // TODO: Throw an exception. } }
void DataTransformer<Dtype>::Transform(const cv::Mat& cv_img, Blob<Dtype>* transformed_blob) { const int crop_size = param_.crop_size(); const int img_channels = cv_img.channels(); const int img_height = cv_img.rows; const int img_width = cv_img.cols; // Check dimensions. const int channels = transformed_blob->channels(); const int height = transformed_blob->height(); const int width = transformed_blob->width(); const int num = transformed_blob->num(); CHECK_EQ(channels, img_channels); CHECK_LE(height, img_height); CHECK_LE(width, img_width); CHECK_GE(num, 1); CHECK(cv_img.depth() == CV_8U) << "Image data type must be unsigned byte"; const Dtype scale = param_.scale(); bool do_mirror = param_.mirror() && Rand(2);//aki_update const bool has_mean_file = param_.has_mean_file(); const bool has_mean_values = mean_values_.size() > 0; CHECK_GT(img_channels, 0); CHECK_GE(img_height, crop_size); CHECK_GE(img_width, crop_size); Dtype* mean = NULL; if (has_mean_file) { CHECK_EQ(img_channels, data_mean_.channels()); CHECK_EQ(img_height, data_mean_.height()); CHECK_EQ(img_width, data_mean_.width()); mean = data_mean_.mutable_cpu_data(); } if (has_mean_values) { CHECK(mean_values_.size() == 1 || mean_values_.size() == img_channels) << "Specify either 1 mean_value or as many as channels: " << img_channels; if (img_channels > 1 && mean_values_.size() == 1) { // Replicate the mean_value for simplicity for (int c = 1; c < img_channels; ++c) { mean_values_.push_back(mean_values_[0]); } } } int h_off = 0; int w_off = 0; cv::Mat cv_cropped_img = cv_img; if (crop_size) { CHECK_EQ(crop_size, height); CHECK_EQ(crop_size, width); // We only do random crop when we do training. if (phase_ == TRAIN) { h_off = Rand(img_height - crop_size + 1); w_off = Rand(img_width - crop_size + 1); } else { h_off = (img_height - crop_size) / 2; w_off = (img_width - crop_size) / 2; } cv::Rect roi(w_off, h_off, crop_size, crop_size); cv_cropped_img = cv_img(roi); } else { CHECK_EQ(img_height, height); CHECK_EQ(img_width, width); } CHECK(cv_cropped_img.data); //aki_update_start //use the multiview strategy in testing const bool use_multiview = param_.multi_view(); if (use_multiview) { std::ifstream in_stream(std::string("multiview_cache").c_str()); int view_type = 0; in_stream >> view_type; in_stream.close(); if (view_type > 5) { //it means we have to use mirror right here do_mirror = true; view_type-=5; } switch(view_type){ case 1: h_off = 0; w_off = 0; break; case 2: h_off = 0; w_off = img_width - crop_size; break; case 3: h_off = img_width - crop_size; w_off = 0; break; case 4: h_off = img_width - crop_size; w_off = img_width - crop_size; break; case 5: h_off = (img_height - crop_size) / 2; w_off = (img_width - crop_size) / 2; break; default: break; } } //aki_update_end Dtype* transformed_data = transformed_blob->mutable_cpu_data(); int top_index; for (int h = 0; h < height; ++h) { const uchar* ptr = cv_cropped_img.ptr<uchar>(h); int img_index = 0; for (int w = 0; w < width; ++w) { for (int c = 0; c < img_channels; ++c) { if (do_mirror) { top_index = (c * height + h) * width + (width - 1 - w); } else { top_index = (c * height + h) * width + w; } // int top_index = (c * height + h) * width + w; Dtype pixel = static_cast<Dtype>(ptr[img_index++]); if (has_mean_file) { int mean_index = (c * img_height + h_off + h) * img_width + w_off + w; transformed_data[top_index] = (pixel - mean[mean_index]) * scale; } else { if (has_mean_values) { transformed_data[top_index] = (pixel - mean_values_[c]) * scale; } else { transformed_data[top_index] = pixel * scale; } } } } } }