所有的操作是基于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_;