首先请参照我的上一篇博文,在windows下配置好caffe-windows这个过程不算太复杂。把ubuntu下的caffe环境也配置好,这样我们就可以在linux环境下训练网络,并且把训练好的网络放在windows下的vs工程中进行一次前馈来提取特征了。
在ubuntu的caffe根目录下把下面4个文件拷出来:新建头文件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
#include
#include
#include
#include
#include
#include
//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 Classify(const cv::Mat& img, int N = 5);
void Extract_Feature(const cv::Mat& img, vector& feature);
std::vector Predict(const cv::Mat& img);
void WrapInputLayer(std::vector* input_channels);
std::vector* input_channels);
shared_ptr > net_;
cv::Mat mean_;
std::vector 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(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* output_layer = net_->output_blobs()[0];
<< "Number of labels is different from the output layer dimension.";
static bool PairCompare(const std::pair& lhs,
return lhs.first > rhs.first;
/* Return the indices of the top N values of vector v. */
static std::vector Argmax(const std::vector& 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 Classifier::Classify(const cv::Mat& img, int N) {
N = std::min(labels_.size(), N);
std::vector 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 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 > & net, char *query_blob_name)
vector< string > const & blob_names = net->blob_names();
{
{
return i;
LOG(FATAL) << "Unknown blob name: " << str_query;
std::vector Classifier::Predict(const cv::Mat& img) {
input_layer->Reshape(1, num_channels_,
input_geometry_.height, input_geometry_.width);
net_->Reshape();
std::vector input_channels;
Preprocess(img, &input_channels);
//char *query_blob_name = "fc6"; /* data, conv1, pool1, norm1, fc6, prob, etc */
//boost::shared_ptr > 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* 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* 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(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& feature)
input_layer->Reshape(1, num_channels_,
input_geometry_.height, input_geometry_.width);
net_->Reshape();
std::vector input_channels;
Preprocess(img, &input_channels);
char *query_blob_name = "fc6"; /* data, conv1, pool1, norm1, fc6, prob, etc */
boost::shared_ptr > 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 predictions = classifier.Classify(img);
for (size_t i = 0; i < predictions.size(); ++i) {
std::cout << std::fixed << std::setprecision(4) << p.second << " - \""
}