blob: 8dc3014763df34e9a6c947743bb3e74de1a1b700 [file] [log] [blame]
Suhail Munshiab840882021-02-09 16:31:00 +00001/*
2 * Copyright (c) 2021 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include "ROIPoolingLayer.h"
26#include "arm_compute/core/Types.h"
27#include "arm_compute/core/utils/misc/ShapeCalculator.h"
28#include "tests/validation/Helpers.h"
29#include <algorithm>
30
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
39template <>
40SimpleTensor<float> roi_pool_layer(const SimpleTensor<float> &src, const SimpleTensor<uint16_t> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo)
41{
42 ARM_COMPUTE_UNUSED(output_qinfo);
43
44 const size_t num_rois = rois.shape()[1];
45 const size_t values_per_roi = rois.shape()[0];
46 DataType output_data_type = src.data_type();
47
48 TensorShape input_shape = src.shape();
49 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), src.shape()[2], num_rois);
50 SimpleTensor<float> output(output_shape, output_data_type);
51
52 const int pooled_w = pool_info.pooled_width();
53 const int pooled_h = pool_info.pooled_height();
54 const float spatial_scale = pool_info.spatial_scale();
55
56 // get sizes of x and y dimensions in src tensor
57 const int width = src.shape()[0];
58 const int height = src.shape()[1];
59
60 // Move pointer across the fourth dimension
61 const size_t input_stride_w = input_shape[0] * input_shape[1] * input_shape[2];
62 const size_t output_stride_w = output_shape[0] * output_shape[1] * output_shape[2];
63
64 const auto *rois_ptr = reinterpret_cast<const uint16_t *>(rois.data());
65
66 // Iterate through pixel width (X-Axis)
67 for(size_t pw = 0; pw < num_rois; ++pw)
68 {
69 const unsigned int roi_batch = rois_ptr[values_per_roi * pw];
70 const auto x1 = rois_ptr[values_per_roi * pw + 1];
71 const auto y1 = rois_ptr[values_per_roi * pw + 2];
72 const auto x2 = rois_ptr[values_per_roi * pw + 3];
73 const auto y2 = rois_ptr[values_per_roi * pw + 4];
74
75 //Iterate through pixel height (Y-Axis)
76 for(size_t fm = 0; fm < input_shape[2]; ++fm)
77 {
78 // Iterate through regions of interest index
79 for(size_t py = 0; py < pool_info.pooled_height(); ++py)
80 {
81 // Scale ROI
82 const int roi_anchor_x = support::cpp11::round(x1 * spatial_scale);
83 const int roi_anchor_y = support::cpp11::round(y1 * spatial_scale);
84 const int roi_width = std::max(support::cpp11::round((x2 - x1) * spatial_scale), 1.f);
85 const int roi_height = std::max(support::cpp11::round((y2 - y1) * spatial_scale), 1.f);
86
87 // Iterate over feature map (Z axis)
88 for(size_t px = 0; px < pool_info.pooled_width(); ++px)
89 {
90 auto region_start_x = static_cast<int>(std::floor((static_cast<float>(px) / pooled_w) * roi_width));
91 auto region_end_x = static_cast<int>(std::floor((static_cast<float>(px + 1) / pooled_w) * roi_width));
92 auto region_start_y = static_cast<int>(std::floor((static_cast<float>(py) / pooled_h) * roi_height));
93 auto region_end_y = static_cast<int>(std::floor((static_cast<float>(py + 1) / pooled_h) * roi_height));
94
95 region_start_x = std::min(std::max(region_start_x + roi_anchor_x, 0), width);
96 region_end_x = std::min(std::max(region_end_x + roi_anchor_x, 0), width);
97 region_start_y = std::min(std::max(region_start_y + roi_anchor_y, 0), height);
98 region_end_y = std::min(std::max(region_end_y + roi_anchor_y, 0), height);
99
100 // Iterate through the pooling region
101 if((region_end_x <= region_start_x) || (region_end_y <= region_start_y))
102 {
103 /* Assign element in tensor 'output' at coordinates px, py, fm, roi_indx, to 0 */
104 auto out_ptr = output.data() + px + py * output_shape[0] + fm * output_shape[0] * output_shape[1] + pw * output_stride_w;
105 *out_ptr = 0;
106 }
107 else
108 {
109 float curr_max = -std::numeric_limits<float>::max();
110 for(int j = region_start_y; j < region_end_y; ++j)
111 {
112 for(int i = region_start_x; i < region_end_x; ++i)
113 {
114 /* Retrieve element from input tensor at coordinates(i, j, fm, roi_batch) */
115 float in_element = *(src.data() + i + j * input_shape[0] + fm * input_shape[0] * input_shape[1] + roi_batch * input_stride_w);
116 curr_max = std::max(in_element, curr_max);
117 }
118 }
119
120 /* Assign element in tensor 'output' at coordinates px, py, fm, roi_indx, to curr_max */
121 auto out_ptr = output.data() + px + py * output_shape[0] + fm * output_shape[0] * output_shape[1] + pw * output_stride_w;
122 *out_ptr = curr_max;
123 }
124 }
125 }
126 }
127 }
128
129 return output;
130}
131
132/*
133 Template genericised method to allow calling of roi_pooling_layer with quantized 8 bit datatype
134*/
135template <>
136SimpleTensor<uint8_t> roi_pool_layer(const SimpleTensor<uint8_t> &src, const SimpleTensor<uint16_t> &rois, const ROIPoolingLayerInfo &pool_info, const QuantizationInfo &output_qinfo)
137{
138 const SimpleTensor<float> src_tmp = convert_from_asymmetric(src);
139 SimpleTensor<float> dst_tmp = roi_pool_layer<float>(src_tmp, rois, pool_info, output_qinfo);
140 SimpleTensor<uint8_t> dst = convert_to_asymmetric<uint8_t>(dst_tmp, output_qinfo);
141 return dst;
142}
143
144} // namespace reference
145} // namespace validation
146} // namespace test
147} // namespace arm_compute