一、整体介绍
神经网络(net)是一个组合模型,它由许多相互连接的层(layers)组合而成,每一层又会涉及到很多参数(定义在caffe.proto文件中)。
Caffe就是组建深度网络的这样一种工具,它按照一定的策略,一层一层的搭建出自己的模型。它将所有的信息数据定义为blobs,从而进行便利的操作和通讯。
1.blob
blob是caffe框架中一种标准的数组,一种统一的内存接口,它详细描述了信息是如何存储的,以及如何在层之间通讯的。
2.layer
layer有多种类型,例如:Data,Python,Convolution,Pooling等,从bottom进行数据的输入,计算后通过top进行输出,layer之间的数据流动是以blobs的方式进行的。
每种layer都定义了三种关键计算:setup,forward,backward
- setup: 层的建立和初始化,以及在整个模型中的连接初始化。
- forward: 从bottom得到输入数据,进行计算,并将计算结果送到top,进行输出。
- backward: 从层的输出端top得到数据的梯度,计算当前层的梯度,并将计算结果送到bottom,向前传递。
这里以rpn/anchor_target_layer.py的部分代码为例,显而易见包含以上几个部分
class AnchorTargetLayer(caffe.Layer):
"""
Assign anchors to ground-truth targets. Produces anchor classification
labels and bounding-box regression targets.
"""
def setup(self, bottom, top):
layer_params = yaml.load(self.param_str_)
anchor_scales = layer_params.get('scales', (8, 16, 32))
self._anchors = generate_anchors(scales=np.array(anchor_scales))
self._num_anchors = self._anchors.shape[0]
self._feat_stride = layer_params['feat_stride']
# …………………………省略
def forward(self, bottom, top):
# Algorithm: