caffe 相关--Blobs, Layers, and Nets: anatomy of a Caffe model

来源:互联网 发布:scott disick 知乎 编辑:程序博客网 时间:2024/06/06 02:54

Deep learning tutorial on Caffe technology : basic commands, Python and C++ code.

http://christopher5106.github.io/deep/learning/2015/09/04/Deep-learning-tutorial-on-Caffe-Technology.html

Forward and backward

Forward pass can be done using net.forward or net.forward_prefilled. Function net.forward takes in a cell array of N-D arrays containing data of input blob(s) and outputs a cell array containing data from output blob(s). Function net.forward_prefilled uses existing data in input blob(s) during forward pass, takes no input and produces no output. After creating some data for input blobs like data = rand(net.blobs(‘data’).shape); you can run

官方介绍:
Blobs, Layers, and Nets: anatomy of a Caffe model

Blob
是Caffe处理和传递的实际数据的包装,并且还包括提供CPU和GPU之间的同步功能。 数学上,blob是以C连续方式存储的N维数组。
Caffe使用blob存储和传送数据。 Blob提供统一的数据存储接口; 例如批量的图像,模型参数和用于优化的衍生物。

对于图像数据来说,常规的blob的尺寸为 number N x channel K x height H x width W。
在布局中 行为先,
For example, in a 4D blob, the value at index (n, k, h, w) is physically located at index ((n * K + k) * H + h) * W + w.

Number / N是数据的批量大小。 批量处理可实现更好的通信和设备处理吞吐量。
channel / K是特征维度。 对于RGB图像K = 3。
blob虽然是4D,其同样适用于非图像运用, 例如,如果您只需像传统的多层感知器一样使用完全连接的网络,则使用2D Blob(shape(N,D)),并调用InnerProductLayer(我们将尽快介绍)。

Parameter blob dimensions vary according to the type and configuration of the layer. 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.

a Blob stores two chunks of memories, data and diff.前者是我们传递的正常数据,后者是由网络计算的梯度。

此外,由于实际值可以存储在CPU和GPU上,所以访问它们有两种不同的方式:常量方式,不改变值,以及可变方式,它们改变值:

const Dtype* cpu_data() const;Dtype* mutable_cpu_data();

(similarly for gpu and diff).

这种设计的原因是,Blob使用SyncedMem类来同步CPU和GPU之间的值,以隐藏同步细节并最小化数据传输。 一个经验法则是,如果不想更改值,请始终使用const调用,并且不要将指针存储在您自己的对象中。 每次在blob上工作时,调用函数来获取指针,因为SyncedMem将需要这个来确定什么时候复制数据。

实际上,当GPU出现时,在CPU代码中一个将数据从磁盘加载到一个blob,调用一个设备内核进行GPU计算,并将该Blob转移到下一层,忽略低级细节,同时保持高级别 性能。 只要所有层都具有GPU实现,所有中间数据和渐变将保留在GPU中。

// Assuming that data are on the CPU initially, and we have a blob.const Dtype* foo;Dtype* bar;foo = blob.gpu_data(); // data copied cpu->gpu.foo = blob.cpu_data(); // no data copied since both have up-to-date contents.bar = blob.mutable_gpu_data(); // no data copied.// ... some operations ...bar = blob.mutable_gpu_data(); // no data copied when we are still on GPU.foo = blob.cpu_data(); // data copied gpu->cpu, since the gpu side has modified the datafoo = blob.gpu_data(); // no data copied since both have up-to-date contentsbar = blob.mutable_cpu_data(); // still no data copied.bar = blob.mutable_gpu_data(); // data copied cpu->gpu.bar = blob.mutable_cpu_data(); // data copied gpu->cpu.

Layer computation and connections

See the layer catalogue :http://caffe.berkeleyvision.org/tutorial/layers.html
Each layer type defines three critical computations: setup, forward, and backward.

Setup: initialize the layer and its connections once at model initialization.
Forward: given input from bottom compute the output and send to the top.
Backward: given the gradient w.r.t. the top output compute the gradient w.r.t. to the input and send to the bottom. A layer with parameters computes the gradient w.r.t. to its parameters and stores it internally.

自定义网络 需要 网络的组成和代码的模块化

Net definition and operation
网络通过组合和自动分化联合定义函数及其梯度。
Caffe models are end-to-end machine learning engines.端到端指的是输入是原始数据,输出是最后的结果,原来输入端不是直接的原始数据,而是在原始数据中提取的特征,这一点在图像问题上尤为突出,因为图像像素数太多,数据维度高,会产生维度灾难,所以原来一个思路是手工提取图像的一些关键特征,这实际就是就一个降维的过程。

A simple logistic regression classifier
这里写图片描述

Model initialization is handled by Net::Init().The initialization mainly does two things:
1.creating the blobs and layers
2.calls the layers’ SetUp() function
3.other bookkeeping things, such as validating the correctness of the overall network architecture.

After construction, the network is run on either CPU or GPU by setting a single switch defined in Caffe::mode() and
set by Caffe::set_mode().

Model format
模型被定义在plaintext protocol buffer schema (prototxt)
learned models 被序列化为二进制协议缓冲区(binaryproto).caffemodel文件。

Caffe谈到了Google协议缓冲区,具有以下优势:序列化时的最小二进制字符串,高效的序列化,与二进制版本兼容的人性化的文本格式,以及多种语言的高效接口实现,最着名的是C ++和Python。 这一切都有助于Caffe建模的灵活性和可扩展性。

原创粉丝点击