From 4aff98fcfd3c736115f3983dc448c3280e570841 Mon Sep 17 00:00:00 2001 From: Michele Di Giorgio Date: Wed, 28 Aug 2019 16:27:26 +0100 Subject: COMPMID-2247: Extend support of CLBoundingBoxTransform for QUANT16_ASYMM Change-Id: I8af7a382c0bccf55cf7f4a64f46ce9e6cd965afe Signed-off-by: Michele Di Giorgio Reviewed-on: https://review.mlplatform.org/c/1833 Comments-Addressed: Arm Jenkins Reviewed-by: Pablo Marquez Tested-by: Arm Jenkins --- .../fixtures/BoundingBoxTransformFixture.h | 205 ++++++++++++++------- 1 file changed, 136 insertions(+), 69 deletions(-) (limited to 'tests/validation/fixtures') diff --git a/tests/validation/fixtures/BoundingBoxTransformFixture.h b/tests/validation/fixtures/BoundingBoxTransformFixture.h index b71da8e97d..5e4c598f73 100644 --- a/tests/validation/fixtures/BoundingBoxTransformFixture.h +++ b/tests/validation/fixtures/BoundingBoxTransformFixture.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 ARM Limited. + * Copyright (c) 2018-2019 ARM Limited. * * SPDX-License-Identifier: MIT * @@ -40,28 +40,117 @@ namespace test { namespace validation { +namespace +{ +std::vector generate_deltas(std::vector &boxes, const TensorShape &image_shape, size_t num_boxes, size_t num_classes, std::mt19937 &gen) +{ + std::vector deltas(num_boxes * 4 * num_classes); + + std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1); + std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1); + std::uniform_int_distribution<> dist_w(1, image_shape[0]); + std::uniform_int_distribution<> dist_h(1, image_shape[1]); + + for(size_t i = 0; i < num_boxes; ++i) + { + const float ex_width = boxes[4 * i + 2] - boxes[4 * i] + 1.f; + const float ex_height = boxes[4 * i + 3] - boxes[4 * i + 1] + 1.f; + const float ex_ctr_x = boxes[4 * i] + 0.5f * ex_width; + const float ex_ctr_y = boxes[4 * i + 1] + 0.5f * ex_height; + + for(size_t j = 0; j < num_classes; ++j) + { + const float x1 = dist_x1(gen); + const float y1 = dist_y1(gen); + const float width = dist_w(gen); + const float height = dist_h(gen); + const float ctr_x = x1 + 0.5f * width; + const float ctr_y = y1 + 0.5f * height; + + deltas[4 * num_classes * i + 4 * j] = (ctr_x - ex_ctr_x) / ex_width; + deltas[4 * num_classes * i + 4 * j + 1] = (ctr_y - ex_ctr_y) / ex_height; + deltas[4 * num_classes * i + 4 * j + 2] = log(width / ex_width); + deltas[4 * num_classes * i + 4 * j + 3] = log(height / ex_height); + } + } + return deltas; +} + +std::vector generate_boxes(const TensorShape &image_shape, size_t num_boxes, std::mt19937 &gen) +{ + std::vector boxes(num_boxes * 4); + + std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1); + std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1); + std::uniform_int_distribution<> dist_w(1, image_shape[0]); + std::uniform_int_distribution<> dist_h(1, image_shape[1]); + + for(size_t i = 0; i < num_boxes; ++i) + { + boxes[4 * i] = dist_x1(gen); + boxes[4 * i + 1] = dist_y1(gen); + boxes[4 * i + 2] = boxes[4 * i] + dist_w(gen) - 1; + boxes[4 * i + 3] = boxes[4 * i + 1] + dist_h(gen) - 1; + } + return boxes; +} +} // namespace + template -class BoundingBoxTransformFixture : public framework::Fixture +class BoundingBoxTransformGenericFixture : public framework::Fixture { public: + using TDeltas = typename std::conditional::type, uint16_t>::value, uint8_t, T>::type; + template - void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type) + void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo) { + const bool is_qasymm16 = data_type == DataType::QASYMM16; + _data_type_deltas = (is_qasymm16) ? DataType::QASYMM8 : data_type; + _boxes_qinfo = (is_qasymm16) ? QuantizationInfo(.125f, 0) : QuantizationInfo(); + std::mt19937 gen_target(library->seed()); - _target = compute_target(deltas_shape, data_type, info, gen_target); + _target = compute_target(deltas_shape, data_type, info, gen_target, deltas_qinfo); std::mt19937 gen_reference(library->seed()); - _reference = compute_reference(deltas_shape, data_type, info, gen_reference); + _reference = compute_reference(deltas_shape, data_type, info, gen_reference, deltas_qinfo); } protected: + template + void fill(U &&tensor, std::vector values) + { + data_type *data_ptr = reinterpret_cast(tensor.data()); + switch(tensor.data_type()) + { + case DataType::QASYMM8: + for(size_t i = 0; i < values.size(); ++i) + { + data_ptr[i] = quantize_qasymm8(values[i], tensor.quantization_info()); + } + break; + case DataType::QASYMM16: + for(size_t i = 0; i < values.size(); ++i) + { + data_ptr[i] = quantize_qasymm16(values[i], tensor.quantization_info()); + } + break; + default: + for(size_t i = 0; i < values.size(); ++i) + { + data_ptr[i] = static_cast(values[i]); + } + } + } + TensorType compute_target(const TensorShape &deltas_shape, DataType data_type, - const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen) + const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen, + QuantizationInfo deltas_qinfo) { // Create tensors TensorShape boxes_shape(4, deltas_shape[1]); - TensorType deltas = create_tensor(deltas_shape, data_type); - TensorType boxes = create_tensor(boxes_shape, data_type); + TensorType deltas = create_tensor(deltas_shape, _data_type_deltas, 1, deltas_qinfo); + TensorType boxes = create_tensor(boxes_shape, data_type, 1, _boxes_qinfo); TensorType pred_boxes; // Create and configure function @@ -81,9 +170,11 @@ protected: ARM_COMPUTE_EXPECT(!boxes.info()->is_resizable(), framework::LogLevel::ERRORS); // Fill tensors - TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height()); - generate_boxes(AccessorType(boxes), img_shape, boxes_shape[1], gen); - generate_deltas(AccessorType(deltas), AccessorType(boxes), img_shape, deltas_shape[1], deltas_shape[0] / 4, gen); + TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height()); + std::vector boxes_vec = generate_boxes(img_shape, boxes_shape[1], gen); + std::vector deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen); + fill(AccessorType(boxes), boxes_vec); + fill(AccessorType(deltas), deltas_vec); // Compute function bbox_transform.run(); @@ -93,80 +184,56 @@ protected: SimpleTensor compute_reference(const TensorShape &deltas_shape, DataType data_type, - const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen) + const BoundingBoxTransformInfo &bbox_info, + std::mt19937 &gen, + QuantizationInfo deltas_qinfo) { // Create reference tensor - TensorShape boxes_shape(4, deltas_shape[1]); - SimpleTensor boxes{ boxes_shape, data_type }; - SimpleTensor deltas{ deltas_shape, data_type }; + TensorShape boxes_shape(4, deltas_shape[1]); + SimpleTensor boxes{ boxes_shape, data_type, 1, _boxes_qinfo }; + SimpleTensor deltas{ deltas_shape, _data_type_deltas, 1, deltas_qinfo }; // Fill reference tensor - TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height()); - generate_boxes(boxes, img_shape, boxes_shape[1], gen); - generate_deltas(deltas, boxes, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen); + TensorShape img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height()); + std::vector boxes_vec = generate_boxes(img_shape, boxes_shape[1], gen); + std::vector deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen); + fill(boxes, boxes_vec); + fill(deltas, deltas_vec); return reference::bounding_box_transform(boxes, deltas, bbox_info); } - TensorType _target{}; - SimpleTensor _reference{}; + TensorType _target{}; + SimpleTensor _reference{}; + DataType _data_type_deltas{}; + QuantizationInfo _boxes_qinfo{}; private: - template - void generate_deltas(U &&deltas, U &&boxes, const TensorShape &image_shape, size_t num_boxes, size_t num_classes, std::mt19937 &gen) - { - T *deltas_ptr = static_cast(deltas.data()); - T *boxes_ptr = static_cast(boxes.data()); - - std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1); - std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1); - std::uniform_int_distribution<> dist_w(1, image_shape[0]); - std::uniform_int_distribution<> dist_h(1, image_shape[1]); - - for(size_t i = 0; i < num_boxes; ++i) - { - const T ex_width = boxes_ptr[4 * i + 2] - boxes_ptr[4 * i] + T(1); - const T ex_height = boxes_ptr[4 * i + 3] - boxes_ptr[4 * i + 1] + T(1); - const T ex_ctr_x = boxes_ptr[4 * i] + T(0.5) * ex_width; - const T ex_ctr_y = boxes_ptr[4 * i + 1] + T(0.5) * ex_height; - - for(size_t j = 0; j < num_classes; ++j) - { - const T x1 = T(dist_x1(gen)); - const T y1 = T(dist_y1(gen)); - const T width = T(dist_w(gen)); - const T height = T(dist_h(gen)); - const T ctr_x = x1 + T(0.5) * width; - const T ctr_y = y1 + T(0.5) * height; - - deltas_ptr[4 * num_classes * i + 4 * j] = (ctr_x - ex_ctr_x) / ex_width; - deltas_ptr[4 * num_classes * i + 4 * j + 1] = (ctr_y - ex_ctr_y) / ex_height; - deltas_ptr[4 * num_classes * i + 4 * j + 2] = log(width / ex_width); - deltas_ptr[4 * num_classes * i + 4 * j + 3] = log(height / ex_height); - } - } - } +}; - template - void generate_boxes(U &&boxes, const TensorShape &image_shape, size_t num_boxes, std::mt19937 &gen) +template +class BoundingBoxTransformFixture : public BoundingBoxTransformGenericFixture +{ +public: + template + void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type) { - T *boxes_ptr = (T *)boxes.data(); + BoundingBoxTransformGenericFixture::setup(deltas_shape, info, data_type, QuantizationInfo()); + } - std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1); - std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1); - std::uniform_int_distribution<> dist_w(1, image_shape[0]); - std::uniform_int_distribution<> dist_h(1, image_shape[1]); +private: +}; - for(size_t i = 0; i < num_boxes; ++i) - { - boxes_ptr[4 * i] = dist_x1(gen); - boxes_ptr[4 * i + 1] = dist_y1(gen); - boxes_ptr[4 * i + 2] = boxes_ptr[4 * i] + dist_w(gen) - 1; - boxes_ptr[4 * i + 3] = boxes_ptr[4 * i + 1] + dist_h(gen) - 1; - } +template +class BoundingBoxTransformQuantizedFixture : public BoundingBoxTransformGenericFixture +{ +public: + template + void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo) + { + BoundingBoxTransformGenericFixture::setup(deltas_shape, info, data_type, deltas_qinfo); } }; - } // namespace validation } // namespace test } // namespace arm_compute -- cgit v1.2.1