void TestLabels(const TLabels& gt_labels, const TLabels& predicted_labels) { int correct_predictions = 0; if (gt_labels.size() != predicted_labels.size()) { cout << "Error! Files with predicted and ground truth labels " "have different number of samples." << endl; return; } if (!gt_labels.size()) { cout << "Error! Dataset is empty."; return; } for (TLabels::const_iterator predicted_it = predicted_labels.begin(); predicted_it != predicted_labels.end(); ++predicted_it) { string sample = predicted_it->first; TLabels::const_iterator gt_it = gt_labels.find(sample); if (gt_it == gt_labels.end()) { cout << "Error! File " << sample << " has no ground truth label." << endl; return; } if (predicted_it->second == gt_it->second) ++correct_predictions; } cout << "Precision: " << double(correct_predictions) / gt_labels.size() << endl; }
/** Save result of prediction to file */ void SavePredictions(const TFileList& file_list, const TLabels& labels, const string& prediction_file) { // Check that list of files and list of labels has equal size assert(file_list.size() == labels.size()); // Open 'prediction_file' for writing ofstream stream(prediction_file.c_str()); // Write file names and labels to stream for (size_t image_idx = 0; image_idx < file_list.size(); ++image_idx) stream << file_list[image_idx].first << " " << labels[image_idx] << endl; stream.close(); }