ArmNN
 21.02
TestInputs.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <boost/test/unit_test.hpp>
7 #include "armnn/IRuntime.hpp"
8 #include "armnn/INetwork.hpp"
9 #include "armnn/Exceptions.hpp"
10 
11 #include "test/TensorHelpers.hpp"
12 
13 #include <string>
14 
16 
17 BOOST_AUTO_TEST_SUITE(CaffeParser)
18 
19 
21 {
22  std::string explicitInput = "name: \"Minimal\"\n"
23  "layer {\n"
24  " name: \"data\"\n"
25  " type: \"Input\"\n"
26  " top: \"data\"\n"
27  " input_param { shape: { dim: 1 dim: 2 dim: 3 dim: 4 } }\n"
28  "}";
29  std::string implicitInput = "name: \"Minimal\"\n"
30  "input: \"data\" \n"
31  "input_dim: 1 \n"
32  "input_dim: 2 \n"
33  "input_dim: 3 \n"
34  "input_dim: 4 \n";
35  std::string implicitInputNoShape = "name: \"Minimal\"\n"
36  "input: \"data\" \n";
37 
41  armnn::INetworkPtr network(nullptr, nullptr);
42  armnn::NetworkId netId;
43 
44  // Check everything works normally
45  std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef};
46  {
47  network = parser->CreateNetworkFromString(explicitInput.c_str(), {}, { "data" });
48  BOOST_TEST(network.get());
49  runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec()));
50 
51  armnnCaffeParser::BindingPointInfo inputBindingInfo = parser->GetNetworkInputBindingInfo("data");
52  armnn::TensorInfo inputTensorInfo = inputBindingInfo.second;
53  BOOST_TEST((inputTensorInfo == runtime->GetInputTensorInfo(netId, inputBindingInfo.first)));
54 
55  BOOST_TEST(inputTensorInfo.GetShape()[0] == 1);
56  BOOST_TEST(inputTensorInfo.GetShape()[1] == 2);
57  BOOST_TEST(inputTensorInfo.GetShape()[2] == 3);
58  BOOST_TEST(inputTensorInfo.GetShape()[3] == 4);
59  }
60 
61  // Checks everything works with implicit input.
62  {
63  network = parser->CreateNetworkFromString(implicitInput.c_str(), {}, { "data" });
64  BOOST_TEST(network.get());
65  runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec()));
66 
67  armnnCaffeParser::BindingPointInfo inputBindingInfo = parser->GetNetworkInputBindingInfo("data");
68  armnn::TensorInfo inputTensorInfo = inputBindingInfo.second;
69  BOOST_TEST((inputTensorInfo == runtime->GetInputTensorInfo(netId, inputBindingInfo.first)));
70 
71  BOOST_TEST(inputTensorInfo.GetShape()[0] == 1);
72  BOOST_TEST(inputTensorInfo.GetShape()[1] == 2);
73  BOOST_TEST(inputTensorInfo.GetShape()[2] == 3);
74  BOOST_TEST(inputTensorInfo.GetShape()[3] == 4);
75  }
76 
77  // Checks everything works with implicit and passing shape.
78  {
79  network = parser->CreateNetworkFromString(implicitInput.c_str(), { {"data", { 2, 2, 3, 4 } } }, { "data" });
80  BOOST_TEST(network.get());
81  runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec()));
82 
83  armnnCaffeParser::BindingPointInfo inputBindingInfo = parser->GetNetworkInputBindingInfo("data");
84  armnn::TensorInfo inputTensorInfo = inputBindingInfo.second;
85  BOOST_TEST((inputTensorInfo == runtime->GetInputTensorInfo(netId, inputBindingInfo.first)));
86 
87  BOOST_TEST(inputTensorInfo.GetShape()[0] == 2);
88  BOOST_TEST(inputTensorInfo.GetShape()[1] == 2);
89  BOOST_TEST(inputTensorInfo.GetShape()[2] == 3);
90  BOOST_TEST(inputTensorInfo.GetShape()[3] == 4);
91  }
92 
93  // Checks everything works with implicit (no shape) and passing shape.
94  {
95  network = parser->CreateNetworkFromString(implicitInputNoShape.c_str(), {{"data", {2, 2, 3, 4} }}, { "data" });
96  BOOST_TEST(network.get());
97  runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec()));
98 
99  armnnCaffeParser::BindingPointInfo inputBindingInfo = parser->GetNetworkInputBindingInfo("data");
100  armnn::TensorInfo inputTensorInfo = inputBindingInfo.second;
101  BOOST_TEST((inputTensorInfo == runtime->GetInputTensorInfo(netId, inputBindingInfo.first)));
102 
103  BOOST_TEST(inputTensorInfo.GetShape()[0] == 2);
104  BOOST_TEST(inputTensorInfo.GetShape()[1] == 2);
105  BOOST_TEST(inputTensorInfo.GetShape()[2] == 3);
106  BOOST_TEST(inputTensorInfo.GetShape()[3] == 4);
107  }
108 
109  // Checks exception on incompatible shapes.
110  {
111  BOOST_CHECK_THROW(parser->CreateNetworkFromString(implicitInput.c_str(), {{"data",{ 2, 2, 3, 2 }}}, {"data"}),
113  }
114 
115  // Checks exception when no shape available.
116  {
117  BOOST_CHECK_THROW(parser->CreateNetworkFromString(implicitInputNoShape.c_str(), {}, { "data" }),
119  }
120 }
121 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:37
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
CPU Execution: Reference C++ kernels.
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:26
int NetworkId
Definition: IRuntime.hpp:20
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1502
static ICaffeParserPtr Create()
Definition: CaffeParser.cpp:75
std::unique_ptr< ICaffeParser, void(*)(ICaffeParser *parser)> ICaffeParserPtr
BOOST_AUTO_TEST_SUITE_END()
std::pair< armnn::LayerBindingId, armnn::TensorInfo > BindingPointInfo
Definition: Tensor.hpp:261
BOOST_AUTO_TEST_CASE(InputShapes)
Definition: TestInputs.cpp:20
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:173