コード例 #1
0
ファイル: classification.cpp プロジェクト: dividiti/ck-caffe
void classify_single_image(Classifier& classifier, const fs::path& file_path) {
  long ct_repeat=0;
  long ct_repeat_max=1;
  int ct_return=0;

  if (getenv("CT_REPEAT_MAIN")!=NULL) ct_repeat_max=atol(getenv("CT_REPEAT_MAIN"));

  string file = file_path.string();

  std::cout << "---------- Prediction for " << file << " ----------" << std::endl;

  x_clock_start(1);
  cv::Mat img = cv::imread(file, -1);
  x_clock_end(1);
  CHECK(!img.empty()) << "Unable to decode image " << file;

  x_clock_start(2);

  std::vector<Prediction> predictions;

  for (ct_repeat=0; ct_repeat<ct_repeat_max; ct_repeat++) {
    predictions = classifier.Classify(img);
  }

  x_clock_end(2);

  /* Print the top N predictions. */
  for (size_t i = 0; i < predictions.size(); ++i) {
    Prediction p = predictions[i];
    std::cout << std::fixed << std::setprecision(4) << p.second << " - \"" << p.first << "\"" << std::endl;
  }
}
コード例 #2
0
ファイル: classification.cpp プロジェクト: dividiti/ck-caffe
void classify_continuously(Classifier& classifier, const fs::path& val_path, const fs::path& dir) {
  std::map<std::string, std::string> correct_labels;

  std::ifstream val_file(val_path.string());
  while (!val_file.eof()) {
    std::string fname = "";
    int index = 0;
    val_file >> fname >> index;
    if (fname != "") {
      std::string label = classifier.GetLabel(index);
      if (label != "") {
        correct_labels[boost::to_upper_copy(fname)] = label;
      }
    }
  }

  const int timer = 2;
  fs::directory_iterator end_iter;
  while (true) {
    for (fs::directory_iterator dir_iter(dir) ; dir_iter != end_iter ; ++dir_iter){
      if (interrupt_requested()) {
        return;
      }
      if (!fs::is_regular_file(dir_iter->status())) {
        // skip non-images
        continue;
      }
      string file = dir_iter->path().string();
      cv::Mat img = cv::imread(file, -1);
      if (img.empty()) {
        // TODO: should we complain?
        continue;
      }
      std::vector<Prediction> predictions;
      x_clock_start(timer);
      predictions = classifier.Classify(img);
      x_clock_end(timer);
      std::cout << "File: " << file << std::endl;
      std::cout << "Duration: " << x_get_time(timer) << " sec" << std::endl;
      auto correct_iter = correct_labels.find(boost::to_upper_copy(dir_iter->path().filename().string()));
      std::string label = "";
      if (correct_iter != correct_labels.end()) {
        label = correct_iter->second;
      }
      std::cout << "Correct label: " << label << std::endl;
      std::cout << "Predictions: " << predictions.size() << std::endl;
      for (size_t i = 0; i < predictions.size(); ++i) {
        Prediction p = predictions[i];
        std::cout << std::fixed << std::setprecision(4) << p.second << " - \"" << p.first << "\"" << std::endl;
      }
      std::cout << std::endl;
      std::cout.flush();
    }
  }
}