Ioan-Cristian Szabo | 9414f64 | 2017-10-27 17:35:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2018 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 | #ifndef ARM_COMPUTE_TEST_CHANNEL_EXTRACT_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_CHANNEL_EXTRACT_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/AssetsLibrary.h" |
| 30 | #include "tests/Globals.h" |
| 31 | #include "tests/IAccessor.h" |
| 32 | #include "tests/framework/Asserts.h" |
| 33 | #include "tests/framework/Fixture.h" |
| 34 | #include "tests/validation/Helpers.h" |
| 35 | #include "tests/validation/reference/ChannelExtract.h" |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace test |
| 40 | { |
| 41 | namespace validation |
| 42 | { |
| 43 | template <typename MultiImageType, typename TensorType, typename AccessorType, typename FunctionType, typename T> |
| 44 | class ChannelExtractValidationFixture : public framework::Fixture |
| 45 | { |
| 46 | public: |
| 47 | template <typename...> |
| 48 | void setup(TensorShape shape, Format format, Channel channel) |
| 49 | { |
| 50 | shape = adjust_odd_shape(shape, format); |
| 51 | |
| 52 | _target = compute_target(shape, format, channel); |
| 53 | _reference = compute_reference(shape, format, channel); |
| 54 | } |
| 55 | |
| 56 | protected: |
| 57 | template <typename U> |
| 58 | void fill(U &&tensor, int i) |
| 59 | { |
| 60 | library->fill_tensor_uniform(tensor, i); |
| 61 | } |
| 62 | |
| 63 | std::vector<SimpleTensor<T>> create_tensor_planes_reference(const TensorShape &shape, Format format) |
| 64 | { |
| 65 | TensorShape input = adjust_odd_shape(shape, format); |
| 66 | |
| 67 | std::vector<SimpleTensor<T>> tensor_planes; |
| 68 | |
| 69 | switch(format) |
| 70 | { |
| 71 | case Format::RGB888: |
| 72 | case Format::RGBA8888: |
| 73 | case Format::YUYV422: |
| 74 | case Format::UYVY422: |
| 75 | { |
| 76 | tensor_planes.emplace_back(input, format); |
| 77 | break; |
| 78 | } |
| 79 | case Format::NV12: |
| 80 | case Format::NV21: |
| 81 | { |
| 82 | const TensorShape shape_uv88 = calculate_subsampled_shape(shape, Format::UV88); |
| 83 | |
| 84 | tensor_planes.emplace_back(input, Format::U8); |
| 85 | tensor_planes.emplace_back(shape_uv88, Format::UV88); |
| 86 | break; |
| 87 | } |
| 88 | case Format::IYUV: |
| 89 | { |
| 90 | const TensorShape shape_sub2 = calculate_subsampled_shape(shape, Format::IYUV); |
| 91 | |
| 92 | tensor_planes.emplace_back(input, Format::U8); |
| 93 | tensor_planes.emplace_back(shape_sub2, Format::U8); |
| 94 | tensor_planes.emplace_back(shape_sub2, Format::U8); |
| 95 | break; |
| 96 | } |
| 97 | case Format::YUV444: |
| 98 | tensor_planes.emplace_back(input, Format::U8); |
| 99 | tensor_planes.emplace_back(input, Format::U8); |
| 100 | tensor_planes.emplace_back(input, Format::U8); |
| 101 | break; |
| 102 | default: |
| 103 | ARM_COMPUTE_ERROR("Not supported"); |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | return tensor_planes; |
| 108 | } |
| 109 | |
| 110 | TensorType compute_target(const TensorShape &shape, Format format, Channel channel) |
| 111 | { |
| 112 | const unsigned int num_planes = num_planes_from_format(format); |
| 113 | |
| 114 | TensorShape dst_shape = calculate_subsampled_shape(shape, format, channel); |
| 115 | |
| 116 | // Create tensors |
| 117 | MultiImageType ref_src = create_multi_image<MultiImageType>(shape, format); |
| 118 | TensorType dst = create_tensor<TensorType>(dst_shape, Format::U8); |
| 119 | |
| 120 | // Create and configure function |
| 121 | FunctionType channel_extract; |
| 122 | |
| 123 | if(1U == num_planes) |
| 124 | { |
| 125 | const TensorType *plane_src = static_cast<TensorType *>(ref_src.plane(0)); |
| 126 | |
| 127 | channel_extract.configure(plane_src, channel, &dst); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | channel_extract.configure(&ref_src, channel, &dst); |
| 132 | } |
| 133 | |
| 134 | for(unsigned int plane_idx = 0; plane_idx < num_planes; ++plane_idx) |
| 135 | { |
| 136 | const TensorType *src_plane = static_cast<const TensorType *>(ref_src.plane(plane_idx)); |
| 137 | |
| 138 | ARM_COMPUTE_EXPECT(src_plane->info()->is_resizable(), framework::LogLevel::ERRORS); |
| 139 | } |
| 140 | |
| 141 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 142 | |
| 143 | // Allocate tensors |
| 144 | ref_src.allocate(); |
| 145 | dst.allocator()->allocate(); |
| 146 | |
| 147 | for(unsigned int plane_idx = 0; plane_idx < num_planes; ++plane_idx) |
| 148 | { |
| 149 | const TensorType *src_plane = static_cast<const TensorType *>(ref_src.plane(plane_idx)); |
| 150 | |
| 151 | ARM_COMPUTE_EXPECT(!src_plane->info()->is_resizable(), framework::LogLevel::ERRORS); |
| 152 | } |
| 153 | |
| 154 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 155 | |
| 156 | // Fill tensor planes |
| 157 | for(unsigned int plane_idx = 0; plane_idx < num_planes; ++plane_idx) |
| 158 | { |
| 159 | TensorType *src_plane = static_cast<TensorType *>(ref_src.plane(plane_idx)); |
| 160 | |
| 161 | fill(AccessorType(*src_plane), plane_idx); |
| 162 | } |
| 163 | |
| 164 | // Compute function |
| 165 | channel_extract.run(); |
| 166 | |
| 167 | return dst; |
| 168 | } |
| 169 | |
| 170 | SimpleTensor<T> compute_reference(const TensorShape &shape, Format format, Channel channel) |
| 171 | { |
| 172 | const unsigned int num_planes = num_planes_from_format(format); |
| 173 | |
| 174 | // Create reference |
| 175 | std::vector<SimpleTensor<T>> ref_src = create_tensor_planes_reference(shape, format); |
| 176 | |
| 177 | // Fill references |
| 178 | for(unsigned int plane_idx = 0; plane_idx < num_planes; ++plane_idx) |
| 179 | { |
| 180 | fill(ref_src[plane_idx], plane_idx); |
| 181 | } |
| 182 | |
| 183 | return reference::channel_extract<T>(shape, ref_src, format, channel); |
| 184 | } |
| 185 | |
| 186 | TensorType _target{}; |
| 187 | SimpleTensor<T> _reference{}; |
| 188 | }; |
| 189 | } // namespace validation |
| 190 | } // namespace test |
| 191 | } // namespace arm_compute |
| 192 | #endif /* ARM_COMPUTE_TEST_CHANNEL_EXTRACT_FIXTURE */ |