Windows下利用VS使用Caffe可以为开发者提供很好的体验,但是每次编译的时候的总是十分钟的时间在改代码,剩下50分钟在编译的过程中,另外在实际图像分类开发中,很多情况下我们可能只需要一两个函数,所以怎么把caffe的classfy封装成我们需要的dll和lib,可以不依赖caffe的框架,在新建的解决方案中,可以直接调用。
本文主要封装了两个版本的caffe
1:happynear版本:https://github.com/happynear/caffe-windows
http://blog.csdn.net/sinat_30071459/article/details/51823390
以上版本主要参考了小咸鱼的博客,给我提供了很大的帮助,大家可以按照他的方法
2:微软caffe版本:
1:编译微软caffe http://blog.csdn.net/shakevincent/article/details/51694686
2:添加需要的文件:
添加classification.h
#ifndef CLASSIFICATION_H_
#define CLASSIFICATION_H_
#include <caffe/caffe.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iosfwd>
#include <memory>
#include <utility>
#include <vector>
#include <iostream>
#include <string>
#include <time.h>
using namespace caffe;
using std::string;
typedef std::pair<int, float> Prediction;
class ClassifierImpl {
public:
ClassifierImpl(const string& model_file,
const string& trained_file,
const string& mean_file
);
std::vector<Prediction> Classify(const cv::Mat& img, int N = 2);
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_;
};
#endif
添加classification.cpp
#include "classification.h"
ClassifierImpl::ClassifierImpl(const string& model_file,
const string& trained_file,
const string& mean_file) {
#ifdef CPU_ONLY
Caffe::set_mode(Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
#endif
/* Load the network. */
net_.reset(new Net<float>(model_file, TEST));
net_->CopyTrainedLayersFrom(trained_file);
CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly o