void DetectionOutputLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) { const DetectionOutputParameter& detection_output_param = this->layer_param_.detection_output_param(); CHECK(detection_output_param.has_num_classes()) << "Must specify num_classes"; num_classes_ = detection_output_param.num_classes(); share_location_ = detection_output_param.share_location(); num_loc_classes_ = share_location_ ? 1 : num_classes_; background_label_id_ = detection_output_param.background_label_id(); code_type_ = detection_output_param.code_type(); variance_encoded_in_target_ = detection_output_param.variance_encoded_in_target(); keep_top_k_ = detection_output_param.keep_top_k(); confidence_threshold_ = detection_output_param.has_confidence_threshold() ? detection_output_param.confidence_threshold() : -FLT_MAX; // Parameters used in nms. nms_threshold_ = detection_output_param.nms_param().nms_threshold(); CHECK_GE(nms_threshold_, 0.) << "nms_threshold must be non negative."; top_k_ = -1; if (detection_output_param.nms_param().has_top_k()) { top_k_ = detection_output_param.nms_param().top_k(); } const SaveOutputParameter& save_output_param = detection_output_param.save_output_param(); output_directory_ = save_output_param.output_directory(); if (!output_directory_.empty() && !boost::filesystem::is_directory(output_directory_)) { if (!boost::filesystem::create_directories(output_directory_)) { LOG(FATAL) << "Failed to create directory: " << output_directory_; } } output_name_prefix_ = save_output_param.output_name_prefix(); need_save_ = output_directory_ == "" ? false : true; output_format_ = save_output_param.output_format(); if (save_output_param.has_label_map_file()) { string label_map_file = save_output_param.label_map_file(); if (label_map_file.empty()) { // Ignore saving if there is no label_map_file provided. LOG(WARNING) << "Provide label_map_file if output results to files."; need_save_ = false; } else { LabelMap label_map; CHECK(ReadProtoFromTextFile(label_map_file, &label_map)) << "Failed to read label map file: " << label_map_file; CHECK(MapLabelToName(label_map, true, &label_to_name_)) << "Failed to convert label to name."; CHECK(MapLabelToDisplayName(label_map, true, &label_to_display_name_)) << "Failed to convert label to display name."; } } else { need_save_ = false; } if (save_output_param.has_name_size_file()) { string name_size_file = save_output_param.name_size_file(); if (name_size_file.empty()) { // Ignore saving if there is no name_size_file provided. LOG(WARNING) << "Provide name_size_file if output results to files."; need_save_ = false; } else { std::ifstream infile(name_size_file.c_str()); CHECK(infile.good()) << "Failed to open name size file: " << name_size_file; // The file is in the following format: // name height width // ... string name; int height, width; while (infile >> name >> height >> width) { names_.push_back(name); sizes_.push_back(std::make_pair(height, width)); } infile.close(); if (save_output_param.has_num_test_image()) { num_test_image_ = save_output_param.num_test_image(); } else { num_test_image_ = names_.size(); } CHECK_LE(num_test_image_, names_.size()); } } else {
inline bool MapLabelToDisplayName(const LabelMap& map, std::map<int, string>* label_to_display_name) { return MapLabelToDisplayName(map, true, label_to_display_name); }