ArmNN
 21.02
DetectionPostProcessLayer.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 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.CreateDetectionPostProcess(descriptor, PrepInfoAndDesc(descriptor));
30 }
31 
33 {
34  auto layer = CloneBase<DetectionPostProcessLayer>(graph, m_Param, GetName());
35  layer->m_Anchors = m_Anchors ? std::make_unique<ScopedCpuTensorHandle>(*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  ARMNN_ASSERT_MSG(m_Anchors != nullptr, "DetectionPostProcessLayer: Anchors data should not be null.");
49 
50  ARMNN_ASSERT_MSG(GetNumOutputSlots() == 4, "DetectionPostProcessLayer: The layer should return 4 outputs.");
51 
52  unsigned int detectedBoxes = m_Param.m_MaxDetections * m_Param.m_MaxClassesPerDetection;
53 
54  const TensorShape& inferredDetectionBoxes = TensorShape({ 1, detectedBoxes, 4 });
55  const TensorShape& inferredDetectionScores = TensorShape({ 1, detectedBoxes });
56  const TensorShape& inferredNumberDetections = TensorShape({ 1 });
57 
58  ValidateAndCopyShape(outputShape, inferredDetectionBoxes, m_ShapeInferenceMethod, "DetectionPostProcessLayer");
59 
61  inferredDetectionScores,
63  "DetectionPostProcessLayer", 1);
64 
66  inferredDetectionScores,
68  "DetectionPostProcessLayer", 2);
69 
71  inferredNumberDetections,
73  "DetectionPostProcessLayer", 3);
74 }
75 
77 {
78  return { m_Anchors };
79 }
80 
82 {
83  ConstTensor anchorTensor(m_Anchors->GetTensorInfo(), m_Anchors->GetConstTensor<void>());
84  visitor.VisitDetectionPostProcessLayer(this, GetParameters(), anchorTensor, GetName());
85 }
86 
88 {
89  std::vector<armnn::ConstTensor> constTensors { {m_Anchors->GetTensorInfo(), m_Anchors->GetConstTensor<void>()} };
90 
91  strategy.ExecuteStrategy(this, GetParameters(), constTensors, GetName());
92 }
93 
94 } // namespace armnn
DetectionPostProcessDescriptor m_Param
The parameters for the layer (not including tensor-valued weights etc.).
const DetectionPostProcessDescriptor & GetParameters() const
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
virtual std::unique_ptr< IWorkload > CreateWorkload(const IWorkloadFactory &factory) const override
Makes a workload for the DetectionPostProcess type.
virtual void ExecuteStrategy(const armnn::IConnectableLayer *layer, const armnn::BaseDescriptor &descriptor, const std::vector< armnn::ConstTensor > &constants, const char *name, const armnn::LayerBindingId id=0)=0
DetectionPostProcessLayer(const DetectionPostProcessDescriptor &param, const char *name)
Constructor to create a DetectionPostProcessLayer.
void Accept(ILayerVisitor &visitor) const override
Apply a visitor to this layer.
This layer represents a detection postprocess operator.
void VerifyShapeInferenceType(const TensorShape &outputShape, ShapeInferenceMethod shapeInferenceMethod)
Definition: Layer.cpp:432
Copyright (c) 2021 ARM Limited and Contributors.
unsigned int GetNumOutputSlots() const override
Returns the number of connectable output slots.
Definition: Layer.hpp:314
void ValidateAndCopyShape(const TensorShape &outputShape, const TensorShape &inferredShape, const ShapeInferenceMethod shapeInferenceMethod, const std::string &layerName, const unsigned int outputSlotIndex=0)
Definition: Layer.cpp:392
uint32_t m_MaxClassesPerDetection
Maximum numbers of classes per detection, used in Fast NMS.
void VerifyLayerConnections(unsigned int expectedConnections, const CheckLocation &location) const
Definition: Layer.cpp:348
uint32_t m_MaxDetections
Maximum numbers of detections.
void DetectionPostProcess(const TensorInfo &boxEncodingsInfo, const TensorInfo &scoresInfo, const TensorInfo &anchorsInfo, const TensorInfo &detectionBoxesInfo, const TensorInfo &detectionClassesInfo, const TensorInfo &detectionScoresInfo, const TensorInfo &numDetectionsInfo, const DetectionPostProcessDescriptor &desc, Decoder< float > &boxEncodings, Decoder< float > &scores, Decoder< float > &anchors, float *detectionBoxes, float *detectionClasses, float *detectionScores, float *numDetections)
virtual std::unique_ptr< IWorkload > CreateDetectionPostProcess(const DetectionPostProcessQueueDescriptor &descriptor, const WorkloadInfo &info) const
std::unique_ptr< ScopedCpuTensorHandle > m_Anchors
A unique pointer to store Anchor values.
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:314
const ConstCpuTensorHandle * m_Anchors
ConstantTensors GetConstantTensorsByRef() override
Retrieve the handles to the constant values stored by the layer.
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
void SetAdditionalInfo(QueueDescriptor &descriptor) const
Definition: Layer.cpp:245
void ValidateTensorShapesFromInputs() override
Check if the input tensor shape(s) will lead to a valid configuration of DetectionPostProcessLayer.
WorkloadInfo PrepInfoAndDesc(QueueDescriptor &descriptor) const
Helper function to reduce duplication in *LayerCreateWorkload.
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
Definition: Layer.hpp:318
const char * GetName() const override
Returns the name of the layer.
Definition: Layer.hpp:311
DetectionPostProcessLayer * Clone(Graph &graph) const override
Creates a dynamically-allocated copy of this layer.
std::vector< std::reference_wrapper< std::unique_ptr< ScopedCpuTensorHandle > >> ConstantTensors
Definition: Layer.hpp:393
const TensorInfo & GetTensorInfo(const ITensorHandle *tensorHandle)
float32 helpers
virtual void VisitDetectionPostProcessLayer(const IConnectableLayer *layer, const DetectionPostProcessDescriptor &descriptor, const ConstTensor &anchors, const char *name=nullptr)=0
Function that a Detection PostProcess layer should call back to when its Accept(ILayerVisitor&) funct...
const TensorInfo & GetTensorInfo() const override
Definition: Layer.cpp:63
ShapeInferenceMethod m_ShapeInferenceMethod
Definition: Layer.hpp:408
void ExecuteStrategy(IStrategy &strategy) const override
Apply a visitor to this layer.
LayerType
When adding a new layer, adapt also the LastLayer enum value in the enum class LayerType below...
Definition: Types.hpp:419