blob: 6bdc2983bba333ee7c44054066baf5ccf8e20473 [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
31 TensorInfo weightsInfo(TensorShape({1, 1}), DataType::Float32);
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010032 ConstTensor weights(weightsInfo, weightsData);
telsoa01c577f2c2018-08-31 09:22:23 +010033
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010034 // Constant layer that now holds weights data for FullyConnected
35 IConnectableLayer* const constantWeightsLayer = myNetwork->AddConstantLayer(weights, "const weights");
telsoa01c577f2c2018-08-31 09:22:23 +010036
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010037 FullyConnectedDescriptor fullyConnectedDesc;
38 IConnectableLayer* const fullyConnectedLayer = myNetwork->AddFullyConnectedLayer(fullyConnectedDesc,
39 "fully connected");
40 IConnectableLayer* InputLayer = myNetwork->AddInputLayer(0);
41 IConnectableLayer* OutputLayer = myNetwork->AddOutputLayer(0);
42
43 InputLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(0));
44 constantWeightsLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(1));
45 fullyConnectedLayer->GetOutputSlot(0).Connect(OutputLayer->GetInputSlot(0));
telsoa01c577f2c2018-08-31 09:22:23 +010046
47 // Create ArmNN runtime
48 IRuntime::CreationOptions options; // default options
49 IRuntimePtr run = IRuntime::Create(options);
50
51 //Set the tensors in the network.
52 TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32);
53 InputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
54
55 TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32);
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010056 fullyConnectedLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
57 constantWeightsLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +010058
59 // Optimise ArmNN network
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010060 IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {Compute::CpuRef}, run->GetDeviceSpec());
Matthew Bentham93b622f2020-01-17 09:36:49 +000061 if (!optNet)
62 {
63 // This shouldn't happen for this simple sample, with reference backend.
64 // But in general usage Optimize could fail if the hardware at runtime cannot
65 // support the model that has been provided.
66 std::cerr << "Error: Failed to optimise the input network." << std::endl;
67 return 1;
68 }
telsoa01c577f2c2018-08-31 09:22:23 +010069
70 // Load graph into runtime
71 run->LoadNetwork(networkIdentifier, std::move(optNet));
72
73 //Creates structures for inputs and outputs.
74 std::vector<float> inputData{number};
75 std::vector<float> outputData(1);
76
77
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010078 InputTensors inputTensors{{0, armnn::ConstTensor(run->GetInputTensorInfo(networkIdentifier, 0),
telsoa01c577f2c2018-08-31 09:22:23 +010079 inputData.data())}};
Francis Murtaghcd92c9c2021-08-06 15:50:46 +010080 OutputTensors outputTensors{{0, armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0),
telsoa01c577f2c2018-08-31 09:22:23 +010081 outputData.data())}};
82
83 // Execute network
84 run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
85
86 std::cout << "Your number was " << outputData[0] << std::endl;
87 return 0;
88
89}