caffe训练出来的模型,进行分类测试
今天装修百科网给各位分享caffe 怎么训练的知识,其中也会对caffe训练出来的模型,进行分类测试(caffe实战)进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在我们开始吧!
caffe训练出来的模型,进行分类测试
你想调用你的模型,最简单的法是看examples/cpp_classification里面的cpp文件,那是教你如何调用caffe获取分类结果的…(你没接触过caffe的话,建议你直接按照这个文件来操作可能会比较简单,下面我的代码我也不知道没接触过caffe的人看起来难度会有多大)不过那个代码我看着不太习惯,所以之前自己稍微写了一个简易的版本,不知道怎么上传附件,懒人一个就直接把代码贴在最后了。先简单解释一下如何使用,把这个代码复制到一个头文件中,然后放在examples里面一个自己创建的文件夹里面,然后写一个main函数调用这个类就可以了,比如:复制,保存到caffe/examples/myproject/net_operator.hpp,然后同目录下写一个main.cpp,在main函数里面#include“net_operator.hpp”,就可以使用这个类了:conststringnet_prototxt=“…”;//你的网络的prototxt文件,用绝对路径,下面同理conststringpre_trained_file=“…”;//你训练好的。caffemodel文件conststringimg_path=“…”;//你要测试的图片路径//创建NetOperator对象NetOperatornet_operator(net_prototxt,pre_trained_file);Blob*blob=net_operator****cessImage(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>类型的,搜一下boostshared_ptr就知道跟普通指针有什么不同了好了,接下来是贴代码了:#include#include#include#include#include#include#include#include#includeusingnamespacecaffe;//NOLINT(build/namespaces)usingstd::string;classNetOperator{public:NetOperator(conststringnet_prototxt);NetOperator(conststringnet_prototxt,conststringtrained_file);~NetOperator(){}intbatch_size(){returnbatch_size_;}Blob*processImage(conststringimg_path,boolis_color=true);Blob*processImages(constvectorimg_paths,boolis_color=true);private:voidcreateNet(conststringnet_prototxt);//readtheimageandstoreitintheidxpositionofimagesintheblobvoidreadImageToBlob(conststringimg_path,intidx=0,boolis_color=true);shared_ptr>net_;cv::Sizeinput_geometry_;intbatch_size_;intnum_channels_;Blob*input_blob_;TransformationParametertransform_param_;shared_ptr>data_transformer_;Blobtransformed_data_;};NetOperator::NetOperator(conststringnet_prototxt){createNet(net_prototxt);}NetOperator::NetOperator(conststringnet_prototxt,conststringtrained_file){createNet(net_prototxt);net_->CopyTrainedLayersFrom(trained_file);}voidNetOperator::createNet(conststringnet_prototxt){#ifdefCPU_ONLYCaffe::set_mode(Caffe::CPU);#elseCaffe::set_mode(Caffe::GPU);#endifnet_.reset(newNet(net_prototxt,TEST));CHECK_EQ(net_->num_inputs(),1)《“Networkshouldhaveexactlyoneinput.”;CHECK_EQ(net_->num_outputs(),1)《“Networkshouldhaveexactlyoneoutput.”;Blob*input_layer=net_->input_blobs()[0];batch_size_=input_layer->num();num_channels_=input_layer->channels();CHECK(num_channels_==3||num_channels_==1)《“Inputlayershouldhave1or3channels.”;input_geometry_=cv::Size(input_layer->width(),input_layer->height());//reshapetheoutputshapeoftheDataTransformervectortop_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*NetOperator::processImage(conststringimg_path,boolis_color){//reshapethenetfortheinputinput_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();returnnet_->output_blobs()[0];}Blob*NetOperator::processImages(constvectorimg_paths,boolis_color){intimg_num=img_paths.size();//reshapethenetfortheinputinput_blob_=net_->input_blobs()[0];input_blob_->Reshape(img_num,num_channels_,input_geometry_.height,input_geometry_.width);net_->Reshape();for(inti=0;iForwardPrefilled();returnnet_->output_blobs()[0];}voidNetOperator::readImageToBlob(conststringimg_path,intidx,boolis_color){//readtheimageandresizetothetargetsizecv::Matimg;intcv_read_flag=(is_color?CV_LOAD_IMAGE_COLOR:CV_LOAD_IMAGE_GRAYSCALE);cv::Matcv_img_origin=cv::imread(img_path,cv_read_flag);if(!cv_img_origin.data){LOG(ERROR)《“Couldnotopenorfindfile”《img_path;return;}if(input_geometry_.height>0input_geometry_.width>0){cv::resize(cv_img_origin,img,input_geometry_);}else{img=cv_img_origin;}//transformtheimagetoablobusingDataTransformer//createaDataTransformerusingdefaultTransformationParameter(notransformation)data_transformer_.reset(newDataTransformer(transform_param_,TEST));data_transformer_->InitRand();//settheoutputofDataTransformertotheidximageoftheinputblobintoffset=input_blob_->offset(idx);this->transformed_data_.set_cpu_data(input_blob_->mutable_cpu_data()+offset);//transformtheinputimagedata_transformer_->Transform(img,(this->transformed_data_));}
如何用caffe训练图像分类深度学习模型
深度学习的概念源于人工神经网络的研究。含多隐层的多层感知器就是一种深度学习结构。深度学习通过组合低层特征形成更加抽象的高层表示属性类别或特征,以发现数据的分布式特征表示。
深度学习的概念由Hinton等人于2006年提出。基于深度置信网络(DBN)提出非监督贪心逐层训练算法,为解决深层结构相关的优化难题带来希望,随后提出多层自动编码器深层结构。此外Lecun等人提出的卷积神经网络是第一个真正多层结构学习算法,它利用空间相对关系减少参数数目以提高训练性能。
深度学习是机器学习研究中的一个新的领域,其动机在于建立、模拟人脑进行分析学习的神经网络,它模仿人脑的机制来解释数据,例如图像,声音和文本。
同机器学习方法一样,深度机器学习方法也有监督学习与无监督学习之分.不同的学习框架下建立的学习模型很是不同.例如,卷积神经网络(Convolutional neural networks,简称CNNs)就是一种深度的监督学习下的机器学习模型,而深度置信网(Deep Belief Nets,简称DBNs)就是一种无监督学习下的机器学习模型。

