caffe源码阅读之layer(2)——DataLayer层

   caffe数据读取层是layer的派生类,用于读取LMDB、LEVELDB,也可以直接读取图片,下面看看caffe怎么读取数据的,该代码的实现在
E:\Caffe\caffe-windows\src\caffe\layers中的base_data_layer.cpp和E:\Caffe\caffe-windows\include\caffe\layers中的base_data_layer.hpp
下面看看具体的源代码。
一、数据读取层
该层的类为BaseDataLayer是从layer上继承而来

成员变量:

 TransformationParameter transform_param_;//数据预处理变换参数
  shared_ptr<DataTransformer<Dtype> > data_transformer_;//数据预处理变换参数
  bool output_labels_;//标签数据
1、构造函数

 explicit BaseDataLayer(const LayerParameter& param);
 2、配置函数
 之前我们看的的layer基类时提到SetUp()函数
//实现数据读取层的配置
  template <typename Dtype>
void BaseDataLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top) {
  if (top.size() == 1) {//判断输出Blob个数,若为1只输出data,2则输出data和label
      output_labels_ = false;
  } else {
    output_labels_ = true;
  }
  data_transformer_.reset(new DataTransformer<Dtype>(transform_param_, this->phase_));//初始化数据转化器对象
  data_transformer_->InitRand();//生成随机种子
  DataLayerSetUp(bottom, top);  // 设置输入输出的大小
}
  virtual inline bool ShareInParallel() const { return true; }//数据读取层被多个并行求解器共享
  virtual void Reshape(const vector<Blob<Dtype>*>& bottom,const vector<Blob<Dtype>*>& top) {} //变形操作
 3、前向和反向传播
 该层只是读取数据,不做相关的任何操作
virtual void Backward_cpu(const vector<Blob<Dtype>*>& top,const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
  virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {}
二、批量数据
该层的类类为Batch,该类主要用于存放读取后的数据
成员变量

Blob<Dtype> data_, label_;//数据和标签

三、预取功能的数据读取层
该层的类类为BasePrefetchingDataLayer,该类主要从BaseDataLayer和InternalThread继承而来
成员变量

Batch<Dtype> prefetch_[PREFETCH_COUNT];//预取Buffer
  BlockingQueue<Batch<Dtype>*> prefetch_free_;//空闲的Batch队列
  BlockingQueue<Batch<Dtype>*> prefetch_full_;//已加载的Batch队列
  Blob<Dtype> transformed_data_;//变换后的数据
一些私有成员函数
   virtual void InternalThreadEntry();//l内部线程入口
  virtual void load_batch(Batch<Dtype>* batch) = 0;//载入批量数据
  1、构造函数
  template <typename Dtype>
BasePrefetchingDataLayer<Dtype>::BasePrefetchingDataLayer(const LayerParameter& param): BaseDataLayer<Dtype>(param),prefetch_free_(), prefetch_full_() {
  for (int i = 0; i < PREFETCH_COUNT; ++i) {
    prefetch_free_.push(&prefetch_[i]);//将Batch对象放入空闲的队列中
  }
}
2、配置函数

template <typename Dtype>
void BasePrefetchingDataLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
  BaseDataLayer<Dtype>::LayerSetUp(bottom, top);
  for (int i = 0; i < PREFETCH_COUNT; ++i) {
    prefetch_[i].data_.mutable_cpu_data();
    if (this->output_labels_) {
      prefetch_[i].label_.mutable_cpu_data();
    }
  }
#ifndef CPU_ONLY
  if (Caffe::mode() == Caffe::GPU) {
    for (int i = 0; i < PREFETCH_COUNT; ++i) {
      prefetch_[i].data_.mutable_gpu_data();
      if (this->output_labels_) {
        prefetch_[i].label_.mutable_gpu_data();
      }
    }
  }
#endif
  DLOG(INFO) << "Initializing prefetch";
  this->data_transformer_->InitRand();
  StartInternalThread();//开启内部预取线程
  DLOG(INFO) << "Prefetch initialized.";
}
3、前向传播

template <typename Dtype>
void BasePrefetchingDataLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, const vector<Blob<Dtype>*>& top) {
  Batch<Dtype>* batch = prefetch_full_.pop("Data layer prefetch queue empty");//从带负载的Batch中取出一个Batch对象
  top[0]->ReshapeLike(batch->data_);// 根据Blob的形状变形
  caffe_copy(batch->data_.count(), batch->data_.cpu_data(),top[0]->mutable_cpu_data());//将Batch中的数据拷贝到输出Blob中
  DLOG(INFO) << "Prefetch copied";
  if (this->output_labels_) {//如果需要输出标签
    top[1]->ReshapeLike(batch->label_);// 根据Blob的形状变形
    caffe_copy(batch->label_.count(), batch->label_.cpu_data(),top[1]->mutable_cpu_data());//将Batch中的数据拷贝到输出Blob中
  }
  prefetch_free_.push(batch);//将一个Batch送入到Net完成任务,返回空闲队列接受下一批数据
}
4、其他

//内部线程入口
template <typename Dtype>
void BasePrefetchingDataLayer<Dtype>::InternalThreadEntry() {
#ifndef CPU_ONLY
  cudaStream_t stream;//创建CUDA stream
  if (Caffe::mode() == Caffe::GPU) {
    CUDA_CHECK(cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking));
  }
#endif

  try {
    while (!must_stop()) {//循环载入批量数据
      Batch<Dtype>* batch = prefetch_free_.pop();//拿到一个空闲Blob
      load_batch(batch);//载入数据
#ifndef CPU_ONLY
      if (Caffe::mode() == Caffe::GPU) {
        batch->data_.data().get()->async_gpu_push(stream);
        CUDA_CHECK(cudaStreamSynchronize(stream));
      }
#endif
      prefetch_full_.push(batch);//将Batch加载到Batch队列中
    }
  } catch (boost::thread_interrupted&) {//捕获异常,退出循环
  }
#ifndef CPU_ONLY
  if (Caffe::mode() == Caffe::GPU) {
    CUDA_CHECK(cudaStreamDestroy(stream));//销毁CUDA
  }
#endif
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值