| 8 | |
| 9 | template <typename Dtype> |
| 10 | void BatchNormLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom, |
| 11 | const vector<Blob<Dtype>*>& top) { |
| 12 | BatchNormParameter param = this->layer_param_.batch_norm_param(); |
| 13 | moving_average_fraction_ = param.moving_average_fraction(); |
| 14 | use_global_stats_ = this->phase_ == TEST; |
| 15 | if (param.has_use_global_stats()) |
| 16 | use_global_stats_ = param.use_global_stats(); |
| 17 | if (bottom[0]->num_axes() == 1) |
| 18 | channels_ = 1; |
| 19 | else |
| 20 | channels_ = bottom[0]->shape(1); |
| 21 | eps_ = param.eps(); |
| 22 | if (this->blobs_.size() > 0) { |
| 23 | LOG(INFO) << "Skipping parameter initialization"; |
| 24 | } else { |
| 25 | this->blobs_.resize(3); |
| 26 | vector<int> sz; |
| 27 | sz.push_back(channels_); |
| 28 | this->blobs_[0].reset(new Blob<Dtype>(sz)); |
| 29 | this->blobs_[1].reset(new Blob<Dtype>(sz)); |
| 30 | sz[0] = 1; |
| 31 | this->blobs_[2].reset(new Blob<Dtype>(sz)); |
| 32 | for (int i = 0; i < 3; ++i) { |
| 33 | caffe_set(this->blobs_[i]->count(), Dtype(0), |
| 34 | this->blobs_[i]->mutable_cpu_data()); |
| 35 | } |
| 36 | } |
| 37 | // Mask statistics from optimization by setting local learning rates |
| 38 | // for mean, variance, and the bias correction to zero. |
| 39 | for (int i = 0; i < this->blobs_.size(); ++i) { |
| 40 | if (this->layer_param_.param_size() == i) { |
| 41 | ParamSpec* fixed_param_spec = this->layer_param_.add_param(); |
| 42 | fixed_param_spec->set_lr_mult(0.f); |
| 43 | } else { |
| 44 | CHECK_EQ(this->layer_param_.param(i).lr_mult(), 0.f) |
| 45 | << "Cannot configure batch normalization statistics as layer " |
| 46 | << "parameters."; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | template <typename Dtype> |
| 52 | void BatchNormLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom, |