data_layer #5772

Open
wants to merge 11 commits into
from
View
9,109 .tags

Large diffs are not rendered by default.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -24,9 +24,7 @@
{
"cell_type": "code",
"execution_count": 1,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"caffe_root = '../' # this file should be run from {caffe_root}/examples (otherwise change this line)\n",
@@ -78,9 +76,7 @@
{
"cell_type": "code",
"execution_count": 2,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -140,9 +136,7 @@
{
"cell_type": "code",
"execution_count": 3,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"import os\n",
@@ -160,9 +154,7 @@
{
"cell_type": "code",
"execution_count": 4,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -214,7 +206,6 @@
"cell_type": "code",
"execution_count": 5,
"metadata": {
- "collapsed": false,
"scrolled": true
},
"outputs": [],
@@ -302,9 +293,7 @@
{
"cell_type": "code",
"execution_count": 6,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"dummy_data = L.DummyData(shape=dict(dim=[1, 3, 227, 227]))\n",
@@ -328,9 +317,7 @@
{
"cell_type": "code",
"execution_count": 7,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"def style_net(train=True, learn_all=False, subset=None):\n",
@@ -361,9 +348,7 @@
{
"cell_type": "code",
"execution_count": 8,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"untrained_style_net = caffe.Net(style_net(train=False, subset='train'),\n",
@@ -385,9 +370,7 @@
{
"cell_type": "code",
"execution_count": 9,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"def disp_preds(net, image, labels, k=5, name='ImageNet'):\n",
@@ -409,9 +392,7 @@
{
"cell_type": "code",
"execution_count": 10,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -441,9 +422,7 @@
{
"cell_type": "code",
"execution_count": 11,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -474,9 +453,7 @@
{
"cell_type": "code",
"execution_count": 12,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -505,9 +482,7 @@
{
"cell_type": "code",
"execution_count": 13,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"diff = untrained_style_net.blobs['fc7'].data[0] - imagenet_net.blobs['fc7'].data[0]\n",
@@ -545,9 +520,7 @@
{
"cell_type": "code",
"execution_count": 15,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"from caffe.proto import caffe_pb2\n",
@@ -630,9 +603,7 @@
{
"cell_type": "code",
"execution_count": 16,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [],
"source": [
"def run_solvers(niter, solvers, disp_interval=10):\n",
@@ -674,9 +645,7 @@
{
"cell_type": "code",
"execution_count": 17,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -746,7 +715,6 @@
"cell_type": "code",
"execution_count": 18,
"metadata": {
- "collapsed": false,
"scrolled": false
},
"outputs": [
@@ -780,9 +748,7 @@
{
"cell_type": "code",
"execution_count": 19,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"data": {
@@ -838,9 +804,7 @@
{
"cell_type": "code",
"execution_count": 21,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -872,9 +836,7 @@
{
"cell_type": "code",
"execution_count": 22,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -948,9 +910,7 @@
{
"cell_type": "code",
"execution_count": 23,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -978,9 +938,7 @@
{
"cell_type": "code",
"execution_count": 24,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -1022,9 +980,7 @@
{
"cell_type": "code",
"execution_count": 25,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -1054,9 +1010,7 @@
{
"cell_type": "code",
"execution_count": 26,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -1085,9 +1039,7 @@
{
"cell_type": "code",
"execution_count": 27,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -1116,9 +1068,7 @@
{
"cell_type": "code",
"execution_count": 28,
- "metadata": {
- "collapsed": false
- },
+ "metadata": {},
"outputs": [
{
"name": "stdout",
@@ -1166,10 +1116,10 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
- "version": "2.7.10"
+ "version": "2.7.13"
},
"priority": 3
},
"nbformat": 4,
- "nbformat_minor": 0
+ "nbformat_minor": 1
}
View
@@ -9,6 +9,10 @@
#include "caffe/proto/caffe.pb.h"
#include "caffe/syncedmem.hpp"
+/*
+主要是分配内存和释放内存。class yncedMemory定义了内存分配管理和CPU与GPU之间同步的函数。
+ Blob会使用SyncedMem自动决定什么时候去copy data以提高运行效率,通常情况是仅当gnu或cpu修改后有copy操作。
+ */
const int kMaxBlobAxes = 32;
namespace caffe {
@@ -23,10 +27,18 @@ namespace caffe {
template <typename Dtype>
class Blob {
public:
+ /**
+ BLob只是一个基本的数据结构,因此内部的变量相对较少,首先是data_指针,指针类型是shared_ptr,
+ 属于boost库的一个智能指针,这一部分主要用来申请内存存储data,data主要是正向传播的时候用的。
+ diff_主要用来存储偏差,update data,shape_data和shape_都是存储Blob的形状,一个
+ 是老版本一个是新版本。
+ count表示Blob中的元素个数,也就是个数*通道数*高度*宽度
+ capacity表示当前的元素个数,因为Blob可能会reshape。
+ */
Blob()
: data_(), diff_(), count_(0), capacity_(0) {}
-
/// @brief Deprecated; use <code>Blob(const vector<int>& shape)</code>.
+ //当构造函数被声明 explicit 时,编译器将不使用它作为转换操作符。
explicit Blob(const int num, const int channels, const int height,
const int width);
explicit Blob(const vector<int>& shape);
@@ -141,6 +153,7 @@ class Blob {
<< "Cannot use legacy accessors on Blobs with > 4 axes.";
CHECK_LT(index, 4);
CHECK_GE(index, -4);
+ //肯定是在做比较Geater or Eqal这样的意思。这其实是GLOG,谷歌的一个日志库,Caffe里面用用了大量这样的宏,看起来也比较直观
if (index >= num_axes() || index < -num_axes()) {
// Axis is out of range, but still in [0, 3] (or [-4, -1] for reverse
// indexing) -- this special case simulates the one-padding used to fill
@@ -149,7 +162,9 @@ class Blob {
}
return shape(index);
}
-
+/*
+offset计算的方式也支持两种方式,一种直接指定n,c,h,w或者放到一个vector中进行计算,偏差是根据对应的n,c,h,w
+*/
inline int offset(const int n, const int c = 0, const int h = 0,
const int w = 0) const {
CHECK_GE(n, 0);
@@ -185,6 +200,7 @@ class Blob {
* of other (and die otherwise); if true, Reshape this Blob to other's
* shape if necessary
*/
+ //CopyFrom()从source拷贝数据,copy_diff来作为标志区分拷贝的是data还是diff。
void CopyFrom(const Blob<Dtype>& source, bool copy_diff = false,
bool reshape = false);
@@ -223,13 +239,18 @@ class Blob {
void set_gpu_data(Dtype* data);
const Dtype* cpu_diff() const;
const Dtype* gpu_diff() const;
+ //data就是存储前向传递的信息的数据,diff指的是神经网络在反向传播时候的梯度。
Dtype* mutable_cpu_data();
Dtype* mutable_gpu_data();
Dtype* mutable_cpu_diff();
Dtype* mutable_gpu_diff();
void Update();
- void FromProto(const BlobProto& proto, bool reshape = true);
- void ToProto(BlobProto* proto, bool write_diff = false) const;
+ /*
+ 这两个函数主要是将数据序列化,存储到BlobProto,这里说到Proto是谷歌的一个数据序列化的存储格式,
+ 可以实现语言、平台无关、可扩展的序列化结构数据格式。
+ */
+ void FromProto(const BlobProto& proto, bool reshape = true); //反序列化
+ void ToProto(BlobProto* proto, bool write_diff = false) const; //序列化
/// @brief Compute the sum of absolute values (L1 norm) of the data.
Dtype asum_data() const;
View
@@ -1,6 +1,7 @@
// Fillers are random number generators that fills a blob using the specified
// algorithm. The expectation is that they are only going to be used during
// initialization time and will not involve any GPUs.
+//在网络初始化时,根据layer的定义进行初始参数的填充
#ifndef CAFFE_FILLER_HPP
#define CAFFE_FILLER_HPP
Oops, something went wrong.