如何调用caffe已经训练好的net

author author     2023-03-19     470

关键词:

参考技术A 关于准确率一直是0.5:

我不了解你是怎么训练的,但是对于二分类问题最后准确率是0.5,有可能是因为最终结果都分到同一类上面去了,这个你需要调用caffe看看网络最终的输出来判断
对于二分类问题,一般训练的时候,正负样本要混合在一起训练(一般在输入层那里设置shuffle来做到),如果没有shuffle,并且你的输入数据正好是前面的是正样本,后面的是负样本,那么就会造成你的网络最开始一直倾向于输出1,在输入一直是正样本的时候,loss的确会下降,然后到负样本的时候,loss一下子跳很高,由于后面全是负样本,网络训练使得它倾向于输出0,就又慢慢降下来了,最终你的网络基本上就是输出值都是1或者都是0了
如果是这种情况,可以在输入层那里设置shuffle为true
当然具体情况具体分析,我也不了解你的情况,只是告诉你有这个可能而已
=====================================================
以下是关于caffe的回答:
你想调用你的模型,最简单的办法是看examples/cpp_classification里面的cpp文件,那是教你如何调用caffe获取分类结果的...(你没接触过caffe的话,建议你直接按照这个文件来操作可能会比较简单,下面我的代码我也不知道没接触过caffe的人看起来难度会有多大)
不过那个代码我看着不太习惯,所以之前自己稍微写了一个简易的版本,不知道怎么上传附件,懒人一个就直接把代码贴在最后了。
先简单解释一下如何使用,把这个代码复制到一个头文件中,然后放在examples里面一个自己创建的文件夹里面,然后写一个main函数调用这个类就可以了,比如:
复制,保存到caffe/examples/myproject/net_operator.hpp,然后同目录下写一个main.cpp,在main函数里面#include "net_operator.hpp",就可以使用这个类了:
const string net_prototxt = "..."; // 你的网络的prototxt文件,用绝对路径,下面同理
const string pre_trained_file = "..."; // 你训练好的.caffemodel文件
const string img_path = "..."; // 你要测试的图片路径
// 创建NetOperator对象
NetOperator net_operator(net_prototxt, pre_trained_file);
Blob<float> *blob = net_operator.processImage(img_path);
// blob就得到了最后一层的输出结果,至于blob里面是怎么存放数据的,你需要去看看官网对它的定义
写完main.cpp之后,到caffe目录下,make,然后它会编译你写的文件,对应生成的可执行文件。比如按我上面写的那样,make之后就会在caffe/build/examples/myproject文件夹里面生成一个main.bin,执行这个文件就可以了。因为生成的可执行文件并不是直接在代码目录下,所以前面我建议你写的路径用绝对路径
另外如果你要获取的不是最后一层的输出,你需要修改一下processImage函数的返回值,通过NetOperator的成员变量net_来获取你需要的blob,比如有个blob名称为"label",你想获取这个blob,可以通过net_->blob_by_name("label")来获取,当然获取到的是shared_ptr<Blob<float> >类型的,搜一下boost shared_ptr就知道跟普通指针有什么不同了
好了,接下来是贴代码了:
#include <caffe/caffe.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iosfwd>
#include <memory>
#include <string>
#include <utility>
#include <vector>
using namespace caffe; // NOLINT(build/namespaces)
using std::string;
class NetOperator

public:
NetOperator(const string& net_prototxt);
NetOperator(const string& net_prototxt, const string& trained_file);
~NetOperator()
int batch_size() return batch_size_;
Blob<float>* processImage(const string &img_path, bool is_color = true);
Blob<float>* processImages(const vector<string> &img_paths, bool is_color = true);
private:
void createNet(const string& net_prototxt);
// read the image and store it in the idx position of images in the blob
void readImageToBlob(const string &img_path, int idx = 0, bool is_color = true);
shared_ptr<Net<float> > net_;
cv::Size input_geometry_;
int batch_size_;
int num_channels_;
Blob<float>* input_blob_;
TransformationParameter transform_param_;
shared_ptr<DataTransformer<float> > data_transformer_;
Blob<float> transformed_data_;
;
NetOperator::NetOperator(const string& net_prototxt)
createNet(net_prototxt);

