blob: 01f078bd567a491503c7f201c15998b250e4b9a4 [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
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010024 ConfigureLogging(true, false, LogSeverity::Warning);
Matthew Bentham93b622f2020-01-17 09:36:49 +000025
telsoa01c577f2c2018-08-31 09:22:23 +010026 // Construct ArmNN network
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010027 NetworkId networkIdentifier;
telsoa01c577f2c2018-08-31 09:22:23 +010028 INetworkPtr myNetwork = INetwork::Create();
29
telsoa01c577f2c2018-08-31 09:22:23 +010030 float weightsData[] = {1.0f}; // Identity
Cathal Corbett5b8093c2021-10-22 11:12:07 +010031 TensorInfo weightsInfo(TensorShape({1, 1}), DataType::Float32, 0.0f, 0, true);
Matthew Sloyanb20d1d42021-08-09 15:33:41 +010032 weightsInfo.SetConstant();
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010033 ConstTensor weights(weightsInfo, weightsData);
telsoa01c577f2c2018-08-31 09:22:23 +010034
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010035 // Constant layer that now holds weights data for FullyConnected
36 IConnectableLayer* const constantWeightsLayer = myNetwork->AddConstantLayer(weights, "const weights");
telsoa01c577f2c2018-08-31 09:22:23 +010037
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010038 FullyConnectedDescriptor fullyConnectedDesc;
39 IConnectableLayer* const fullyConnectedLayer = myNetwork->AddFullyConnectedLayer(fullyConnectedDesc,
40 "fully connected");
41 IConnectableLayer* InputLayer = myNetwork->AddInputLayer(0);
42 IConnectableLayer* OutputLayer = myNetwork->AddOutputLayer(0);
43
44 InputLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(0));
45 constantWeightsLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(1));
46 fullyConnectedLayer->GetOutputSlot(0).Connect(OutputLayer->GetInputSlot(0));
telsoa01c577f2c2018-08-31 09:22:23 +010047
48 // Create ArmNN runtime
49 IRuntime::CreationOptions options; // default options
50 IRuntimePtr run = IRuntime::Create(options);
51
52 //Set the tensors in the network.
53 TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32);
54 InputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
55
56 TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32);
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010057 fullyConnectedLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Matthew Sloyanb20d1d42021-08-09 15:33:41 +010058 constantWeightsLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo);
telsoa01c577f2c2018-08-31 09:22:23 +010059
60 // Optimise ArmNN network
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010061 IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {Compute::CpuRef}, run->GetDeviceSpec());
Matthew Bentham93b622f2020-01-17 09:36:49 +000062 if (!optNet)
63 {
64 // This shouldn't happen for this simple sample, with reference backend.
65 // But in general usage Optimize could fail if the hardware at runtime cannot
66 // support the model that has been provided.
67 std::cerr << "Error: Failed to optimise the input network." << std::endl;
68 return 1;
69 }
telsoa01c577f2c2018-08-31 09:22:23 +010070
71 // Load graph into runtime
72 run->LoadNetwork(networkIdentifier, std::move(optNet));
73
74 //Creates structures for inputs and outputs.
75 std::vector<float> inputData{number};
76 std::vector<float> outputData(1);
77
Cathal Corbett5b8093c2021-10-22 11:12:07 +010078 inputTensorInfo = run->GetInputTensorInfo(networkIdentifier, 0);
79 inputTensorInfo.SetConstant(true);
80 InputTensors inputTensors{{0, armnn::ConstTensor(inputTensorInfo,
81 inputData.data())}};
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010082 OutputTensors outputTensors{{0, armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0),
Cathal Corbett5b8093c2021-10-22 11:12:07 +010083 outputData.data())}};
telsoa01c577f2c2018-08-31 09:22:23 +010084
85 // Execute network
86 run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
87
88 std::cout << "Your number was " << outputData[0] << std::endl;
89 return 0;
90
91}