blob: 5276d74a4d1339cc5edf84c2d40c8dee9dd44ca6 [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//
Matthew Bentham93b622f2020-01-17 09:36:49 +00005#include <armnn/INetwork.hpp>
6#include <armnn/IRuntime.hpp>
7#include <armnn/Utils.hpp>
8#include <armnn/Descriptors.hpp>
9
telsoa01c577f2c2018-08-31 09:22:23 +010010#include <iostream>
telsoa01c577f2c2018-08-31 09:22:23 +010011
12/// A simple example of using the ArmNN SDK API. In this sample, the users single input number is multiplied by 1.0f
13/// using a fully connected layer with a single neuron to produce an output number that is the same as the input.
14int main()
15{
16 using namespace armnn;
17
18 float number;
19 std::cout << "Please enter a number: " << std::endl;
20 std::cin >> number;
21
Matthew Bentham93b622f2020-01-17 09:36:49 +000022 // Turn on logging to standard output
23 // This is useful in this sample so that users can learn more about what is going on
24 armnn::ConfigureLogging(true, false, LogSeverity::Warning);
25
telsoa01c577f2c2018-08-31 09:22:23 +010026 // Construct ArmNN network
27 armnn::NetworkId networkIdentifier;
28 INetworkPtr myNetwork = INetwork::Create();
29
30 armnn::FullyConnectedDescriptor fullyConnectedDesc;
31 float weightsData[] = {1.0f}; // Identity
32 TensorInfo weightsInfo(TensorShape({1, 1}), DataType::Float32);
33 armnn::ConstTensor weights(weightsInfo, weightsData);
Matteo Martincighfc598e12019-05-14 10:36:13 +010034 IConnectableLayer *fullyConnected = myNetwork->AddFullyConnectedLayer(fullyConnectedDesc,
35 weights,
36 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +010037 "fully connected");
38
39 IConnectableLayer *InputLayer = myNetwork->AddInputLayer(0);
40 IConnectableLayer *OutputLayer = myNetwork->AddOutputLayer(0);
41
42 InputLayer->GetOutputSlot(0).Connect(fullyConnected->GetInputSlot(0));
43 fullyConnected->GetOutputSlot(0).Connect(OutputLayer->GetInputSlot(0));
44
45 // Create ArmNN runtime
46 IRuntime::CreationOptions options; // default options
47 IRuntimePtr run = IRuntime::Create(options);
48
49 //Set the tensors in the network.
50 TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32);
51 InputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
52
53 TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32);
54 fullyConnected->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
55
56 // Optimise ArmNN network
57 armnn::IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {Compute::CpuRef}, run->GetDeviceSpec());
Matthew Bentham93b622f2020-01-17 09:36:49 +000058 if (!optNet)
59 {
60 // This shouldn't happen for this simple sample, with reference backend.
61 // But in general usage Optimize could fail if the hardware at runtime cannot
62 // support the model that has been provided.
63 std::cerr << "Error: Failed to optimise the input network." << std::endl;
64 return 1;
65 }
telsoa01c577f2c2018-08-31 09:22:23 +010066
67 // Load graph into runtime
68 run->LoadNetwork(networkIdentifier, std::move(optNet));
69
70 //Creates structures for inputs and outputs.
71 std::vector<float> inputData{number};
72 std::vector<float> outputData(1);
73
74
75 armnn::InputTensors inputTensors{{0, armnn::ConstTensor(run->GetInputTensorInfo(networkIdentifier, 0),
76 inputData.data())}};
77 armnn::OutputTensors outputTensors{{0, armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0),
78 outputData.data())}};
79
80 // Execute network
81 run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
82
83 std::cout << "Your number was " << outputData[0] << std::endl;
84 return 0;
85
86}