telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 4 | // |
| 5 | #include <boost/test/unit_test.hpp> |
| 6 | #include "armnnCaffeParser/ICaffeParser.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 | |
| 15 | #include "ParserPrototxtFixture.hpp" |
| 16 | |
| 17 | BOOST_AUTO_TEST_SUITE(CaffeParser) |
| 18 | |
| 19 | |
| 20 | BOOST_AUTO_TEST_CASE(InputShapes) |
| 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 38 | armnn::IRuntime::CreationOptions options; |
| 39 | armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options)); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 40 | armnnCaffeParser::ICaffeParserPtr parser(armnnCaffeParser::ICaffeParser::Create()); |
| 41 | armnn::INetworkPtr network(nullptr, nullptr); |
| 42 | armnn::NetworkId netId; |
| 43 | |
| 44 | // Check everything works normally |
David Beck | f0b4845 | 2018-10-19 15:20:56 +0100 | [diff] [blame] | 45 | std::vector<armnn::BackendId> backends = {armnn::Compute::CpuRef}; |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 46 | { |
| 47 | network = parser->CreateNetworkFromString(explicitInput.c_str(), {}, { "data" }); |
| 48 | BOOST_TEST(network.get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 49 | runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 61 | // Checks everything works with implicit input. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 62 | { |
| 63 | network = parser->CreateNetworkFromString(implicitInput.c_str(), {}, { "data" }); |
| 64 | BOOST_TEST(network.get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 65 | runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 77 | // Checks everything works with implicit and passing shape. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 78 | { |
| 79 | network = parser->CreateNetworkFromString(implicitInput.c_str(), { {"data", { 2, 2, 3, 4 } } }, { "data" }); |
| 80 | BOOST_TEST(network.get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 81 | runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 93 | // Checks everything works with implicit (no shape) and passing shape. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 94 | { |
| 95 | network = parser->CreateNetworkFromString(implicitInputNoShape.c_str(), {{"data", {2, 2, 3, 4} }}, { "data" }); |
| 96 | BOOST_TEST(network.get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 97 | runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec())); |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 109 | // Checks exception on incompatible shapes. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 110 | { |
| 111 | BOOST_CHECK_THROW(parser->CreateNetworkFromString(implicitInput.c_str(), {{"data",{ 2, 2, 3, 2 }}}, {"data"}), |
| 112 | armnn::ParseException); |
| 113 | } |
| 114 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 115 | // Checks exception when no shape available. |
telsoa01 | 4fcda01 | 2018-03-09 14:13:49 +0000 | [diff] [blame] | 116 | { |
| 117 | BOOST_CHECK_THROW(parser->CreateNetworkFromString(implicitInputNoShape.c_str(), {}, { "data" }), |
| 118 | armnn::ParseException); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_SUITE_END() |