aboutsummaryrefslogtreecommitdiff
path: root/tests/validation/reference/PriorBoxLayer.cpp
blob: 4745df87bea89def617ce9efb87456f7e72e1009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (c) 2018 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "PriorBoxLayer.h"

#include "ActivationLayer.h"

#include "tests/validation/Helpers.h"

namespace arm_compute
{
namespace test
{
namespace validation
{
namespace reference
{
template <typename T>
SimpleTensor<T> prior_box_layer(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape)
{
    const auto layer_width  = static_cast<int>(src1.shape()[0]);
    const auto layer_height = static_cast<int>(src1.shape()[1]);

    int img_width  = info.img_size().x;
    int img_height = info.img_size().y;
    if(img_width == 0 || img_height == 0)
    {
        img_width  = static_cast<int>(src2.shape()[0]);
        img_height = static_cast<int>(src2.shape()[1]);
    }

    float step_x = info.steps()[0];
    float step_y = info.steps()[1];
    if(step_x == 0.f || step_y == 0.f)
    {
        step_x = static_cast<float>(img_width) / layer_width;
        step_x = static_cast<float>(img_height) / layer_height;
    }

    // Calculate number of aspect ratios
    const int num_priors     = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size();
    const int total_elements = layer_width * layer_height * num_priors * 4;

    SimpleTensor<T> result(output_shape, src1.data_type());

    int idx = 0;
    for(int y = 0; y < layer_height; ++y)
    {
        for(int x = 0; x < layer_width; ++x)
        {
            const float center_x = (x + info.offset()) * step_x;
            const float center_y = (y + info.offset()) * step_y;
            float       box_width;
            float       box_height;
            for(unsigned int i = 0; i < info.min_sizes().size(); ++i)
            {
                const float min_size = info.min_sizes().at(i);
                box_width            = min_size;
                box_height           = min_size;
                // (xmin, ymin, xmax, ymax)
                result[idx++] = (center_x - box_width / 2.f) / img_width;
                result[idx++] = (center_y - box_height / 2.f) / img_height;
                result[idx++] = (center_x + box_width / 2.f) / img_width;
                result[idx++] = (center_y + box_height / 2.f) / img_height;

                if(!info.max_sizes().empty())
                {
                    const float max_size = info.max_sizes().at(i);
                    box_width            = sqrt(min_size * max_size);
                    box_height           = box_width;

                    // (xmin, ymin, xmax, ymax)
                    result[idx++] = (center_x - box_width / 2.f) / img_width;
                    result[idx++] = (center_y - box_height / 2.f) / img_height;
                    result[idx++] = (center_x + box_width / 2.f) / img_width;
                    result[idx++] = (center_y + box_height / 2.f) / img_height;
                }

                // rest of priors
                for(auto ar : info.aspect_ratios())
                {
                    if(fabs(ar - 1.) < 1e-6)
                    {
                        continue;
                    }

                    box_width  = min_size * sqrt(ar);
                    box_height = min_size / sqrt(ar);

                    // (xmin, ymin, xmax, ymax)
                    result[idx++] = (center_x - box_width / 2.f) / img_width;
                    result[idx++] = (center_y - box_height / 2.f) / img_height;
                    result[idx++] = (center_x + box_width / 2.f) / img_width;
                    result[idx++] = (center_y + box_height / 2.f) / img_height;
                }
            }
        }
    }

    // clip the coordinates
    if(info.clip())
    {
        for(int i = 0; i < total_elements; ++i)
        {
            result[i] = std::min<T>(std::max<T>(result[i], 0.f), 1.f);
        }
    }

    // set the variance.
    if(info.variances().size() == 1)
    {
        std::fill_n(result.data() + idx, total_elements, info.variances().at(0));
    }
    else
    {
        for(int h = 0; h < layer_height; ++h)
        {
            for(int w = 0; w < layer_width; ++w)
            {
                for(int i = 0; i < num_priors; ++i)
                {
                    for(int j = 0; j < 4; ++j)
                    {
                        result[idx++] = info.variances().at(j);
                    }
                }
            }
        }
    }

    return result;
}
template SimpleTensor<float> prior_box_layer(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape);

} // namespace reference
} // namespace validation
} // namespace test
} // namespace arm_compute