1. 程式人生 > >caffe原始碼導讀(二)關於Blob資料結構

caffe原始碼導讀(二)關於Blob資料結構

開啟proto/caffe.proto中,剛開始就是介紹Blob資料結構,這個資料結構是其他大部分資料結構的重要依賴.

caffe中進行網路層計算時,每一層的輸入輸出都是以Blob物件為緩衝,是cagge的基本儲存單元.

一、先看Blob的資料結構描述

// Specifies the shape (dimensions) of a Blob.
message BlobShape {
//來實現高維度的封裝,既vector<N>,分別表示Blob每個維度的大小;packed表示這些值在記憶體中緊密排布
  repeated int64 dim = 1 [packed = true];
}

message BlobProto {
  optional BlobShape shape = 7; //可選,包含一個blobshape物件
  repeated float data = 5 [packed = true];
  // 包含若干浮點數元素,儲存資料和權值,元素數目有shape或(num, channels, height, width)確定,在記憶體中緊密排布
  repeated float diff = 6 [packed = true]; // 浮點數,用來儲存增量資訊
  repeated double double_data = 8 [packed = true];
  repeated double double_diff = 9 [packed = true];

  // 4D dimensions -- deprecated.  Use "shape" instead.
  // 資料4D形狀,已經使用shape代替了,意思就是新版本推薦使用shape,而不再用後面的值
  optional int32 num = 1 [default = 0];
  optional int32 channels = 2 [default = 0];
  optional int32 height = 3 [default = 0];
  optional int32 width = 4 [default = 0];
}

// The BlobProtoVector is simply a way to pass multiple blobproto instances
// around.
message BlobProtoVector {
  repeated BlobProto blobs = 1;//多個 BlobProto
}

使用protobuf資料結構而不用結構體是因為

1)結構體的序列化和反序列化需要額外的程式設計實現,難以做到藉口標準化

2)結構體中包含很長的資料(一般指向某個記憶體的指標),需要更加細緻的工作保證資料結構的完整性

3)protobuffer可以將程式設計更容易出問題的地方加以隱藏,讓機器自動處理,提高程式的健壯性.