aboutsummaryrefslogtreecommitdiff
path: root/tests/validation/fixtures/BoundingBoxTransformFixture.h
blob: 7155848b4dfc402965a17e242bde7486985d1c48 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * Copyright (c) 2018-2019 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.
 */
#ifndef ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE
#define ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE

#include "arm_compute/core/TensorShape.h"
#include "arm_compute/core/Types.h"
#include "tests/AssetsLibrary.h"
#include "tests/Globals.h"
#include "tests/IAccessor.h"
#include "tests/framework/Asserts.h"
#include "tests/framework/Fixture.h"
#include "tests/validation/Helpers.h"
#include "tests/validation/reference/BoundingBoxTransform.h"

namespace arm_compute
{
namespace test
{
namespace validation
{
namespace
{
std::vector<float> generate_deltas(std::vector<float> &boxes, const TensorShape &image_shape, size_t num_boxes, size_t num_classes, std::mt19937 &gen)
{
    std::vector<float> deltas(num_boxes * 4 * num_classes);

    std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1);
    std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1);
    std::uniform_int_distribution<> dist_w(1, image_shape[0]);
    std::uniform_int_distribution<> dist_h(1, image_shape[1]);

    for(size_t i = 0; i < num_boxes; ++i)
    {
        const float ex_width  = boxes[4 * i + 2] - boxes[4 * i] + 1.f;
        const float ex_height = boxes[4 * i + 3] - boxes[4 * i + 1] + 1.f;
        const float ex_ctr_x  = boxes[4 * i] + 0.5f * ex_width;
        const float ex_ctr_y  = boxes[4 * i + 1] + 0.5f * ex_height;

        for(size_t j = 0; j < num_classes; ++j)
        {
            const float x1     = dist_x1(gen);
            const float y1     = dist_y1(gen);
            const float width  = dist_w(gen);
            const float height = dist_h(gen);
            const float ctr_x  = x1 + 0.5f * width;
            const float ctr_y  = y1 + 0.5f * height;

            deltas[4 * num_classes * i + 4 * j]     = (ctr_x - ex_ctr_x) / ex_width;
            deltas[4 * num_classes * i + 4 * j + 1] = (ctr_y - ex_ctr_y) / ex_height;
            deltas[4 * num_classes * i + 4 * j + 2] = log(width / ex_width);
            deltas[4 * num_classes * i + 4 * j + 3] = log(height / ex_height);
        }
    }
    return deltas;
}

std::vector<float> generate_boxes(const TensorShape &image_shape, size_t num_boxes, std::mt19937 &gen)
{
    std::vector<float> boxes(num_boxes * 4);

    std::uniform_int_distribution<> dist_x1(0, image_shape[0] - 1);
    std::uniform_int_distribution<> dist_y1(0, image_shape[1] - 1);
    std::uniform_int_distribution<> dist_w(1, image_shape[0]);
    std::uniform_int_distribution<> dist_h(1, image_shape[1]);

    for(size_t i = 0; i < num_boxes; ++i)
    {
        boxes[4 * i]     = dist_x1(gen);
        boxes[4 * i + 1] = dist_y1(gen);
        boxes[4 * i + 2] = boxes[4 * i] + dist_w(gen) - 1;
        boxes[4 * i + 3] = boxes[4 * i + 1] + dist_h(gen) - 1;
    }
    return boxes;
}
} // namespace

template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
class BoundingBoxTransformGenericFixture : public framework::Fixture
{
public:
    using TDeltas = typename std::conditional<std::is_same<typename std::decay<T>::type, uint16_t>::value, uint8_t, T>::type;

    template <typename...>
    void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo)
    {
        const bool is_qasymm16 = data_type == DataType::QASYMM16;
        _data_type_deltas      = (is_qasymm16) ? DataType::QASYMM8 : data_type;
        _boxes_qinfo           = (is_qasymm16) ? QuantizationInfo(.125f, 0) : QuantizationInfo();

        std::mt19937 gen_target(library->seed());
        _target = compute_target(deltas_shape, data_type, info, gen_target, deltas_qinfo);

        std::mt19937 gen_reference(library->seed());
        _reference = compute_reference(deltas_shape, data_type, info, gen_reference, deltas_qinfo);
    }

protected:
    template <typename data_type, typename U>
    void fill(U &&tensor, std::vector<float> values)
    {
        data_type *data_ptr = reinterpret_cast<data_type *>(tensor.data());
        switch(tensor.data_type())
        {
            case DataType::QASYMM8:
                for(size_t i = 0; i < values.size(); ++i)
                {
                    data_ptr[i] = quantize_qasymm8(values[i], tensor.quantization_info());
                }
                break;
            case DataType::QASYMM16:
                for(size_t i = 0; i < values.size(); ++i)
                {
                    data_ptr[i] = quantize_qasymm16(values[i], tensor.quantization_info());
                }
                break;
            default:
                for(size_t i = 0; i < values.size(); ++i)
                {
                    data_ptr[i] = static_cast<data_type>(values[i]);
                }
        }
    }

