2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
5 #include <armnn/INetwork.hpp>
6 #include <armnn/IRuntime.hpp>
7 #include <armnn/Utils.hpp>
8 #include <armnn/Descriptors.hpp>
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.
16 using namespace armnn;
19 std::cout << "Please enter a number: " << std::endl;
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);
26 // Construct ArmNN network
27 armnn::NetworkId networkIdentifier;
28 INetworkPtr myNetwork = INetwork::Create();
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,
39 IConnectableLayer *InputLayer = myNetwork->AddInputLayer(0);
40 IConnectableLayer *OutputLayer = myNetwork->AddOutputLayer(0);
42 InputLayer->GetOutputSlot(0).Connect(fullyConnected->GetInputSlot(0));
43 fullyConnected->GetOutputSlot(0).Connect(OutputLayer->GetInputSlot(0));
45 // Create ArmNN runtime
46 IRuntime::CreationOptions options; // default options
47 IRuntimePtr run = IRuntime::Create(options);
49 //Set the tensors in the network.
50 TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32);
51 InputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
53 TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32);
54 fullyConnected->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
56 // Optimise ArmNN network
57 armnn::IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {Compute::CpuRef}, run->GetDeviceSpec());
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;
67 // Load graph into runtime
68 run->LoadNetwork(networkIdentifier, std::move(optNet));
70 //Creates structures for inputs and outputs.
71 std::vector<float> inputData{number};
72 std::vector<float> outputData(1);
75 armnn::InputTensors inputTensors{{0, armnn::ConstTensor(run->GetInputTensorInfo(networkIdentifier, 0),
77 armnn::OutputTensors outputTensors{{0, armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0),
81 run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
83 std::cout << "Your number was " << outputData[0] << std::endl;