1 #include "caffe2/operators/channel_stats_op.h" 3 #include "caffe2/utils/eigen_utils.h" 9 bool ChannelStatsOp<CPUContext>::ComputeChannelStatsNCHW<
float>(
16 ConstEigenArrayMap<float> X_arr(X, HxW, N * C);
17 for (
int i = 0; i < C; ++i) {
18 sum[i] = X_arr.col(i).sum();
19 sumsq[i] = X_arr.col(i).square().sum();
21 for (
int i = 1; i < N; ++i) {
22 for (
int j = 0; j < C; ++j) {
23 const int c = i * C + j;
24 sum[j] += X_arr.col(c).sum();
25 sumsq[j] += X_arr.col(c).square().sum();
33 bool ChannelStatsOp<CPUContext>::ComputeChannelStatsNHWC<
float>(
40 ConstEigenArrayMap<float> X_arr(X, C, N * HxW);
41 EigenVectorArrayMap<float> sum_arr(sum, C);
42 EigenVectorArrayMap<float> sumsq_arr(sumsq, C);
43 sum_arr = X_arr.col(0);
44 sumsq_arr = X_arr.col(0).square();
45 for (
int i = 1; i < N * HxW; ++i) {
46 sum_arr += X_arr.col(i);
47 sumsq_arr += X_arr.col(i).square();
52 REGISTER_CPU_OPERATOR(ChannelStats, ChannelStatsOp<CPUContext>);
54 OPERATOR_SCHEMA(ChannelStats)
58 Given an input tensor in NCHW format, computes the sum of all elements per 59 channel and the sum of all elements squared per channel. These values can be 60 reduced across multiple batches and used to obtain the mean and variance across 61 the full set of batches. Using the new mean and variance as input to SpatialBN 62 has the effect of changing the batch size over which SpatialBN is applied. 64 .Input(0, "X",
"The input 4-dimensional tensor of shape NCHW")
68 "The output 1-dimensional tensor of size C containing the sum of " 69 "elements of X per channel.")
73 "The output 1-dimensional tensor of size C containing the sum of " 74 "elements squared per channel.");
76 SHOULD_NOT_DO_GRADIENT(ChannelStats);
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...