1 #include "caffe2/operators/channel_stats_op.h" 6 bool ChannelStatsOp<CPUContext>::RunOnDevice() {
7 const auto& X = Input(INPUT);
8 CAFFE_ENFORCE(X.ndim() >= 3 && X.ndim() <= 5);
9 const int N = X.dim32(0);
10 const int C = X.dim32(1);
11 const int H = X.dim32(2);
12 const int W = X.ndim() > 3 ? X.dim32(3) : 1;
13 const int D = X.ndim() > 4 ? X.dim32(4) : 1;
15 const int sampleSize = H * W * D;
17 Output(SUM)->Resize(C);
18 Output(SUMSQ)->Resize(C);
19 EigenVectorArrayMap<float> sum(Output(SUM)->mutable_data<float>(), C);
20 EigenVectorArrayMap<float> sumsq(Output(SUMSQ)->mutable_data<float>(), C);
24 ConstEigenArrayMap<float> X_arr(X.data<
float>(), sampleSize, N * C);
26 for (
int n = 0; n < N; ++n) {
27 for (
int c = 0; c < C; ++c) {
28 sum(c) += X_arr.col(index).sum();
29 sumsq(c) += X_arr.col(index).matrix().squaredNorm();
37 REGISTER_CPU_OPERATOR(ChannelStats, ChannelStatsOp<CPUContext>);
39 OPERATOR_SCHEMA(ChannelStats)
43 Given an input tensor in NCHW format, computes the sum of all elements per 44 channel and the sum of all elements squared per channel. These values can be 45 reduced across multiple batches and used to obtain the mean and variance across 46 the full set of batches. Using the new mean and variance as input to SpatialBN 47 has the effect of changing the batch size over which SpatialBN is applied. 50 .Input(0, "X",
"The input 4-dimensional tensor of shape NCHW")
54 "The output 1-dimensional tensor of size C containing the sum of " 55 "elements of X per channel.")
59 "The output 1-dimensional tensor of size C containing the sum of " 60 "elements squared per channel.");
61 SHOULD_NOT_DO_GRADIENT(ChannelStats);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...