资源简介
将此cpp替换掉vs2015编译好的caffe里的classification.cpp,输入不同的模型参数,最终效果很好

代码片段和文件信息
#include
#ifdef USE_OPENCV
#include
#include
#include
#endif // USE_OPENCV
#include
#include
#include
#include
#include
#include
using namespace caffe; // NOLINT(build/namespaces)
using std::string;
/* Pair (label confidence) representing a prediction. */
typedef std::pair Prediction;
class Classifier {
public:
Classifier(const string& model_file
const string& trained_file
const string& mean_file
const string& label_file);
std::vector Classify(const cv::Mat& img int N = 10);
private:
void SetMean(const string& mean_file);
std::vector Predict(const cv::Mat& img);
void WrapInputlayer(std::vector* input_channels);
void Preprocess(const cv::Mat& img
std::vector* input_channels);
private:
shared_ptr > net_;
cv::Size input_geometry_;
int num_channels_;
cv::Mat mean_;
std::vector labels_;
};
Classifier::Classifier(const string& model_file
const string& trained_file
const string& mean_file
const string& label_file) {
Caffe::set_mode(Caffe::GPU);
/* Load the network. */
net_.reset(new Net(model_file TEST));
net_->CopyTrainedlayersFrom(trained_file);
CHECK_EQ(net_->num_inputs() 1) << “Network should have exactly one input.“;
CHECK_EQ(net_->num_outputs() 1) << “Network should have exactly one output.“;
Blob* input_layer = net_->input_blobs()[0];
num_channels_ = input_layer->channels();
CHECK(num_channels_ == 3 || num_channels_ == 1)
<< “Input layer should have 1 or 3 channels.“;
input_geometry_ = cv::Size(input_layer->width() input_layer->height());
/* Load the binaryproto mean file. */
SetMean(mean_file);
/* Load labels. */
std::ifstream labels(label_file.c_str());
CHECK(labels) << “Unable to open labels file “ << label_file;
string line;
while (std::getline(labels line))
labels_.push_back(string(line));
Blob* output_layer = net_->output_blobs()[0];
CHECK_EQ(labels_.size() output_layer->channels())
<< “Number of labels is different from the output layer dimension.“;
}
static bool PairCompare(const std::pair& lhs
const std::pair& rhs) {
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) {
std::vector > pairs;
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);
std::vector result;
for (int i = 0; i < N; ++i)
result.push_back(pairs[i].second);
return result;
}
/* Return the top N predictions. */
std::vector Classifier::Classify(const cv::Mat&
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 8801 2018-06-30 16:05 myclassification.cpp
----------- --------- ---------- ----- ----
8801 1
相关资源
- res10_300x300_ssd_iter_140000.caffemodel与dep
- lmdb代码——caffe
- 基于CAFFE的人脸识别系统
- 测试工程源码1(一种基于深度学习的
- caffe图片资源转换数据库文件
- mnist-leveldb
- ( caffe-windows.zip )
- 中文车牌字符识别训练数据
- 复数版卷积神经网络,复数版CAFFE
- model.h5 model.json
- caffe-master官方
- caffe tutorial
- caffe-master.zip
- SSD源码的QT工程
- 无痛的机器学习 第一季
- 薛开宇caffe学习笔记完整版
- caffe深度学习薛开宇笔记-基于卷积神
- ilsvrc_2012_mean.npy
- Caffe官方教程中译本_CaffeCN社区翻译(
- 以性别预测为例,谈谈数据挖掘中常
- 手写数字识别10000次cnn结果 (.caffem
- 基于卷积神经网络的声音识别
- mnist的模型
- 深度学习---Caffe之经典模型详解与实战
- 编译好的Caffe2压缩包
- 格式转换后的mnist数据集
- Kaggle入门——猫狗大战
- caff-lenet5数据集
- caffe imagenet_mean.binaryproto
- caffe2安装文件
评论
共有 条评论