caffe测试单张图片

所有的操作是基于caffe的根目录/caffe-master/来操作的:

在caffe框架中用训练好的模型分类单张图片需要用到classification.bin,本博客主要提供其源码文件classification.cpp的注释。

1、caffe提供了一个用已经训练好的caffemodel来分类单张图片的库(./build/examples/cpp_classification/classification.bin),该库的源码为文件./examples/cpp-classification/classification.cpp。

2、利用该库的分类单张图片的具体方法::

./build/examples/cpp_classification/classification.bin \
网络结构文件:xx/xx/deploy.prototxt                      \
训练的模型文件:xx/xx/xx.caffemodel                      \
训练的图像的均值文件:xx/xx/xx.binaryproto                \
类别名称标签文件:xx/xx/synset_words.txt                 \
待测试图像:xx/xx/xx.jpg                                

具体可见这位大牛的博客:http://www.cnblogs.com/denny402/p/5111018.html

下面记录下我在看classification.cpp代码时一些注释,如有错误望指教:::

#include <caffe/caffe.hpp>
#ifdef USE_OPENCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif  // USE_OPENCV
#include <algorithm>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#ifdef USE_OPENCV
using namespace caffe;  // NOLINT(build/namespaces)
using std::string;

/* Pair (label, confidence) representing a prediction. */
typedef std::pair<string, float> Prediction;

class Classifier {
 public:
  Classifier(const string& model_file,
             const string& trained_file,
             const string& mean_file,
             const string& label_file);

  std::vector<Prediction> Classify(const cv::Mat& img, int N = 5);

 private:
  void SetMean(const string& mean_file);

  std::vector<float> Predict(const cv::Mat& img);

  void WrapInputLayer(std::vector<cv::Mat>* input_channels);

  void Preprocess(const cv::Mat& img,
                  std::vector<cv::Mat>* input_channels);

 private:
  shared_ptr<Net<float> > net_;
  cv::Size input_geometry_;
  
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值