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.
(You can find more complex examples using the TfLite Parser in samples/ObjectDetection and samples/SpeechRecognition.)
#include <iostream>
int main()
{
float number;
std::cout << "Please enter a number: " << std::endl;
std::cin >> number;
float weightsData[] = {1.0f};
weightsInfo.SetConstant();
ConstTensor weights(weightsInfo, weightsData);
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));
IRuntime::CreationOptions options;
InputLayer->GetOutputSlot(0).SetTensorInfo(inputTensorInfo);
fullyConnectedLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
constantWeightsLayer->GetOutputSlot(0).SetTensorInfo(weightsInfo);
if (!optNet)
{
std::cerr << "Error: Failed to optimise the input network." << std::endl;
return 1;
}
run->LoadNetwork(networkIdentifier, std::move(optNet));
std::vector<float> inputData{number};
std::vector<float> outputData(1);
inputTensorInfo = run->GetInputTensorInfo(networkIdentifier, 0);
inputTensorInfo.SetConstant(true);
inputData.data())}};
outputData.data())}};
run->EnqueueWorkload(networkIdentifier, inputTensors, outputTensors);
std::cout << "Your number was " << outputData[0] << std::endl;
return 0;
}
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
static INetworkPtr Create(const NetworkOptions &networkOptions={})
static IRuntimePtr Create(const CreationOptions &options)
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Copyright (c) 2021 ARM Limited and Contributors.
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
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.
void ConfigureLogging(bool printToStandardOutput, bool printToDebugOutput, LogSeverity severity)
Configures the logging behaviour of the ARMNN library.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
@ CpuRef
CPU Execution: Reference C++ kernels.