Caffe图片分类-修改classification.cpp实现分类多张图片

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 \

你可能感兴趣的:(分类,caffe,caffemodel)