aboutsummaryrefslogtreecommitdiff
path: root/src/armnnCaffeParser/test/TestInputs.cpp
blob: 616d75d95c27538cce2d840994c3e711ec503714 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include <boost/test/unit_test.hpp>
#include "armnnCaffeParser/ICaffeParser.hpp"
#include "armnn/IRuntime.hpp"
#include "armnn/INetwork.hpp"
#include "armnn/Exceptions.hpp"

#include "test/TensorHelpers.hpp"

#include <string>

#include "ParserPrototxtFixture.hpp"

BOOST_AUTO_TEST_SUITE(CaffeParser)


BOOST_AUTO_TEST_CASE(InputShapes)
{
    std::string explicitInput = "name: \"Minimal\"\n"
                                "layer {\n"
                                "  name: \"data\"\n"
                                "  type: \"Input\"\n"
                                "  top: \"data\"\n"
                                "  input_param { shape: { dim: 1 dim: 2 dim: 3 dim: 4 } }\n"
                                "}";
    std::string implicitInput = "name: \"Minimal\"\n"
                                "input: \"data\" \n"
                                "input_dim: 1 \n"
                                "input_dim: 2 \n"
                                "input_dim: 3 \n"
                                "input_dim: 4 \n";
    std::string implicitInputNoShape = "name: \"Minimal\"\n"
                                       "input: \"data\" \n";

    armnn::IRuntime::CreationOptions options;
    armnn::IRuntimePtr runtime(armnn::IRuntime::Create(options));
    armnnCaffeParser::ICaffeParserPtr parser(armnnCaffeParser::ICaffeParser::Create());
    armnn::INetworkPtr network(nullptr, nullptr);
    armnn::NetworkId netId;

    // Check everything works normally
    std::vector<armnn::Compute> backends = {armnn::Compute::CpuRef};
    {
        network = parser->CreateNetworkFromString(explicitInput.c_str(), {}, { "data" });
        BOOST_TEST(network.get());
        runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec()));

        armnnCaffeParser::BindingPointInfo inputBindingInfo = parser->GetNetworkInputBindingInfo("data");
        armnn::TensorInfo inputTensorInfo = inputBindingInfo.second;
        BOOST_TEST((inputTensorInfo == runtime->GetInputTensorInfo(netId, inputBindingInfo.first)));

        BOOST_TEST(inputTensorInfo.GetShape()[0] == 1);
        BOOST_TEST(inputTensorInfo.GetShape()[1] == 2);
        BOOST_TEST(inputTensorInfo.GetShape()[2] == 3);
        BOOST_TEST(inputTensorInfo.GetShape()[3] == 4);
    }

    // Checks everything works with implicit input.
    {
        network = parser->CreateNetworkFromString(implicitInput.c_str(), {}, { "data" });
        BOOST_TEST(network.get());
        runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec()));

        armnnCaffeParser::BindingPointInfo inputBindingInfo = parser->GetNetworkInputBindingInfo("data");
        armnn::TensorInfo inputTensorInfo = inputBindingInfo.second;
        BOOST_TEST((inputTensorInfo == runtime->GetInputTensorInfo(netId, inputBindingInfo.first)));

        BOOST_TEST(inputTensorInfo.GetShape()[0] == 1);
        BOOST_TEST(inputTensorInfo.GetShape()[1] == 2);
        BOOST_TEST(inputTensorInfo.GetShape()[2] == 3);
        BOOST_TEST(inputTensorInfo.GetShape()[3] == 4);
    }

    // Checks everything works with implicit and passing shape.
    {
        network = parser->CreateNetworkFromString(implicitInput.c_str(), { {"data", { 2, 2, 3, 4 } } }, { "data" });
        BOOST_TEST(network.get());
        runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec()));

        armnnCaffeParser::BindingPointInfo inputBindingInfo = parser->GetNetworkInputBindingInfo("data");
        armnn::TensorInfo inputTensorInfo = inputBindingInfo.second;
        BOOST_TEST((inputTensorInfo == runtime->GetInputTensorInfo(netId, inputBindingInfo.first)));

        BOOST_TEST(inputTensorInfo.GetShape()[0] == 2);
        BOOST_TEST(inputTensorInfo.GetShape()[1] == 2);
        BOOST_TEST(inputTensorInfo.GetShape()[2] == 3);
        BOOST_TEST(inputTensorInfo.GetShape()[3] == 4);
    }

    // Checks everything works with implicit (no shape) and passing shape.
    {
        network = parser->CreateNetworkFromString(implicitInputNoShape.c_str(), {{"data", {2, 2, 3, 4} }}, { "data" });
        BOOST_TEST(network.get());
        runtime->LoadNetwork(netId, Optimize(*network, backends, runtime->GetDeviceSpec()));

        armnnCaffeParser::BindingPointInfo inputBindingInfo = parser->GetNetworkInputBindingInfo("data");
        armnn::TensorInfo inputTensorInfo = inputBindingInfo.second;
        BOOST_TEST((inputTensorInfo == runtime->GetInputTensorInfo(netId, inputBindingInfo.first)));

        BOOST_TEST(inputTensorInfo.GetShape()[0] == 2);
        BOOST_TEST(inputTensorInfo.GetShape()[1] == 2);
        BOOST_TEST(inputTensorInfo.GetShape()[2] == 3);
        BOOST_TEST(inputTensorInfo.GetShape()[3] == 4);
    }

    // Checks exception on incompatible shapes.
    {
        BOOST_CHECK_THROW(parser->CreateNetworkFromString(implicitInput.c_str(), {{"data",{ 2, 2, 3, 2 }}}, {"data"}),
            armnn::ParseException);
    }

    // Checks exception when no shape available.
    {
        BOOST_CHECK_THROW(parser->CreateNetworkFromString(implicitInputNoShape.c_str(), {}, { "data" }),
            armnn::ParseException);
    }
}

BOOST_AUTO_TEST_SUITE_END()