Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / engines / mkldnn / graph / layers / internal / graph_crop_test.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <gtest/gtest.h>
6 #include <gmock/gmock-spec-builders.h>
7 #include "mkldnn_plugin/mkldnn_graph.h"
8
9 #include "test_graph.hpp"
10
11 #include "single_layer_common.hpp"
12 #include <mkldnn_plugin/mkldnn_extension_utils.h>
13 #include <inference_engine/cnn_network_impl.hpp>
14 #include "tests_common.hpp"
15
16
17 using namespace ::testing;
18 using namespace std;
19 using namespace mkldnn;
20
21 struct crop_test_params {
22     struct {
23         size_t n;
24         size_t c;
25         size_t h;
26         size_t w;
27     } in;
28
29     std::vector<int> axis;
30     std::vector<int> offsets;
31     std::vector<int> dims;
32
33     size_t num_prim_desc;
34
35     MKLDNNPlugin::impl_desc_type selectedType;
36
37     std::vector<std::function<void(MKLDNNPlugin::PrimitiveDescInfo)>> comp;
38 };
39
40
41
42 template <typename data_t>
43 void ref_crop(InferenceEngine::TBlob<data_t> &src, InferenceEngine::TBlob<data_t> &dst, crop_test_params prm) {
44     data_t *dst_ptr = dst.data();
45
46     std::vector<int> offsets(4);
47     for (size_t i = 0; i < prm.offsets.size(); i++) {
48         offsets[prm.axis[i]] = prm.offsets[i];
49     }
50     int OFFSET_N = offsets.at(0);
51     int OFFSET_C = offsets.at(1);
52     int OFFSET_H = offsets.at(2);
53     int OFFSET_W = offsets.at(3);
54
55     const int ON = dst.dims().at(3);
56     const int OC = dst.dims().at(2);
57     const int OH = dst.dims().at(1);
58     const int OW = dst.dims().at(0);
59
60     const int _IN = src.dims().at(0);
61     const int IC = src.dims().at(1);
62     const int IH = src.dims().at(2);
63     const int IW = src.dims().at(3);
64
65     auto dst_off = [=](int n, int c, int h, int w) {
66         return (n * OW * OH * OC + c * OW * OH + h * OW + w);
67     };
68     auto src_off = [=](int n, int c, int h, int w) {
69         return (n * IW * IH * IC + c * IW * IH + h * IW + w);
70     };
71
72     ASSERT_GE(_IN - OFFSET_N, ON);
73     ASSERT_GE(IC - OFFSET_C, OC);
74     ASSERT_GE(IH - OFFSET_H, OH);
75     ASSERT_GE(IW - OFFSET_W, OW);
76
77     data_t* src_ptr = src.data();
78     for (int n = 0; n < ON; ++n) {
79         for (int c = 0; c < OC; ++c) {
80             for (int h = 0; h < OH; ++h) {
81                 for (int w = 0; w < OW; ++w) {
82                     dst_ptr[dst_off(n, c, h, w)] = src_ptr[src_off(n + OFFSET_N, c + OFFSET_C,
83                                                                    h + OFFSET_H, w + OFFSET_W)];
84                 }
85             }
86         }
87     }
88 }
89
90 class MKLDNNGraphCropTests: public TestsCommon,
91                                      public WithParamInterface<crop_test_params> {
92     std::string model_t = R"V0G0N(
93 <Net Name="Crop_Only" version="2" precision="FP32" batch="1">
94     <layers>
95         <layer name="in1" type="Input" precision="FP32" id="0">
96             <output>
97                 <port id="0">
98                     <dim>_IN_</dim>
99                     <dim>_IC_</dim>
100                     <dim>_IH_</dim>
101                     <dim>_IW_</dim>
102                 </port>
103             </output>
104         </layer>
105         <layer name="crop" id="1" type="Crop" precision="FP32">
106             <data axis="_AXC_" offset="_OFC_" dim="_DIMC_" />
107             <input>
108                 <port id="1">
109                     <dim>_IN_</dim>
110                     <dim>_IC_</dim>
111                     <dim>_IH_</dim>
112                     <dim>_IW_</dim>
113                 </port>
114             </input>
115             <output>
116                 <port id="2">
117                     <dim>_DN_</dim>
118                     <dim>_DC_</dim>
119                     <dim>_DH_</dim>
120                     <dim>_DW_</dim>
121                 </port>
122             </output>
123         </layer>
124     </layers>
125     <edges>
126         <edge from-layer="0" from-port="0" to-layer="1" to-port="1"/>
127     </edges>
128 </Net>
129 )V0G0N";
130
131 protected:
132     std::string getModel(crop_test_params p) {
133         std::string model = model_t;
134
135         std::string axis, offset, dim;
136         std::vector<size_t> outDims = {p.in.n, p.in.c, p.in.h, p.in.w};
137         for (size_t i = 0; i < p.offsets.size(); i++) {
138             if (!axis.empty())
139                 axis += ",";
140             axis += std::to_string(p.axis[i]);
141             if (!offset.empty())
142                 offset += ",";
143             offset += std::to_string(p.offsets[i]);
144             if (!dim.empty())
145                 dim += ",";
146             dim += std::to_string(p.dims[i]);
147             outDims[p.axis[i]] = p.dims[i];
148         }
149
150         REPLACE_WITH_NUM(model, "_IW_", p.in.w);
151         REPLACE_WITH_NUM(model, "_IH_", p.in.h);
152         REPLACE_WITH_NUM(model, "_IC_", p.in.c);
153         REPLACE_WITH_NUM(model, "_IN_", p.in.n);
154         REPLACE_WITH_NUM(model, "_DN_", outDims[0]);
155         REPLACE_WITH_NUM(model, "_DC_", outDims[1]);
156         REPLACE_WITH_NUM(model, "_DH_", outDims[2]);
157         REPLACE_WITH_NUM(model, "_DW_", outDims[3]);
158         REPLACE_WITH_STR(model, "_AXC_", axis);
159         REPLACE_WITH_STR(model, "_OFC_", offset);
160         REPLACE_WITH_STR(model, "_DIMC_", dim);
161         return model;
162     }
163
164     virtual void TearDown() {
165     }
166
167     virtual void SetUp() {
168         try {
169             TestsCommon::SetUp();
170             crop_test_params p = ::testing::WithParamInterface<crop_test_params>::GetParam();
171             std::string model = getModel(p);
172
173             InferenceEngine::CNNNetReader net_reader;
174             ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
175
176             MKLDNNGraphTestClass graph;
177             graph.CreateGraph(net_reader.getNetwork());
178
179             auto& nodes = graph.getNodes();
180             for (int i = 0; i < nodes.size(); i++) {
181                 if (nodes[i]->getType() == MKLDNNPlugin::Crop) {
182                     ASSERT_EQ(p.num_prim_desc, nodes[i]->getSupportedPrimitiveDescriptors().size());
183                     for (size_t j = 0; j < p.num_prim_desc && j < p.comp.size(); j++) {
184                         p.comp.at(j)(nodes[i]->getSupportedPrimitiveDescriptors().at(j));
185                     }
186                     ASSERT_NE(nullptr, nodes[i]->getSelectedPrimitiveDescriptor());
187                     ASSERT_EQ(p.selectedType, nodes[i]->getSelectedPrimitiveDescriptor()->getImplementationType());
188                 }
189             }
190
191             InferenceEngine::SizeVector dims_src = {p.in.n, p.in.c, p.in.h, p.in.w};
192
193             InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW,  dims_src);
194             src->allocate();
195             fill_data(src->buffer(), src->size());
196
197             InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
198
199             if (srcPtr == nullptr)
200                 FAIL() << "Cannot cast blob to TBlob<float>.";
201
202             InferenceEngine::BlobMap srcs;
203             srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
204
205             InferenceEngine::OutputsDataMap out;
206             out = net_reader.getNetwork().getOutputsInfo();
207             InferenceEngine::BlobMap outputBlobs;
208
209             std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
210
211             InferenceEngine::TBlob<float>::Ptr output;
212             output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
213             output->allocate();
214             outputBlobs[item.first] = output;
215
216             graph.Infer(srcs, outputBlobs);
217
218             InferenceEngine::TBlob<float> dst_ref(item.second->getTensorDesc());
219             dst_ref.allocate();
220
221             ref_crop(*srcPtr, dst_ref, p);
222
223             compare(*output, dst_ref);
224         } catch (const InferenceEngine::details::InferenceEngineException &e) {
225             FAIL() << e.what();
226         }
227     }
228 };
229
230 TEST_P(MKLDNNGraphCropTests, TestCrop) {}
231
232
233 INSTANTIATE_TEST_CASE_P(
234         TestCrop, MKLDNNGraphCropTests,
235         ::testing::Values(
236                 crop_test_params{{1, 5, 32, 32}, {1, 2, 3}, {2, 5, 4}, {2, 23, 23}, 1, MKLDNNPlugin::impl_desc_type::unknown, {
237                         [](MKLDNNPlugin::PrimitiveDescInfo impl) {
238                             ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
239                             ASSERT_EQ(1, impl.getConfig().inConfs.size());
240                             ASSERT_EQ(1, impl.getConfig().outConfs.size());
241                             ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
242                             ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
243                         }}},
244                 crop_test_params{{3, 8, 32, 32}, {0, 1, 2, 3}, {1, 0, 20, 20}, {2, 8, 5, 5}, 2, MKLDNNPlugin::impl_desc_type::unknown, {
245                         [](MKLDNNPlugin::PrimitiveDescInfo impl) {
246                             ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
247                             ASSERT_EQ(1, impl.getConfig().inConfs.size());
248                             ASSERT_EQ(1, impl.getConfig().outConfs.size());
249                             ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
250                             ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
251                         }} },
252                 crop_test_params{{1, 5, 32, 32}, {3}, {10}, {20}, 1, MKLDNNPlugin::impl_desc_type::unknown },
253                 crop_test_params{{1, 5, 32, 20}, {2, 3}, {30, 10}, {2, 10}, 1, MKLDNNPlugin::impl_desc_type::unknown }));
254
255 class MKLDNNGraphDynBatchCropTests: public MKLDNNGraphCropTests {
256 protected:
257
258     virtual void SetUp() {
259         try {
260             TestsCommon::SetUp();
261             crop_test_params p = ::testing::WithParamInterface<crop_test_params>::GetParam();
262             std::string model = getModel(p);
263             size_t MB = p.in.n;
264             if (MB < 2)
265                 MB = 2;
266
267             InferenceEngine::CNNNetReader net_reader;
268             ASSERT_NO_THROW(net_reader.ReadNetwork(model.data(), model.length()));
269             InferenceEngine::CNNNetwork network = net_reader.getNetwork();
270             network.setBatchSize(MB);
271
272             MKLDNNGraphTestClass graph;
273             graph.setProperty({{InferenceEngine::PluginConfigParams::KEY_DYN_BATCH_ENABLED, InferenceEngine::PluginConfigParams::YES}});
274             graph.CreateGraph(network);
275
276             InferenceEngine::SizeVector dims_src = {MB, p.in.c, p.in.h, p.in.w};
277             InferenceEngine::Blob::Ptr src = InferenceEngine::make_shared_blob<float, const InferenceEngine::SizeVector>(InferenceEngine::Precision::FP32, InferenceEngine::NCHW, dims_src);
278             InferenceEngine::TBlob<float>* srcPtr = dynamic_cast<InferenceEngine::TBlob<float>*>(src.get());
279             if (srcPtr == nullptr)
280                 FAIL() << "Cannot cast blob to TBlob<float>.";
281
282             src->allocate();
283             fill_data(src->buffer(), src->size());
284
285             InferenceEngine::BlobMap srcs;
286             srcs.insert(std::pair<std::string, InferenceEngine::Blob::Ptr>("in1", src));
287
288             InferenceEngine::OutputsDataMap out;
289             out = net_reader.getNetwork().getOutputsInfo();
290             InferenceEngine::BlobMap outputBlobs;
291
292             std::pair<std::string, InferenceEngine::DataPtr> item = *out.begin();
293
294             InferenceEngine::TBlob<float>::Ptr output;
295             output = InferenceEngine::make_shared_blob<float>(item.second->getTensorDesc());
296             output->allocate();
297             outputBlobs[item.first] = output;
298
299             auto checkCrop = [](const MKLDNNPlugin::MKLDNNNodePtr& node) {
300                 return node->getType() == MKLDNNPlugin::Crop;
301             };
302
303             graph.checkDynBatch(srcs, outputBlobs, MB, MB, checkCrop);
304             graph.checkDynBatch(srcs, outputBlobs, 1, MB, checkCrop);
305         } catch (const InferenceEngine::details::InferenceEngineException &e) {
306             FAIL() << e.what();
307         }
308     }
309 };
310
311 TEST_P(MKLDNNGraphDynBatchCropTests, TestsDynBatchCrop) {}
312
313 INSTANTIATE_TEST_CASE_P(
314         TestsDynBatchCrop, MKLDNNGraphDynBatchCropTests,
315         ::testing::Values(
316                 crop_test_params{{1, 5, 32, 32}, {1, 2, 3}, {2, 5, 4}, {2, 23, 23}, 1, MKLDNNPlugin::impl_desc_type::unknown, {
317                         [](MKLDNNPlugin::PrimitiveDescInfo impl) {
318                             ASSERT_EQ(MKLDNNPlugin::impl_desc_type::unknown, impl.getImplementationType());
319                             ASSERT_EQ(1, impl.getConfig().inConfs.size());
320                             ASSERT_EQ(1, impl.getConfig().outConfs.size());
321                             ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().inConfs.at(0).desc.getLayout());
322                             ASSERT_EQ(InferenceEngine::Layout::NCHW, impl.getConfig().outConfs.at(0).desc.getLayout());
323                         }}},
324                 crop_test_params{{1, 5, 32, 32}, {3}, {10}, {20}, 1, MKLDNNPlugin::impl_desc_type::unknown },
325                 crop_test_params{{1, 5, 32, 20}, {2, 3}, {30, 10}, {2, 10}, 1, MKLDNNPlugin::impl_desc_type::unknown }));