blob: 0950b2688a1c11f2e8d3d5be49de531020bbc6b2 [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//
5#include <iostream>
6#include "armnn/ArmNN.hpp"
7
8/// A simple example of using the ArmNN SDK API. In this sample, the users single input number is multiplied by 1.0f
9/// using a fully connected layer with a single neuron to produce an output number that is the same as the input.
10int main()
11{
12 using namespace armnn;
13
14 float number;
15 std::cout << "Please enter a number: " << std::endl;
16 std::cin >> number;
17
18 // Construct ArmNN network
19 armnn::NetworkId networkIdentifier;
20 INetworkPtr myNetwork = INetwork::Create();
21
22 armnn::FullyConnectedDescriptor fullyConnectedDesc;
23 float weightsData[] = {1.0f}; // Identity
24 TensorInfo weightsInfo(TensorShape({1, 1}), DataType::Float32);
25 armnn::ConstTensor weights(weightsInfo, weightsData);
26 IConnectableLayer *fullyConnected = myNetwork->AddFullyConnectedLayer(fullyConnectedDesc, weights,
27 "fully connected");
28
29 IConnectableLayer *InputLayer = myNetwork->AddInputLayer(0);
30 IConnectableLayer *OutputLayer = myNetwork->AddOutputLayer(0);
31
32 InputLayer->GetOutputSlot(0).Connect(fullyConnected->GetInputSlot(0));
33 fullyConnected->GetOutputSlot(0).Connect(OutputLayer->GetInputSlot(0));
34
35 // Create ArmNN runtime
36 IRuntime::CreationOptions options; // default options
37 IRuntimePtr run = IRuntime::Create(options);
38
39 //Set the tensors in the network.
40 TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32);
41 InputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
42
43 TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32);
44 fullyConnected->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
45
46 // Optimise ArmNN network
47 armnn::IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {Compute::CpuRef}, run->GetDeviceSpec());
48
49 // Load graph into runtime
50 run->LoadNetwork(networkIdentifier, std::move(optNet));
51
52 //Creates structures for inputs and outputs.
53 std::vector<float> inputData{number};
54 std::vector<float> outputData(1);
55
56
57 armnn::InputTensors inputTensors{{0, armnn::ConstTensor(run->GetInputTensorInfo(networkIdentifier, 0),
58 inputData.data())}};
59 armnn::OutputTensors outputTensors{{0, armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0),
60 outputData.data())}};
61
62 // Execute network
63 run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
64
65 std::cout << "Your number was " << outputData[0] << std::endl;
66 return 0;
67
68}