1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
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"
14 #include "details/ie_exception.hpp"
17 using namespace InferenceEngine;
18 using namespace testing;
20 class V2TopologyVerificationTests : public ::testing::Test {
22 virtual void TearDown() {}
23 virtual void SetUp() {
24 xmlHelper.reset(new XMLHelper(new details::FormatParser(2)));
27 unique_ptr<CNNNetwork> cnnNetwork;
28 unique_ptr<XMLHelper> xmlHelper;
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 };
34 return V2NetBuilder::buildNetworkWithOneInput("",layerInput)
36 .convolutionLayer(layerPrecision, { {inputDims}, {outputDims} })
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};
45 return V2NetBuilder::buildNetworkWithOneInput("",layerInput, inputPrecision)
47 .convolutionLayer(layerPrecision, {{inputDims}, {outputDims}})
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 };
55 return V2NetBuilder::buildNetworkWithOneInput("",layerInput)
57 .poolingLayer({ { inputDims },{ outputDims } })
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 };
65 return V2NetBuilder::buildNetworkWithOneInput("",layerInput)
67 .cropLayer(params, { {inputDims}, {outputDims} })
72 TEST_F(V2TopologyVerificationTests, testNoThrow) {
73 string testContent = getNetworkWithConvLayer();
75 xmlHelper->loadContent(testContent);
78 } catch (InferenceEngine::details::InferenceEngineException ex) {
83 TEST_F(V2TopologyVerificationTests, testDefaultPrecisionsForFP16InputAndOutputLayers) {
84 string testContent = getNetworkWithConvLayerWithInputPrecision(Precision(Precision::FP16).name(),
85 Precision(Precision::FP16).name());
87 InferenceEngine::details::CNNNetworkImplPtr cnnNetworkImplPtr;
88 xmlHelper->loadContent(testContent);
90 cnnNetworkImplPtr = xmlHelper->parseWithReturningNetwork();
91 } catch (InferenceEngine::details::InferenceEngineException ex) {
94 OutputsDataMap outputsDataMap;
95 cnnNetworkImplPtr->getOutputsInfo(outputsDataMap);
96 for (auto outputData: outputsDataMap) {
97 ASSERT_TRUE(outputData.second->getPrecision() == Precision::FP32);
99 InputsDataMap inputsDataMap;
100 cnnNetworkImplPtr->getInputsInfo(inputsDataMap);
101 for (auto inputData: inputsDataMap) {
102 ASSERT_TRUE(inputData.second->getInputPrecision() == Precision::FP32);
106 TEST_F(V2TopologyVerificationTests, testDefaultPrecisionsFP32InputAndOutputLayers) {
107 string testContent = getNetworkWithConvLayerWithInputPrecision(Precision(Precision::FP32).name(),
108 Precision(Precision::FP32).name());
110 InferenceEngine::details::CNNNetworkImplPtr cnnNetworkImplPtr;
111 xmlHelper->loadContent(testContent);
113 cnnNetworkImplPtr = xmlHelper->parseWithReturningNetwork();
114 } catch (InferenceEngine::details::InferenceEngineException ex) {
117 OutputsDataMap outputsDataMap;
118 cnnNetworkImplPtr->getOutputsInfo(outputsDataMap);
119 for (auto outputData: outputsDataMap) {
120 ASSERT_TRUE(outputData.second->getPrecision() == Precision::FP32);
122 InputsDataMap inputsDataMap;
123 cnnNetworkImplPtr->getInputsInfo(inputsDataMap);
124 for (auto inputData: inputsDataMap) {
125 ASSERT_TRUE(inputData.second->getInputPrecision() == Precision::FP32);
129 TEST_F(V2TopologyVerificationTests, testDefaultPrecisionsForQ78InputAndOutputLayers) {
130 string testContent = getNetworkWithConvLayerWithInputPrecision(Precision(Precision::Q78).name(),
131 Precision(Precision::Q78).name());
133 InferenceEngine::details::CNNNetworkImplPtr cnnNetworkImplPtr;
134 xmlHelper->loadContent(testContent);
136 cnnNetworkImplPtr = xmlHelper->parseWithReturningNetwork();
137 } catch (InferenceEngine::details::InferenceEngineException ex) {
140 OutputsDataMap outputsDataMap;
141 cnnNetworkImplPtr->getOutputsInfo(outputsDataMap);
142 for (auto outputData: outputsDataMap) {
143 ASSERT_TRUE(outputData.second->getPrecision() == Precision::FP32);
145 InputsDataMap inputsDataMap;
146 cnnNetworkImplPtr->getInputsInfo(inputsDataMap);
147 for (auto inputData: inputsDataMap) {
148 ASSERT_TRUE(inputData.second->getInputPrecision() == Precision::I16);
152 //convolution input must be 4D
153 TEST_F(V2TopologyVerificationTests, testCheckConvolutionInputDim_More) {
154 string testContent = getNetworkWithConvLayer("Q78", { 1, 1, 3, 227, 227 });
156 xmlHelper->loadContent(testContent);
157 EXPECT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
160 //convolution input must be 4D
161 TEST_F(V2TopologyVerificationTests, testCheckConvolutionInputDim_Less) {
162 string testContent = getNetworkWithConvLayer("Q78", { 227, 227 });
164 xmlHelper->loadContent(testContent);
165 EXPECT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
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);
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);
182 TEST_F(V2TopologyVerificationTests, testLeayerPrecisionIsNotMIXED) {
183 string testContent = getNetworkWithConvLayer("MIXED");
184 xmlHelper->loadContent(testContent);
185 EXPECT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
188 TEST_F(V2TopologyVerificationTests, testMixedPrecisionIfLayerAndNetworkPrecisionsDiffer) {
189 string testContent = getNetworkWithConvLayer("Q78");
190 xmlHelper->loadContent(testContent);
194 } catch (InferenceEngine::details::InferenceEngineException ex) {
199 TEST_F(V2TopologyVerificationTests, throwsIfCropDimIsTooBig) {
200 CropData data = { 1, 0, 200 };
202 string testContent = getNetworkWithCropLayer({ data });
203 xmlHelper->loadContent(testContent);
204 ASSERT_THROW(xmlHelper->parse(), InferenceEngine::details::InferenceEngineException);
207 TEST_F(V2TopologyVerificationTests, testNoThrowWithProperCropParameters) {
208 CropData data = { 2, 0, 200 };
210 string testContent = getNetworkWithCropLayer({ data });
211 xmlHelper->loadContent(testContent);
212 ASSERT_NO_THROW(xmlHelper->parse());