Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / topology_verification_tests / v2_topology_verification_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 "cpp/ie_cnn_network.h"
7 #include "xml_father.hpp"
8 #include "xml_net_builder.hpp"
9 #include "xml_helper.hpp"
10 #include "pugixml.hpp"
11 #include "inference_engine/ie_format_parser.h"
12 #include <fstream>
13 #include <stdio.h>
14 #include "details/ie_exception.hpp"
15
16 using namespace std;
17 using namespace InferenceEngine;
18 using namespace testing;
19
20 class V2TopologyVerificationTests : public ::testing::Test {
21 protected:
22     virtual void TearDown() {}
23     virtual void SetUp() {
24         xmlHelper.reset(new XMLHelper(new details::FormatParser(2)));
25     }
26 public:
27     unique_ptr<CNNNetwork> cnnNetwork;
28     unique_ptr<XMLHelper> xmlHelper;
29
30     string getNetworkWithConvLayer(string layerPrecision = "Q78", std::vector<size_t > layerInput = { 1, 3, 227, 227 }) {
31         std::vector<size_t > inputDims = { 1, 3, 227, 227 };
32         std::vector<size_t > outputDims = { 1, 96, 55, 55 };
33
34         return V2NetBuilder::buildNetworkWithOneInput("",layerInput)
35             .havingLayers()
36                 .convolutionLayer(layerPrecision, { {inputDims}, {outputDims} })
37             .finish();
38     }
39
40     string getNetworkWithConvLayerWithInputPrecision(string inputPrecision, string layerPrecision = "Q78",
41                                                      std::vector<size_t > layerInput = {1, 3, 227, 227}) {
42         std::vector<size_t > inputDims = {1, 3, 227, 227};
43         std::vector<size_t > outputDims = {1, 96, 55, 55};
44
45         return V2NetBuilder::buildNetworkWithOneInput("",layerInput, inputPrecision)
46                 .havingLayers()
47                 .convolutionLayer(layerPrecision, {{inputDims}, {outputDims}})
48                 .finish();
49     }
50
51     string getNetworkWithPoolLayer(std::vector<size_t > layerInput = { 1, 3, 227, 227 }) {
52         std::vector<size_t > inputDims = { 1, 3, 227, 227 };
53         std::vector<size_t > outputDims = { 1, 96, 55, 55 };
54
55         return V2NetBuilder::buildNetworkWithOneInput("",layerInput)
56             .havingLayers()
57                 .poolingLayer({ { inputDims },{ outputDims } })
58             .finish();
59     }
60
61     string getNetworkWithCropLayer(CropParams params, std::vector<size_t > layerInput = { 1, 3, 227, 227 }) {
62         std::vector<size_t > inputDims = { 1, 3, 227, 227 };
63         std::vector<size_t > outputDims = { 1, 3, 200, 227 };
64
65         return V2NetBuilder::buildNetworkWithOneInput("",layerInput)
66             .havingLayers()
67                 .cropLayer(params, { {inputDims}, {outputDims} })
68             .finish();
69     }
70 };
71
72 TEST_F(V2TopologyVerificationTests, testNoThrow) {
73     string testContent = getNetworkWithConvLayer();
74
75     xmlHelper->loadContent(testContent);
76     try {
77         xmlHelper->parse();
78     } catch (InferenceEngine::details::InferenceEngineException ex) {
79         FAIL() << ex.what();
80     }
81 }
82
83 TEST_F(V2TopologyVerificationTests, testDefaultPrecisionsForFP16InputAndOutputLayers) {
84     string testContent = getNetworkWithConvLayerWithInputPrecision(Precision(Precision::FP16).name(),
85                                                                    Precision(Precision::FP16).name());
86
87     InferenceEngine::details::CNNNetworkImplPtr cnnNetworkImplPtr;
88     xmlHelper->loadContent(testContent);
89     try {
90         cnnNetworkImplPtr = xmlHelper->parseWithReturningNetwork();
91     } catch (InferenceEngine::details::InferenceEngineException ex) {
92         FAIL() << ex.what();
93     }
94     OutputsDataMap outputsDataMap;
95     cnnNetworkImplPtr->getOutputsInfo(outputsDataMap);
96     for (auto outputData: outputsDataMap) {
97         ASSERT_TRUE(outputData.second->getPrecision() == Precision::FP32);
98     }
99     InputsDataMap inputsDataMap;
100     cnnNetworkImplPtr->getInputsInfo(inputsDataMap);
101     for (auto inputData: inputsDataMap) {
102         ASSERT_TRUE(inputData.second->getInputPrecision() == Precision::FP32);
103     }
104 }
105
106 TEST_F(V2TopologyVerificationTests, testDefaultPrecisionsFP32InputAndOutputLayers) {
107     string testContent = getNetworkWithConvLayerWithInputPrecision(Precision(Precision::FP32).name(),
108                                                                    Precision(Precision::FP32).name());
109
110     InferenceEngine::details::CNNNetworkImplPtr cnnNetworkImplPtr;
111     xmlHelper->loadContent(testContent);
112     try {
113         cnnNetworkImplPtr = xmlHelper->parseWithReturningNetwork();
114     } catch (InferenceEngine::details::InferenceEngineException ex) {
115         FAIL() << ex.what();
116     }
117     OutputsDataMap outputsDataMap;
118     cnnNetworkImplPtr->getOutputsInfo(outputsDataMap);
119     for (auto outputData: outputsDataMap) {
120         ASSERT_TRUE(outputData.second->getPrecision() == Precision::FP32);
121     }
122     InputsDataMap inputsDataMap;
123     cnnNetworkImplPtr->getInputsInfo(inputsDataMap);
124     for (auto inputData: inputsDataMap) {
125         ASSERT_TRUE(inputData.second->getInputPrecision() == Precision::FP32);
126     }
127 }
128
129 TEST_F(V2TopologyVerificationTests, testDefaultPrecisionsForQ78InputAndOutputLayers) {
130     string testContent = getNetworkWithConvLayerWithInputPrecision(Precision(Precision::Q78).name(),
131                                                                    Precision(Precision::Q78).name());
132
133     InferenceEngine::details::CNNNetworkImplPtr cnnNetworkImplPtr;
134     xmlHelper->loadContent(testContent);
135     try {
136         cnnNetworkImplPtr = xmlHelper->parseWithReturningNetwork();
137     } catch (InferenceEngine::details::InferenceEngineException ex) {
138         FAIL() << ex.what();
139     }
140     OutputsDataMap outputsDataMap;
141     cnnNetworkImplPtr->getOutputsInfo(outputsDataMap);
142     for (auto outputData: outputsDataMap) {
143         ASSERT_TRUE(outputData.second->getPrecision() == Precision::FP32);
144     }
145     InputsDataMap inputsDataMap;
146     cnnNetworkImplPtr->getInputsInfo(inputsDataMap);
147     for (auto inputData: inputsDataMap) {
148         ASSERT_TRUE(inputData.second->getInputPrecision() == Precision::I16);
149     }
150 }
151
152 //convolution input must be 4D
153 TEST_F(V2TopologyVerificationTests, testCheckConvolutionInputDim_More) {
154     string testContent = getNetworkWithConvLayer("Q78", { 1, 1, 3, 227, 227 });
155
156     xmlHelper->loadContent(testContent);
157     EXPECT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
158 }
159
160 //convolution input must be 4D
161 TEST_F(V2TopologyVerificationTests, testCheckConvolutionInputDim_Less) {
162     string testContent = getNetworkWithConvLayer("Q78", { 227, 227 });
163
164     xmlHelper->loadContent(testContent);
165     EXPECT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
166 }
167
168 //pooling input must be 4D
169 TEST_F(V2TopologyVerificationTests, testCheckPoolingInputDim_Less) {
170     string testContent = getNetworkWithPoolLayer({ 227, 227 });
171     xmlHelper->loadContent(testContent);
172     EXPECT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
173 }
174
175 //pooling input must be 4D
176 TEST_F(V2TopologyVerificationTests, testCheckPoolingInputDim_More) {
177     string testContent = getNetworkWithPoolLayer({ 1, 1, 3, 227, 227 });
178     xmlHelper->loadContent(testContent);
179     EXPECT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
180 }
181
182 TEST_F(V2TopologyVerificationTests, testLeayerPrecisionIsNotMIXED) {
183     string testContent = getNetworkWithConvLayer("MIXED");
184     xmlHelper->loadContent(testContent);
185     EXPECT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
186 }
187
188 TEST_F(V2TopologyVerificationTests, testMixedPrecisionIfLayerAndNetworkPrecisionsDiffer) {
189     string testContent = getNetworkWithConvLayer("Q78");
190     xmlHelper->loadContent(testContent);
191
192     try {
193         xmlHelper->parse();
194     } catch (InferenceEngine::details::InferenceEngineException ex) {
195         FAIL() << ex.what();
196     }
197 }
198
199 TEST_F(V2TopologyVerificationTests, throwsIfCropDimIsTooBig) {
200     CropData data = { 1, 0, 200 };
201
202     string testContent = getNetworkWithCropLayer({ data });
203     xmlHelper->loadContent(testContent);
204     ASSERT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
205 }
206
207 TEST_F(V2TopologyVerificationTests, testNoThrowWithProperCropParameters) {
208     CropData data = { 2, 0, 200 };
209
210     string testContent = getNetworkWithCropLayer({ data });
211     xmlHelper->loadContent(testContent);
212     ASSERT_NO_THROW(xmlHelper->parse());
213 }