首先请参照我的上一篇博文,在windows下配置好caffe-windows这个过程不算太复杂。把ubuntu下的caffe环境也配置好,这样我们就可以在linux环境下训练网络,并且把训练好的网络放在windows下的vs工程中进行一次前馈来提取特征了。
在ubuntu的caffe根目录下把下面4个文件拷出来:a. 找到caffe-master/models/bvlc_alexnet/readme.md,这里有alexnet的caffemodel下载地址可以直接下载:http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel。这个文件是一个已经训练好的网络参数
b. caffe-master/models/下的deploy.prototxt。这个文件说明了网络的结构
c. caffe-master/examples/imagenet下的imagenet_mean.binaryproto。这个文件记录了输入数据的均值
c. caffe-master/examples/imagenet下的synset_words.txt。这个文件记录了网络分类的标签含义
将这几个文件拷出来之后就可以回到windows了,新建一个vs2013工程,将刚才拷出来的4个文件,以及任意一张你要分类的图片放在工程的一个子目录下。如下图所示:
之后将工程调整为release x64,我们在编译caffe-windows时只编译了release x64的版本,所以debug下,或者是win32下都是无法跑这个demo的。
新建头文件head.h
//head.h
#include "caffe/common.hpp"
#include "caffe/layers/input_layer.hpp"
#include "caffe/layers/inner_product_layer.hpp"
#include "caffe/layers/conv_layer.hpp"
#include "caffe/layers/relu_layer.hpp"
#include "caffe/layers/pooling_layer.hpp"
#include "caffe/layers/softmax_layer.hpp"
namespace caffe
extern INSTANTIATE_CLASS(DataLayer);
extern INSTANTIATE_CLASS(InputLayer);
extern INSTANTIATE_CLASS(InnerProductLayer);
//REGISTER_LAYER_CLASS(Dropout);
extern INSTANTIATE_CLASS(ConvolutionLayer);
extern INSTANTIATE_CLASS(ReLULayer);
extern INSTANTIATE_CLASS(PoolingLayer);
extern INSTANTIATE_CLASS(LRNLayer);
extern INSTANTIATE_CLASS(SoftmaxLayer);
}
新建头文件Classifier.h
//Classifier.h
#define USE_OPENCV
//#define CPU_ONLY
#include "caffe/common.hpp"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iosfwd>
#include <string>
#include <vector>
#include <fstream>
//using namespace std;
using namespace caffe; // NOLINT(build/namespaces)
/* Pair (label, confidence) representing a prediction. */
class Classifier {
Classifier(const string& model_file,
const string& mean_file,
const string& label_file);
std::ofstream ofile;
std::vector<Prediction> Classify(const cv::Mat& img, int N = 5);
void Extract_Feature(const cv::Mat& img, vector<float>& feature);
std::vector<float> Predict(const cv::Mat& img);
void WrapInputLayer(std::vector<cv::Mat>* input_channels);
std::vector<cv::Mat>* input_channels);
shared_ptr<Net<float> > net_;
cv::Mat mean_;
std::vector<string> labels_;
};
Classifier.cpp
#include "Classifier.h"
Classifier::Classifier(const string& model_file,
const string& mean_file,
const string& label_file) {
Caffe::set_mode(Caffe::CPU);
Caffe::set_mode(Caffe::GPU);
/* Load the network. */
ofile.open("feature.txt");
net_.reset(new Net<float>(model_file, TEST));
CHECK_EQ(net_->num_inputs(), 1) << "Network should have exactly one input.";
CHECK_EQ(net_->num_outputs(), 1) << "Network should have exactly one output.";
num_channels_ = input_layer->channels();
CHECK(num_channels_ == 3 || num_channels_ == 1)
<< "Input layer should have 1 or 3 channels.";
SetMean(mean_file);
/* Load labels. */
CHECK(labels) << "Unable to open labels file " << label_file;
labels_.push_back(string(line));
Blob<float>* output_layer = net_->output_blobs()[0];
<< "Number of labels is different from the output layer dimension.";
static bool PairCompare(const std::pair<float, int>& lhs,
return lhs.first > rhs.first;
/* Return the indices of the top N values of vector v. */
static std::vector<int> Argmax(const std::vector<float>& v, int N) {
for (size_t i = 0; i < v.size(); ++i)
pairs.push_back(std::make_pair(v[i], i));
std::partial_sort(pairs.begin(), pairs.begin() + N, pairs.end(), PairCompare);
result.push_back(pairs[i].second);
/* Return the top N predictions. */
std::vector<Prediction> Classifier::Classify(const cv::Mat& img, int N) {
N = std::min<int>(labels_.size(), N);
std::vector<int> maxN = Argmax(output, N);
for (int i = 0; i < N; ++i) {
predictions.push_back(std::make_pair(labels_[idx], output[idx]));
/* Load the mean file in binaryproto format. */
void Classifier::SetMean(const string& mean_file) {
ReadProtoFromBinaryFileOrDie(mean_file.c_str(), &blob_proto);
Blob<float> mean_blob;
mean_blob.FromProto(blob_proto);
CHECK_EQ(mean_blob.channels(), num_channels_)
<< "Number of channels of mean file doesn't match input layer.";
/* The format of the mean file is planar 32-bit float BGR or grayscale. */
float* data = mean_blob.mutable_cpu_data();
/* Extract an individual channel. */
cv::Mat channel(mean_blob.height(), mean_blob.width(), CV_32FC1, data);
data += mean_blob.height() * mean_blob.width();
/* Merge the separate channels into a single image. */
/* Compute the global mean pixel value and create a mean image
cv::Scalar channel_mean = cv::mean(mean);
mean_ = cv::Mat(input_geometry_, mean.type(), channel_mean);
unsigned int get_blob_index(boost::shared_ptr< Net<float> > & net, char *query_blob_name)
vector< string > const & blob_names = net->blob_names();
{
{
return i;
LOG(FATAL) << "Unknown blob name: " << str_query;
std::vector<float> Classifier::Predict(const cv::Mat& img) {
input_layer->Reshape(1, num_channels_,
input_geometry_.height, input_geometry_.width);
net_->Reshape();
std::vector<cv::Mat> input_channels;
Preprocess(img, &input_channels);
//char *query_blob_name = "fc6"; /* data, conv1, pool1, norm1, fc6, prob, etc */
//boost::shared_ptr<Blob<float> > blob = net_->blobs()[blob_id];
//std::ofstream ofile("feature.txt");
//const float *blob_ptr = (const float *)blob->cpu_data();
// ofile << *(blob_ptr + i)<<" ";
//ofile.close();
/* Copy the output layer to a std::vector */
Blob<float>* output_layer = net_->output_blobs()[0];
const float* end = begin + output_layer->channels();
/* Wrap the input layer of the network in separate cv::Mat objects
* don't need to rely on cudaMemcpy2D. The last preprocessing
* operation will write the separate channels directly to the input
void Classifier::WrapInputLayer(std::vector<cv::Mat>* input_channels) {
int width = input_layer->width();
int height = input_layer->height();
float* input_data = input_layer->mutable_cpu_data();
cv::Mat channel(height, width, CV_32FC1, input_data);
input_data += width * height;
void Classifier::Preprocess(const cv::Mat& img,
/* Convert the input image to the input image format of the network. */
cv::cvtColor(img, sample, cv::COLOR_BGR2GRAY);
else if (img.channels() == 4 && num_channels_ == 1)
else if (img.channels() == 4 && num_channels_ == 3)
else if (img.channels() == 1 && num_channels_ == 3)
else
sample = img;
cv::Mat sample_resized;
if (sample.size() != input_geometry_)
cv::resize(sample, sample_resized, input_geometry_);
cv::Mat sample_float;
if (num_channels_ == 3)
sample_resized.convertTo(sample_float, CV_32FC3);
sample_resized.convertTo(sample_float, CV_32FC1);
cv::subtract(sample_float, mean_, sample_normalized);
/* This operation will write the separate BGR planes directly to the
* objects in input_channels. */
cv::split(sample_normalized, *input_channels);
CHECK(reinterpret_cast<float*>(input_channels->at(0).data)
<< "Input channels are not wrapping the input layer of the network.";
void Classifier::Extract_Feature(const cv::Mat& img, vector<float>& feature)
input_layer->Reshape(1, num_channels_,
input_geometry_.height, input_geometry_.width);
net_->Reshape();
std::vector<cv::Mat> input_channels;
Preprocess(img, &input_channels);
char *query_blob_name = "fc6"; /* data, conv1, pool1, norm1, fc6, prob, etc */
boost::shared_ptr<Blob<float> > blob = net_->blobs()[blob_id];
const float *blob_ptr = (const float *)blob->cpu_data();
//ofile << *(blob_ptr + i) << " ";
}
//ofile << std::endl;
}
test.cpp
#include "Classifier.h"
using namespace cv;
int main(int argc, char** argv) {
std::cerr << "Usage: " << argv[0]
<< " deploy.prototxt network.caffemodel"
<< " mean.binaryproto labels.txt img.jpg" << std::endl;
string model_file = argv[1];
string trained_file = argv[2];
string label_file = argv[4];
Classifier classifier(model_file, trained_file, mean_file, label_file);
<< file << " ----------" << std::endl;
CHECK(!img.empty()) << "Unable to decode image " << file;
std::vector<Prediction> predictions = classifier.Classify(img);
for (size_t i = 0; i < predictions.size(); ++i) {
std::cout << std::fixed << std::setprecision(4) << p.second << " - \""
}
接下来再配置一下工程的一些路径:
1. 配置属性->调试->命令参数 添加./imagenet/deploy.prototxt ./imagenet/bvlc_alexnet.caffemodel ./imagenet/imagenet_mean.binaryproto ./imagenet/synset_words.txt ./imagenet/car.jpg
2. 配置属性->VC++目录->包含目录 添加自己的boost、caffe、cuda目录
3. 配置属性->VC++目录->库目录 添加相应的lib文件所在目录
4. 链接器->附加库目录
5. 链接器->输入->附加依赖项 添加:
caffelib.lib
ntdll.lib
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
comdlg32.lib
advapi32.lib
cudart.lib
cublas.lib
curand.lib
libprotobuf.lib
hdf5_tools.lib
hdf5_hl_fortran.lib
hdf5_fortran.lib
hdf5_hl_f90cstub.lib
hdf5_f90cstub.lib
hdf5_cpp.lib
hdf5_hl_cpp.lib
hdf5_hl.lib
hdf5.lib
zlib.lib
szip.lib
opencv_world300.lib
shlwapi.lib
leveldb.lib
cublas_device.lib
cuda.lib
libglog.lib
lmdb.lib
cudnn.lib
libopenblas.dll.a
libgflags.lib
这样应该就可以跑这个工程了。
程序的结果:
可以看到分类的前5名,还是非常准确的。
期间遇到的问题:
1. Check failed: registry.count(t ype) == 1 (0 vs. 1) Unknown layer type
这个问题纠结了好久。。。参考这个解决的:http://blog.youkuaiyun.com/fangjin_kl/article/details/50936952,就添加了head.h中的那几句话就行了
2. 如何提取fc6层的特征:
我在Classifier.cpp的Predict函数中注释了几行,那个是我提取fc6层特征并且将特征保存在文件里的代码,给大家作参考。
3. 一开始我以为不用自己再装一个boost,因为caffe-windows的第三方库里已经自带boost了,后来程序老提示我找不到boost的一个1.58版本的lib,我就自己配置了一下boost1.59然后没有那个错误了。