Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
Matteo Martincigh | f02e6cd | 2019-05-17 12:15:30 +0100 | [diff] [blame] | 8 | #include "CommonTestUtils.hpp" |
| 9 | |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 10 | #include <armnn/INetwork.hpp> |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 11 | #include <ResolveType.hpp> |
Narumol Prangnawarat | 6d302bf | 2019-02-04 11:46:26 +0000 | [diff] [blame] | 12 | |
| 13 | namespace{ |
| 14 | |
| 15 | template<typename T> |
| 16 | armnn::INetworkPtr CreateDetectionPostProcessNetwork(const armnn::TensorInfo& boxEncodingsInfo, |
| 17 | const armnn::TensorInfo& scoresInfo, |
| 18 | const armnn::TensorInfo& anchorsInfo, |
| 19 | const std::vector<T>& anchors, |
| 20 | bool useRegularNms) |
| 21 | { |
| 22 | armnn::TensorInfo detectionBoxesInfo({ 1, 3, 4 }, armnn::DataType::Float32); |
| 23 | armnn::TensorInfo detectionScoresInfo({ 1, 3 }, armnn::DataType::Float32); |
| 24 | armnn::TensorInfo detectionClassesInfo({ 1, 3 }, armnn::DataType::Float32); |
| 25 | armnn::TensorInfo numDetectionInfo({ 1 }, armnn::DataType::Float32); |
| 26 | |
| 27 | armnn::DetectionPostProcessDescriptor desc; |
| 28 | desc.m_UseRegularNms = useRegularNms; |
| 29 | desc.m_MaxDetections = 3; |
| 30 | desc.m_MaxClassesPerDetection = 1; |
| 31 | desc.m_DetectionsPerClass =1; |
| 32 | desc.m_NmsScoreThreshold = 0.0; |
| 33 | desc.m_NmsIouThreshold = 0.5; |
| 34 | desc.m_NumClasses = 2; |
| 35 | desc.m_ScaleY = 10.0; |
| 36 | desc.m_ScaleX = 10.0; |
| 37 | desc.m_ScaleH = 5.0; |
| 38 | desc.m_ScaleW = 5.0; |
| 39 | |
| 40 | armnn::INetworkPtr net(armnn::INetwork::Create()); |
| 41 | |
| 42 | armnn::IConnectableLayer* boxesLayer = net->AddInputLayer(0); |
| 43 | armnn::IConnectableLayer* scoresLayer = net->AddInputLayer(1); |
| 44 | armnn::ConstTensor anchorsTensor(anchorsInfo, anchors.data()); |
| 45 | armnn::IConnectableLayer* detectionLayer = net->AddDetectionPostProcessLayer(desc, anchorsTensor, |
| 46 | "DetectionPostProcess"); |
| 47 | armnn::IConnectableLayer* detectionBoxesLayer = net->AddOutputLayer(0, "detectionBoxes"); |
| 48 | armnn::IConnectableLayer* detectionClassesLayer = net->AddOutputLayer(1, "detectionClasses"); |
| 49 | armnn::IConnectableLayer* detectionScoresLayer = net->AddOutputLayer(2, "detectionScores"); |
| 50 | armnn::IConnectableLayer* numDetectionLayer = net->AddOutputLayer(3, "numDetection"); |
| 51 | Connect(boxesLayer, detectionLayer, boxEncodingsInfo, 0, 0); |
| 52 | Connect(scoresLayer, detectionLayer, scoresInfo, 0, 1); |
| 53 | Connect(detectionLayer, detectionBoxesLayer, detectionBoxesInfo, 0, 0); |
| 54 | Connect(detectionLayer, detectionClassesLayer, detectionClassesInfo, 1, 0); |
| 55 | Connect(detectionLayer, detectionScoresLayer, detectionScoresInfo, 2, 0); |
| 56 | Connect(detectionLayer, numDetectionLayer, numDetectionInfo, 3, 0); |
| 57 | |
| 58 | return net; |
| 59 | } |
| 60 | |
| 61 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 62 | void DetectionPostProcessEndToEnd(const std::vector<BackendId>& backends, bool useRegularNms, |
| 63 | const std::vector<T>& boxEncodings, |
| 64 | const std::vector<T>& scores, |
| 65 | const std::vector<T>& anchors, |
| 66 | const std::vector<float>& expectedDetectionBoxes, |
| 67 | const std::vector<float>& expectedDetectionClasses, |
| 68 | const std::vector<float>& expectedDetectionScores, |
| 69 | const std::vector<float>& expectedNumDetections, |
| 70 | float boxScale = 1.0f, |
| 71 | int32_t boxOffset = 0, |
| 72 | float scoreScale = 1.0f, |
| 73 | int32_t scoreOffset = 0, |
| 74 | float anchorScale = 1.0f, |
| 75 | int32_t anchorOffset = 0) |
| 76 | { |
| 77 | armnn::TensorInfo boxEncodingsInfo({ 1, 6, 4 }, ArmnnType); |
| 78 | armnn::TensorInfo scoresInfo({ 1, 6, 3}, ArmnnType); |
| 79 | armnn::TensorInfo anchorsInfo({ 6, 4 }, ArmnnType); |
| 80 | |
| 81 | boxEncodingsInfo.SetQuantizationScale(boxScale); |
| 82 | boxEncodingsInfo.SetQuantizationOffset(boxOffset); |
| 83 | scoresInfo.SetQuantizationScale(scoreScale); |
| 84 | scoresInfo.SetQuantizationOffset(scoreOffset); |
| 85 | anchorsInfo.SetQuantizationScale(anchorScale); |
| 86 | anchorsInfo.SetQuantizationOffset(anchorOffset); |
| 87 | |
| 88 | // Builds up the structure of the network |
| 89 | armnn::INetworkPtr net = CreateDetectionPostProcessNetwork<T>(boxEncodingsInfo, scoresInfo, |
| 90 | anchorsInfo, anchors, useRegularNms); |
| 91 | |
| 92 | BOOST_TEST_CHECKPOINT("create a network"); |
| 93 | |
| 94 | std::map<int, std::vector<T>> inputTensorData = {{ 0, boxEncodings }, { 1, scores }}; |
| 95 | std::map<int, std::vector<float>> expectedOutputData = {{ 0, expectedDetectionBoxes }, |
| 96 | { 1, expectedDetectionClasses }, |
| 97 | { 2, expectedDetectionScores }, |
| 98 | { 3, expectedNumDetections }}; |
| 99 | |
| 100 | EndToEndLayerTestImpl<ArmnnType, armnn::DataType::Float32>( |
| 101 | move(net), inputTensorData, expectedOutputData, backends); |
| 102 | } |
| 103 | |
| 104 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 105 | void DetectionPostProcessRegularNmsEndToEnd(const std::vector<BackendId>& backends, |
| 106 | const std::vector<T>& boxEncodings, |
| 107 | const std::vector<T>& scores, |
| 108 | const std::vector<T>& anchors, |
| 109 | float boxScale = 1.0f, |
| 110 | int32_t boxOffset = 0, |
| 111 | float scoreScale = 1.0f, |
| 112 | int32_t scoreOffset = 0, |
| 113 | float anchorScale = 1.0f, |
| 114 | int32_t anchorOffset = 0) |
| 115 | { |
| 116 | std::vector<float> expectedDetectionBoxes({ |
| 117 | 0.0f, 10.0f, 1.0f, 11.0f, |
| 118 | 0.0f, 10.0f, 1.0f, 11.0f, |
| 119 | 0.0f, 0.0f, 0.0f, 0.0f |
| 120 | }); |
| 121 | std::vector<float> expectedDetectionScores({ 0.95f, 0.93f, 0.0f }); |
| 122 | std::vector<float> expectedDetectionClasses({ 1.0f, 0.0f, 0.0f }); |
| 123 | std::vector<float> expectedNumDetections({ 2.0f }); |
| 124 | |
| 125 | DetectionPostProcessEndToEnd<ArmnnType>(backends, true, boxEncodings, scores, anchors, |
| 126 | expectedDetectionBoxes, expectedDetectionClasses, |
| 127 | expectedDetectionScores, expectedNumDetections, |
| 128 | boxScale, boxOffset, scoreScale, scoreOffset, |
| 129 | anchorScale, anchorOffset); |
| 130 | |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | template<armnn::DataType ArmnnType, typename T = armnn::ResolveType<ArmnnType>> |
| 135 | void DetectionPostProcessFastNmsEndToEnd(const std::vector<BackendId>& backends, |
| 136 | const std::vector<T>& boxEncodings, |
| 137 | const std::vector<T>& scores, |
| 138 | const std::vector<T>& anchors, |
| 139 | float boxScale = 1.0f, |
| 140 | int32_t boxOffset = 0, |
| 141 | float scoreScale = 1.0f, |
| 142 | int32_t scoreOffset = 0, |
| 143 | float anchorScale = 1.0f, |
| 144 | int32_t anchorOffset = 0) |
| 145 | { |
| 146 | std::vector<float> expectedDetectionBoxes({ |
| 147 | 0.0f, 10.0f, 1.0f, 11.0f, |
| 148 | 0.0f, 0.0f, 1.0f, 1.0f, |
| 149 | 0.0f, 100.0f, 1.0f, 101.0f |
| 150 | }); |
| 151 | std::vector<float> expectedDetectionScores({ 0.95f, 0.9f, 0.3f }); |
| 152 | std::vector<float> expectedDetectionClasses({ 1.0f, 0.0f, 0.0f }); |
| 153 | std::vector<float> expectedNumDetections({ 3.0f }); |
| 154 | |
| 155 | DetectionPostProcessEndToEnd<ArmnnType>(backends, false, boxEncodings, scores, anchors, |
| 156 | expectedDetectionBoxes, expectedDetectionClasses, |
| 157 | expectedDetectionScores, expectedNumDetections, |
| 158 | boxScale, boxOffset, scoreScale, scoreOffset, |
| 159 | anchorScale, anchorOffset); |
| 160 | |
| 161 | }; |
| 162 | |
| 163 | } // anonymous namespace |