2018-01-11 caffe tutorial 笔记

原文

Blobs, Layers, and Nets: anatomy of a Caffe model

caffe使用自己的模式通过layer之间的连接定义网络(network),整个网络从底层到顶层分别代表data到loss。当数据(以张量的形式)从底层到上层前向或者反向传递的时候,caffe以BLOBS的形式存储、操作、传递这些数据:BLOB也就是caffe中数据处理的标准格式和框架的内存接口。

Blob storage and communication

BLOB就是一个N维矩阵,以C-contiguous fashion存储。其中信息包括caffe要处理的数据以及cpu和gpu的信息。具体信息包括模型参数、数据的batches、优化的导数(?)。

BLOB存储是从weight开始的,也就是 in a 4D blob, the value at index (n, k, h, w) is physically located at index ((n * K + k) * H + h) * W + w.

图片数据BLOB一般维度(如果是4维)n*k*h*w,而参数BLOB根据不同的层有不同的维度。For a convolution layer with 96 filters of 11 x 11 spatial dimension and 3 inputs the blob is 96 x 3 x 11 x 11.  For an inner product / fully-connected layer with 1000 output channels and 1024 input channels the parameter blob is 1000 x 1024.

由于我们经常对数据本身和数据的渐变感兴趣,因此BLOB中存储了这两部分。前部分就是传递的正常的数据,后一部分是网络计算过程中产生的梯度值。data and diff

Layer computation and connections

Layer是网络的基础,包括基本的运算比如filters, pool, take inner products, apply nonlinearities like rectified-linear and sigmoid and other elementwise transformations, normalize, load data, and compute losses like softmax and hinge. 

一个layer通过bottom connection 获取输入以及通过top connection得到输出


一个layer

Net definition and operation

一个神经网络可以看做一个有向无环图,有多个layer组成,一个典型的net以data layer(从硬盘中读数据)开始到loss layer结束,输出分类结果或者重建结果。

模型的定义是由prototxt文件,输出的是.caffemodel 的二进制文件。



Forward and Backward


forward and backward

forward:inference过程

backward: 根据loss计算梯度的过程



Loss

layer {

    name: "loss"

    type: "SoftmaxWithLoss"

    bottom: "pred"

    bottom: "label"

    top: "loss"

}

以softmaxWithLoss为例,top是一个空的标量,将整个batch的loss结果求平均值,其中loss是根据bottom中的inference得到的pred结果和ground truth lable的差值。

Loss weights

对于有多个layer都提供loss的网络需要loss权重来得到最后的loss值。比如使用softmax得到分类结果并且用EuclideanLoss reconstruct的网络,loss权重可以用来衡量这两个loss的重要性。

通常来说中间的layer只是被用来中间计算,但是只要加上loss_weight参数就可以被当做loss层来处理,计入最后loss值的计算。


此外有后缀“Loss”的top layer会被默认为loss_weight:1的layer,而非top layer如果带有后缀“Loss”的话会被认为loss_weight:0的layer

然后,在Caffe中最后的损失是通过对网络上的总加权损失进行计算




你可能感兴趣的:(2018-01-11 caffe tutorial 笔记)