Skip to content

Commit 18ef30f

Browse files
committed
Add init iteration param for BatchNormLayer
1 parent c1884c1 commit 18ef30f

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

include/caffe/common_layers.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class BatchNormLayer : public Layer<Dtype> {
130130
Blob<Dtype> mean_, variance_, temp_, x_norm_;
131131
bool use_global_stats_;
132132
BatchNormParameter_AvgType avg_type_;
133+
int init_iterations_;
133134
Dtype moving_average_fraction_;
134135
int channels_;
135136
Dtype eps_;

src/caffe/layers/batch_norm_layer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void BatchNormLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
1313
moving_average_fraction_ = param.moving_average_fraction();
1414
use_global_stats_ = this->phase_ == TEST;
1515
avg_type_ = param.avg_type();
16+
init_iterations_ = param.init_iterations();
1617
if (param.has_use_global_stats())
1718
use_global_stats_ = param.use_global_stats();
1819
if (bottom[0]->num_axes() == 1)
@@ -125,7 +126,8 @@ void BatchNormLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
125126
variance_.mutable_cpu_data()); // E((X_EX)^2)
126127

127128
// compute and save moving average
128-
if (avg_type_ == BatchNormParameter_AvgType_EXPONENTIAL) {
129+
if (init_iterations_ < this->blobs_[2]->cpu_data()[0] ||
130+
avg_type_ == BatchNormParameter_AvgType_EXPONENTIAL) {
129131
Dtype scale_factor = this->blobs_[2]->cpu_data()[0] == 0 ?
130132
1 : 1 - moving_average_fraction_;
131133
caffe_cpu_axpby(mean_.count(), scale_factor,

src/caffe/layers/batch_norm_layer.cu

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ void BatchNormLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
6060
variance_.mutable_gpu_data()); // E((X_EX)^2)
6161

6262
// compute and save moving average
63-
if (avg_type_ == BatchNormParameter_AvgType_EXPONENTIAL) {
63+
if (init_iterations_ < this->blobs_[2]->cpu_data()[0] ||
64+
avg_type_ == BatchNormParameter_AvgType_EXPONENTIAL) {
6465
Dtype scale_factor = this->blobs_[2]->cpu_data()[0] == 0 ?
6566
1 : 1 - moving_average_fraction_;
6667
caffe_gpu_axpby(mean_.count(), scale_factor,

src/caffe/proto/caffe.proto

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,9 +481,12 @@ message BatchNormParameter {
481481
// How much does the moving average decay each iteration?
482482
// Used when avg_type = EXPONENTIAL
483483
optional float moving_average_fraction = 3 [default = .999];
484+
// How many iterations to use to make initial moving average value?
485+
// Used when avg_type = EXPONENTIAL
486+
optional int32 init_iterations = 4 [default = 100];
484487
// Small value to add to the variance estimate so that we don't divide by
485488
// zero.
486-
optional float eps = 4 [default = 1e-5];
489+
optional float eps = 5 [default = 1e-5];
487490
}
488491

489492
message ContrastiveLossParameter {

0 commit comments

Comments
 (0)