    TensorType compute_target(const TensorShape &deltas_shape, DataType data_type,
                              const BoundingBoxTransformInfo &bbox_info, std::mt19937 &gen,
                              QuantizationInfo deltas_qinfo)
    {
        // Create tensors
        TensorShape boxes_shape(4, deltas_shape[1]);
        TensorType  deltas = create_tensor<TensorType>(deltas_shape, _data_type_deltas, 1, deltas_qinfo);
        TensorType  boxes  = create_tensor<TensorType>(boxes_shape, data_type, 1, _boxes_qinfo);
        TensorType  pred_boxes;

        // Create and configure function
        FunctionType bbox_transform;
        bbox_transform.configure(&boxes, &pred_boxes, &deltas, bbox_info);

        ARM_COMPUTE_EXPECT(deltas.info()->is_resizable(), framework::LogLevel::ERRORS);
        ARM_COMPUTE_EXPECT(boxes.info()->is_resizable(), framework::LogLevel::ERRORS);
        ARM_COMPUTE_EXPECT(pred_boxes.info()->is_resizable(), framework::LogLevel::ERRORS);

        // Allocate tensors
        deltas.allocator()->allocate();
        boxes.allocator()->allocate();
        pred_boxes.allocator()->allocate();

        ARM_COMPUTE_EXPECT(!deltas.info()->is_resizable(), framework::LogLevel::ERRORS);
        ARM_COMPUTE_EXPECT(!boxes.info()->is_resizable(), framework::LogLevel::ERRORS);

        // Fill tensors
        TensorShape        img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
        std::vector<float> boxes_vec  = generate_boxes(img_shape, boxes_shape[1], gen);
        std::vector<float> deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
        fill<T>(AccessorType(boxes), boxes_vec);
        fill<TDeltas>(AccessorType(deltas), deltas_vec);

        // Compute function
        bbox_transform.run();

        return pred_boxes;
    }

    SimpleTensor<T> compute_reference(const TensorShape              &deltas_shape,
                                      DataType                        data_type,
                                      const BoundingBoxTransformInfo &bbox_info,
                                      std::mt19937                   &gen,
                                      QuantizationInfo                deltas_qinfo)
    {
        // Create reference tensor
        TensorShape           boxes_shape(4, deltas_shape[1]);
        SimpleTensor<T>       boxes{ boxes_shape, data_type, 1, _boxes_qinfo };
        SimpleTensor<TDeltas> deltas{ deltas_shape, _data_type_deltas, 1, deltas_qinfo };

        // Fill reference tensor
        TensorShape        img_shape(bbox_info.scale() * bbox_info.img_width(), bbox_info.scale() * bbox_info.img_height());
        std::vector<float> boxes_vec  = generate_boxes(img_shape, boxes_shape[1], gen);
        std::vector<float> deltas_vec = generate_deltas(boxes_vec, img_shape, deltas_shape[1], deltas_shape[0] / 4, gen);
        fill<T>(boxes, boxes_vec);
        fill<TDeltas>(deltas, deltas_vec);

        return reference::bounding_box_transform(boxes, deltas, bbox_info);
    }

    TensorType       _target{};
    SimpleTensor<T>  _reference{};
    DataType         _data_type_deltas{};
    QuantizationInfo _boxes_qinfo{};

private:
};

template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
class BoundingBoxTransformFixture : public BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>
{
public:
    template <typename...>
    void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type)
    {
        BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(deltas_shape, info, data_type, QuantizationInfo());
    }

private:
};

template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
class BoundingBoxTransformQuantizedFixture : public BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>
{
public:
    template <typename...>
    void setup(TensorShape deltas_shape, const BoundingBoxTransformInfo &info, DataType data_type, QuantizationInfo deltas_qinfo)
    {
        BoundingBoxTransformGenericFixture<TensorType, AccessorType, FunctionType, T>::setup(deltas_shape, info, data_type, deltas_qinfo);
    }
};
} // namespace validation
} // namespace test
} // namespace arm_compute
#endif /* ARM_COMPUTE_TEST_BOUNDINGBOXTRANSFORM_FIXTURE */