Find default build location for Compute Library binaries.
[platform/upstream/armnn.git] / samples / SimpleSample.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <armnn/INetwork.hpp>
6 #include <armnn/IRuntime.hpp>
7 #include <armnn/Utils.hpp>
8 #include <armnn/Descriptors.hpp>
9
10 #include <iostream>
11
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.
14 int main()
15 {
16     using namespace armnn;
17
18     float number;
19     std::cout << "Please enter a number: " << std::endl;
20     std::cin >> number;
21
22     // 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
26     // 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);
34     IConnectableLayer *fullyConnected = myNetwork->AddFullyConnectedLayer(fullyConnectedDesc,
35                                                                           weights,
36                                                                           EmptyOptional(),
37                                                                           "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());
58     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     }
66
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 }