Caffe2 - C++ API
A deep learning, cross platform ML framework
norm_planar_yuv_op.cc
1 #include "caffe2/mobile/contrib/arm-compute/core/context.h"
2 #include "caffe2/mobile/contrib/arm-compute/core/operator.h"
3 
4 namespace caffe2 {
5 
6 template <typename T>
7 class GLNormalizePlanarYUVOp final : public Operator<GLContext> {
8 public:
9  GLNormalizePlanarYUVOp(const OperatorDef &operator_def, Workspace *ws)
10  : Operator<GLContext>(operator_def, ws) {}
11  virtual ~GLNormalizePlanarYUVOp() noexcept {}
12  USE_OPERATOR_FUNCTIONS(GLContext);
13  bool RunOnDevice() override;
14 private:
15  arm_compute::GCNormalizePlanarYUVLayer norm_layer_;
16  bool first_run_ = true, second_run_ = true;
17  GLContext::deleted_unique_ptr<const GLTensor<T>> X_, mean_, sd_;
18 };
19 
20 template <typename T> bool GLNormalizePlanarYUVOp<T>::RunOnDevice() {
21 
22  auto Xblob = OperatorBase::Inputs()[0];
23  auto *meanblob = OperatorBase::Inputs()[1];
24  auto *sdblob = OperatorBase::Inputs()[2];
25 
26  if (first_run_) {
27  X_ = GLContext::getGLTensor<T>(Xblob);
28  mean_ = GLContext::getGLTensor<T>(meanblob);
29  sd_ = GLContext::getGLTensor<T>(sdblob);
30  }
31 
32  CAFFE_ENFORCE_EQ(X_->ndim(), 4);
33  auto N = X_->dim32(0);
34  auto C = X_->dim32(1);
35  auto H = X_->dim32(2);
36  auto W = X_->dim32(3);
37 
38  CAFFE_ENFORCE_EQ(C, mean_->dim32(1));
39  CAFFE_ENFORCE_EQ(C, sd_->dim32(1));
40 
41  GLTensor<T> *Y =
42  OperatorBase::Outputs()[0]->template GetMutable<GLTensor<T>>();
43  if (first_run_) {
44  first_run_ = false;
45  Y->ResizeLike(*X_);
46  norm_layer_.configure(X_->get_underlying(), Y->get_underlying(), mean_->get_underlying(), sd_->get_underlying());
47  } else {
48  X_->lazy_allocate(Xblob, second_run_, true);
49  mean_->lazy_allocate(meanblob, second_run_, second_run_);
50  sd_->lazy_allocate(sdblob, second_run_, second_run_);
51  if (second_run_) {
52  second_run_ = false;
53  Y->allocate();
54  }
55  norm_layer_.run();
56  }
57 
58  return true;
59 }
60 
61 REGISTER_GL_OPERATOR(NormalizePlanarYUV, GLNormalizePlanarYUVOp<DataType>);
62 
63 } // namespace caffe2
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...