caffe
训练网络模型一般直接使用的caffe.bin
: caffe train -solver solver.prototxt
,其实这个命令的本质也是调用c++的Solver
.
本文给出使用纯c++
代码,使用mnist数据
+多层感知机网络
,训练数字分类问题。然后用C++
调用训练好的模型测试分类。
solver.prototxt
文件内容如下:
net: "/home/xy/caffe_analysis/my_caffe/example/lenet.prototxt"
test_iter: 1000
test_interval: 100
base_lr: 0.001
lr_policy: "step"
gamma: 0.1
stepsize: 5000
display: 1000
max_iter: 20000
momentum: 0.9
weight_decay: 0.0005
snapshot: 10000
snapshot_prefix: "model"
solver_mode: CPU
注:网络名称虽然为lenet.prototxt,实际是多层感知机。
训练网络如下:
name: "LeNet"
layer {
name: "mnist"
#name: "data"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
transform_param {
scale: 0.00390625
}
data_param {
source: "/home/xy/caffe-master/examples/mnist/mnist_train_lmdb"
batch_size: 64
backend: LMDB
}
}
layer {
name: "mnist"
type: "Data"
top: "data"
top: "label"
include {
phase: TEST
}
transform_param {
scale: 0.00390625
}
data_param {
source: "/home/xy/caffe-master/examples/mnist/mnist_test_lmdb"
batch_size: 100
backend: LMDB
}
}
layer {
name: "fc1"
type: "InnerProduct"
# learning rate and decay multipliers for the weights
param { lr_mult: 1 decay_mult: 1 }
# learning rate and decay multipliers for the biases
param { lr_mult: 2 decay_mult: 0 }
inner_product_param {
num_output: 300
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
value: 0
}
}
bottom: "data"
top: "fc1"
}
layer {
name: "relu1"
type: "ReLU"
bottom: "fc1"
top: "fc1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "fc1"
top: "fc2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "accuracy"
type: "Accuracy"
bottom: "fc2"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}
}
layer {
name: "loss"
type: "SoftmaxWithLoss"
bottom: "fc2"
bottom: "label"
top: "loss"
}
训练代码如下:
google::InitGoogleLogging("test_solver");
FLAGS_alsologtostderr = 1;
string base_dir = "/home/xy/caffe_analysis/my_caffe/example/";
string path = base_dir + "mlp_solver.prototxt";
SGDSolver solver(path);
// resuming from trained weights, 也可以加载已经训练好的模型
string trained_weight_path = base_dir + "model_iter_20000.caffemodel";
solver.net()->CopyTrainedLayersFrom(trained_weight_path);
// 开始优化
solver.Solve();
训练过程如下:
接下来测试训练模型的效果的,代码如下:
void test_net(){
Caffe::set_mode(Caffe::CPU);
string base_dir = "/home/xy/caffe_analysis/my_caffe/example/";
string net_path = base_dir + "lenet_deploy.prototxt";
string trained_path = base_dir + "model_iter_20000.caffemodel";
Net net(net_path, Phase::TEST);
net.CopyTrainedLayersFrom(trained_path);
CHECK_EQ(net.num_inputs(), 1) << "Network should have exactly one input.";
CHECK_EQ(net.num_outputs(), 1) << "Network should have exactly one output.";
// read Datum from file
string datum_path = base_dir + "0.proto";
Datum datum;
ReadProtoFromBinaryFileOrDie(datum_path, &datum);
// Datum convert
TransformationParameter parameter;
parameter.set_scale(0.00390625);
DataTransformer dataTransformer(parameter, Phase::TEST);
Blob* input_layer = net.input_blobs()[0];
// input_layer->Reshape(1, 1, 28, 28);//no need
dataTransformer.Transform(datum, input_layer);
// net.Reshape();
net.Forward();
Blob* output_layer = net.output_blobs()[0];
const float* begin = output_layer->cpu_data();
const float* end = begin + output_layer->channels();
std::vector ret(begin, end);
for(auto item:ret)cout<
运行输出如下:可以看到0的概率最大。
测试inference的网络定义如下:
name: "LeNet"
layer {
name: "data"
type: "Input"
top: "data"
input_param { shape: { dim: 1 dim: 1 dim: 28 dim: 28 } }
}
layer {
name: "fc1"
type: "InnerProduct"
# learning rate and decay multipliers for the weights
param { lr_mult: 1 decay_mult: 1 }
# learning rate and decay multipliers for the biases
param { lr_mult: 2 decay_mult: 0 }
inner_product_param {
num_output: 300
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
value: 0
}
}
bottom: "data"
top: "fc1"
}
layer {
name: "relu1"
type: "ReLU"
bottom: "fc1"
top: "fc1"
}
layer {
name: "ip2"
type: "InnerProduct"
bottom: "fc1"
top: "fc2"
param {
lr_mult: 1
}
param {
lr_mult: 2
}
inner_product_param {
num_output: 10
weight_filler {
type: "xavier"
}
bias_filler {
type: "constant"
}
}
}
layer {
name: "prob"
type: "Softmax"
bottom: "fc2"
top: "prob"
}
caffe
系列源码分析介绍本系列深度学习框架caffe
源码分析主要内容如下:
caffe源码分析-cmake 工程构建主要内容:
自己从头构建一遍工程,这样能让我更好的了解大型的项目的构建。当然原始的caffe的构建感觉还是比较复杂(主要是cmake),我这里仅仅使用cmake构建,而且简化点,当然最重要的是支持CLion直接运行调试(如果需要这个工程可以评论留下你的邮箱,我给你发送过去)。
SyncedMemory
, 以及类Blob
数据传输的媒介.主要内容:
caffe源码分析-SyncedMemory
caffe源码分析-Blob
其中Blob
分析给出了其直接与opencv的图片相互转化以及操作,可以使得我们更好的理解Blob
.
layer
的源码分析,包括从整体上说明了layer
类别以及其proto定义与核心函数.内容如下:
caffe源码分析-layer
caffe源码分析-ReLULayer
caffe源码分析-inner_product_layer
caffe源码分析-layer_factory
首先分析了最简单的layer
Relu
,然后在是inner_product_layer全连接层
, 最后是layer_factory
caffe中 以此工厂模式create各种Layer.
内容如下:
caffe源码分析-BlockingQueue
caffe源码分析-InternalThread
caffe源码分析-DataReader
内容如下:
caffe源码分析-DataTransformer
caffe源码分析-db, io
内容如下:
caffe c++示例(mnist 多层感知机c++训练,测试)
类似与caffe
一样按照layer、solver、loss、net
等模块构建的神经网络实现可以见下面这篇blog,相信看懂了这个python的代码理解caffe框架会更简单点.
神经网络python实现
最后如果需要cmake
+ CLion
直接运行调试caffe
的代码工程,可以评论留下你的邮箱,我给你发送过去.