【caffe】caffe之Slice层

最近在做re-di任务时,经常要用到parts信息,因此slice层是非常有用的,但是caffe中这一层的参数需要弄明白,这里将slice层的代码解读一下。

——————————————————————–caffe.proto———————————————————————–

message SliceParameter {
  // The axis along which to slice -- may be negative to index from the end
  // (e.g., -1 for the last axis).
  // By default, SliceLayer concatenates blobs along the "channels" axis (1).
  //默认是在channel维进行切分
  optional int32 axis = 3 [default = 1];
  //切分点,个数要比输出个数少1
  repeated uint32 slice_point = 2;

  // DEPRECATED: alias for "axis" -- does not support negative indexing.
  optional uint32 slice_dim = 1 [default = 1];
}

—————————————————————SliceLayer::LayerSetUp————————————————————–

template <typename Dtype>
void SliceLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  //获取本层参数
  const SliceParameter& slice_param = this->layer_param_.slice_param();
  CHECK(!(slice_param.has_axis() && slice_param.has_slice_dim()))
      << "Either axis or slice_dim should be specified; not both.";
  //vector清空,并从参数将切分点拷贝进来
  slice_point_.clear();
  std::copy(slice_param.slice_point().begin(),
      slice_param.slice_point().end(),
      std::back_inserter(slice_point_));
}

—————————————————————SliceLayer::Reshape————————————————————–

template <typename Dtype>
void SliceLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  //获取输入blob维度,为4
  const int num_axes = bottom[0]->num_axes();
  const SliceParameter& slice_param = this->layer_param_.slice_param();
  //如果指定切分维度的话,则在指定维度上进行切分,比如在H维
  if (slice_param.has_slice_dim()) {
    slice_axis_ = static_cast<int>(slice_param.slice_dim());
    // Don't allow negative indexing for slice_dim, a uint32 -- almost
    // certainly unintended.
    CHECK_GE(slice_axis_, 0) << "casting slice_dim from uint32 to int32 "
        << "produced negative result; slice_dim must satisfy "
        << "0 <= slice_dim < " << kMaxBlobAxes;
    CHECK_LT(slice_axis_, num_axes) << "slice_dim out of range.";
  } 
  //不指定的话默认在channel维进行切分
  else {
    slice_axis_ = bottom[0]->CanonicalAxisIndex(slice_param.axis());
  }
  vector<int> top_shape = bottom[0]->shape();
  //获取指定维度的大小,比如channel=16,切分点为[4, 8, 12]
  const int bottom_slice_axis = bottom[0]->shape(slice_axis_);
  //计算每次切分在一个batch中需要切分的次数,若在channel维进行切分,等于batchsize
  num_slices_ = bottom[0]->count(0, slice_axis_);
  //计算每次切分最小feature map的大小,若在channel维进行切分,等于HxW
  slice_size_ = bottom[0]->count(slice_axis_ + 1);
  //统计输出切分维度的大小
  int count = 0;
  if (slice_point_.size() != 0) {
    CHECK_EQ(slice_point_.size(), top.size() - 1);
    CHECK_LE(top.size(), bottom_slice_axis);
    int prev = 0;
    vector<int> slices;
    for (int i = 0; i < slice_point_.size(); ++i) {
      CHECK_GT(slice_point_[i], prev);
      //统计每次切分的跨度,为[4, 4, 4, 4]
      slices.push_back(slice_point_[i] - prev);
      prev = slice_point_[i];
    }
    slices.push_back(bottom_slice_axis - prev);
    //根据切分跨度,对输出进行reshape,并统计每个输出切分维度的大小
    for (int i = 0; i < top.size(); ++i) {
      top_shape[slice_axis_] = slices[i];
      top[i]->Reshape(top_shape);
      count += top[i]->count();
    }
  } else {
    CHECK_EQ(bottom_slice_axis % top.size(), 0)
        << "Number of top blobs (" << top.size() << ") should evenly "
        << "divide input slice axis (" << bottom_slice_axis << ")";
    top_shape[slice_axis_] = bottom_slice_axis / top.size();
    for (int i = 0; i < top.size(); ++i) {
      top[i]->Reshape(top_shape);
      count += top[i]->count();
    }
  }
  //check一下
  CHECK_EQ(count, bottom[0]->count());
}

—————————————————————SliceLayer::Forward_cpu————————————————————–

template <typename Dtype>
void SliceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
      const vector<Blob<Dtype>*>& top) {
  int offset_slice_axis = 0;
  const Dtype* bottom_data = bottom[0]->cpu_data();
  //获取要切分的维度大小,bottom_slice_axis = 16
  const int bottom_slice_axis = bottom[0]->shape(slice_axis_);
  //做法就是对每个输出计算输出和输入之间的offset关系,然后拷贝一下数据就ok了
  for (int i = 0; i < top.size(); ++i) {
    //一个输出的指针
    Dtype* top_data = top[i]->mutable_cpu_data();
    //获取输出指定切分维度大小
    const int top_slice_axis = top[i]->shape(slice_axis_);
    //将输入中一个batch的数据,一个一个的按照最小连续feature map拷贝到输出
    //比如在channel维切分,num_slices_ = batchsize, top_slice_axis = 4
    for (int n = 0; n < num_slices_; ++n) {
      //将batch中的一个输入的连续4个feature map拷贝到输出
      //输出指针移动4个feature map大小
      const int top_offset = n * top_slice_axis * slice_size_;
      //输入指针移动到下一个feature map的切分点
      const int bottom_offset =
          (n * bottom_slice_axis + offset_slice_axis) * slice_size_;
      //拷贝数据
      caffe_copy(top_slice_axis * slice_size_,
          bottom_data + bottom_offset, top_data + top_offset);
    }
    //移动下一个切分点
    offset_slice_axis += top_slice_axis;
  }
}

————————————————————–SliceLayer::Backward_cpu————————————————————

//反向与前向一样只是得到输入与输出之间的offset关系,然后拷贝数据
template <typename Dtype>
void SliceLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
      const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) {
  if (!propagate_down[0]) { return; }
  int offset_slice_axis = 0;
  Dtype* bottom_diff = bottom[0]->mutable_cpu_diff();
  const int bottom_slice_axis = bottom[0]->shape(slice_axis_);
  //从每个top中将diff拷贝到bottom diff中
  for (int i = 0; i < top.size(); ++i) {
    const Dtype* top_diff = top[i]->cpu_diff();
    const int top_slice_axis = top[i]->shape(slice_axis_);
    for (int n = 0; n < num_slices_; ++n) {
      //将top diff按照切分大小分别拷贝的bottom diff中,每次移动top_slice_axis = 4个feature map
      const int top_offset = n * top_slice_axis * slice_size_;
      //而bottom diff则每次要移动16个feature map,对应不同的输入
      const int bottom_offset =
          (n * bottom_slice_axis + offset_slice_axis) * slice_size_;
      //拷贝数据
      caffe_copy(top_slice_axis * slice_size_,
          top_diff + top_offset, bottom_diff + bottom_offset);
    }
    //bottom diff指针移动到下一个切分点,然后继续从top diff中拷贝
    offset_slice_axis += top_slice_axis;
  }
}

————————————————————————–用法举例————————————————————————–

将每个feature map切分成块

这里写图片描述

结果如图所示

这里写图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值