|
| 1 | +// Copyright (C) 2018-2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "shared_test_classes/single_op/roi_align_rotated.hpp" |
| 6 | + |
| 7 | +#include <random> |
| 8 | + |
| 9 | +#include "openvino/core/enum_names.hpp" |
| 10 | + |
| 11 | +namespace ov { |
| 12 | +namespace test { |
| 13 | + |
| 14 | +static constexpr int ROI_DEF_SIZE = 5; |
| 15 | +static constexpr int SEED = 7877; |
| 16 | +static constexpr float PI = 3.14159265358979323846f; |
| 17 | + |
| 18 | +struct TestParams { |
| 19 | + std::vector<InputShape> input_shapes; |
| 20 | + int num_rois; |
| 21 | + int pooled_h; |
| 22 | + int pooled_w; |
| 23 | + int sampliong_ratio; |
| 24 | + float spatial_scale; |
| 25 | + bool clockwise_mode; |
| 26 | + ov::element::Type model_type; |
| 27 | + std::string target_device; |
| 28 | +}; |
| 29 | + |
| 30 | +static TestParams ExtractTestParams(const roialignrotatedParams& param) { |
| 31 | + TestParams tp; |
| 32 | + std::tie(tp.input_shapes, |
| 33 | + tp.num_rois, |
| 34 | + tp.pooled_h, |
| 35 | + tp.pooled_w, |
| 36 | + tp.sampliong_ratio, |
| 37 | + tp.spatial_scale, |
| 38 | + tp.clockwise_mode, |
| 39 | + tp.model_type, |
| 40 | + tp.target_device) = param; |
| 41 | + return tp; |
| 42 | +} |
| 43 | + |
| 44 | +static float RandomFloat(float low, float high) { |
| 45 | + static std::default_random_engine engine(SEED); |
| 46 | + std::uniform_real_distribution<float> dis(low, high); |
| 47 | + return dis(engine); |
| 48 | +} |
| 49 | + |
| 50 | +static std::vector<float> FillRoisTensor(int num_rois, int height, int width) { |
| 51 | + std::vector<float> rois; |
| 52 | + rois.resize(num_rois * ROI_DEF_SIZE); |
| 53 | + |
| 54 | + for (int i = 0; i < rois.size() / ROI_DEF_SIZE; i++) { |
| 55 | + // center_x, center_y, width, height, angle |
| 56 | + rois[i * ROI_DEF_SIZE + 0] = RandomFloat(0.0f, width); |
| 57 | + rois[i * ROI_DEF_SIZE + 1] = RandomFloat(0.0f, height); |
| 58 | + rois[i * ROI_DEF_SIZE + 2] = RandomFloat(0.0f, width); |
| 59 | + rois[i * ROI_DEF_SIZE + 3] = RandomFloat(0.0f, height); |
| 60 | + rois[i * ROI_DEF_SIZE + 4] = RandomFloat(0.0f, 2 * PI); |
| 61 | + } |
| 62 | + |
| 63 | + return rois; |
| 64 | +} |
| 65 | + |
| 66 | +static std::vector<int> FillBAtchIdxTensor(int num_rois, int batch_size) { |
| 67 | + std::vector<int> idx; |
| 68 | + idx.resize(num_rois); |
| 69 | + int batch_id = 0; |
| 70 | + for (int i = 0; i < idx.size(); i++) { |
| 71 | + idx[i] = batch_id; |
| 72 | + batch_id = (batch_id + 1) % batch_size; |
| 73 | + } |
| 74 | + |
| 75 | + return idx; |
| 76 | +} |
| 77 | + |
| 78 | +std::string ROIAlignRotatedLayerTest::getTestCaseName(const testing::TestParamInfo<roialignrotatedParams>& obj) { |
| 79 | + const TestParams tp = ExtractTestParams(obj.param); |
| 80 | + |
| 81 | + std::ostringstream result; |
| 82 | + result << "IS=("; |
| 83 | + for (size_t i = 0lu; i < tp.input_shapes.size(); i++) { |
| 84 | + result << ov::test::utils::partialShape2str({tp.input_shapes[i].first}) |
| 85 | + << (i < tp.input_shapes.size() - 1lu ? "_" : ""); |
| 86 | + } |
| 87 | + result << ")_TS="; |
| 88 | + for (size_t i = 0lu; i < tp.input_shapes.front().second.size(); i++) { |
| 89 | + result << "{"; |
| 90 | + for (size_t j = 0lu; j < tp.input_shapes.size(); j++) { |
| 91 | + result << ov::test::utils::vec2str(tp.input_shapes[j].second[i]) |
| 92 | + << (j < tp.input_shapes.size() - 1lu ? "_" : ""); |
| 93 | + } |
| 94 | + result << "}_"; |
| 95 | + } |
| 96 | + result << "numRois=" << tp.num_rois << "_"; |
| 97 | + result << "pooledH=" << tp.pooled_h << "_"; |
| 98 | + result << "pooledW=" << tp.pooled_w << "_"; |
| 99 | + result << "samplingRatio=" << tp.sampliong_ratio << "_"; |
| 100 | + result << "spatialScale=" << tp.spatial_scale << "_"; |
| 101 | + result << "clockwiseMode=" << tp.clockwise_mode << "_"; |
| 102 | + result << "modelType=" << tp.model_type.to_string() << "_"; |
| 103 | + result << "trgDev=" << tp.target_device; |
| 104 | + return result.str(); |
| 105 | +} |
| 106 | + |
| 107 | +void ROIAlignRotatedLayerTest::SetUp() { |
| 108 | + const TestParams tp = ExtractTestParams(this->GetParam()); |
| 109 | + targetDevice = tp.target_device; |
| 110 | + init_input_shapes(tp.input_shapes); |
| 111 | + |
| 112 | + const auto input_batch_size = inputDynamicShapes[0][0].get_length(); |
| 113 | + const auto input_height = inputDynamicShapes[0][2].get_length(); |
| 114 | + const auto input_width = inputDynamicShapes[0][3].get_length(); |
| 115 | + |
| 116 | + auto input = std::make_shared<ov::op::v0::Parameter>(tp.model_type, inputDynamicShapes[0]); |
| 117 | + const auto rois_shape = ov::Shape{static_cast<size_t>(tp.num_rois), ROI_DEF_SIZE}; |
| 118 | + const auto rois_idx_shape = ov::Shape{static_cast<size_t>(tp.num_rois)}; |
| 119 | + |
| 120 | + auto rois = std::make_shared<ov::op::v0::Constant>(tp.model_type, |
| 121 | + rois_shape, |
| 122 | + FillRoisTensor(tp.num_rois, input_height, input_width).data()); |
| 123 | + auto rois_idx = std::make_shared<ov::op::v0::Constant>(ov::element::i32, |
| 124 | + rois_idx_shape, |
| 125 | + FillBAtchIdxTensor(tp.num_rois, input_batch_size).data()); |
| 126 | + auto roi_align = std::make_shared<ov::op::v15::ROIAlignRotated>(input, |
| 127 | + rois, |
| 128 | + rois_idx, |
| 129 | + tp.pooled_h, |
| 130 | + tp.pooled_w, |
| 131 | + tp.sampliong_ratio, |
| 132 | + tp.spatial_scale, |
| 133 | + tp.clockwise_mode); |
| 134 | + function = std::make_shared<ov::Model>(roi_align->outputs(), ov::ParameterVector{input}, "roi_align_rotated"); |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace test |
| 138 | +} // namespace ov |
0 commit comments