From 03c7ff3f6188240baaeaeb405a357a0c58195fec Mon Sep 17 00:00:00 2001 From: Nikhil Raj Date: Tue, 22 Aug 2023 12:00:04 +0100 Subject: IVGCVSW-7702 Update Doxygen Docu for 23.08 Signed-off-by: Nikhil Raj Change-Id: I357a9f7e47614589327c1ac5d95b6224ff77103d --- latest/_simple_sample_8cpp-example.html | 241 ++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 latest/_simple_sample_8cpp-example.html (limited to 'latest/_simple_sample_8cpp-example.html') diff --git a/latest/_simple_sample_8cpp-example.html b/latest/_simple_sample_8cpp-example.html new file mode 100644 index 0000000000..ae953973ac --- /dev/null +++ b/latest/_simple_sample_8cpp-example.html @@ -0,0 +1,241 @@ + + + + + + + + +Arm NN: SimpleSample.cpp + + + + + + + + + + + + + + + + +
+
+ + + + ArmNN + + + +
+
+  23.08 +
+
+
+ + + + + + + +
+
+ +
+
+
+ +
+ +
+
+ + +
+ +
+ +
+
+
SimpleSample.cpp
+
+
+

This is a very simple example which uses the Arm NN SDK API to create a neural network which consists of nothing else but a single fully connected layer with a single weights value. It's as minimalistic as it can get.

+
Note
Most of our users won't use our API to create a network manually. Usually you would use one of our software tools like the TfLite Parser that will translate a TfLite model into Arm NN for you. Still it's a very nice example to see how an Arm NN network is created, optimized and executed.
+

(You can find more complex examples using the TfLite Parser in samples/ObjectDetection and samples/SpeechRecognition. And another example using PyArmnn in samples/ImageClassification)

+
//
+
// Copyright © 2017 Arm Ltd. All rights reserved.
+
// SPDX-License-Identifier: MIT
+
//
+ + +
#include <armnn/Utils.hpp>
+ +
+
#include <iostream>
+
+
/// A simple example of using the ArmNN SDK API. In this sample, the users single input number is multiplied by 1.0f
+
/// using a fully connected layer with a single neuron to produce an output number that is the same as the input.
+
int main()
+
{
+
using namespace armnn;
+
+
float number;
+
std::cout << "Please enter a number: " << std::endl;
+
std::cin >> number;
+
+
// Turn on logging to standard output
+
// This is useful in this sample so that users can learn more about what is going on
+ +
+
// Construct ArmNN network
+
NetworkId networkIdentifier;
+ +
+
float weightsData[] = {1.0f}; // Identity
+
TensorInfo weightsInfo(TensorShape({1, 1}), DataType::Float32, 0.0f, 0, true);
+
weightsInfo.SetConstant();
+
ConstTensor weights(weightsInfo, weightsData);
+
+
// Constant layer that now holds weights data for FullyConnected
+
IConnectableLayer* const constantWeightsLayer = myNetwork->AddConstantLayer(weights, "const weights");
+
+
FullyConnectedDescriptor fullyConnectedDesc;
+
IConnectableLayer* const fullyConnectedLayer = myNetwork->AddFullyConnectedLayer(fullyConnectedDesc,
+
"fully connected");
+
IConnectableLayer* InputLayer = myNetwork->AddInputLayer(0);
+
IConnectableLayer* OutputLayer = myNetwork->AddOutputLayer(0);
+
+
InputLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(0));
+
constantWeightsLayer->GetOutputSlot(0).Connect(fullyConnectedLayer->GetInputSlot(1));
+
fullyConnectedLayer->GetOutputSlot(0).Connect(OutputLayer->GetInputSlot(0));
+
+
// Create ArmNN runtime
+
IRuntime::CreationOptions options; // default options
+
IRuntimePtr run = IRuntime::Create(options);
+
+
//Set the tensors in the network.
+
TensorInfo inputTensorInfo(TensorShape({1, 1}), DataType::Float32);
+
InputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
+
+
TensorInfo outputTensorInfo(TensorShape({1, 1}), DataType::Float32);
+
fullyConnectedLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
+
constantWeightsLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo);
+
+
// Optimise ArmNN network
+
IOptimizedNetworkPtr optNet = Optimize(*myNetwork, {Compute::CpuRef}, run->GetDeviceSpec());
+
if (!optNet)
+
{
+
// This shouldn't happen for this simple sample, with reference backend.
+
// But in general usage Optimize could fail if the hardware at runtime cannot
+
// support the model that has been provided.
+
std::cerr << "Error: Failed to optimise the input network." << std::endl;
+
return 1;
+
}
+
+
// Load graph into runtime
+
run->LoadNetwork(networkIdentifier, std::move(optNet));
+
+
//Creates structures for inputs and outputs.
+
std::vector<float> inputData{number};
+
std::vector<float> outputData(1);
+
+
inputTensorInfo = run->GetInputTensorInfo(networkIdentifier, 0);
+
inputTensorInfo.SetConstant(true);
+
InputTensors inputTensors{{0, armnn::ConstTensor(inputTensorInfo,
+
inputData.data())}};
+
OutputTensors outputTensors{{0, armnn::Tensor(run->GetOutputTensorInfo(networkIdentifier, 0),
+
outputData.data())}};
+
+
// Execute network
+
run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
+
+
std::cout << "Your number was " << outputData[0] << std::endl;
+
return 0;
+
+
}
+
+
+
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: InputLayer.hpp:13
+
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:339
+
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:340
+
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:319
+ +
A FullyConnectedDescriptor for the FullyConnectedLayer.
+ +
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:392
+
@ CpuRef
CPU Execution: Reference C++ kernels.
+
void SetTensorInfo(const TensorInfo &tensorInfo) override
Definition: Layer.cpp:87
+ +
void ConfigureLogging(bool printToStandardOutput, bool printToDebugOutput, LogSeverity severity)
Configures the logging behaviour of the ARMNN library.
Definition: Utils.cpp:18
+ +
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
+
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:393
+
int Connect(InputSlot &destination)
Definition: Layer.cpp:112
+
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
+ +
int NetworkId
Definition: IRuntime.hpp:35
+ +
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:41
+
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
+ +
A layer user-provided data can be bound to (e.g. inputs, outputs).
Definition: OutputLayer.hpp:13
+
int main(int argc, char *argv[])
+
virtual int Connect(IInputSlot &destination)=0
+ +
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:52
+ +
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
+
Copyright (c) 2021 ARM Limited and Contributors.
+
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
+
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
+
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:80
+
void SetConstant(const bool IsConstant=true)
Marks the data corresponding to this tensor info as constant.
Definition: Tensor.cpp:514
+
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptionsOpaque &options=OptimizerOptionsOpaque(), Optional< std::vector< std::string > & > messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:2091
+
static INetworkPtr Create(const NetworkOptions &networkOptions={})
Definition: Network.cpp:664
+ + + + -- cgit v1.2.1