Skip to content

Commit 7f42af0

Browse files
committed
Returned shift and scale back to BN layer + stability improvement
1 parent c9eda39 commit 7f42af0

11 files changed

+1110
-321
lines changed

include/caffe/layers/batch_norm_layer.hpp

+23-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include "caffe/layer.hpp"
88
#include "caffe/proto/caffe.pb.h"
99

10+
#define BN_VARIANCE_CLIP_START 200
11+
#define BN_VARIANCE_CLIP_CONST 4.0
12+
1013
namespace caffe {
1114

1215
/**
@@ -63,17 +66,30 @@ class BatchNormLayer : public Layer<Dtype> {
6366
virtual void Backward_gpu(const vector<Blob<Dtype>*>& top,
6467
const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
6568

66-
Blob<Dtype> mean_, variance_, temp_, x_norm_;
69+
virtual void multicast_cpu(int N, int C, int S, const Dtype *x, Dtype *y);
70+
virtual void compute_sum_per_channel_cpu(int N, int C, int S,
71+
const Dtype *x, Dtype *y);
72+
virtual void compute_mean_per_channel_cpu(int N, int C, int S,
73+
const Dtype *x, Dtype *y);
74+
#ifndef CPU_ONLY
75+
virtual void compute_sum_per_channel_gpu(int N, int C, int S,
76+
const Dtype *x, Dtype *y);
77+
virtual void multicast_gpu(int N, int C, int S, const Dtype *x, Dtype *y);
78+
virtual void compute_mean_per_channel_gpu(int N, int C, int S,
79+
const Dtype *x, Dtype *y);
80+
#endif
81+
82+
Blob<Dtype> mean_, variance_, inv_variance_, x_norm_;
6783
bool use_global_stats_;
6884
Dtype moving_average_fraction_;
6985
int channels_;
7086
Dtype eps_;
71-
72-
// extra temporarary variables is used to carry out sums/broadcasting
73-
// using BLAS
74-
Blob<Dtype> batch_sum_multiplier_;
75-
Blob<Dtype> num_by_chans_;
76-
Blob<Dtype> spatial_sum_multiplier_;
87+
int iter_;
88+
// auxiliary arrays
89+
Blob<Dtype> ones_N_, ones_HW_, ones_C_;
90+
Blob<Dtype> temp_;
91+
Blob<Dtype> temp_C_;
92+
Blob<Dtype> temp_NC_;
7793
};
7894

7995
} // namespace caffe

include/caffe/layers/cudnn_batch_norm_layer.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ template <typename Dtype>
1616
class CuDNNBatchNormLayer : public BatchNormLayer<Dtype> {
1717
public:
1818
explicit CuDNNBatchNormLayer(const LayerParameter& param)
19-
: BatchNormLayer<Dtype>(param), epsilon_(1e-4), handles_setup_(false) {}
19+
: BatchNormLayer<Dtype>(param), handles_setup_(false) {}
2020
virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
2121
const vector<Blob<Dtype>*>& top);
2222
virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
@@ -34,7 +34,6 @@ class CuDNNBatchNormLayer : public BatchNormLayer<Dtype> {
3434
cudnnTensorDescriptor_t scale_bias_mean_var_desc_;
3535
cudnnBatchNormMode_t mode_;
3636

37-
double epsilon_;
3837
Blob<Dtype> save_mean_, save_inv_var_;
3938
bool handles_setup_;
4039
};

include/caffe/util/math_functions.hpp

+21
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ template <typename Dtype>
3333
void caffe_cpu_axpby(const int N, const Dtype alpha, const Dtype* X,
3434
const Dtype beta, Dtype* Y);
3535

36+
// y[i]= max(a*x[i], b*y[i])
37+
template <typename Dtype>
38+
void caffe_cpu_eltwise_max(const int N, const Dtype alpha, const Dtype* X,
39+
const Dtype beta, Dtype* Y);
40+
41+
// y[i]= min(a*x[i], b*y[i])
42+
template <typename Dtype>
43+
void caffe_cpu_eltwise_min(const int N, const Dtype alpha, const Dtype* X,
44+
const Dtype beta, Dtype* Y);
45+
3646
template <typename Dtype>
3747
void caffe_copy(const int N, const Dtype *X, Dtype *Y);
3848

@@ -246,6 +256,17 @@ void caffe_gpu_fabs(const int n, const Dtype* x, Dtype* y);
246256
template <typename Dtype>
247257
void caffe_gpu_scale(const int n, const Dtype alpha, const Dtype *x, Dtype* y);
248258

259+
// y[i]= max(a*x[i], b*y[i])
260+
template <typename Dtype>
261+
void caffe_gpu_eltwise_max(const int n, const Dtype alpha, const Dtype* x,
262+
const Dtype beta, Dtype* y);
263+
264+
// y[i]= min(a*x[i], b*y[i])
265+
template <typename Dtype>
266+
void caffe_gpu_eltwise_min(const int n, const Dtype alpha, const Dtype* x,
267+
const Dtype beta, Dtype* y);
268+
269+
249270
#define DEFINE_AND_INSTANTIATE_GPU_UNARY_FUNC(name, operation) \
250271
template<typename Dtype> \
251272
__global__ void name##_kernel(const int n, const Dtype* x, Dtype* y) { \

0 commit comments

Comments
 (0)