1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2018 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 #include "PriorBoxLayer.h"
25*c217d954SCole Faust
26*c217d954SCole Faust #include "ActivationLayer.h"
27*c217d954SCole Faust
28*c217d954SCole Faust #include "tests/validation/Helpers.h"
29*c217d954SCole Faust
30*c217d954SCole Faust namespace arm_compute
31*c217d954SCole Faust {
32*c217d954SCole Faust namespace test
33*c217d954SCole Faust {
34*c217d954SCole Faust namespace validation
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace reference
37*c217d954SCole Faust {
38*c217d954SCole Faust template <typename T>
prior_box_layer(const SimpleTensor<T> & src1,const SimpleTensor<T> & src2,const PriorBoxLayerInfo & info,const TensorShape & output_shape)39*c217d954SCole Faust SimpleTensor<T> prior_box_layer(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape)
40*c217d954SCole Faust {
41*c217d954SCole Faust const auto layer_width = static_cast<int>(src1.shape()[0]);
42*c217d954SCole Faust const auto layer_height = static_cast<int>(src1.shape()[1]);
43*c217d954SCole Faust
44*c217d954SCole Faust int img_width = info.img_size().x;
45*c217d954SCole Faust int img_height = info.img_size().y;
46*c217d954SCole Faust if(img_width == 0 || img_height == 0)
47*c217d954SCole Faust {
48*c217d954SCole Faust img_width = static_cast<int>(src2.shape()[0]);
49*c217d954SCole Faust img_height = static_cast<int>(src2.shape()[1]);
50*c217d954SCole Faust }
51*c217d954SCole Faust
52*c217d954SCole Faust float step_x = info.steps()[0];
53*c217d954SCole Faust float step_y = info.steps()[1];
54*c217d954SCole Faust if(step_x == 0.f || step_y == 0.f)
55*c217d954SCole Faust {
56*c217d954SCole Faust step_x = static_cast<float>(img_width) / layer_width;
57*c217d954SCole Faust step_x = static_cast<float>(img_height) / layer_height;
58*c217d954SCole Faust }
59*c217d954SCole Faust
60*c217d954SCole Faust // Calculate number of aspect ratios
61*c217d954SCole Faust const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
62*c217d954SCole Faust const int total_elements = layer_width * layer_height * num_priors * 4;
63*c217d954SCole Faust
64*c217d954SCole Faust SimpleTensor<T> result(output_shape, src1.data_type());
65*c217d954SCole Faust
66*c217d954SCole Faust int idx = 0;
67*c217d954SCole Faust for(int y = 0; y < layer_height; ++y)
68*c217d954SCole Faust {
69*c217d954SCole Faust for(int x = 0; x < layer_width; ++x)
70*c217d954SCole Faust {
71*c217d954SCole Faust const float center_x = (x + info.offset()) * step_x;
72*c217d954SCole Faust const float center_y = (y + info.offset()) * step_y;
73*c217d954SCole Faust float box_width;
74*c217d954SCole Faust float box_height;
75*c217d954SCole Faust for(unsigned int i = 0; i < info.min_sizes().size(); ++i)
76*c217d954SCole Faust {
77*c217d954SCole Faust const float min_size = info.min_sizes().at(i);
78*c217d954SCole Faust box_width = min_size;
79*c217d954SCole Faust box_height = min_size;
80*c217d954SCole Faust // (xmin, ymin, xmax, ymax)
81*c217d954SCole Faust result[idx++] = (center_x - box_width / 2.f) / img_width;
82*c217d954SCole Faust result[idx++] = (center_y - box_height / 2.f) / img_height;
83*c217d954SCole Faust result[idx++] = (center_x + box_width / 2.f) / img_width;
84*c217d954SCole Faust result[idx++] = (center_y + box_height / 2.f) / img_height;
85*c217d954SCole Faust
86*c217d954SCole Faust if(!info.max_sizes().empty())
87*c217d954SCole Faust {
88*c217d954SCole Faust const float max_size = info.max_sizes().at(i);
89*c217d954SCole Faust box_width = sqrt(min_size * max_size);
90*c217d954SCole Faust box_height = box_width;
91*c217d954SCole Faust
92*c217d954SCole Faust // (xmin, ymin, xmax, ymax)
93*c217d954SCole Faust result[idx++] = (center_x - box_width / 2.f) / img_width;
94*c217d954SCole Faust result[idx++] = (center_y - box_height / 2.f) / img_height;
95*c217d954SCole Faust result[idx++] = (center_x + box_width / 2.f) / img_width;
96*c217d954SCole Faust result[idx++] = (center_y + box_height / 2.f) / img_height;
97*c217d954SCole Faust }
98*c217d954SCole Faust
99*c217d954SCole Faust // rest of priors
100*c217d954SCole Faust for(auto ar : info.aspect_ratios())
101*c217d954SCole Faust {
102*c217d954SCole Faust if(fabs(ar - 1.) < 1e-6)
103*c217d954SCole Faust {
104*c217d954SCole Faust continue;
105*c217d954SCole Faust }
106*c217d954SCole Faust
107*c217d954SCole Faust box_width = min_size * sqrt(ar);
108*c217d954SCole Faust box_height = min_size / sqrt(ar);
109*c217d954SCole Faust
110*c217d954SCole Faust // (xmin, ymin, xmax, ymax)
111*c217d954SCole Faust result[idx++] = (center_x - box_width / 2.f) / img_width;
112*c217d954SCole Faust result[idx++] = (center_y - box_height / 2.f) / img_height;
113*c217d954SCole Faust result[idx++] = (center_x + box_width / 2.f) / img_width;
114*c217d954SCole Faust result[idx++] = (center_y + box_height / 2.f) / img_height;
115*c217d954SCole Faust }
116*c217d954SCole Faust }
117*c217d954SCole Faust }
118*c217d954SCole Faust }
119*c217d954SCole Faust
120*c217d954SCole Faust // clip the coordinates
121*c217d954SCole Faust if(info.clip())
122*c217d954SCole Faust {
123*c217d954SCole Faust for(int i = 0; i < total_elements; ++i)
124*c217d954SCole Faust {
125*c217d954SCole Faust result[i] = std::min<T>(std::max<T>(result[i], 0.f), 1.f);
126*c217d954SCole Faust }
127*c217d954SCole Faust }
128*c217d954SCole Faust
129*c217d954SCole Faust // set the variance.
130*c217d954SCole Faust if(info.variances().size() == 1)
131*c217d954SCole Faust {
132*c217d954SCole Faust std::fill_n(result.data() + idx, total_elements, info.variances().at(0));
133*c217d954SCole Faust }
134*c217d954SCole Faust else
135*c217d954SCole Faust {
136*c217d954SCole Faust for(int h = 0; h < layer_height; ++h)
137*c217d954SCole Faust {
138*c217d954SCole Faust for(int w = 0; w < layer_width; ++w)
139*c217d954SCole Faust {
140*c217d954SCole Faust for(int i = 0; i < num_priors; ++i)
141*c217d954SCole Faust {
142*c217d954SCole Faust for(int j = 0; j < 4; ++j)
143*c217d954SCole Faust {
144*c217d954SCole Faust result[idx++] = info.variances().at(j);
145*c217d954SCole Faust }
146*c217d954SCole Faust }
147*c217d954SCole Faust }
148*c217d954SCole Faust }
149*c217d954SCole Faust }
150*c217d954SCole Faust
151*c217d954SCole Faust return result;
152*c217d954SCole Faust }
153*c217d954SCole Faust template SimpleTensor<float> prior_box_layer(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape);
154*c217d954SCole Faust
155*c217d954SCole Faust } // namespace reference
156*c217d954SCole Faust } // namespace validation
157*c217d954SCole Faust } // namespace test
158*c217d954SCole Faust } // namespace arm_compute
159