Caffe圖片分類

本文通過修改classification.cpp實現用訓練好的model文件實現多張圖片的分類。

classification.cpp中main函數的源碼爲:::

int main(int argc, char** argv) {
  if (argc != 6) {
    std::cerr << "Usage: " << argv[0]
              << " deploy.prototxt network.caffemodel"
              << " mean.binaryproto labels.txt img.jpg" << std::endl;
    return 1;
  }

  ::google::InitGoogleLogging(argv[0]);

  string model_file   = argv[1];
  string trained_file = argv[2];
  string mean_file    = argv[3];
  string label_file   = argv[4];
  Classifier classifier(model_file, trained_file, mean_file, label_file);

  string file = argv[5];

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

  cv::Mat img = cv::imread(file, -1);
  CHECK(!img.empty()) << "Unable to decode image " << file;
  std::vector<Prediction> predictions = classifier.Classify(img);

  /* 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;
  }
}

根據博客http://blog.csdn.net/lanxuecc/article/details/52795344 我們知道,該文件編譯生成的.bin只能分類單張,那麼如果我們訓練出來了一個caffemodel,我們如何實現分類大量圖片來統計分類準確率呢,我把代碼做了一些修改,實現了這個功能

int main(int argc, char** argv) {

 string imgfilename;

  if (argc != 6) {
    std::cerr << "Usage: " << argv[0]
              << " deploy.prototxt network.caffemodel"
              << " mean.binaryproto labels.txt img.jpg" << std::endl;
    return 1;
  }

  ::google::InitGoogleLogging(argv[0]);

  string model_file   = argv[1];
  string trained_file = argv[2];
  string mean_file    = argv[3];
  string label_file   = argv[4];
  Classifier classifier(model_file, trained_file, mean_file, label_file);

  string file = argv[5];  //傳入的字符串是保存所有圖片路徑的文件

  fstream fin( file.c_str());  

  string fnd = "sglimg";
  string rep = "sglimgbak";
  string imgsave;
  string strinsert;

  while(getline(fin, imgfilename))  //依次讀入文件中每個圖片路徑
   {
     std::cout << "---------- Prediction for "
            << imgfilename << " ----------" << std::endl;     

     imgsave = imgfilename;

     cv::Mat img = cv::imread(imgfilename, -1);   //讀取圖片

     CHECK(!img.empty()) << "Unable to decode image " << imgfilename;

       std::vector<Prediction> predictions = classifier.Classify(img);  //對圖片進行分類

       imgsave = imgsave.erase(61,2);

       imgsave = imgsave.replace(imgsave.find(fnd), fnd.length(), rep);

      strinsert = predictions[0].first + "/";

      imgsave.insert(64, strinsert);

      std::cout << "---------- Save2  for "
        <<predictions[0].first<<"in"<< imgsave << " ----------" << std::endl;  

      imwrite(imgsave, img);  //按圖片圍住度最高的分類來保存圖片
   }

}

修改後重新編譯caffe就可以分類圖片了

make clean  //在caffe根目錄下
make all    //在caffe根目錄下

只是傳入的圖片路徑參數,改爲記錄所有待分類圖片的路徑的文件。

例如::::我的所有待分類的圖片在目錄/home/schao/sc_tmp/caffe/caffe-master/examples/numlet/sglimg/0~9,A~Z中,那麼傳入程序的圖片目錄爲(見下截圖,只保存一部分)
這裏寫圖片描述

現在我把這0~9,A~Z文件夾內的圖片分類分別保存到/home/schao/sc_tmp/caffe/caffe-master/examples/numlet/sglimgbak這個目錄下0~9,A~Z目錄中
這裏寫圖片描述

則代碼改成如上形式並且編譯後執行下述命令:::

./build/examples/cpp_classification/classification.bin \ 
./examples/numlet/lenet.prototxt \
./examples/numlet/caffenet_train_iter_20000.caffemodel \ 
./examples/numlet/mean.binaryproto \
./examples/numlet/synset_words.txt \
./examples/numlet/sglimg/imglist.txt \
發佈了57 篇原創文章 · 獲贊 28 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章