Skip to content

Commit 886dc26

Browse files
committed
Apply add_eps_before_sqrt.patch & revert MCW yolo2 patch
1 parent add6984 commit 886dc26

File tree

10 files changed

+51
-31
lines changed

10 files changed

+51
-31
lines changed

include/caffe/layers/batch_norm_layer.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ class BatchNormLayer : public Layer<Dtype> {
6565
Dtype moving_average_fraction_;
6666
int channels_;
6767
Dtype eps_;
68-
bool yolo_bn_;
69-
Dtype yolo_eps_;
68+
bool add_eps_before_sqrt_;
7069
bool update_global_stats_;
7170
bool icnet_; //CUSTOMIZATION
7271

include/caffe/layers/mvn_layer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class MVNLayer : public Layer<Dtype> {
4141
/// sum_multiplier is used to carry out sum using BLAS
4242
Blob<Dtype> sum_multiplier_;
4343
Dtype eps_;
44+
bool add_eps_before_sqrt_;
4445
};
4546

4647
} // namespace caffe

include/caffe/layers/normalize_layer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class NormalizeLayer : public Layer<Dtype> {
4444
bool across_spatial_;
4545
bool channel_shared_;
4646
Dtype eps_;
47+
bool add_eps_before_sqrt_;
4748
};
4849

4950
} // namespace caffe

src/caffe/layers/batch_norm_layer.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ void BatchNormLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
2020
else
2121
channels_ = bottom[0]->shape(1);
2222
eps_ = param.eps();
23-
yolo_bn_ = param.yolo_bn();
24-
yolo_eps_ = param.yolo_eps();
23+
add_eps_before_sqrt_ = param.add_eps_before_sqrt();
2524
update_global_stats_ = param.update_global_stats();
2625
icnet_ = param.icnet();
2726
if (this->blobs_.size() > 0) {
@@ -153,15 +152,14 @@ void BatchNormLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
153152
}
154153

155154
// normalize variance
156-
if(yolo_bn_) {
157-
caffe_sqrt(variance_.count(), variance_.cpu_data(),
158-
variance_.mutable_cpu_data());
159-
caffe_add_scalar(variance_.count(), yolo_eps_, variance_.mutable_cpu_data());
160-
}
161-
else {
155+
if (add_eps_before_sqrt_) {
162156
caffe_add_scalar(variance_.count(), eps_, variance_.mutable_cpu_data());
163157
caffe_sqrt(variance_.count(), variance_.cpu_data(),
164-
variance_.mutable_cpu_data());
158+
variance_.mutable_cpu_data());
159+
} else {
160+
caffe_sqrt(variance_.count(), variance_.cpu_data(),
161+
variance_.mutable_cpu_data());
162+
caffe_add_scalar(variance_.count(), eps_, variance_.mutable_cpu_data());
165163
}
166164

167165
// replicate variance to input size

src/caffe/layers/batch_norm_layer.cu

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,14 @@ void BatchNormLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
7171
}
7272

7373
// normalize variance
74-
if(yolo_bn_) {
75-
caffe_sqrt(variance_.count(), variance_.gpu_data(),
76-
variance_.mutable_gpu_data());
77-
caffe_add_scalar(variance_.count(), yolo_eps_, variance_.mutable_gpu_data());
78-
}
79-
else {
74+
if (add_eps_before_sqrt_) {
8075
caffe_gpu_add_scalar(variance_.count(), eps_, variance_.mutable_gpu_data());
8176
caffe_gpu_sqrt(variance_.count(), variance_.gpu_data(),
82-
variance_.mutable_gpu_data());
77+
variance_.mutable_gpu_data());
78+
} else {
79+
caffe_gpu_sqrt(variance_.count(), variance_.gpu_data(),
80+
variance_.mutable_gpu_data());
81+
caffe_gpu_add_scalar(variance_.count(), eps_, variance_.mutable_gpu_data());
8382
}
8483

8584
// replicate variance to input size

src/caffe/layers/mvn_layer.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void MVNLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
2525
Dtype* multiplier_data = sum_multiplier_.mutable_cpu_data();
2626
caffe_set(sum_multiplier_.count(), Dtype(1), multiplier_data);
2727
eps_ = this->layer_param_.mvn_param().eps();
28+
add_eps_before_sqrt_ = this->layer_param_.mvn_param().add_eps_before_sqrt();
2829
}
2930

3031
template <typename Dtype>
@@ -57,10 +58,15 @@ void MVNLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
5758
variance_.mutable_cpu_data()); // E((X-EX)^2)
5859

5960
// normalize variance
60-
caffe_powx(variance_.count(), variance_.cpu_data(), Dtype(0.5),
61-
variance_.mutable_cpu_data());
62-
63-
caffe_add_scalar(variance_.count(), eps_, variance_.mutable_cpu_data());
61+
if (add_eps_before_sqrt_) {
62+
caffe_add_scalar(variance_.count(), eps_, variance_.mutable_cpu_data());
63+
caffe_powx(variance_.count(), variance_.cpu_data(), Dtype(0.5),
64+
variance_.mutable_cpu_data());
65+
} else {
66+
caffe_powx(variance_.count(), variance_.cpu_data(), Dtype(0.5),
67+
variance_.mutable_cpu_data());
68+
caffe_add_scalar(variance_.count(), eps_, variance_.mutable_cpu_data());
69+
}
6470