如何利用Caffe训练ImageNet分类网络
训练配置:batchsize=128
caffe自有的imagenet with cuDNN模型快于googlenet with cuDNN
VGG16层不用cuDNN慢于caffe自有的imagenet with cuDNN模型
VGG19层不用cuDNN慢于caffe自有的imagenet with cuDNN模型
一、CAFFE 自带配置,使用cuDNN
Forward速度 : 220ms
Backward速度 :360ms
二、CAFFE 自带配置,不使用cuDNN
Forward速度 : 300ms
Backward速度 :410ms
三、GoogleNet,使用cuDNN
Forward速度 : 614ms
Backward速度 :1377ms
四、GoogleNet,不使用cuDNN
Forward速度 : 1145ms
Backward速度 :2009ms
五、VGG16层,使用cuDNN
Forward速度 : 3101ms
Backward速度 :8002ms
六、VGG19层,使用cuDNN
Forward速度 : 3972ms
Backward速度 :8540ms
回答不容易,希望能帮到您,满意请帮忙采纳一下,谢谢 !
如何使用caffe训练自己的数据
很简单
如何使用caffe进行训练的命令
写完main.cppcaffe目录make编译写文件应执行文件比按我面写makecaffe/build/examples/myproject文件夹面main.bin执行文件
如何使用caffe for windows 训练cifar
1 cifar10数据库
60000张32*32 彩**片 共10类
50000张训练
10000张测试
下载cifar10数据库
这是binary格式的,所以我们要把它转换成leveldb格式。
2 在../caffe-windows/examples/cifar10文件夹中有一个 convert_cifar_data.cpp
将他include到MainCaller.cpp中。如下:
编译....我是一次就通过了 ,在bin文件夹里出现convert_cifar_data****。然后 就可以进行格式转换。binary→leveldb
可以在bin文件夹下新建一个input文件夹。将cifar10.binary文件放在input文件夹中,这样转换时就不用写路径了。
cmd进入bin文件夹
执行后,在output文件夹下有cifar_train_leveldb和cifar_test_leveldb两个文件夹。里面是转化好的leveldb格式数据。
当然,也可以写一个bat文件处理,方便以后再次使用。
3 下面我们要求数据图像的均值
编译../../tools/comput_image_mean.cpp
编译成功后。接下来求mean
cmd进入bin。
执行后,在bin文件夹下出现一个mean.binaryproto文件,这就是所需的均值文件。
4 训练cifar网络
在.../examples/cifar10文件夹里已经有网络的配置文件,我们只需要将cifar_train_leveldb和cifar_test_leveldb两个文件夹还有mean.binaryproto文件拷到cifar0文件夹下。
修改cifar10_quick_train****totxt中的source: "cifar-train-leveldb" mean_file: "mean.binaryproto" 和cifar10_quick_test****totxt中的source: "cifar-test-leveldb"
mean_file: "mean.binaryproto"就可以了,
后面再训练就类似于MNIST的训练。写一个train_quick.bat,内容如下:
[plain] view plaincopy
copy ..\\..\\bin\\MainCaller**** ..\\..\\bin\\train_net****
SET GLOG_logtostderr=1
"../../bin/train_net****" cifar10_quick_solver****totxt
pause
先编译一遍 train_net.cpp
运行train_quick.bat
如何利用Caffe训练ImageNet分类网络
训练配置:batchsize=128
caffe自有的imagenet with cuDNN模型快于googlenet with cuDNN
VGG16层不用cuDNN慢于caffe自有的imagenet with cuDNN模型
VGG19层不用cuDNN慢于caffe自有的imagenet with cuDNN模型
一、CAFFE 自带配置,使用cuDNN
Forward速度 : 220ms
Backward速度 :360ms
二、CAFFE 自带配置,不使用cuDNN
Forward速度 : 300ms
Backward速度 :410ms
三、GoogleNet,使用cuDNN
Forward速度 : 614ms
Backward速度 :1377ms
四、GoogleNet,不使用cuDNN
Forward速度 : 1145ms
Backward速度 :2009ms
五、VGG16层,使用cuDNN
Forward速度 : 3101ms
Backward速度 :8002ms
六、VGG19层,使用cuDNN
Forward速度 : 3972ms
Backward速度 :8540ms
回答不容易,希望能帮到您,满意请帮忙采纳一下,谢谢 !
深度学习中利用caffe如何训练自己的模型
作者:圣行
链接:https://****zhihu***m/question/30091667/answer/47951446
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
matlab 和python没有用过。如果是习惯用opencv的话,可以使用memory_data,请参考这个链接里的例子:C++ Image Classification with memory_data_param · Issue #1443 · BVLC/caffe · GitHub
给一个具体点的例子吧(不知道贴代码是不是有点不合知乎气质?),总共分三步:
第一步,构造网络:
enum Phase p = TEST;
Net caffe_test_net(argv[1],p);
caffe_test_net.CopyTrainedLayersFrom(argv[2]);
第二步,构造数据并加入到网络输入层:
//create the input data
vector md_images;
vector md_labels;
//////operations for the input data
Mat original = imread("images\\lena_gray.png"); //随便的图片,没有实用意义,可忽略
Mat *sub_img = new Mat;
for (int i = 0; i < 10; i++){
original(Range(i, i + 28), Range(i, i + 28))***pyTo(*sub_img); // 28x28,可以直接用lenet
md_images.push_back(*sub_img);
md_labels.push_back(0);
}
第三步,执行test操作:
for (int i = 0; i < 10; i++){
const vector*>& result = caffe_test_net.ForwardPrefilled();
如何调用训练好的caffemodel
你想调用你的模型,最简单的办法是看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 *blob = net_operator****cessImage(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 >类型的,搜一下boost shared_ptr就知道跟普通指针有什么不同了
好了,接下来是贴代码了:
#include
#include
#include
#include
#include
#include
#include
#include
#include
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* processImage(const string &img_path, bool is_color = true);
Blob* processImages(const vector &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_;
cv::Size input_geometry_;
int batch_size_;
int num_channels_;
Blob* input_blob_;
TransformationParameter transform_param_;
shared_ptr > data_transformer_;
Blob 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(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* 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 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* 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* NetOperator::processImages(const vector &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(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训练图像分类深度学习模型
深度学习的概念源于人工神经网络的研究。含多隐层的多层感知器就是一种深度学习结构。深度学习通过组合低层特征形成更加抽象的高层表示属性类别或特征,以发现数据的分布式特征表示。[1]
深度学习的概念由Hinton等人于2006年提出。基于深度置信网络(DBN)提出非监督贪心逐层训练算法,为解决深层结构相关的优化难题带来希望,随后提出多层自动编码器深层结构。此外Lecun等人提出的卷积神经网络是第一个真正多层结构学习算法,它利用空间相对关系减少参数数目以提高训练性能。[1]
深度学习是机器学习研究中的一个新的领域,其动机在于建立、模拟人脑进行分析学习的神经网络,它模仿人脑的机制来解释数据,例如图像,声音和文本。[2]
同机器学习方法一样,深度机器学习方法也有监督学习与无监督学习之分.不同的学习框架下建立的学习模型很是不同.例如,卷积神经网络(Convolutional neural networks,简称CNNs)就是一种深度的监督学习下的机器学习模型,而深度置信网(Deep Belief Nets,简称DBNs)就是一种无监督学习下的机器学习模型。