NetOperator::NetOperator(const string& net_prototxt, const string& trained_file)
createNet(net_prototxt);
net_->CopyTrainedLayersFrom(trained_file);

void NetOperator::createNet(const string& net_prototxt)
#ifdef CPU_ONLY
Caffe::set_mode(Caffe::CPU);
#else
Caffe::set_mode(Caffe::GPU);
#endif
net_.reset(new Net<float>(net_prototxt, 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.";
Blob<float>* input_layer = net_->input_blobs()[0];
batch_size_ = input_layer->num();
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());
// reshape the output shape of the DataTransformer
vector<int> top_shape(4);
top_shape[0] = 1;
top_shape[1] = num_channels_;
top_shape[2] = input_geometry_.height;
top_shape[3] = input_geometry_.width;
this->transformed_data_.Reshape(top_shape);

Blob<float>* NetOperator::processImage(const string &img_path, bool is_color)
// reshape the net for the input
input_blob_ = net_->input_blobs()[0];
input_blob_->Reshape(1, num_channels_,
input_geometry_.height, input_geometry_.width);
net_->Reshape();
readImageToBlob(img_path, 0, is_color);
net_->ForwardPrefilled();
return net_->output_blobs()[0];

Blob<float>* NetOperator::processImages(const vector<string> &img_paths, bool is_color)
int img_num = img_paths.size();
// reshape the net for the input
input_blob_ = net_->input_blobs()[0];
input_blob_->Reshape(img_num, num_channels_,
input_geometry_.height, input_geometry_.width);
net_->Reshape();
for (int i=0; i<img_num; i++)
readImageToBlob(img_paths[i], i, is_color);

net_->ForwardPrefilled();
return net_->output_blobs()[0];

void NetOperator::readImageToBlob(const string &img_path, int idx, bool is_color)
// read the image and resize to the target size
cv::Mat img;
int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
CV_LOAD_IMAGE_GRAYSCALE);
cv::Mat cv_img_origin = cv::imread(img_path, cv_read_flag);
if (!cv_img_origin.data)
LOG(ERROR) << "Could not open or find file " << img_path;
return ;

if (input_geometry_.height > 0 && input_geometry_.width > 0)
cv::resize(cv_img_origin, img, input_geometry_);
else
img = cv_img_origin;

// transform the image to a blob using DataTransformer
// create a DataTransformer using default TransformationParameter (no transformation)
data_transformer_.reset(
new DataTransformer<float>(transform_param_, TEST));
data_transformer_->InitRand();
// set the output of DataTransformer to the idx image of the input blob
int offset = input_blob_->offset(idx);
this->transformed_data_.set_cpu_data(input_blob_->mutable_cpu_data() + offset);
// transform the input image
data_transformer_->Transform(img, &(this->transformed_data_));

caffe学习

caffe学习官网notebookexample第一个样例:给一张图片,然后调用已经训练好的caffemodel(其实里边存的是网络的参数),通过深度学习的网络来给这张图片分类 查看详情

如何使用opencvdnn模块调用caffe预训练模型?(代码片段)

QStringmodelPrototxt="D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt";QStringmodelBin="D:\\Qt\\qmake\\CaffeModelTest\\caffe\\snapshot_iter_10000.caffemodel";QStringimageFile= 查看详情

caffec++中使用训练好的caffe模型,classification工程生成动态链接库——caffe学习六(代码片段)

...因,于是还是考虑将classification.cpp写成库供别的程序调用。1.配置环境。新建工程切换到 查看详情

caffe训练好的网络对图像分类

 对于训练好的Caffe网络 输入:彩色or灰度图片 做minist下手写识别分类,不能直接使用,需去除均值图像,同时将输入图像像素归一化到0-1直接即可。              #incl... 查看详情

如何利用bing算法训练自己的模型

参考技术A  在MNIST调用已经训练好的模型,测试。  这个测试,假定可能是新加入的测试集,还是按照原来的需求转换,存放数据到指定的位置。  ./build/tools/caffe.bintest-model=examples/mnist/lenet_train_test.prototxt-weights=examples/mnis... 查看详情

caffe上用ssd训练和测试自己的数据

....com/weiliu89/caffe.git下载caffe-ssd代码,是一个caffe文件夹2.从已经配置好的caffe目录下拷贝一个Makefile.config放到$ 查看详情

