![](https://i-blog.csdnimg.cn/blog_migrate/7e104c7692a0d091edde5021e6b29ce0.png)
Caffe入门与实践-简介
目录:
- 一,整体结构
- 二,接口
- 三,使用流程
- 四,实战案例
一,整体结构
神经网络一般包括:训练,测试两大阶段。
按照 李沐 的说法,训练: 就是把训练数据(原料)和 神经网络模型:如AlexNet(丹方) “倒进” 神经网络训练框架例如cafffe,Mxnet(炼丹炉)然后用 CPU或GPU(真火) “提炼出”模型参数(仙丹)的过程。测试: 就是把 测试数据 用 训练好的模型(神经网络模型 + 模型参数)跑一跑 看看结果如何。
作为炼丹炉之一的caffe,就是把炼制过程所涉及的概念做抽象,形成一套体系。总的来讲,由低到高依次把 网络中的数据抽象成Blob, 各层网络抽象成 Layer ,整个网络抽象成Net,网络模型的求解方法 抽象成 Solver。
- Blob 主要用来表示网络中的数据,包括训练数据,网络各层自身的参数,网络之间传递的数据都是通过 Blob 来实现的,同时 Blob 数据也支持在 CPU 与 GPU 上存储,能够在两者之间做同步。
- Layer 是对神经网络中各种层的一个抽象,包括我们熟知的卷积层和下采样层,还有全连接层和各种激活函数层等等。同时每种 Layer 都实现了前向传播和反向传播,并通过 Blob 来传递数据。
- Net 是对整个网络的表示,由各种 Layer 前后连接组合而成,也是我们所构建的网络模型。
- Solver 定义了针对 Net 网络模型的求解方法,记录网络的训练过程,保存网络模型参数,中断并恢复网络的训练过程。自定义 Solver 能够实现不同的网络求解方式。
上段内容来自:http://blog.luoyetx.com/2015/10/reading-caffe-1/
二,接口
一个系统必须提供方便人类使用的人机交互的接口。而当前可选接口无非是那么几种: 命令行,配置文件,GUI,编程语言API,web API(例如RESTful) 等等。Caffe提供了三大接口。 命令行(cmdcaffe ),python API(pycaffe),matlab API (matcaffe)。
命令行(Command Line):
命令行简单高效,配合一下配置文件就可以向机器完美表达人类的意图。
训练: solver.prototxt 是网络求解文件,由它定义 一些网络训练参数和网络结构文件路径等。
# 训练示例 (参数: 求解文件)
caffe train -solver examples/mnist/lenet_solver.prototxt
# 从训练一半的模型快照中恢复训练 (参数:求解文件 快照)
caffe train -solver examples/mnist/lenet_solver.prototxt -snapshot examples/mnist/lenet_iter_5000.solversta
# 由其它训练好的模型 fine-tune (参数:求解文件 其它训练好的模型参数)
caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
测试:
# score the learned LeNet model on the validation set as defined in the
# model architeture lenet_train_test.prototxt
# 测试 (参数: 求解文件 训练好的模型参数 )
caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100
注意:网络结构必须定义输出精度或者输出损失作为结果。
Python:
- http://caffe.Net is the central interface for loading, configuring, and running models. caffe.Classifierand caffe.Detector provide convenience interfaces for common tasks.
- caffe.SGDSolver exposes the solving interface.
- http://caffe.io handles input / output with preprocessing and protocol buffers.
- caffe.draw visualizes network architectures.
- Caffe blobs are exposed as numpy ndarrays for ease-of-use and efficiency.
举例:
caffe.set_mode_cpu() #设置cpu模式 caffe.set_device(0) #设置GPU模式 caffe.set_mode_gpu() net = caffe.Net('conv.prototxt', caffe.TEST) #加载网络 net.blobs #for input data and its propagation in the layers net.params #a vector of blobs for weight and bias parameters net.forward() #前向传播 net.save('mymodel.caffemodel') #保存模型参数
具体请参考官网:http://caffe.berkeleyvision.org/tutorial/interfaces.html
MATLAB:
具体请参考官网:http://caffe.berkeleyvision.org/tutorial/interfaces.html
三,使用流程
1、数据格式处理,把原始图片处理成caffe支持的如下格式之一:
- 数据库格式 (LEVELDB or LMDB) $CAFFEROOT/build/tools/convert_imageset 可以用来做把原始图片转换为LevelDB或者 Lmdb格式。
- 内存数据
- HDF5数据
- 图像数据
-
Windows
-
Dummy
参考:http://caffe.berkeleyvision.org/tutorial/layers.html#data-layers
2. 编写网络结构文件 ( .prototxt)
作用就是定义网络结构: 例如 caffe/examples/mnist/lenet_train_test.prototxt 定义了下图的结构:
3、网络求解文件 (.prototxt)
定义了网络模型训练过程中需要设置的参数,比如学习率,权重衰减系数,迭代次数,使用GPU还是CP等。
示例: caffe/examples/mnist/lenet_solver.prototxt
# The train/test net protocol buffer definition
net: "examples/mnist/lenet_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: CPU
其中训练网络和测试网络的定义有两种方式:
方式一: 在solver.prototxt 文件中分别定义训练网络和测试网络
train_net: "examples/hdf5_classification/nonlinear_auto_train.prototxt" test_net: "examples/hdf5_classification/nonlinear_auto_test.prototxt"
net: "examples/mnist/lenet_train_test.prototxt"
layer { name: "data" type: "Data" top: "data" top: "label" include { phase: TRAIN } data_param { source: "examples/imagenet/ilsvrc12_train_lmdb" batch_size: 256 backend: LMDB } } layer { name: "data" type: "Data" top: "data" top: "label" top: "label" include { phase: TEST } data_param { source: "examples/imagenet/ilsvrc12_val_lmdb" batch_size: 50 backend: LMDB } }
4. 训练
基于命令行的训练:
# 训练示例 (参数: 求解文件) caffe train -solver examples/mnist/lenet_solver.prototxt # 从训练一半的模型快照中恢复训练 (参数:求解文件 快照) caffe train -solver examples/mnist/lenet_solver.prototxt -snapshot examples/mnist/lenet_iter_5000.solversta # 由其它训练好的模型 fine-tune (参数:求解文件 其它训练好的模型参数) caffe train -solver examples/finetuning_on_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
基于Python和Matlab的训练请看官网。
5.测试
基于命令行测试:
# score the learned LeNet model on the validation set as defined in the # model architeture lenet_train_test.prototxt # 测试 (参数: 求解文件 训练好的模型参数 ) caffe test -model examples/mnist/lenet_train_test.prototxt -weights examples/mnist/lenet_iter_10000.caffemodel -gpu 0 -iterations 100
基于python和Matlab的测试请看官网和后续实战案例。