reid from https://github.com/michuanhaohao/reid-strong-baseline
zhangmeng
2020-01-17 f7c4a3cfd07adede3308f8d9d3d7315427d90a7c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef CAFFE2_OPERATORS_CHANNEL_STATS_OP_H_
#define CAFFE2_OPERATORS_CHANNEL_STATS_OP_H_
 
#include <string>
 
#include "caffe2/core/context.h"
#include "caffe2/core/operator.h"
#include "caffe2/utils/math.h"
 
namespace caffe2 {
 
template <class Context>
class ChannelStatsOp final : public Operator<Context> {
 public:
  USE_OPERATOR_CONTEXT_FUNCTIONS;
 
  template <class... Args>
  explicit ChannelStatsOp(Args&&... args)
      : Operator<Context>(std::forward<Args>(args)...),
        order_(StringToStorageOrder(
            this->template GetSingleArgument<std::string>("order", "NCHW"))) {
    CAFFE_ENFORCE_NE(order_, StorageOrder::UNKNOWN);
  }
 
  bool RunOnDevice() override {
    return DispatchHelper<TensorTypes<float>>::call(this, Input(0));
  }
 
  template <typename T>
  bool DoRunWithType() {
    const auto& X = Input(0);
    const int ndim = X.dim();
    const int N = X.dim32(0);
    const int C = order_ == StorageOrder::NCHW ? X.dim32(1) : X.dim32(ndim - 1);
    const int HxW = X.numel() / (N * C);
    auto* sum = Output(0, {C}, at::dtype<T>());
    auto* sumsq = Output(1, {C}, at::dtype<T>());
    const T* X_data = X.template data<T>();
    T* sum_data = sum->template mutable_data<T>();
    T* sumsq_data = sumsq->template mutable_data<T>();
    return order_ == StorageOrder::NCHW
        ? ComputeChannelStatsNCHW<T>(N, C, HxW, X_data, sum_data, sumsq_data)
        : ComputeChannelStatsNHWC<T>(N, C, HxW, X_data, sum_data, sumsq_data);
  }
 
 private:
  template <typename T>
  bool
  ComputeChannelStatsNCHW(int N, int C, int HxW, const T* X, T* sum, T* sumsq);
 
  template <typename T>
  bool
  ComputeChannelStatsNHWC(int N, int C, int HxW, const T* X, T* sum, T* sumsq);
 
  const StorageOrder order_;
};
 
} // namespace caffe2
 
#endif // CAFFE2_OPERATORS_CHANNEL_STATS_OP_H_