caffe绘制训练过程的loss和accuracy曲线

...写代码记录训练过程的数据,那就太麻烦了,caffe中其实已经自带了这样的小工具caffe-master/tools/extra/parse_log.sh caffe-master/too 查看详情

caffe初步实践---------使用训练好的模型完成语义分割任务

caffe刚刚安装配置结束,乘热打铁!(一)环境准备前面我有两篇文章写到caffe的搭建,第一篇cpuonly,第二篇是在服务器上搭建的,其中第二篇因为硬件环境更佳我们的步骤稍显复杂。其实,第二篇也仅仅是caffe的初步搭建完成... 查看详情

怎样用自己的数据集对caffe训练好的model进行fineture

...行修改,使之符合自己的需求。最难的就是从零开始设计训练网络模型。题主可以体验一下从零开始设计训练模型,但是不要陷太深。如果题主设计了很好的网络模型,欢迎分享啊根据您的数据量,强烈建议finetune 查看详情

Caffe 如何确定测试集的准确性?

】Caffe如何确定测试集的准确性?【英文标题】:HowdoesCaffedeterminetestsetaccuracy?【发布时间】:2017-05-0323:53:39【问题描述】:使用BVLC参考AlexNet文件,我一直在针对我创建的训练集训练CNN。为了衡量训练的进度,我一直在使用一种... 查看详情

caffe训练resume

 MODEL=${EXP}/model/${NET_ID}/pspnet101_VOC2012.caffemodelSNAPSHOT=${EXP}/model/${NET_ID}/train_iter_7000.solverstateCMD="${CAFFE_BIN}train--solver=${CONFIG_DIR}/solver.prototxt--gpu=${DEV_ID}"#C 查看详情

caffe+ssd网络训练过程

參考博客:https://blog.csdn.net/xiao_lxl/article/details/791068371获取源代码:gitclonehttps://github.com/weiliu89/caffe.git2进入目录中:cdcaffe3,gitcheckoutssd主要参考https://github.com/weiliu89/caffe/tree/ssd   获取SSD的代码,下载完成后有一个caffe... 查看详情

我如何从两个已经训练好的分类器中构建一个分类器?

】我如何从两个已经训练好的分类器中构建一个分类器?【英文标题】:Howdoibuildaclassifieroutoftwoalreadytrainedclassifiers?【发布时间】:2018-12-2911:41:19【问题描述】:我想将文本分类为正面、负面或中性。因此,我构建了两个不同的S... 查看详情

如何利用c++来调用pytorch训练好的模型(代码片段)

...版本,新增的功能让模型部署变得更为地简单,本文记录如何利用C++来调用PyTorch训练好的模型,其实也是利用官方强大的LibTorch库。LibTorch的安装虽然说安装,其实就是下载官方的LibTorch包而已,从官方网站中选择PyTorch(1.1),libto... 查看详情

如何用tensorflow训练模型成pb文件和和如何加载已经训练好的模型文件

参考技术A基本使用使用TensorFlow,你必须明白TensorFlow:使用图(graph)来表示计算任务.在被称之为会话(Session)的上下文(context)中执行图.使用tensor表示数据.通过变量(Variable)维护状态.使用feed和fetch可以为任意的操作(arb... 查看详情

如何将经过训练的 caffe 模型以 h5 格式加载到 c++ caffe 网络?

】如何将经过训练的caffe模型以h5格式加载到c++caffe网络?【英文标题】:Howtoloadtrainedcaffemodelinh5formattoc++caffenet?【发布时间】:2017-07-0907:38:28【问题描述】:正常训练的caffe模型是.caffemodel扩展名,实际上它们是binaryprotobuf格式。... 查看详情

caffe这个c++工程的目录结构

...是用于生成二进制处理程序的,caffe在训练时实际是直接调用这些二进制文件。includeCaffe的实现代码的头文件 查看详情

如何在 caffe 中训练/测试我自己的数据集?

】如何在caffe中训练/测试我自己的数据集?【英文标题】:Howtotraining/testingmyowndatasetincaffe?【发布时间】:2016-03-1820:53:55【问题描述】:我从Caffe开始,mnist示例运行良好。我的火车和标签数据为data.mat。(我有300个训练数据,其... 查看详情