| 20 | |
| 21 | template <typename Dtype> |
| 22 | void SliceLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |
| 23 | const vector<Blob<Dtype>*>& top) { |
| 24 | const int num_axes = bottom[0]->num_axes(); |
| 25 | const SliceParameter& slice_param = this->layer_param_.slice_param(); |
| 26 | if (slice_param.has_slice_dim()) { |
| 27 | slice_axis_ = static_cast<int>(slice_param.slice_dim()); |
| 28 | // Don't allow negative indexing for slice_dim, a uint32 -- almost |
| 29 | // certainly unintended. |
| 30 | CHECK_GE(slice_axis_, 0) << "casting slice_dim from uint32 to int32 " |
| 31 | << "produced negative result; slice_dim must satisfy " |
| 32 | << "0 <= slice_dim < " << kMaxBlobAxes; |
| 33 | CHECK_LT(slice_axis_, num_axes) << "slice_dim out of range."; |
| 34 | } else { |
| 35 | slice_axis_ = bottom[0]->CanonicalAxisIndex(slice_param.axis()); |
| 36 | } |
| 37 | vector<int> top_shape = bottom[0]->shape(); |
| 38 | const int bottom_slice_axis = bottom[0]->shape(slice_axis_); |
| 39 | num_slices_ = bottom[0]->count(0, slice_axis_); |
| 40 | slice_size_ = bottom[0]->count(slice_axis_ + 1); |
| 41 | int count = 0; |
| 42 | if (slice_point_.size() != 0) { |
| 43 | CHECK_EQ(slice_point_.size(), top.size() - 1); |
| 44 | CHECK_LE(top.size(), bottom_slice_axis); |
| 45 | int prev = 0; |
| 46 | vector<int> slices; |
| 47 | for (int i = 0; i < slice_point_.size(); ++i) { |
| 48 | CHECK_GT(slice_point_[i], prev); |
| 49 | slices.push_back(slice_point_[i] - prev); |
| 50 | prev = slice_point_[i]; |
| 51 | } |
| 52 | slices.push_back(bottom_slice_axis - prev); |
| 53 | for (int i = 0; i < top.size(); ++i) { |
| 54 | top_shape[slice_axis_] = slices[i]; |
| 55 | top[i]->Reshape(top_shape); |
| 56 | count += top[i]->count(); |
| 57 | } |
| 58 | } else { |
| 59 | CHECK_EQ(bottom_slice_axis % top.size(), 0) |
| 60 | << "Number of top blobs (" << top.size() << ") should evenly " |
| 61 | << "divide input slice axis (" << bottom_slice_axis << ")"; |
| 62 | top_shape[slice_axis_] = bottom_slice_axis / top.size(); |
| 63 | for (int i = 0; i < top.size(); ++i) { |
| 64 | top[i]->Reshape(top_shape); |
| 65 | count += top[i]->count(); |
| 66 | } |
| 67 | } |
| 68 | CHECK_EQ(count, bottom[0]->count()); |
| 69 | if (top.size() == 1) { |
| 70 | top[0]->ShareData(*bottom[0]); |
| 71 | top[0]->ShareDiff(*bottom[0]); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | template <typename Dtype> |
| 76 | void SliceLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, |