blob: b881f1f58f4a430e6dbd80e34d609574610cd54b [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5#include <boost/test/unit_test.hpp>
6#include "armnnCaffeParser/ICaffeParser.hpp"
7#include "ParserPrototxtFixture.hpp"
8#include <sstream>
9#include <initializer_list>
10
11namespace
12{
13
14template <typename T>
15std::string TaggedSequence(const std::string & tag, const std::initializer_list<T> & data)
16{
17 bool first = true;
18 std::stringstream ss;
19 for (auto && d : data)
20 {
21 if (!first)
22 {
23 ss << " , ";
24 }
25 else
26 {
27 first = false;
28 }
29 ss << " " << tag << " : " << d << " ";
30 }
31 return ss.str();
32}
33
34template <typename T>
35std::string TaggedSequence(const std::string & tag, T data, unsigned int n)
36{
37 std::stringstream ss;
38 for (unsigned int i=0; i<n; ++i)
39 {
40 if (i>0)
41 {
42 ss << " , ";
43 }
44 ss << " " << tag << " : " << data << " ";
45 }
46 return ss.str();
47}
48
49} // namespace <anonymous>
50
51BOOST_AUTO_TEST_SUITE(CaffeParser)
52
53struct ConvolutionFixture : public armnnUtils::ParserPrototxtFixture<armnnCaffeParser::ICaffeParser>
54{
55 ConvolutionFixture(const std::initializer_list<unsigned int> & inputDims,
56 const std::initializer_list<float> & filterData,
57 unsigned int kernelSize,
58 unsigned int numOutput=1,
59 unsigned int group=1)
60 {
61 m_Prototext = R"(
62 name: "ConvolutionTest"
63 layer {
64 name: "input1"
65 type: "Input"
66 top: "input1"
67 input_param { shape: { )" + TaggedSequence("dim", inputDims) + R"( } }
68 }
69 layer {
70 name: "conv1"
71 type: "Convolution"
72 bottom: "input1"
73 top: "conv1"
74 blobs: { )" + TaggedSequence("data", filterData) + R"( }
75 blobs: { )" + TaggedSequence("data", 0, numOutput) + R"( }
76 convolution_param {
77 num_output: )" + std::to_string(numOutput) + R"(
78 pad: 0
79 kernel_size: )" + std::to_string(kernelSize) + R"(
80 stride: 1
81 group: )" + std::to_string(group) + R"(
82 }
83 }
84 )";
85 SetupSingleInputSingleOutput("input1", "conv1");
86 }
87};
88
89struct SimpleConvolutionFixture : public ConvolutionFixture
90{
91 SimpleConvolutionFixture()
92 : ConvolutionFixture( {1, 1, 2, 2}, {1.0f, 1.0f, 1.0f, 1.0f}, 2)
93 {
94 }
95};
96
97BOOST_FIXTURE_TEST_CASE(SimpleConvolution, SimpleConvolutionFixture)
98{
99 RunTest<4>({ 1, 3, 5, 7 }, { 16 });
100}
101
102struct GroupConvolutionFixture : public ConvolutionFixture
103{
104 GroupConvolutionFixture()
105 : ConvolutionFixture(
106 {1, 2, 2, 2},
107 {
108 1.0f, 1.0f, 1.0f, 1.0f, // filter for channel #0
109 2.0f, 2.0f, 2.0f, 2.0f // filter for channel #1
110 },
111 2, // kernel size is 2x2
112 2, // number of output channels is 2
113 2) // number of groups (separate filters)
114 {
115 }
116};
117
118BOOST_FIXTURE_TEST_CASE(GroupConvolution, GroupConvolutionFixture)
119{
120 RunTest<4>(
121 {
122 1, 2, 3, 4, // input channel #0
123 5, 6, 7, 8, // input channel #1
124 },
125 {
126 10, // convolution result for channel #0 applying filter #0
127 52 // same for channel #1 and filter #1
128 }
129 );
130}
131
132
133BOOST_AUTO_TEST_SUITE_END()