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;
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

根据博客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);  //按图片围住度最高的分类来保存图片
   }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

修改后重新编译caffe就可以分类图片了

make clean  //在caffe根目录下
make all    //在caffe根目录下
  • 1
  • 2

只是传入的图片路径参数,改为记录所有待分类图片的路径的文件。

例如::::我的所有待分类的图片在目录/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 \
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lanxueCC/article/details/52948881

猜你喜欢

转载自blog.csdn.net/haoji007/article/details/80299132