Caffe學習--Blob分析

Caffe_blob

1.基本數據結構

Blob爲模板類,能夠理解爲四維數組,n * c * h * w的結構,Layer內爲blob輸入data和diff,Layer間的blob爲學習的參數.內部封裝了SyncedMemory類,該類負責存儲分配和主機與設備的同步c++

protected:
    shared_ptr<SyncedMemory> data_; // data指針
    shared_ptr<SyncedMemory> diff_; // diff指針
    vector<int> shape_; // blob形狀
    int count_; // blob的nchw
    // 當前的Blob容量,當Blob reshape後count> capacity_時,capacity_ = count_;
    // 從新new 而後 reset data和 diff
    int capacity_;

2.經常使用函數

Blob類中經常使用的函數以下所示
Blob<float>test;
    //explicit關鍵字的做用是禁止單參數構造函數的隱式轉換
    explicit Blob(const int num, const int channels, const int height,
      const int width);
    test.shape_string();//初始爲空 0 0 0 0
    //Reshape函數將num,channels,height,width傳遞給vector shape_
    test.Reshape(1,2,3,4);// shape_string() 1,2,3,4

    test.shape(i);// NCHW
    test.count(int start_axis,int end_axis); // start_axis---end_axis .x* shape[i]
    test.count();// nchw  count(1) chw count(2) hw.....
    //shared_ptr<SyncedMemory> data_->cpu_data();
    const float* data = test.cpu_data();
    const float* diff = test.cpu_diff();
    float* data_1 = test.mutable_cpu_data();//mutable修飾的表示能夠修改內部值
    float* diff_1 = test.mutable_cpu_diff();
    test.asum_data();//求和 L1範數
    test.sumsq_data();//平方和 L2範數
    test.Update();//data = data-diff;
    a.ToProto(BlobProto& bp,true/false);//(FromProto)
    // if < 0 ,return num_axis()+axis_index;//索引序列
    int index = a.CanonicalAxisIndex(int axis_index);
    int offset(n,c,h,w);//((n*channels()+c)*height()+h)*width()+w
    float data_at(n,c,h,w);//return cpu_data()[offset(n,c,h,w)];
    float diff_at(n,c,h,w);//return cpu_diff()[offset(n,c,h,w)];
    inline const shared_ptr<SyncedMemory>& data() const{return _data};
    void scale_data(Dtype scale_factor);// data乘以一個標量。同理 scale_diff();
    void CopyFrom(const Blob<Dtype>& source, bool copy_diff = false,
      bool reshape = false);  // copy_diff是否複製diff

3.寫入磁盤操做

//Blob內部值寫入到磁盤
  Blob<float>a;
  a.Reshape(1,2,3,4);
  const int count = a.count();
  for (size_t i = 0; i < count; i++) {
      a[i] = i;//init the test Blob
  }
  BlobProto bp,bp2;
  a.ToProto(&bp,true);//寫入data和diff到bp中
  WriteProtoToBinaryFile(bp,"a.blob");//寫入磁盤
  ReadProtoFromBinaryFile("a.blob",&bp2);//從磁盤讀取blob
  Blob<float>b;
  b.FromProto(bp2,true);//序列化對象bp2中克隆b,完整克隆
  for (size_t n = 0; n < b.num(); n++) {
      for (size_t c = 0; c < b.channels(); c++) {
         for (size_t h = 0; h < b.height(); h++) {
             for (size_t w = 0; w < b.width(); w++) {
                 cout<<"b["<<n<<"]["<<c<<"]["<<h<<"]["<<w<<"]["<<w<<"]="<<
                 b[(((n*b.channels()+c)*b.height)+h)*b.width()+w]<<endl;
                 //(((n*c+ci)*h+hi)*w+wi)
             }
         }
      }
  }

4.部分函數的具體實現

