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