随着深度学习的发展,越来越多的人开始进入这个行业,希望可以有所进展,但是各行业的大牛们,速度超神,deep哈希、deep稀疏、deep做分类、识别、跟踪、等等。很多人也开始训练自己的数据集或者下载别人训练好的model。训练好的caffemodel怎么使用对于刚入门的深度学习人来说是比较困难的,或者怎么用模型去验证自己的数据集等等
下面就从以下方向来说明怎么使用训练好model来预测自己的图片
1:编译caffe for windows
微软提供Windows工具包(caffe-master):https://github.com/Microsoft/caffe
此版严格只支持VS2013。此版本无需配置任何第三方环境哦,因为在项目属性里面都已经包含进去了,非常方便。如果出现了一些文件打不开或者其它问题,第一可能是VS自身问题,第二可能你配置过其它caffe,修改过环境变量,导致找不到头文件之类的。
caffe for windows的编译:
可以参考我的博客:
http://blog.youkuaiyun.com/shakevincent/article/details/51694686
http://m.blog.youkuaiyun.com/article/details?id=51355143
http://m.blog.youkuaiyun.com/article/details?id=51549105
编译好caffe-windows后删除其他不需要的工程,仅保留caffelib 和classfication工程。
打开classfication工程里面的classfication.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>
#include <iostream>
#include <string>
#include <sstream>
#include "io.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#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_;
int num_channels_;
cv::Mat mean_;
std::vector<string> labels_;
};
Classifier::Classifier(const string& model_file,
const string& trained_file,
const string& mean_file,
const string& label_file) {