Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / tests / unit / cnn_network / v2_format_parser_test.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 #include <cpp/ie_cnn_network.h>
6 #include <gtest/gtest.h>
7 #include "xml_father.hpp"
8 #include "inference_engine/ie_format_parser.h"
9 #include <string>
10 #include <pugixml.hpp>
11 #include <fstream>
12 #include <tests_common.hpp>
13 #include <ie_blob_proxy.hpp>
14 #include <inference_engine/xml_parse_utils.h>
15 #include "mean_image.h"
16
17 #define MT_BATCH 1
18 #define MT_CHANNELS 3
19 #define MT_HEIGHT 1
20 #define MT_WIDTH 2
21 #define LAYER_COUNT 3
22
23 #include "parser_tests_base.hpp"
24
25 using namespace std;
26 using namespace InferenceEngine;
27 using namespace InferenceEngine::details;
28 using namespace testing;
29
30 class V2FormatParserTest : public FormatParserTest {
31 };
32
33
34 TEST_F (V2FormatParserTest, invalidXml_ShouldThrow) {
35     string content = XMLFather()
36             .node("net")
37             .attr("name", "AlexNet").attr("version", 2);
38
39     ASSERT_THROW(parse(content), InferenceEngine::details::InferenceEngineException);
40 }
41
42 TEST_F (V2FormatParserTest, canParseDims) {
43     // <input name="data"><dim>10</dim><dim>3</dim><dim>227</dim><dim>227</dim></input>
44     string content = xml().node("net").attr("name", "AlexNet").attr("version", 2)
45             .node("layers")
46             .initInputlayer("data", 0, 0)
47             .initPowerlayerInOutV2("power1", 1, 1, 2)
48             .initlayerInV2("power2", 2, 3)
49             .newnode("edges")
50             .initedge(0, 0, 1, 1)
51             .initedge(1, 2, 2, 3);
52
53      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
54 }
55
56 TEST_F(V2FormatParserTest, failWhenNoDims) {
57     string content = xml().node("net").attr("name", "AlexNet").attr("version", 2)
58             .node("layer").attr("type", "Input").attr("name", "data").attr("id", 0);
59
60     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
61 }
62
63 TEST_F(V2FormatParserTest, failOnZeroDim) {
64     string content = xml().node("net").attr("name", "AlexNet").attr("version", 2)
65             .node("layer").attr("type", "Input").attr("name", "data").attr("id", 0)
66                 .node("output")
67                     .node("port").attr("id", 0)
68                       .node("dim", 0)
69                     .close()
70                 .close()
71             .close()
72             .close();
73
74     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
75 }
76 TEST_F(V2FormatParserTest, canParseMeanImageValues) {
77     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
78             .node("channel").attr("id", "0").node("mean").attr("value", "104.5").close()
79             .newnode("channel").attr("id", "1").node("mean").attr("value", "117.8").close()
80             .newnode("channel").attr("id", "2").node("mean").attr("value", "123");
81
82      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
83         auto & pp = getFirstInput()->getPreProcess();
84
85     ASSERT_EQ(pp.getMeanVariant(), MEAN_VALUE);
86     ASSERT_EQ(MT_CHANNELS, pp.getNumberOfChannels());
87     InferenceEngine::PreProcessChannel::Ptr preProcessChannel;
88     ASSERT_FLOAT_EQ(104.5f, pp[0]->meanValue);
89     ASSERT_FLOAT_EQ(117.8f, pp[1]->meanValue);
90     ASSERT_FLOAT_EQ(123.f, pp[2]->meanValue);
91
92     ASSERT_EQ(nullptr, pp[0]->meanData);
93     ASSERT_EQ(nullptr, pp[1]->meanData);
94     ASSERT_EQ(nullptr, pp[2]->meanData);
95 }
96
97 TEST_F(V2FormatParserTest, canParseScaleValuesOnly) {
98     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
99             .node("channel").attr("id", "0").node("scale").attr("value", "104.5").close()
100             .newnode("channel").attr("id", "1").node("scale").attr("value", "117.8").close()
101             .newnode("channel").attr("id", "2").node("scale").attr("value", "123");
102      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
103         auto & pp = getFirstInput()->getPreProcess();
104     ASSERT_EQ(pp.getMeanVariant(), NONE);
105 }
106
107 TEST_F(V2FormatParserTest, failIfOneOfMeanImageIdsMissed) {
108     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
109             .node("channel").attr("id", "1").node("mean").attr("value", "117.8").close()
110             .newnode("channel").attr("id", "2").node("mean").attr("value", "123");
111
112     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
113 }
114
115 TEST_F(V2FormatParserTest, failIfValueAttributeIsNotValid) {
116     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
117             .node("channel").attr("id", "0").node("mean").attr("value1", "104.5").close()
118             .newnode("channel").attr("id", "1").node("mean").attr("value", "117.8").close()
119             .newnode("channel").attr("id", "2").node("mean").attr("value", "123");
120
121     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
122 }
123
124 TEST_F(V2FormatParserTest, failIfMeanValueIsNotSpecified) {
125     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
126             .node("channel").attr("id", "0").node("mean").attr("value", "").close();
127
128     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
129 }
130
131 TEST_F(V2FormatParserTest, failIfMeanValueNotSpecifiedInPreProcessing) {
132     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
133             .node("channel").attr("id", "0").node("mean").attr("value", "104.5").close()
134             .newnode("channel").attr("id", "1").node("mean").attr("value", "117.8").close()
135             .newnode("channel").attr("id", "2").node("mean1").attr("value", "123");
136
137     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
138 }
139
140 TEST_F(V2FormatParserTest, failIfIdLessThanZero) {
141     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
142             .node("channel").attr("id", "-1").node("mean").attr("value", "104.5").close();
143
144     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
145 }
146
147 TEST_F(V2FormatParserTest, failIfIdNotInteger) {
148     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
149             .node("channel").attr("id", "0").node("mean").attr("value", "104.5").close()
150             .newnode("channel").attr("id", "1").node("mean").attr("value", "117.8").close()
151             .newnode("channel").attr("id", "2_2").node("mean").attr("value", "123").close();
152
153     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
154 }
155
156 TEST_F(V2FormatParserTest, failIfValueNotFloat) {
157     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
158             .node("channel").attr("id", "0").node("mean").attr("value", "104,5").close()
159             .newnode("channel").attr("id", "1").node("mean").attr("value", "117.8").close()
160             .newnode("channel").attr("id", "2").node("mean").attr("value", "123").close();
161
162     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
163 }
164
165 TEST_F(V2FormatParserTest, failIfIdMoreThanNumChannels) {
166     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
167             .node("channel").attr("id", "4").node("mean").attr("value", "104.5").close();
168     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
169 }
170
171 TEST_F(V2FormatParserTest, failIfIdIsDuplicated) {
172     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
173             .node("channel").attr("id", "0").node("mean").attr("value", "104.5").close()
174             .newnode("channel").attr("id", "0").node("mean").attr("value", "117.8").close()
175             .newnode("channel").attr("id", "2").node("mean").attr("value", "123").close();
176
177     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
178 }
179
180 TEST_F(V2FormatParserTest, failParseMeanImageWithoutSpecifyingPrecision) {
181     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
182             .node("channel").attr("id", "0").node("mean").attr("offset", "0").attr("size", "5").close();
183
184     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
185 }
186
187 TEST_F(V2FormatParserTest, failIfOneOfMeanImageIfMeanNotSpecified) {
188     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
189             .node("channel").attr("id", "0").close();
190
191      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
192         auto & pp = getFirstInput()->getPreProcess();
193     ASSERT_EQ(pp.getMeanVariant(), NONE);
194 }
195
196 TEST_F(V2FormatParserTest, failIfOffsetValueMissing) {
197     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "FP32")
198             .node("channel").attr("id", "0").node("mean").attr("offset", "").attr("size", "5").close();
199
200     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
201 }
202
203 TEST_F(V2FormatParserTest, failIfSizeValueMissing) {
204     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "FP32")
205             .node("channel").attr("id", "0").node("mean").attr("offset", "1").attr("size", "").close();
206
207     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
208 }
209
210 TEST_F(V2FormatParserTest, throwsIfSizeOfMeanElementsMismatchWithExpected) {
211     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "FP32")
212             .node("channel").attr("id", "0").node("mean")
213             .attr("offset", "0").attr("size", "2").close()
214             .newnode("channel").attr("id", "1").node("mean")
215             .attr("offset", "2").attr("size", "2").close()
216             .newnode("channel").attr("id", "2").node("mean")
217             .attr("offset", "4").attr("size", "2").close();
218
219         ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
220 }
221
222 TEST_F(V2FormatParserTest, canHandleQ78MeanValues) {
223     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "Q78")
224             .node("channel").attr("id", "0").node("mean")
225             .attr("offset", "0").attr("size", "4").close()
226             .newnode("channel").attr("id", "1").node("mean")
227             .attr("offset", "4").attr("size", "4").close()
228             .newnode("channel").attr("id", "2").node("mean")
229             .attr("offset", "8").attr("size", "4").close();
230
231      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
232         auto & pp = getFirstInput()->getPreProcess();
233     ASSERT_EQ(pp.getMeanVariant(), MEAN_IMAGE);
234     auto binBlob = makeBinBlobForMeanTest<short>();
235     assertSetWeightsSucceed(binBlob);
236 }
237
238 TEST_F(V2FormatParserTest, canParseBinFileWithMeanImageUINT8Values) {
239     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "U8")
240             .node("channel").attr("id", "0").node("mean")
241             .attr("offset", "0").attr("size", "2").close()
242             .newnode("channel").attr("id", "1").node("mean")
243             .attr("offset", "2").attr("size", "2").close()
244             .newnode("channel").attr("id", "2").node("mean")
245             .attr("offset", "4").attr("size", "2").close();
246
247      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
248         auto & pp = getFirstInput()->getPreProcess();
249     ASSERT_EQ(pp.getMeanVariant(), MEAN_IMAGE);
250     auto binBlob = makeBinBlobForMeanTest<uint8_t>();
251     assertSetWeightsSucceed(binBlob);
252     assertMeanImagePerChannelCorrect<uint8_t>();
253     assertMeanImageCorrect<uint8_t>();
254 }
255
256 TEST_F(V2FormatParserTest, canParseBinFileWithMeanImageI16Values) {
257     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "I16")
258             .node("channel").attr("id", "0").node("mean")
259             .attr("offset", "0").attr("size", "4").close()
260             .newnode("channel").attr("id", "1").node("mean")
261             .attr("offset", "4").attr("size", "4").close()
262             .newnode("channel").attr("id", "2").node("mean")
263             .attr("offset", "8").attr("size", "4").close();
264
265      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
266         auto & pp = getFirstInput()->getPreProcess();
267     ASSERT_EQ(pp.getMeanVariant(), MEAN_IMAGE);
268     auto binBlob = makeBinBlobForMeanTest<short>();
269     assertSetWeightsSucceed(binBlob);
270     assertMeanImagePerChannelCorrect<short>();
271     assertMeanImageCorrect<short>();
272 }
273
274 TEST_F(V2FormatParserTest, canParseBinFileWithMeanImageFloatValues) {
275     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "FP32")
276             .node("channel").attr("id", "0").node("mean")
277             .attr("offset", "0").attr("size", "8").close()
278             .newnode("channel").attr("id", "1").node("mean")
279             .attr("offset", "8").attr("size", "8").close()
280             .newnode("channel").attr("id", "2").node("mean")
281             .attr("offset", "16").attr("size", "8").close();
282
283      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
284         auto & pp = getFirstInput()->getPreProcess();
285     ASSERT_EQ(pp.getMeanVariant(), MEAN_IMAGE);
286     auto binBlob = makeBinBlobForMeanTest<float>();
287     assertSetWeightsSucceed(binBlob);
288     assertMeanImagePerChannelCorrect<float>();
289     assertMeanImageCorrect<float>();
290 }
291
292 TEST_F(V2FormatParserTest, throwIfSizeDoesNotMatchExpectedMeanSize) {
293     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "FP32")
294             .node("channel").attr("id", "0").node("mean")
295             .attr("offset", "0").attr("size", "9").close()
296             .newnode("channel").attr("id", "1").node("mean")
297             .attr("offset", "8").attr("size", "8").close()
298             .newnode("channel").attr("id", "2").node("mean")
299             .attr("offset", "16").attr("size", "8").close();
300
301         ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
302 }
303
304 TEST_F(V2FormatParserTest, failIfSizeExceedBinaryFileSize) {
305     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "FP32")
306             .node("channel").attr("id", "0").node("mean")
307             .attr("offset", "0").attr("size", "8").close()
308             .newnode("channel").attr("id", "1").node("mean")
309             .attr("offset", "8").attr("size", "8").close()
310             .newnode("channel").attr("id", "2").node("mean")
311             .attr("offset", "1600").attr("size", "8").close();
312
313      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
314         auto & pp = getFirstInput()->getPreProcess();
315     ASSERT_EQ(pp.getMeanVariant(), MEAN_IMAGE);
316     auto binBlob = makeBinBlobForMeanTest<float>();
317     assertSetWeightsFail(binBlob);
318 }
319
320 TEST_F(V2FormatParserTest, failIfMixedAttributesAreSet) {
321     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "FP32")
322             .node("channel").attr("id", "0").node("mean")
323             .attr("value", "0").close()
324             .newnode("channel").attr("id", "1").node("mean")
325             .attr("offset", "8").attr("size", "8").close()
326             .newnode("channel").attr("id", "2").node("mean")
327             .attr("offset", "16").attr("size", "8").close();
328
329     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
330 }
331
332 TEST_F(V2FormatParserTest, parseSucceedIfMixedButAllValuesSet) {
333     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2().attr("mean-precision", "FP32")
334             .node("channel").attr("id", "0").node("mean")
335             .attr("value", "0").close()
336             .newnode("channel").attr("id", "1").node("mean")
337             .attr("value", "0").attr("offset", "8").attr("size", "8").close()
338             .newnode("channel").attr("id", "2").node("mean")
339             .attr("value", "0").attr("offset", "16").attr("size", "8").close();
340
341      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
342         auto & pp = getFirstInput()->getPreProcess();
343     ASSERT_EQ(pp.getMeanVariant(), MEAN_VALUE);
344 }
345
346
347 TEST_F(V2FormatParserTest, parseTileLayer) {
348     string content = BEGIN_NET()
349         .initlayerInOut("tile", "Tile", 1, 1, 2)
350             .node("data").attr("axis",3).attr("tiles", 88).close()
351         .close()
352     END_NET();
353
354
355      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
356     auto lp = getLayer<TileLayer>("tile");
357     ASSERT_TRUE(lp);
358
359     ASSERT_EQ(lp->axis, 3);
360     ASSERT_EQ(lp->tiles, 88);
361 }
362
363
364 TEST_F(V2FormatParserTest, checkPreProcessWithRefName) {
365     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
366             .attr("mean-precision", "FP32")
367             .attr("reference-layer-name", "data")
368         .node("channel").attr("id", "0").node("mean")
369         .attr("offset", "0").attr("size", "8").close()
370         .newnode("channel").attr("id", "1").node("mean")
371         .attr("offset", "8").attr("size", "8").close()
372         .newnode("channel").attr("id", "2").node("mean")
373         .attr("offset", "16").attr("size", "8").close();
374
375      ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
376     auto & pp = getFirstInput()->getPreProcess();
377     ASSERT_EQ(pp.getMeanVariant(), MEAN_IMAGE);
378     auto binBlob = makeBinBlobForMeanTest<float>();
379     assertSetWeightsSucceed(binBlob);
380     assertMeanImagePerChannelCorrect<float>();
381     assertMeanImageCorrect<float>();
382 }
383
384 TEST_F(V2FormatParserTest, failWhenPreProcessNameMissing) {
385     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2()
386         .attr("mean-precision", "FP32")
387         .attr("reference-layer-name", "foo")
388         .node("channel").attr("id", "0").node("mean")
389         .attr("offset", "0").attr("size", "8").close()
390         .newnode("channel").attr("id", "1").node("mean")
391         .attr("offset", "8").attr("size", "8").close()
392         .newnode("channel").attr("id", "2").node("mean")
393         .attr("offset", "16").attr("size", "8").close();
394
395     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
396 }
397
398 TEST_F(V2FormatParserTest, cannotParseUnknownEltwiseOperation) {
399     string content = BEGIN_NET()
400         .initlayerInOut("e", "Eltwise", 1, 1, 2)
401         .node("data").attr("operation", "unknown").close()
402         .close()
403          END_NET();
404
405     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
406 }
407
408 TEST_F(V2FormatParserTest, asserOnUnknownEltwiseOperation) {
409     string content = BEGIN_NET()
410         .initlayerInOut("e", "Eltwise", 1, 1, 2)
411         .node("data").attr("operation", "unknown").close()
412         .close()
413         END_NET();
414
415     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
416 }
417
418
419 TEST_F(V2FormatParserTest, canParseEmptyElementwiseNodeAsSum) {
420     string content = BEGIN_NET()
421         .initlayerInOut("e", "Eltwise", 1, 1, 2)
422         .node("data").attr("operation", "").close()
423         .close()
424         END_NET();
425
426     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
427     CNNLayerPtr ewise;
428     ASSERT_EQ(OK, net->getLayerByName("e", ewise, nullptr));
429     auto *eltwise = dynamic_cast<EltwiseLayer *>(ewise.get());
430     ASSERT_NE(nullptr, eltwise);
431     ASSERT_EQ(eltwise->_operation, EltwiseLayer::Sum);
432 }
433
434 TEST_F(V2FormatParserTest, canParseEmptyElementwiseNodeAsSumAmazonIR) {
435     string content = BEGIN_NET()
436         .initlayerInOut("e", "Eltwise", 1, 1, 2)
437         .node("elementwise_data").attr("operation", "").close()
438         .close()
439             END_NET();
440
441     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
442     CNNLayerPtr ewise;
443     ASSERT_EQ(OK, net->getLayerByName("e", ewise, nullptr));
444     auto *eltwise = dynamic_cast<EltwiseLayer *>(ewise.get());
445     ASSERT_NE(nullptr, eltwise);
446     ASSERT_EQ(eltwise->_operation, EltwiseLayer::Sum);
447 }
448
449 TEST_F(V2FormatParserTest, canParseMissedElementwiseOperationNodeAsSum) {
450     string content = BEGIN_NET()
451         .initlayerInOut("e", "Eltwise", 1, 1, 2)
452         .node("data").close()
453         .close()
454             END_NET();
455
456     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
457     CNNLayerPtr ewise;
458     ASSERT_EQ(OK, net->getLayerByName("e", ewise, nullptr));
459     auto *eltwise = dynamic_cast<EltwiseLayer *>(ewise.get());
460     ASSERT_NE(nullptr, eltwise);
461     ASSERT_EQ(eltwise->_operation, EltwiseLayer::Sum);
462 }
463
464 TEST_F(V2FormatParserTest, canParseMissedElementwiseDataNodeAsSum) {
465     string content = BEGIN_NET()
466         .initlayerInOut("e", "Eltwise", 1, 1, 2)
467         .close()
468     END_NET();
469
470     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
471     CNNLayerPtr ewise;
472     ASSERT_EQ(OK, net->getLayerByName("e", ewise, nullptr));
473     auto *eltwise = dynamic_cast<EltwiseLayer *>(ewise.get());
474     ASSERT_NE(nullptr, eltwise);
475     ASSERT_EQ(eltwise->_operation, EltwiseLayer::Sum);
476 }
477
478 TEST_F(V2FormatParserTest, canParseProdInElementwiseNode) {
479     string content = BEGIN_NET()
480         .initlayerInOut("e", "Eltwise", 1, 1, 2)
481         .node("data").attr("operation", "prod").close()
482         .close()
483     END_NET();
484
485     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
486     CNNLayerPtr ewise;
487     ASSERT_EQ(OK, net->getLayerByName("e", ewise, nullptr));
488     auto *eltwise = dynamic_cast<EltwiseLayer *>(ewise.get());
489     ASSERT_NE(nullptr, eltwise);
490     ASSERT_EQ(eltwise->_operation, EltwiseLayer::Prod);
491 }
492
493 TEST_F(V2FormatParserTest, canParseMulInElementwiseNode) {
494     string content = BEGIN_NET()
495         .initlayerInOut("e", "Eltwise", 1, 1, 2)
496         .node("data").attr("operation", "mul").close()
497         .close()
498             END_NET();
499
500     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
501     CNNLayerPtr ewise;
502     ASSERT_EQ(OK, net->getLayerByName("e", ewise, nullptr));
503     auto *eltwise = dynamic_cast<EltwiseLayer *>(ewise.get());
504     ASSERT_NE(nullptr, eltwise);
505     ASSERT_EQ(eltwise->_operation, EltwiseLayer::Prod);
506 }
507
508 TEST_F(V2FormatParserTest, canParseSumInElementwiseNode) {
509     string content = BEGIN_NET()
510         .initlayerInOut("e", "Eltwise", 1, 1, 2)
511         .node("data").attr("operation", "sum").close()
512         .close()
513     END_NET();
514
515     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
516     CNNLayerPtr ewise;
517     ASSERT_EQ(OK, net->getLayerByName("e", ewise, nullptr));
518     auto *eltwise = dynamic_cast<EltwiseLayer *>(ewise.get());
519     ASSERT_NE(nullptr, eltwise);
520     ASSERT_EQ(eltwise->_operation, EltwiseLayer::Sum);
521 }
522
523 TEST_F(V2FormatParserTest, parsesNumberOfLayersCorrectly) {
524     string content = MAKE_ALEXNET_FOR_MEAN_TESTS_V2();
525
526     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
527     CNNNetwork network(net.get());
528     ASSERT_EQ(network.layerCount(), LAYER_COUNT);
529 }
530
531 TEST_F(V2FormatParserTest, canThrowExceptionIfUnknownActivation) {
532
533     string content = BEGIN_NET()
534         .initlayerInOut("a", "Activation", 1, 1, 2)
535         .node("data").attr("type", "tanH1").close()
536         .close()
537             END_NET();
538
539     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
540 }
541
542 TEST_F(V2FormatParserTest, canThrowExceptionIfNoType) {
543
544     string content = BEGIN_NET()
545         .initlayerInOut("a", "Activation", 1, 1, 2)
546         .node("data1").attr("type", "tanH").close()
547         .close()
548             END_NET();
549
550     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
551 }
552
553 TEST_F(V2FormatParserTest, canThrowExceptionIfMultipleTypes) {
554
555     string content = BEGIN_NET()
556         .initlayerInOut("a", "Activation", 1, 1, 2)
557         .node("data").attr("type", "tanH").attr("type", "sigmoid").close()
558         .close()
559             END_NET();
560
561     ASSERT_NO_FATAL_FAILURE(assertParseFail(content));
562 }
563
564 TEST_F(V2FormatParserTest, canConvertActivationLayerAsTanH) {
565
566     string content = BEGIN_NET()
567         .initlayerInOut("a", "Activation", 1, 1, 2)
568         .node("data").attr("type", "tanH").close()
569         .close()
570             END_NET();
571
572     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
573
574     CNNLayerPtr tanh;
575     ASSERT_EQ(OK, net->getLayerByName("a", tanh, nullptr));
576     ASSERT_STREQ(tanh->type.c_str(), "tanh");
577     ASSERT_EQ(tanh->params.find("type"), tanh->params.end());
578 }
579
580 TEST_F(V2FormatParserTest, canConvertActivationLayerAsELU) {
581
582     string content = BEGIN_NET()
583         .initlayerInOut("a", "Activation", 1, 1, 2)
584         .node("data").attr("type", "elu").attr("alpha", "0.1").close()
585         .close()
586             END_NET();
587
588     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
589
590     CNNLayerPtr elu;
591     ASSERT_EQ(OK, net->getLayerByName("a", elu, nullptr));
592     ASSERT_STREQ(elu->type.c_str(), "elu");
593     ASSERT_FLOAT_EQ(elu->GetParamAsFloat("alpha"), 0.1);
594     ASSERT_EQ(elu->params.find("type"), elu->params.end());
595 }
596
597 TEST_F(V2FormatParserTest, canConvertActivationLayerAsRelu) {
598
599     string content = BEGIN_NET()
600         .initlayerInOut("a", "Activation", 1, 1, 2)
601         .node("data").attr("type", "relu").attr("negative_slope", "0.1").close()
602         .close()
603             END_NET();
604
605     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
606
607     CNNLayerPtr relu;
608     ASSERT_EQ(OK, net->getLayerByName("a", relu, nullptr));
609     ASSERT_STREQ(relu->type.c_str(), "relu");
610
611     auto *reluLayer = dynamic_cast<ReLULayer *>(relu.get());
612     ASSERT_NE(nullptr, reluLayer);
613
614     ASSERT_FLOAT_EQ(reluLayer->negative_slope, 0.1);
615     ASSERT_EQ(reluLayer->params.find("type"), reluLayer->params.end());
616 }
617
618 TEST_F(V2FormatParserTest, canConvertActivationLayerAsPRelu) {
619
620     string content = BEGIN_NET()
621         .initlayerInOut("a", "Activation", 1, 1, 2)
622         .node("data").attr("type", "pRelu").attr("channel_shared", "1").close()
623         .close()
624             END_NET();
625
626     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
627
628     CNNLayerPtr layer;
629     ASSERT_EQ(OK, net->getLayerByName("a", layer, nullptr));
630     ASSERT_STREQ(layer->type.c_str(), "prelu");
631
632     auto *preluLayer = dynamic_cast<PReLULayer *>(layer.get());
633     ASSERT_NE(nullptr, preluLayer);
634
635     ASSERT_EQ(preluLayer->_channel_shared, 1);
636     ASSERT_EQ(preluLayer->params.find("type"), preluLayer->params.end());
637 }
638
639 TEST_F(V2FormatParserTest, canConvertActivationLayerAsSigmoid) {
640
641     string content = BEGIN_NET()
642         .initlayerInOut("a", "Activation", 1, 1, 2)
643         .node("data").attr("type", "sigmoid").close()
644         .close()
645             END_NET();
646
647     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
648
649     CNNLayerPtr sigmoid;
650     ASSERT_EQ(OK, net->getLayerByName("a", sigmoid, nullptr));
651     ASSERT_STREQ(sigmoid->type.c_str(), "sigmoid");
652     ASSERT_EQ(sigmoid->params.find("type"), sigmoid->params.end());
653 }
654
655 TEST_F(V2FormatParserTest, canConvertActivationLayerAsClamp) {
656
657     string content = BEGIN_NET()
658         .initlayerInOut("a", "Activation", 1, 1, 2)
659         .node("data").attr("type", "clamp").attr("max","5").attr("min","-5").close()
660         .close()
661             END_NET();
662
663     ASSERT_NO_FATAL_FAILURE(assertParseSucceed(content));
664
665     CNNLayerPtr layer;
666     ASSERT_EQ(OK, net->getLayerByName("a", layer, nullptr));
667     ASSERT_STREQ(layer->type.c_str(), "clamp");
668     auto  clamp = dynamic_cast<ClampLayer*>(layer.get());
669     ASSERT_NE(clamp, nullptr);
670
671     ASSERT_EQ(clamp->min_value, -5);
672     ASSERT_EQ(clamp->max_value, 5);
673     ASSERT_EQ(clamp->params.find("type"), clamp->params.end());
674 }