6571
caffe_cpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, 1.,
6672
variance_.cpu_data(), sum_multiplier_.cpu_data(), 0.,

src/caffe/layers/mvn_layer.cu

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ void MVNLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
3636
variance_.mutable_gpu_data()); // E((X-EX)^2)
3737

3838
// normalize variance
39-
caffe_gpu_powx(variance_.count(), variance_.gpu_data(), Dtype(0.5),
40-
variance_.mutable_gpu_data());
41-
42-
caffe_gpu_add_scalar(variance_.count(), eps_, variance_.mutable_gpu_data());
39+
if (add_eps_before_sqrt_) {
40+
caffe_gpu_add_scalar(variance_.count(), eps_, variance_.mutable_gpu_data());
41+
caffe_gpu_powx(variance_.count(), variance_.gpu_data(), Dtype(0.5),
42+
variance_.mutable_gpu_data());
43+
} else {
44+
caffe_gpu_powx(variance_.count(), variance_.gpu_data(), Dtype(0.5),
45+
variance_.mutable_gpu_data());
46+
caffe_gpu_add_scalar(variance_.count(), eps_, variance_.mutable_gpu_data());
47+
}
4348

4449
caffe_gpu_gemm<Dtype>(CblasNoTrans, CblasNoTrans, num, dim, 1, 1.,
4550
variance_.gpu_data(), sum_multiplier_.gpu_data(), 0.,

src/caffe/layers/normalize_layer.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ void NormalizeLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
2222
norm_.Reshape(bottom[0]->num(), 1, bottom[0]->height(), bottom[0]->width());
2323
}
2424
eps_ = norm_param.eps();
25+
add_eps_before_sqrt_ = norm_param.add_eps_before_sqrt();
2526
int channels = bottom[0]->channels();
2627
int spatial_dim = bottom[0]->width() * bottom[0]->height();
2728
sum_channel_multiplier_.Reshape(1, channels, 1, 1);
@@ -101,8 +102,13 @@ void NormalizeLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
101102
caffe_sqr<Dtype>(dim, bottom_data, buffer_data);
102103
if (across_spatial_) {
103104
// add eps to avoid overflow
104-
norm_data[n] = pow(caffe_cpu_asum<Dtype>(dim, buffer_data)+eps_,
105-
Dtype(0.5));
105+
if (add_eps_before_sqrt_) {
106+
norm_data[n] = pow(caffe_cpu_asum<Dtype>(dim, buffer_data)+eps_,
107+
Dtype(0.5));
108+
} else {
109+
norm_data[n] = pow(caffe_cpu_asum<Dtype>(dim, buffer_data),
110+
Dtype(0.5))+eps_;
111+
}
106112
caffe_cpu_scale<Dtype>(dim, Dtype(1.0 / norm_data[n]), bottom_data,
107113
top_data);
108114
} else {

src/caffe/layers/normalize_layer.cu

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ void NormalizeLayer<Dtype>::Forward_gpu(const vector<Blob<Dtype>*>& bottom,
7373
Dtype normsqr;
7474
caffe_gpu_asum<Dtype>(dim, buffer_data, &normsqr);
7575
// add eps to avoid overflow
76-
norm_data[n] = pow(normsqr+eps_, Dtype(0.5));
76+
if (add_eps_before_sqrt_) {
77+
norm_data[n] = pow(normsqr+eps_, Dtype(0.5));
78+
} else {
79+
norm_data[n] = pow(normsqr, Dtype(0.5))+eps_;
80+
}
7781
caffe_gpu_scale<Dtype>(dim, Dtype(1.0 / norm_data[n]), bottom_data,
7882
top_data);
7983
} else {

src/caffe/proto/caffe.proto

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,7 @@ message BatchNormParameter {
11361136
// Small value to add to the variance estimate so that we don't divide by
11371137
// zero.
11381138
optional float eps = 3 [default = 1e-5];
1139-
optional bool yolo_bn = 4 [default = false];
1140-
optional float yolo_eps = 5 [default = 1e-6];
1139+
optional bool add_eps_before_sqrt = 4 [default = true];
11411140
optional bool update_global_stats = 6 [default = true]; //CUSTOMIZATION
11421141
optional bool icnet = 7 [default = false]; //CUSTOMIZATION
11431142
}
@@ -1862,6 +1861,7 @@ message MVNParameter {
18621861

18631862
// Epsilon for not dividing by zero while normalizing variance
18641863
optional float eps = 3 [default = 1e-9];
1864+
optional bool add_eps_before_sqrt = 4 [default = false];
18651865
}
18661866

18671867
// Message that stores parameters used by NormalizeLayer
@@ -1873,6 +1873,7 @@ message NormalizeParameter {
18731873
optional bool channel_shared = 3 [default = true];
18741874
// Epsilon for not dividing by zero while normalizing variance
18751875
optional float eps = 4 [default = 1e-10];
1876+
optional bool add_eps_before_sqrt = 5 [default = true];
18761877
}
18771878

18781879
message ParameterParameter {

0 commit comments

Comments
 (0)