|
| 1 | +#include <vector> |
| 2 | + |
| 3 | +#include "caffe/filler.hpp" |
| 4 | +#include "caffe/layers/bias_layer.hpp" |
| 5 | +#include "caffe/util/math_functions.hpp" |
| 6 | + |
| 7 | +namespace caffe { |
| 8 | + |
| 9 | +template <typename Dtype> |
| 10 | +void BiasLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, |
| 11 | + const vector<Blob<Dtype>*>& top) { |
| 12 | + if (bottom.size() == 1 && this->blobs_.size() > 0) { |
| 13 | + LOG(INFO) << "Skipping parameter initialization"; |
| 14 | + } else if (bottom.size() == 1) { |
| 15 | + // bias is a learned parameter; initialize it |
| 16 | + const BiasParameter& param = this->layer_param_.bias_param(); |
| 17 | + const int axis = bottom[0]->CanonicalAxisIndex(param.axis()); |
| 18 | + const int num_axes = param.num_axes(); |
| 19 | + CHECK_GE(num_axes, -1) << "num_axes must be non-negative, " |
| 20 | + << "or -1 to extend to the end of bottom[0]"; |
| 21 | + if (num_axes >= 0) { |
| 22 | + CHECK_GE(bottom[0]->num_axes(), axis + num_axes) |
| 23 | + << "bias blob's shape extends past bottom[0]'s shape when applied " |
| 24 | + << "starting with bottom[0] axis = " << axis; |
| 25 | + } |
| 26 | + this->blobs_.resize(1); |
| 27 | + const vector<int>::const_iterator& shape_start = |
| 28 | + bottom[0]->shape().begin() + axis; |
| 29 | + const vector<int>::const_iterator& shape_end = |
| 30 | + (num_axes == -1) ? bottom[0]->shape().end() : (shape_start + num_axes); |
| 31 | + vector<int> bias_shape(shape_start, shape_end); |
| 32 | + this->blobs_[0].reset(new Blob<Dtype>(bias_shape)); |
| 33 | + shared_ptr<Filler<Dtype> > filler(GetFiller<Dtype>(param.filler())); |
| 34 | + filler->Fill(this->blobs_[0].get()); |
| 35 | + } |
| 36 | + this->param_propagate_down_.resize(this->blobs_.size(), true); |
| 37 | +} |
| 38 | + |
| 39 | +template <typename Dtype> |
| 40 | +void BiasLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |
| 41 | + const vector<Blob<Dtype>*>& top) { |
| 42 | + const BiasParameter& param = this->layer_param_.bias_param(); |
| 43 | + Blob<Dtype>* bias = (bottom.size() > 1) ? bottom[1] : this->blobs_[0].get(); |
| 44 | + // Always set axis == 0 in special case where bias is a scalar |
| 45 | + // (num_axes == 0). Mathematically equivalent for any choice of axis, so the |
| 46 | + // actual setting can be safely ignored; and computation is most efficient |
| 47 | + // with axis == 0 and (therefore) outer_dim_ == 1. |
| 48 | + const int axis = (bias->num_axes() == 0) ? |
| 49 | + 0 : bottom[0]->CanonicalAxisIndex(param.axis()); |
| 50 | + CHECK_GE(bottom[0]->num_axes(), axis + bias->num_axes()) |
| 51 | + << "bias blob's shape extends past bottom[0]'s shape when applied " |
| 52 | + << "starting with bottom[0] axis = " << axis; |
| 53 | + for (int i = 0; i < bias->num_axes(); ++i) { |
| 54 | + CHECK_EQ(bottom[0]->shape(axis + i), bias->shape(i)) |
| 55 | + << "dimension mismatch between bottom[0]->shape(" << axis + i |
| 56 | + << ") and bias->shape(" << i << ")"; |
| 57 | + } |
| 58 | + outer_dim_ = bottom[0]->count(0, axis); |
| 59 | + bias_dim_ = bias->count(); |
| 60 | + inner_dim_ = bottom[0]->count(axis + bias->num_axes()); |
| 61 | + dim_ = bias_dim_ * inner_dim_; |
| 62 | + if (bottom[0] != top[0]) { |
| 63 | + top[0]->ReshapeLike(*bottom[0]); |
| 64 | + } |
| 65 | + bias_multiplier_.Reshape(vector<int>(1, inner_dim_)); |
| 66 | + if (bias_multiplier_.cpu_data()[inner_dim_ - 1] != Dtype(1)) { |
| 67 | + caffe_set(inner_dim_, Dtype(1), bias_multiplier_.mutable_cpu_data()); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +template <typename Dtype> |
| 72 | +void BiasLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom, |
| 73 | + const vector<Blob<Dtype>*>& top) { |
| 74 | + const Dtype* bias_data = |
| 75 | + ((bottom.size() > 1) ? bottom[1] : this->blobs_[0].get())->cpu_data(); |
| 76 | + Dtype* top_data = top[0]->mutable_cpu_data(); |
| 77 | + if (bottom[0] != top[0]) { |
| 78 | + const Dtype* bottom_data = bottom[0]->cpu_data(); |
| 79 | + caffe_copy(bottom[0]->count(), bottom_data, top_data); |
| 80 | + } |
| 81 | + for (int n = 0; n < outer_dim_; ++n) { |
| 82 | + caffe_cpu_gemm(CblasNoTrans, CblasNoTrans, bias_dim_, |
| 83 | + inner_dim_, Dtype(1), Dtype(1), bias_data, |
| 84 | + bias_multiplier_.cpu_data(), Dtype(1), top_data); |
| 85 | + top_data += dim_; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +template <typename Dtype> |
| 90 | +void BiasLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top, |
| 91 | + const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom) { |
| 92 | + if (propagate_down[0] && bottom[0] != top[0]) { |
| 93 | + const Dtype* top_diff = top[0]->cpu_diff(); |
| 94 | + Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); |
| 95 | + caffe_copy(bottom[0]->count(), top_diff, bottom_diff); |
| 96 | + } |
| 97 | + // in-place, we don't need to do anything with the data diff |
| 98 | + const bool bias_param = (bottom.size() == 1); |
| 99 | + if ((!bias_param && propagate_down[1]) || |
| 100 | + (bias_param && this->param_propagate_down_[0])) { |
| 101 | + const Dtype* top_diff = top[0]->cpu_diff(); |
| 102 | + Dtype* bias_diff = (bias_param ? this->blobs_[0].get() : bottom[1]) |
| 103 | + ->mutable_cpu_diff(); |
| 104 | + bool accum = bias_param; |
| 105 | + for (int n = 0; n < outer_dim_; ++n) { |
| 106 | + caffe_cpu_gemv(CblasNoTrans, bias_dim_, inner_dim_, Dtype(1), |
| 107 | + top_diff, bias_multiplier_.cpu_data(), Dtype(accum), bias_diff); |
| 108 | + top_diff += dim_; |
| 109 | + accum = true; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +#ifdef CPU_ONLY |
| 115 | +STUB_GPU(BiasLayer); |
| 116 | +#endif |
| 117 | + |
| 118 | +INSTANTIATE_CLASS(BiasLayer); |
| 119 | +REGISTER_LAYER_CLASS(Bias); |
| 120 | + |
| 121 | +} // namespace caffe |
0 commit comments