keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "../TfLiteParser.hpp" |
| 7 | |
| 8 | #include <boost/test/unit_test.hpp> |
| 9 | #include "test/GraphUtils.hpp" |
| 10 | |
| 11 | #include "ParserFlatbuffersFixture.hpp" |
| 12 | #include "ParserPrototxtFixture.hpp" |
keidav01 | 222c753 | 2019-03-14 17:12:10 +0000 | [diff] [blame] | 13 | #include "ParserHelper.hpp" |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 14 | |
| 15 | BOOST_AUTO_TEST_SUITE(TensorflowLiteParser) |
| 16 | |
| 17 | struct DetectionPostProcessFixture : ParserFlatbuffersFixture |
| 18 | { |
keidav01 | 222c753 | 2019-03-14 17:12:10 +0000 | [diff] [blame] | 19 | explicit DetectionPostProcessFixture(const std::string& custom_options) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 20 | { |
| 21 | /* |
| 22 | The following values were used for the custom_options: |
keidav01 | 07d58c7 | 2019-02-26 11:57:39 +0000 | [diff] [blame] | 23 | use_regular_nms = true |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 24 | max_classes_per_detection = 1 |
keidav01 | 222c753 | 2019-03-14 17:12:10 +0000 | [diff] [blame] | 25 | detections_per_class = 1 |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 26 | nms_score_threshold = 0.0 |
| 27 | nms_iou_threshold = 0.5 |
| 28 | max_detections = 3 |
| 29 | max_detections = 3 |
| 30 | num_classes = 2 |
| 31 | h_scale = 5 |
| 32 | w_scale = 5 |
| 33 | x_scale = 10 |
| 34 | y_scale = 10 |
| 35 | */ |
| 36 | m_JsonString = R"( |
| 37 | { |
| 38 | "version": 3, |
| 39 | "operator_codes": [{ |
| 40 | "builtin_code": "CUSTOM", |
| 41 | "custom_code": "TFLite_Detection_PostProcess" |
| 42 | }], |
| 43 | "subgraphs": [{ |
| 44 | "tensors": [{ |
| 45 | "shape": [1, 6, 4], |
| 46 | "type": "UINT8", |
| 47 | "buffer": 0, |
| 48 | "name": "box_encodings", |
| 49 | "quantization": { |
| 50 | "min": [0.0], |
| 51 | "max": [255.0], |
| 52 | "scale": [1.0], |
| 53 | "zero_point": [ 1 ] |
| 54 | } |
| 55 | }, |
| 56 | { |
| 57 | "shape": [1, 6, 3], |
| 58 | "type": "UINT8", |
| 59 | "buffer": 1, |
| 60 | "name": "scores", |
| 61 | "quantization": { |
| 62 | "min": [0.0], |
| 63 | "max": [255.0], |
| 64 | "scale": [0.01], |
| 65 | "zero_point": [0] |
| 66 | } |
| 67 | }, |
| 68 | { |
| 69 | "shape": [6, 4], |
| 70 | "type": "UINT8", |
| 71 | "buffer": 2, |
| 72 | "name": "anchors", |
| 73 | "quantization": { |
| 74 | "min": [0.0], |
| 75 | "max": [255.0], |
| 76 | "scale": [0.5], |
| 77 | "zero_point": [0] |
| 78 | } |
| 79 | }, |
| 80 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 81 | "type": "FLOAT32", |
| 82 | "buffer": 3, |
| 83 | "name": "detection_boxes", |
| 84 | "quantization": {} |
| 85 | }, |
| 86 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 87 | "type": "FLOAT32", |
| 88 | "buffer": 4, |
| 89 | "name": "detection_classes", |
| 90 | "quantization": {} |
| 91 | }, |
| 92 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 93 | "type": "FLOAT32", |
| 94 | "buffer": 5, |
| 95 | "name": "detection_scores", |
| 96 | "quantization": {} |
| 97 | }, |
| 98 | { |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 99 | "type": "FLOAT32", |
| 100 | "buffer": 6, |
| 101 | "name": "num_detections", |
| 102 | "quantization": {} |
| 103 | } |
| 104 | ], |
| 105 | "inputs": [0, 1, 2], |
| 106 | "outputs": [3, 4, 5, 6], |
| 107 | "operators": [{ |
| 108 | "opcode_index": 0, |
| 109 | "inputs": [0, 1, 2], |
| 110 | "outputs": [3, 4, 5, 6], |
| 111 | "builtin_options_type": 0, |
keidav01 | 222c753 | 2019-03-14 17:12:10 +0000 | [diff] [blame] | 112 | "custom_options": [)" + custom_options + R"(], |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 113 | "custom_options_format": "FLEXBUFFERS" |
| 114 | }] |
| 115 | }], |
| 116 | "buffers": [{}, |
| 117 | {}, |
| 118 | { "data": [ 1, 1, 2, 2, |
| 119 | 1, 1, 2, 2, |
| 120 | 1, 1, 2, 2, |
| 121 | 1, 21, 2, 2, |
| 122 | 1, 21, 2, 2, |
| 123 | 1, 201, 2, 2]}, |
| 124 | {}, |
| 125 | {}, |
| 126 | {}, |
| 127 | {}, |
| 128 | ] |
| 129 | } |
| 130 | )"; |
| 131 | } |
| 132 | }; |
| 133 | |
keidav01 | 222c753 | 2019-03-14 17:12:10 +0000 | [diff] [blame] | 134 | struct ParseDetectionPostProcessCustomOptions : DetectionPostProcessFixture |
| 135 | { |
| 136 | private: |
| 137 | static armnn::DetectionPostProcessDescriptor GenerateDescriptor() |
| 138 | { |
| 139 | static armnn::DetectionPostProcessDescriptor descriptor; |
| 140 | descriptor.m_UseRegularNms = true; |
| 141 | descriptor.m_MaxDetections = 3u; |
| 142 | descriptor.m_MaxClassesPerDetection = 1u; |
| 143 | descriptor.m_DetectionsPerClass = 1u; |
| 144 | descriptor.m_NumClasses = 2u; |
| 145 | descriptor.m_NmsScoreThreshold = 0.0f; |
| 146 | descriptor.m_NmsIouThreshold = 0.5f; |
| 147 | descriptor.m_ScaleH = 5.0f; |
| 148 | descriptor.m_ScaleW = 5.0f; |
| 149 | descriptor.m_ScaleX = 10.0f; |
| 150 | descriptor.m_ScaleY = 10.0f; |
| 151 | |
| 152 | return descriptor; |
| 153 | } |
| 154 | |
| 155 | public: |
| 156 | ParseDetectionPostProcessCustomOptions() |
| 157 | : DetectionPostProcessFixture( |
| 158 | GenerateDetectionPostProcessJsonString(GenerateDescriptor())) |
| 159 | {} |
| 160 | }; |
| 161 | |
| 162 | BOOST_FIXTURE_TEST_CASE( ParseDetectionPostProcess, ParseDetectionPostProcessCustomOptions ) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 163 | { |
| 164 | Setup(); |
| 165 | |
| 166 | // Inputs |
| 167 | using UnquantizedContainer = std::vector<float>; |
| 168 | UnquantizedContainer boxEncodings = |
| 169 | { |
| 170 | 0.0f, 0.0f, 0.0f, 0.0f, |
| 171 | 0.0f, 1.0f, 0.0f, 0.0f, |
| 172 | 0.0f, -1.0f, 0.0f, 0.0f, |
| 173 | 0.0f, 0.0f, 0.0f, 0.0f, |
| 174 | 0.0f, 1.0f, 0.0f, 0.0f, |
| 175 | 0.0f, 0.0f, 0.0f, 0.0f |
| 176 | }; |
| 177 | |
| 178 | UnquantizedContainer scores = |
| 179 | { |
| 180 | 0.0f, 0.9f, 0.8f, |
| 181 | 0.0f, 0.75f, 0.72f, |
| 182 | 0.0f, 0.6f, 0.5f, |
| 183 | 0.0f, 0.93f, 0.95f, |
| 184 | 0.0f, 0.5f, 0.4f, |
| 185 | 0.0f, 0.3f, 0.2f |
| 186 | }; |
| 187 | |
| 188 | // Outputs |
| 189 | UnquantizedContainer detectionBoxes = |
| 190 | { |
| 191 | 0.0f, 10.0f, 1.0f, 11.0f, |
| 192 | 0.0f, 10.0f, 1.0f, 11.0f, |
| 193 | 0.0f, 0.0f, 0.0f, 0.0f |
| 194 | }; |
| 195 | |
| 196 | UnquantizedContainer detectionClasses = { 1.0f, 0.0f, 0.0f }; |
| 197 | UnquantizedContainer detectionScores = { 0.95f, 0.93f, 0.0f }; |
| 198 | |
| 199 | UnquantizedContainer numDetections = { 2.0f }; |
| 200 | |
| 201 | // Quantize inputs and outputs |
| 202 | using QuantizedContainer = std::vector<uint8_t>; |
| 203 | QuantizedContainer quantBoxEncodings = QuantizedVector<uint8_t>(1.0f, 1, boxEncodings); |
| 204 | QuantizedContainer quantScores = QuantizedVector<uint8_t>(0.01f, 0, scores); |
| 205 | |
| 206 | std::map<std::string, QuantizedContainer> input = |
| 207 | { |
| 208 | { "box_encodings", quantBoxEncodings }, |
| 209 | { "scores", quantScores } |
| 210 | }; |
| 211 | |
| 212 | std::map<std::string, UnquantizedContainer> output = |
| 213 | { |
| 214 | { "detection_boxes", detectionBoxes}, |
| 215 | { "detection_classes", detectionClasses}, |
| 216 | { "detection_scores", detectionScores}, |
| 217 | { "num_detections", numDetections} |
| 218 | }; |
| 219 | |
| 220 | RunTest<armnn::DataType::QuantisedAsymm8, armnn::DataType::Float32>(0, input, output); |
| 221 | } |
| 222 | |
keidav01 | 222c753 | 2019-03-14 17:12:10 +0000 | [diff] [blame] | 223 | BOOST_FIXTURE_TEST_CASE(DetectionPostProcessGraphStructureTest, ParseDetectionPostProcessCustomOptions) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 224 | { |
| 225 | /* |
| 226 | Inputs: box_encodings scores |
| 227 | \ / |
| 228 | DetectionPostProcess |
| 229 | / / \ \ |
| 230 | / / \ \ |
| 231 | Outputs: detection detection detection num_detections |
| 232 | boxes classes scores |
| 233 | */ |
| 234 | |
| 235 | ReadStringToBinary(); |
| 236 | |
| 237 | armnn::INetworkPtr network = m_Parser->CreateNetworkFromBinary(m_GraphBinary); |
| 238 | |
| 239 | auto optimized = Optimize(*network, { armnn::Compute::CpuRef }, m_Runtime->GetDeviceSpec()); |
| 240 | |
| 241 | auto optimizedNetwork = boost::polymorphic_downcast<armnn::OptimizedNetwork*>(optimized.get()); |
| 242 | auto graph = optimizedNetwork->GetGraph(); |
| 243 | |
| 244 | // Check the number of layers in the graph |
| 245 | BOOST_TEST((graph.GetNumInputs() == 2)); |
| 246 | BOOST_TEST((graph.GetNumOutputs() == 4)); |
| 247 | BOOST_TEST((graph.GetNumLayers() == 7)); |
| 248 | |
| 249 | // Input layers |
| 250 | armnn::Layer* boxEncodingLayer = GetFirstLayerWithName(graph, "box_encodings"); |
| 251 | BOOST_TEST((boxEncodingLayer->GetType() == armnn::LayerType::Input)); |
| 252 | BOOST_TEST(CheckNumberOfInputSlot(boxEncodingLayer, 0)); |
| 253 | BOOST_TEST(CheckNumberOfOutputSlot(boxEncodingLayer, 1)); |
| 254 | |
| 255 | armnn::Layer* scoresLayer = GetFirstLayerWithName(graph, "scores"); |
| 256 | BOOST_TEST((scoresLayer->GetType() == armnn::LayerType::Input)); |
| 257 | BOOST_TEST(CheckNumberOfInputSlot(scoresLayer, 0)); |
| 258 | BOOST_TEST(CheckNumberOfOutputSlot(scoresLayer, 1)); |
| 259 | |
| 260 | // DetectionPostProcess layer |
| 261 | armnn::Layer* detectionPostProcessLayer = GetFirstLayerWithName(graph, "DetectionPostProcess:0:0"); |
| 262 | BOOST_TEST((detectionPostProcessLayer->GetType() == armnn::LayerType::DetectionPostProcess)); |
| 263 | BOOST_TEST(CheckNumberOfInputSlot(detectionPostProcessLayer, 2)); |
| 264 | BOOST_TEST(CheckNumberOfOutputSlot(detectionPostProcessLayer, 4)); |
| 265 | |
| 266 | // Output layers |
| 267 | armnn::Layer* detectionBoxesLayer = GetFirstLayerWithName(graph, "detection_boxes"); |
| 268 | BOOST_TEST((detectionBoxesLayer->GetType() == armnn::LayerType::Output)); |
| 269 | BOOST_TEST(CheckNumberOfInputSlot(detectionBoxesLayer, 1)); |
| 270 | BOOST_TEST(CheckNumberOfOutputSlot(detectionBoxesLayer, 0)); |
| 271 | |
| 272 | armnn::Layer* detectionClassesLayer = GetFirstLayerWithName(graph, "detection_classes"); |
| 273 | BOOST_TEST((detectionClassesLayer->GetType() == armnn::LayerType::Output)); |
| 274 | BOOST_TEST(CheckNumberOfInputSlot(detectionClassesLayer, 1)); |
| 275 | BOOST_TEST(CheckNumberOfOutputSlot(detectionClassesLayer, 0)); |
| 276 | |
| 277 | armnn::Layer* detectionScoresLayer = GetFirstLayerWithName(graph, "detection_scores"); |
| 278 | BOOST_TEST((detectionScoresLayer->GetType() == armnn::LayerType::Output)); |
| 279 | BOOST_TEST(CheckNumberOfInputSlot(detectionScoresLayer, 1)); |
| 280 | BOOST_TEST(CheckNumberOfOutputSlot(detectionScoresLayer, 0)); |
| 281 | |
| 282 | armnn::Layer* numDetectionsLayer = GetFirstLayerWithName(graph, "num_detections"); |
| 283 | BOOST_TEST((numDetectionsLayer->GetType() == armnn::LayerType::Output)); |
| 284 | BOOST_TEST(CheckNumberOfInputSlot(numDetectionsLayer, 1)); |
| 285 | BOOST_TEST(CheckNumberOfOutputSlot(numDetectionsLayer, 0)); |
| 286 | |
| 287 | // Check the connections |
| 288 | armnn::TensorInfo boxEncodingTensor(armnn::TensorShape({ 1, 6, 4 }), armnn::DataType::QuantisedAsymm8, 1, 1); |
| 289 | armnn::TensorInfo scoresTensor(armnn::TensorShape({ 1, 6, 3 }), armnn::DataType::QuantisedAsymm8, |
| 290 | 0.00999999978f, 0); |
| 291 | |
| 292 | armnn::TensorInfo detectionBoxesTensor(armnn::TensorShape({ 1, 3, 4 }), armnn::DataType::Float32, 0, 0); |
| 293 | armnn::TensorInfo detectionClassesTensor(armnn::TensorShape({ 1, 3 }), armnn::DataType::Float32, 0, 0); |
| 294 | armnn::TensorInfo detectionScoresTensor(armnn::TensorShape({ 1, 3 }), armnn::DataType::Float32, 0, 0); |
| 295 | armnn::TensorInfo numDetectionsTensor(armnn::TensorShape({ 1} ), armnn::DataType::Float32, 0, 0); |
| 296 | |
| 297 | BOOST_TEST(IsConnected(boxEncodingLayer, detectionPostProcessLayer, 0, 0, boxEncodingTensor)); |
| 298 | BOOST_TEST(IsConnected(scoresLayer, detectionPostProcessLayer, 0, 1, scoresTensor)); |
| 299 | BOOST_TEST(IsConnected(detectionPostProcessLayer, detectionBoxesLayer, 0, 0, detectionBoxesTensor)); |
| 300 | BOOST_TEST(IsConnected(detectionPostProcessLayer, detectionClassesLayer, 1, 0, detectionClassesTensor)); |
| 301 | BOOST_TEST(IsConnected(detectionPostProcessLayer, detectionScoresLayer, 2, 0, detectionScoresTensor)); |
| 302 | BOOST_TEST(IsConnected(detectionPostProcessLayer, numDetectionsLayer, 3, 0, numDetectionsTensor)); |
| 303 | } |
| 304 | |
| 305 | BOOST_AUTO_TEST_SUITE_END() |