ArmNN
 24.08
DetectionPostProcessLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include "LayerCloneBase.hpp"
9 
10 #include <armnn/TypesUtils.hpp>
14 
15 namespace armnn
16 {
17 
20 {
21 }
22 
23 std::unique_ptr<IWorkload> DetectionPostProcessLayer::CreateWorkload(const armnn::IWorkloadFactory& factory) const
24 {
26  descriptor.m_Anchors = m_Anchors.get();
27  SetAdditionalInfo(descriptor);
28 
29  return factory.CreateWorkload(LayerType::DetectionPostProcess, descriptor, PrepInfoAndDesc(descriptor));
30 }
31 
33 {
34  auto layer = CloneBase<DetectionPostProcessLayer>(graph, m_Param, GetName());
35  layer->m_Anchors = m_Anchors ? m_Anchors : nullptr;
36  return std::move(layer);
37 }
38 
40 {
42 
43  const TensorShape& outputShape = GetOutputSlot(0).GetTensorInfo().GetShape();
44 
46 
47  // on this level constant data should not be released.
48  if (!m_Anchors)
49  {
50  throw armnn::LayerValidationException("DetectionPostProcessLayer: Anchors data should not be null.");
51  }
52 
53  if (GetNumOutputSlots() != 4)
54  {
55  throw armnn::LayerValidationException("DetectionPostProcessLayer: The layer should return 4 outputs.");
56  }
57 
58  std::vector<TensorShape> inferredShapes = InferOutputShapes(
61 
62  if (inferredShapes.size() != 4)
63  {
64  throw armnn::LayerValidationException("inferredShapes has "
65  + std::to_string(inferredShapes.size()) +
66  " element(s) - should only have 4.");
67  }
68 
69  if (std::any_of(inferredShapes.begin(), inferredShapes.end(), [] (auto&& inferredShape) {
70  return inferredShape.GetDimensionality() != Dimensionality::Specified;
71  }))
72  {
73  throw armnn::Exception("One of inferredShapes' dimensionalities is not specified.");
74  }
75 
76  ValidateAndCopyShape(outputShape, inferredShapes[0], m_ShapeInferenceMethod, "DetectionPostProcessLayer");
77 
79  inferredShapes[1],
81  "DetectionPostProcessLayer", 1);
82 
84  inferredShapes[2],
86  "DetectionPostProcessLayer", 2);
87 
89  inferredShapes[3],
91  "DetectionPostProcessLayer", 3);
92 }
93 
94 std::vector<TensorShape> DetectionPostProcessLayer::InferOutputShapes(const std::vector<TensorShape>&) const
95 {
96  unsigned int detectedBoxes = m_Param.m_MaxDetections * m_Param.m_MaxClassesPerDetection;
97 
98  std::vector<TensorShape> results;
99  results.push_back({ 1, detectedBoxes, 4 });
100  results.push_back({ 1, detectedBoxes });
101  results.push_back({ 1, detectedBoxes });
102  results.push_back({ 1 });
103  return results;
104 }
105 
107 {
108  // For API stability DO NOT ALTER order and add new members to the end of vector
109  return { m_Anchors };
110 }
111 
113 {
114  ManagedConstTensorHandle managedAnchors(m_Anchors);
115  std::vector<armnn::ConstTensor> constTensors { {managedAnchors.GetTensorInfo(), managedAnchors.Map()} };
116  strategy.ExecuteStrategy(this, GetParameters(), constTensors, GetName());
117 }
118 
119 } // namespace armnn
armnn::DetectionPostProcessLayer::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the DetectionPostProcess type.
Definition: DetectionPostProcessLayer.cpp:23
armnn::DetectionPostProcessLayer::InferOutputShapes
std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const override
The model does not specify the output shapes.
Definition: DetectionPostProcessLayer.cpp:94
DetectionPostProcessLayer.hpp
armnn::OutputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:100
WorkloadData.hpp
armnn::DetectionPostProcessDescriptor::m_MaxDetections
uint32_t m_MaxDetections
Maximum numbers of detections.
Definition: Descriptors.hpp:745
TypesUtils.hpp
armnn::DetectionPostProcessLayer::GetConstantTensorsByRef
ImmutableConstantTensors GetConstantTensorsByRef() const override
Retrieve the handles to the constant values stored by the layer.
Definition: DetectionPostProcessLayer.cpp:106
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::Layer::ValidateAndCopyShape
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:457
armnn::Layer::GetOutputSlot
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:339
armnn::IStrategy
Definition: IStrategy.hpp:16
armnn::ManagedConstTensorHandle
Definition: TensorHandle.hpp:187
armnn::DetectionPostProcessDescriptor::m_MaxClassesPerDetection
uint32_t m_MaxClassesPerDetection
Maximum numbers of classes per detection, used in Fast NMS.
Definition: Descriptors.hpp:747
armnn::DetectionPostProcessQueueDescriptor::m_Anchors
const ConstTensorHandle * m_Anchors
Definition: WorkloadData.hpp:246
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
armnn::LayerWithParameters< DetectionPostProcessDescriptor >::GetParameters
const DetectionPostProcessDescriptor & GetParameters() const override
Definition: LayerWithParameters.hpp:19
WorkloadFactory.hpp
armnn::LayerWithParameters
Definition: LayerWithParameters.hpp:14
armnn::Layer::GetName
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:332
armnn::IConnectableLayer::ImmutableConstantTensors
std::vector< std::reference_wrapper< const std::shared_ptr< ConstTensorHandle > >> ImmutableConstantTensors
Definition: INetwork.hpp:141
armnn::InputSlot::GetTensorInfo
const TensorInfo & GetTensorInfo() const override
Gets the TensorInfo for this InputSlot.
Definition: Layer.cpp:614
armnn::TensorShape
Definition: Tensor.hpp:20
armnn::LayerWithParameters< DetectionPostProcessDescriptor >::m_Param
DetectionPostProcessDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
Definition: LayerWithParameters.hpp:52
armnn::LayerWithParameters< DetectionPostProcessDescriptor >::PrepInfoAndDesc
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *Layer::CreateWorkload.
Definition: LayerWithParameters.hpp:44
armnn::LayerValidationException
Definition: Exceptions.hpp:105
armnn::IWorkloadFactory
Definition: WorkloadFactory.hpp:22
armnn::DetectionPostProcessQueueDescriptor
Definition: WorkloadData.hpp:239
armnn::Layer::GetNumOutputSlots
unsigned int GetNumOutputSlots() const override
Returns the number of connectable output slots.
Definition: Layer.hpp:335
armnn::Layer::VerifyShapeInferenceType
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:526
armnn::GetTensorInfo
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
Definition: RefWorkloadUtils.hpp:33
armnn::ManagedConstTensorHandle::Map
const void * Map(bool blocking=true)
RAII Managed resource Unmaps MemoryArea once out of scope.
Definition: TensorHandle.hpp:196
armnn::Layer::SetAdditionalInfo
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:303
armnn::Exception
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
armnn::LayerType::DetectionPostProcess
@ DetectionPostProcess
armnn::DetectionPostProcessLayer::ValidateTensorShapesFromInputs
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of DetectionPostProcessLayer.
Definition: DetectionPostProcessLayer.cpp:39
armnn::DetectionPostProcessLayer::m_Anchors
std::shared_ptr< ConstTensorHandle > m_Anchors
A unique pointer to store Anchor values.
Definition: DetectionPostProcessLayer.hpp:20
armnn::DetectionPostProcessLayer::ExecuteStrategy
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
Definition: DetectionPostProcessLayer.cpp:112
TensorHandle.hpp
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
armnn::DetectionPostProcessLayer::Clone
DetectionPostProcessLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
Definition: DetectionPostProcessLayer.cpp:32
armnn::DetectionPostProcessLayer::DetectionPostProcessLayer
DetectionPostProcessLayer(const DetectionPostProcessDescriptor &param, const char *name)
Constructor to create a DetectionPostProcessLayer.
Definition: DetectionPostProcessLayer.cpp:18
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::DetectionPostProcessLayer
This layer represents a detection postprocess operator.
Definition: DetectionPostProcessLayer.hpp:16
armnn::Layer::VerifyLayerConnections
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:410
armnn::DetectionPostProcessDescriptor
Definition: Descriptors.hpp:713
armnn::Layer::m_ShapeInferenceMethod
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:441
armnn::LayerType
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below.
Definition: Types.hpp:491
armnn::Graph
Definition: Graph.hpp:30
armnn::IWorkloadFactory::CreateWorkload
virtual std::unique_ptr< IWorkload > CreateWorkload(LayerType type, const QueueDescriptor &descriptor, const WorkloadInfo &info) const =0
Backends should implement their own CreateWorkload function with a switch statement.
armnn::IStrategy::ExecuteStrategy
virtual void ExecuteStrategy(const IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
LayerCloneBase.hpp
armnn::ManagedConstTensorHandle::GetTensorInfo
const TensorInfo & GetTensorInfo() const
Definition: TensorHandle.hpp:239
armnn::DetectionPostProcess
void DetectionPostProcess(const TensorInfo &boxEncodingsInfo, const TensorInfo &scoresInfo, const TensorInfo &, const TensorInfo &detectionBoxesInfo, const TensorInfo &, const TensorInfo &, const TensorInfo &, const DetectionPostProcessDescriptor &desc, Decoder< float > &boxEncodings, Decoder< float > &scores, Decoder< float > &anchors, float *detectionBoxes, float *detectionClasses, float *detectionScores, float *numDetections)
Definition: DetectionPostProcess.cpp:139