资源简介
将此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
相关资源
- vgg16_caffe.pth
- resnet各种预训练模型
- Caffe/Pytorch转为TensorRT 4.0的
- faster_rcnn-master 直接运行即可,重新编
- FCN Caffe Net
- Windows下caffe安装详解
- 训练mask_rcnn所用配置文件
- Caffe编译的配置文件Makefile.config环境:
- 老版包含windows文件夹的caffe-windows库
- caffe下faster-rcnn的ResNet-50配置文件
- flownet2的网络定义文件
- Caffe-ssd的宽高比聚类
- 修改过的caffe编译时用到的build_win.c
- 【新版】【caffe】将图片转化为lmdb脚
- Caffe源码深入解析
- res10_300x300_ssd_iter_140000.caffemodel与dep
- synset_words
- caffe学习笔记1-7-完整版-薛开宇
- caffemodel的剪枝压缩(部分权重置0)
- caffe 自动生成traintxt 和 valtxt
- caffe.exe 产生0xc000007b错误的解决办法
- EfficientNet-Caffe.zip
- zlib-1.2.9
- caffe 加权交叉熵损失函数 SigmoidCross
- 将图片转化为lmdb脚本之直接运行版包
- Ubuntu16.04+cuda9.0+cudnn7.05+opencv3.4.0+caff
- caffe 绘制训练集和测试集的loss和acc
评论
共有 条评论