Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018 Arm Limited. |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 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 | |
| 28 | #include "tests/validation/Helpers.h" |
| 29 | |
| 30 | namespace arm_compute |
| 31 | { |
| 32 | namespace test |
| 33 | { |
| 34 | namespace validation |
| 35 | { |
| 36 | namespace reference |
| 37 | { |
| 38 | template <typename T> |
| 39 | SimpleTensor<T> prior_box_layer(const SimpleTensor<T> &src1, const SimpleTensor<T> &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape) |
| 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 | } |
| 153 | template SimpleTensor<float> prior_box_layer(const SimpleTensor<float> &src1, const SimpleTensor<float> &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape); |
| 154 | |
| 155 | } // namespace reference |
| 156 | } // namespace validation |
| 157 | } // namespace test |
| 158 | } // namespace arm_compute |