本部分的實現未考慮參數是否合理。通常操做blob須要分CPU和GPU,採用math_functions具體計算
template <typename Dtype>
  void Blob<Dtype>::Reshape(const vector<int>& shape){//reshape操做
    count_ = 1;//初始count_ NCHW;
    shape_.resize(shape.size());
    for (size_t i = 0; i < shape.size(); i++) {
      count_ *= shape[i];
      shape_[i] = shape[i];
      if (count_ > capacity_) { //reshape的size大於了目前的最大容量
         capacity_ = count_;
         data_.reset(new SyncedMemory(capacity_*sizeof(Dtype)));
         diff_.reset(new SyncedMemory(capacity_*sizeof(Dtype)));
      }
    }
  }

 template <typename Dtype>
 void Blob<Dtype>::Reshape(int n,int c,int h ,int w){//reshape操做
   vector<int>shape(4);
   shape[0] = n;
   shape[1] = c;
   shape[2] = h;
   shape[3] = w;
   Reshape(shape);
 }

 template <typename Dtype>
 const Dtype* Blob<Dtype>::cpu_data(){
   //實際調用的shared_ptr<SyncedMemory>data_->cpu_data();,同理cpu_diff();
   CHECK(data_);
   return (const Dtype*)data_->cpu_data();
 }

template <typename Dtype>
void Blob<Dtype>::Updata(){ //data = data-diff;須要判斷cpu OR gpu
  switch (data_->head()) {
    case SyncedMemory::HEAD_AT_CPU:
      caffe_axpy<Dtype>(count_,Dtype(-1),
      static_cast<const<Dtype*>(diff_->cpu_data()),
      static_cast<Dtype*>(data_->mutable_cpu_data()));
  }
    case SyncedMemory::HEAD_AT_GPU://在gpu或者CPU/GPU已經同步
    case SyncedMemory::SYNCED:
    #ifndef CPU_ONLY
      caffe_gpu_axpy<Dtype>(count_.Dtype(-1),
      static_cast<const<Dtype*>(diff_->gpu_data()),
      static_cast<Dtype*>(data_->mutable_gpu_data()))
}

template <typename Dtype> //從source 拷貝數據,copy_diff控制是拷貝diff仍是data
void Blob<Dtype>::CopyFrom(const Blob& source, bool copy_diff, bool reshape) {
  if (source.count() != count_ || source.shape() != shape_) {
    if (reshape) {
      ReshapeLike(source);
    }
  }
  switch (Caffe::mode()) {
  case Caffe::GPU:
    if (copy_diff) {  //copy diff
      caffe_copy(count_, source.gpu_diff(),
          static_cast<Dtype*>(diff_->mutable_gpu_data()));
    } else {
      caffe_copy(count_, source.gpu_data(),
          static_cast<Dtype*>(data_->mutable_gpu_data()));
    }
    break;
  case Caffe::CPU:
    if (copy_diff) {
      caffe_copy(count_, source.cpu_diff(),
          static_cast<Dtype*>(diff_->mutable_cpu_data()));
    } else {
      caffe_copy(count_, source.cpu_data(),
          static_cast<Dtype*>(data_->mutable_cpu_data()));
    }
    break;
  default:  
    LOG(FATAL) << "Unknown caffe mode.";
  }
}


template <typename Dtype>
void Blob<Dtype>::ToProto(BlobProto* proto,bool write_diff){
    proto->clear_shape();
    for (size_t i = 0; i < shaoe_.size(); i++) {
        proto->mutable_shape()->add_dim(shape_[i]);
    }
    proto->clear_data();
    proto->clear_diff();
    const Dtype* data_vec = cpu_data();
    for (size_t i = 0; i < count_; i++) {
        proto->add_data(data_vec[i]);//data寫入proto
    }
    if (write_diff) {
        const Dtype* diff_vec = cpu_diff();
        for (size_t i = 0; i < count_; i++) {
            proto->add_diff(diff_vec[i]);//diff寫入proto
        }
    }
}

5.說明

/*Blob做爲一個最基礎的類,其中構造函數開闢一個內存空間來存儲數據,Reshape
    函數在Layer中的reshape或者forward操做中來調整top的輸出維度。同時在改變Blob
    大小時, 內存將會被從新分配若是內存大小不夠了,而且額外的內存將不會被釋放。
    對input的blob進行reshape, 若立馬調用Net::Backward是會出錯的,由於reshape
    以後,要麼Net::forward或者Net::Reshape就會被調用來將新的input shape傳播
    到高層 */
相關文章
相關標籤/搜索