Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 "validation/Helpers.h" |
| 25 | |
| 26 | using namespace arm_compute::test; |
| 27 | |
| 28 | namespace arm_compute |
| 29 | { |
| 30 | namespace test |
| 31 | { |
| 32 | namespace validation |
| 33 | { |
| 34 | std::vector<ROI> generate_random_rois(const TensorShape &shape, const ROIPoolingLayerInfo &pool_info, unsigned int num_rois, std::random_device::result_type seed) |
| 35 | { |
| 36 | ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() < 4) || (pool_info.pooled_height() < 4)); |
| 37 | |
| 38 | std::vector<ROI> rois; |
| 39 | std::mt19937 gen(seed); |
| 40 | const int pool_width = pool_info.pooled_width(); |
| 41 | const int pool_height = pool_info.pooled_height(); |
| 42 | const float roi_scale = pool_info.spatial_scale(); |
| 43 | |
| 44 | // Calculate distribution bounds |
| 45 | const auto scaled_width = static_cast<int>((shape.x() / roi_scale) / pool_width); |
| 46 | const auto scaled_height = static_cast<int>((shape.y() / roi_scale) / pool_height); |
| 47 | const auto min_width = static_cast<int>(pool_width / roi_scale); |
| 48 | const auto min_height = static_cast<int>(pool_height / roi_scale); |
| 49 | |
| 50 | // Create distributions |
| 51 | std::uniform_int_distribution<int> dist_batch(0, shape[3] - 1); |
| 52 | std::uniform_int_distribution<int> dist_x(0, scaled_width); |
| 53 | std::uniform_int_distribution<int> dist_y(0, scaled_height); |
| 54 | std::uniform_int_distribution<int> dist_w(min_width, std::max(min_width, (pool_width - 2) * scaled_width)); |
| 55 | std::uniform_int_distribution<int> dist_h(min_height, std::max(min_height, (pool_height - 2) * scaled_height)); |
| 56 | |
| 57 | for(unsigned int r = 0; r < num_rois; ++r) |
| 58 | { |
| 59 | ROI roi{}; |
| 60 | roi.batch_idx = dist_batch(gen); |
| 61 | roi.rect.x = dist_x(gen); |
| 62 | roi.rect.y = dist_y(gen); |
| 63 | roi.rect.width = dist_w(gen); |
| 64 | roi.rect.height = dist_h(gen); |
| 65 | rois.push_back(roi); |
| 66 | } |
| 67 | |
| 68 | return rois; |
| 69 | } |
| 70 | } // namespace validation |
| 71 | } // namespace test |
| 72 | } // namespace arm_compute |