Publishing 2019 R1.1 content and Myriad plugin sources (#162)
[platform/upstream/dldt.git] / inference-engine / tests / unit / opencv_test_gapi / fluid_test_computations / fluid_test_computations.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <fluid_test_computations.hpp>
6 #include <opencv2/gapi.hpp>
7 #include <ie_preprocess_gapi_kernels.hpp>
8
9 #define CV_MAT_CHANNELS(flags) (((flags) >> CV_CN_SHIFT) + 1)
10
11 namespace opencv_test
12 {
13 struct FluidComputation::Priv
14 {
15     cv::GComputation m_c;
16     std::vector<cv::gapi::own::Mat> m_v_in;
17     std::vector<cv::gapi::own::Mat> m_v_out;
18 };
19
20 FluidComputation::FluidComputation(Priv *priv)
21     : m_priv(priv)
22 {}
23
24 void FluidComputation::warmUp()
25 {
26     m_priv->m_c.apply(m_priv->m_v_in, m_priv->m_v_out, cv::compile_args(InferenceEngine::gapi::preprocKernels()));
27 }
28
29 void FluidComputation::apply()
30 {
31     m_priv->m_c.apply(m_priv->m_v_in, m_priv->m_v_out);
32 }
33
34 namespace
35 {
36 cv::gapi::own::Mat to_own(test::Mat mat) { return {mat.rows, mat.cols, mat.type, mat.data}; }
37
38 std::vector<cv::gapi::own::Mat> to_own(std::vector<test::Mat> mats)
39 {
40     std::vector<cv::gapi::own::Mat> own_mats(mats.size());
41     for (int i = 0; i < mats.size(); i++) {
42         own_mats[i] = to_own(mats[i]);
43     }
44     return own_mats;
45 }
46
47 template<typename... Ts, int... IIs>
48 std::vector<cv::GMat> to_vec_impl(std::tuple<Ts...> &&gmats, cv::detail::Seq<IIs...>) {
49     return { std::get<IIs>(gmats)... };
50 }
51
52 template<typename... Ts>
53 std::vector<cv::GMat> to_vec(std::tuple<Ts...> &&gmats) {
54     return to_vec_impl(std::move(gmats), typename cv::detail::MkSeq<sizeof...(Ts)>::type());
55 }
56 } // anonymous namespace
57
58 static cv::GComputation buildResizeComputation(test::Mat inMat, test::Mat outMat, int interp)
59 {
60     cv::gapi::own::Size sz_in  { inMat.cols,  inMat.rows};
61     cv::gapi::own::Size sz_out {outMat.cols, outMat.rows};
62     int type = outMat.type;
63     cv::GMat in, out;
64     switch (CV_MAT_CHANNELS(type)) {
65     case 1:
66         out = InferenceEngine::gapi::ScalePlane::on(in, type, sz_in, sz_out, interp);
67         break;
68     case 3:
69         {
70         int depth = CV_MAT_DEPTH(type);
71         int type1 = CV_MAKE_TYPE(depth, 1);
72         cv::GMat in0, in1, in2, out0, out1, out2;
73         std::tie(in0, in1, in2) = InferenceEngine::gapi::Split3::on(in);
74         out0 = InferenceEngine::gapi::ScalePlane::on(in0, type1, sz_in, sz_out, interp);
75         out1 = InferenceEngine::gapi::ScalePlane::on(in1, type1, sz_in, sz_out, interp);
76         out2 = InferenceEngine::gapi::ScalePlane::on(in2, type1, sz_in, sz_out, interp);
77         out = InferenceEngine::gapi::Merge3::on(out0, out1, out2);
78         }
79         break;
80     default: GAPI_Assert(!"ERROR: unsupported number of channels!");
81     }
82
83     return cv::GComputation(in, out);
84 }
85
86 FluidResizeComputation::FluidResizeComputation(test::Mat inMat, test::Mat outMat, int interp)
87     : FluidComputation(new Priv{buildResizeComputation(inMat, outMat, interp)
88                                ,{to_own(inMat)}
89                                ,{to_own(outMat)}
90                                })
91 {}
92
93 static cv::GComputation buildSplitComputation(int planes)
94 {
95     std::vector<cv::GMat> ins(1);
96     std::vector<cv::GMat> outs(planes);
97
98     switch (planes) {
99     case 2: outs = to_vec(InferenceEngine::gapi::Split2::on(ins[0])); break;
100     case 3: outs = to_vec(InferenceEngine::gapi::Split3::on(ins[0])); break;
101     case 4: outs = to_vec(InferenceEngine::gapi::Split4::on(ins[0])); break;
102     default: GAPI_Assert(false);
103     }
104
105     return cv::GComputation(ins, outs);
106 }
107
108 FluidSplitComputation::FluidSplitComputation(test::Mat inMat, std::vector<test::Mat> outMats)
109     : FluidComputation(new Priv{buildSplitComputation(outMats.size())
110                                ,{to_own(inMat)}
111                                ,to_own(outMats)
112                                })
113 {}
114
115 static cv::GComputation buildMergeComputation(int planes)
116 {
117     std::vector<cv::GMat> ins(planes);
118     std::vector<cv::GMat> outs(1);
119
120     switch (planes) {
121     case 2: outs[0] = InferenceEngine::gapi::Merge2::on(ins[0], ins[1]); break;
122     case 3: outs[0] = InferenceEngine::gapi::Merge3::on(ins[0], ins[1], ins[2]); break;
123     case 4: outs[0] = InferenceEngine::gapi::Merge4::on(ins[0], ins[1], ins[2], ins[3]); break;
124     default: GAPI_Assert(false);
125     }
126
127     return cv::GComputation(ins, outs);
128 }
129
130 FluidMergeComputation::FluidMergeComputation(std::vector<test::Mat> inMats, test::Mat outMat)
131     : FluidComputation(new Priv{buildMergeComputation(inMats.size())
132                                ,to_own(inMats)
133                                ,{to_own(outMat)}
134                                })
135 {}
136
137 } // namespace opencv_test