Compute Library
 21.08
PriorBoxLayer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 #include "PriorBoxLayer.h"
25 
26 #include "ActivationLayer.h"
27 
29 
30 namespace arm_compute
31 {
32 namespace test
33 {
34 namespace validation
35 {
36 namespace reference
37 {
38 template <typename T>
40 {
41  const auto layer_width = static_cast<int>(src1.shape()[0]);
42  const auto layer_height = static_cast<int>(src1.shape()[1]);
43 
44  int img_width = info.img_size().x;
45  int img_height = info.img_size().y;
46  if(img_width == 0 || img_height == 0)
47  {
48  img_width = static_cast<int>(src2.shape()[0]);
49  img_height = static_cast<int>(src2.shape()[1]);
50  }
51 
52  float step_x = info.steps()[0];
53  float step_y = info.steps()[1];
54  if(step_x == 0.f || step_y == 0.f)
55  {
56  step_x = static_cast<float>(img_width) / layer_width;
57  step_x = static_cast<float>(img_height) / layer_height;
58  }
59 
60  // Calculate number of aspect ratios
61  const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
62  const int total_elements = layer_width * layer_height * num_priors * 4;
63 
64  SimpleTensor<T> result(output_shape, src1.data_type());
65 
66  int idx = 0;
67  for(int y = 0; y < layer_height; ++y)
68  {
69  for(int x = 0; x < layer_width; ++x)
70  {
71  const float center_x = (x + info.offset()) * step_x;
72  const float center_y = (y + info.offset()) * step_y;
73  float box_width;
74  float box_height;
75  for(unsigned int i = 0; i < info.min_sizes().size(); ++i)
76  {
77  const float min_size = info.min_sizes().at(i);
78  box_width = min_size;
79  box_height = min_size;
80  // (xmin, ymin, xmax, ymax)
81  result[idx++] = (center_x - box_width / 2.f) / img_width;
82  result[idx++] = (center_y - box_height / 2.f) / img_height;
83  result[idx++] = (center_x + box_width / 2.f) / img_width;
84  result[idx++] = (center_y + box_height / 2.f) / img_height;
85 
86  if(!info.max_sizes().empty())
87  {
88  const float max_size = info.max_sizes().at(i);
89  box_width = sqrt(min_size * max_size);
90  box_height = box_width;
91 
92  // (xmin, ymin, xmax, ymax)
93  result[idx++] = (center_x - box_width / 2.f) / img_width;
94  result[idx++] = (center_y - box_height / 2.f) / img_height;
95  result[idx++] = (center_x + box_width / 2.f) / img_width;
96  result[idx++] = (center_y + box_height / 2.f) / img_height;
97  }
98 
99  // rest of priors
100  for(auto ar : info.aspect_ratios())
101  {
102  if(fabs(ar - 1.) < 1e-6)
103  {
104  continue;
105  }
106 
107  box_width = min_size * sqrt(ar);
108  box_height = min_size / sqrt(ar);
109 
110  // (xmin, ymin, xmax, ymax)
111  result[idx++] = (center_x - box_width / 2.f) / img_width;
112  result[idx++] = (center_y - box_height / 2.f) / img_height;
113  result[idx++] = (center_x + box_width / 2.f) / img_width;
114  result[idx++] = (center_y + box_height / 2.f) / img_height;
115  }
116  }
117  }
118  }
119 
120  // clip the coordinates
121  if(info.clip())
122  {
123  for(int i = 0; i < total_elements; ++i)
124  {
125  result[i] = std::min<T>(std::max<T>(result[i], 0.f), 1.f);
126  }
127  }
128 
129  // set the variance.
130  if(info.variances().size() == 1)
131  {
132  std::fill_n(result.data() + idx, total_elements, info.variances().at(0));
133  }
134  else
135  {
136  for(int h = 0; h < layer_height; ++h)
137  {
138  for(int w = 0; w < layer_width; ++w)
139  {
140  for(int i = 0; i < num_priors; ++i)
141  {
142  for(int j = 0; j < 4; ++j)
143  {
144  result[idx++] = info.variances().at(j);
145  }
146  }
147  }
148  }
149  }
150 
151  return result;
152 }
154 
155 } // namespace reference
156 } // namespace validation
157 } // namespace test
158 } // namespace arm_compute
SimpleTensor< float > w
Definition: DFT.cpp:156
Shape of a tensor.
Definition: TensorShape.h:39
std::vector< float > aspect_ratios() const
Get aspect ratios.
Definition: Types.h:869
DataType data_type() const override
Data type of the tensor.
Definition: SimpleTensor.h:357
TensorShape shape() const override
Shape of the tensor.
Definition: SimpleTensor.h:320
Copyright (c) 2017-2021 Arm Limited.
int32_t x
X coordinates.
Definition: Types.h:415
int32_t y
Y coordinates.
Definition: Types.h:416
std::array< float, 2 > steps() const
Get the step coordinates.
Definition: Types.h:839
SimpleTensor< T > prior_box_layer(const SimpleTensor< T > &src1, const SimpleTensor< T > &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape)
PriorBox layer info.
Definition: Types.h:764
float offset() const
Get the offset.
Definition: Types.h:849
Coordinates2D img_size() const
Get the image size coordinates.
Definition: Types.h:844
Simple tensor object that stores elements in a consecutive chunk of memory.
Definition: SimpleTensor.h:58
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
std::vector< float > max_sizes() const
Get max sizes.
Definition: Types.h:864
std::vector< float > variances() const
Get min variances.
Definition: Types.h:834
bool clip() const
Get the clip value.
Definition: Types.h:859
std::vector< float > min_sizes() const
Get min sizes.
Definition: Types.h:829