| 30 | |
| 31 | template <typename Dtype> |
| 32 | void ReshapeLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |
| 33 | const vector<Blob<Dtype>*>& top) { |
| 34 | const int input_start_axis = this->layer_param_.reshape_param().axis(); |
| 35 | const int start_axis = (input_start_axis >= 0) ? input_start_axis : |
| 36 | bottom[0]->num_axes() + input_start_axis + 1; |
| 37 | CHECK_GE(start_axis, 0) << "axis " << input_start_axis << " out of range"; |
| 38 | CHECK_LE(start_axis, bottom[0]->num_axes()) << "axis " << input_start_axis |
| 39 | << " out of range for " << bottom[0]->num_axes() << "-D input blob"; |
| 40 | const int num_axes = this->layer_param_.reshape_param().num_axes(); |
| 41 | CHECK_GE(num_axes, -1) << "num_axes must be >= 0, or -1 for all"; |
| 42 | const int end_axis = |
| 43 | (num_axes == -1) ? bottom[0]->num_axes() : (start_axis + num_axes); |
| 44 | CHECK_LE(end_axis, bottom[0]->num_axes()) |
| 45 | << "end_axis = axis + num_axes is out of range"; |
| 46 | const int num_axes_replaced = end_axis - start_axis; |
| 47 | const int num_axes_retained = bottom[0]->num_axes() - num_axes_replaced; |
| 48 | const BlobShape& top_blob_shape = this->layer_param_.reshape_param().shape(); |
| 49 | const int num_new_axes = top_blob_shape.dim_size(); |
| 50 | vector<int> top_shape(num_axes_retained + num_new_axes); |
| 51 | int top_shape_index = 0; |
| 52 | for (int i = 0; i < start_axis; ++i) { |
| 53 | top_shape[top_shape_index++] = bottom[0]->shape(i); |
| 54 | } |
| 55 | for (int i = 0; i < num_new_axes; ++i) { |
| 56 | top_shape[top_shape_index++] = top_blob_shape.dim(i); |
| 57 | } |
| 58 | for (int i = end_axis; i < bottom[0]->num_axes(); ++i) { |
| 59 | top_shape[top_shape_index++] = bottom[0]->shape(i); |
| 60 | } |
| 61 | CHECK_EQ(top_shape_index, top_shape.size()); |
| 62 | for (int i = 0; i < copy_axes_.size(); ++i) { |
| 63 | const int copy_axis_index = copy_axes_[i]; |
| 64 | CHECK_GT(bottom[0]->num_axes(), start_axis + copy_axis_index) |
| 65 | << "new shape contains a 0, but there was no corresponding bottom axis " |
| 66 | << "to copy"; |
| 67 | top_shape[start_axis + copy_axis_index] = |
| 68 | bottom[0]->shape(start_axis + copy_axis_index); |
| 69 | } |
| 70 | if (inferred_axis_ >= 0) { |
| 71 | // A -1 dim was specified; infer the correct dimension by computing the |
| 72 | // product of the other dimensions. |
| 73 | int explicit_count = constant_count_; |
| 74 | explicit_count *= bottom[0]->count(0, start_axis); |
| 75 | explicit_count *= bottom[0]->count(end_axis); |
| 76 | for (int i = 0; i < copy_axes_.size(); ++i) { |
| 77 | const int copy_axis_index = copy_axes_[i]; |
| 78 | explicit_count *= top_shape[start_axis + copy_axis_index]; |
| 79 | } |
| 80 | CHECK_EQ(0, bottom[0]->count() % explicit_count) << "bottom count (" |
| 81 | << bottom[0]->count() << ") must be divisible by the product of " |
| 82 | << "the specified dimensions (" << explicit_count << ")"; |
| 83 | const int inferred_dim = bottom[0]->count() / explicit_count; |
| 84 | top_shape[start_axis + inferred_axis_] = inferred_dim; |
| 85 | } |
| 86 | top[0]->Reshape(top_shape); |
| 87 | CHECK_EQ(top[0]->count(), bottom[0]->count()) |
| 88 | << "output count must match input count"; |
| 89 | top[0]->ShareData(*bottom[0]); |