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