Caffe2 - C++ API
A deep learning, cross platform ML framework
norm_planar_yuv_op.cc
1 #include <array>
2 #include "caffe2/core/operator.h"
3 #include "caffe2/utils/math.h"
4 
5 namespace caffe2 {
6 
7 namespace {
8 
9 class NormalizePlanarYUVOp : public Operator<CPUContext> {
10  public:
11  USE_OPERATOR_FUNCTIONS(CPUContext);
12  using Operator<CPUContext>::Operator;
13 
14  bool RunOnDevice() {
15  const auto& X = Input(0);
16  const auto& M = Input(1); // mean
17  const auto& S = Input(2); // standard deviation
18  auto* Z = Output(0);
19  Z->ResizeLike(X);
20 
21  CAFFE_ENFORCE(X.dims().size() == 4);
22 
23  const auto N = X.dim32(0);
24  auto C = X.dim(1);
25  const auto H = X.dim(2);
26  const auto W = X.dim(3);
27  CAFFE_ENFORCE(C == M.dim(1));
28  CAFFE_ENFORCE(C == S.dim(1));
29  const auto* Xdata = X.data<float>();
30  auto* Zdata = Z->mutable_data<float>();
31 
32  int offset = H * W;
33  for (auto n = 0; n < N; n++) { // realistically N will always be 1
34  int batch_offset = n * C * offset;
35  for (auto c = 0; c < C; c++) {
36  ConstEigenVectorMap<float> channel_s(
37  &Xdata[batch_offset + (c * offset)], offset);
38  EigenVectorMap<float> channel_d(
39  &Zdata[batch_offset + (c * offset)], offset);
40  channel_d = channel_s.array() - M.data<float>()[c];
41  channel_d = channel_d.array() / S.data<float>()[c];
42  }
43  }
44  return true;
45  }
46 };
47 
48 REGISTER_CPU_OPERATOR(NormalizePlanarYUV, NormalizePlanarYUVOp);
49 OPERATOR_SCHEMA(NormalizePlanarYUV)
50  .NumInputs(3)
51  .NumOutputs(1)
52  .AllowInplace({{0, 0}});
53 ;
54 } // namespace
55 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...