Abe Mbise | 1b99338 | 2017-12-19 13:51:59 +0000 | [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_CANNY_EDGE_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_CANNY_EDGE_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/framework/Asserts.h" |
| 32 | #include "tests/framework/Fixture.h" |
| 33 | #include "tests/validation/Helpers.h" |
| 34 | #include "tests/validation/reference/CannyEdgeDetector.h" |
| 35 | |
| 36 | namespace arm_compute |
| 37 | { |
| 38 | class CLCannyEdge; |
| 39 | class NECannyEdge; |
| 40 | |
| 41 | namespace test |
| 42 | { |
| 43 | namespace validation |
| 44 | { |
| 45 | template <typename TensorType, typename AccessorType, typename ArrayType, typename FunctionType, typename T> |
| 46 | class CannyEdgeValidationFixture : public framework::Fixture |
| 47 | { |
| 48 | public: |
| 49 | template <typename...> |
| 50 | void setup(std::string image, int gradient_size, MagnitudeType norm_type, BorderMode border_mode, bool use_fp16, Format format) |
| 51 | { |
| 52 | CannyEdgeParameters params = canny_edge_parameters(); |
| 53 | |
| 54 | _target = compute_target(image, gradient_size, norm_type, border_mode, use_fp16, format, params); |
| 55 | //TODO(COMPMID-543): Add use_fp16 to reference |
| 56 | _reference = compute_reference(image, gradient_size, norm_type, border_mode, format, params); |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | template <typename U> |
| 61 | void fill(U &&tensor, RawTensor raw) |
| 62 | { |
| 63 | library->fill(tensor, raw); |
| 64 | } |
| 65 | |
| 66 | template <typename F, typename std::enable_if<std::is_same<F, NECannyEdge>::value, int>::type = 0> |
| 67 | void configure_target(F &func, TensorType &src, TensorType &dst, int gradient_size, int norm_type, BorderMode border_mode, bool use_fp16, const CannyEdgeParameters ¶ms) |
| 68 | { |
| 69 | func.configure(&src, &dst, params.upper_thresh, params.lower_thresh, gradient_size, norm_type, border_mode, params.constant_border_value, use_fp16); |
| 70 | } |
| 71 | |
| 72 | template <typename F, typename std::enable_if<std::is_same<F, CLCannyEdge>::value, int>::type = 0> |
| 73 | void configure_target(F &func, TensorType &src, TensorType &dst, int gradient_size, int norm_type, BorderMode border_mode, bool use_fp16, const CannyEdgeParameters ¶ms) |
| 74 | { |
| 75 | ARM_COMPUTE_UNUSED(use_fp16); |
| 76 | ARM_COMPUTE_ERROR_ON(use_fp16); |
| 77 | func.configure(&src, &dst, params.upper_thresh, params.lower_thresh, gradient_size, norm_type, border_mode, params.constant_border_value); |
| 78 | } |
| 79 | |
| 80 | TensorType compute_target(const std::string &image, int gradient_size, MagnitudeType norm_type, BorderMode border_mode, bool use_fp16, Format format, const CannyEdgeParameters ¶ms) |
| 81 | { |
| 82 | // Load the image (cached by the library if loaded before) |
| 83 | const RawTensor &raw = library->get(image, format); |
| 84 | |
| 85 | // Create tensors |
| 86 | TensorType src = create_tensor<TensorType>(raw.shape(), format); |
| 87 | TensorType dst = create_tensor<TensorType>(raw.shape(), format); |
| 88 | src.info()->set_format(format); |
| 89 | dst.info()->set_format(format); |
| 90 | |
| 91 | // Create Canny edge configure function |
| 92 | FunctionType canny_edge; |
| 93 | configure_target<FunctionType>(canny_edge, src, dst, gradient_size, static_cast<int>(norm_type) + 1, border_mode, use_fp16, params); |
| 94 | |
| 95 | ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 96 | ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 97 | |
| 98 | // Allocate tensors |
| 99 | src.allocator()->allocate(); |
| 100 | dst.allocator()->allocate(); |
| 101 | |
| 102 | ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 103 | ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); |
| 104 | |
| 105 | // Fill tensors |
| 106 | fill(AccessorType(src), raw); |
| 107 | |
| 108 | // Compute function |
| 109 | canny_edge.run(); |
| 110 | |
| 111 | return dst; |
| 112 | } |
| 113 | |
| 114 | SimpleTensor<T> compute_reference(const std::string &image, int gradient_size, MagnitudeType norm_type, BorderMode border_mode, Format format, const CannyEdgeParameters ¶ms) |
| 115 | { |
| 116 | ARM_COMPUTE_ERROR_ON(format != Format::U8); |
| 117 | |
| 118 | // Load the image (cached by the library if loaded before) |
| 119 | const RawTensor &raw = library->get(image, format); |
| 120 | |
| 121 | // Create reference |
| 122 | SimpleTensor<T> src{ raw.shape(), format }; |
| 123 | |
| 124 | // Fill reference |
| 125 | fill(src, raw); |
| 126 | |
| 127 | return reference::canny_edge_detector<T>(src, params.upper_thresh, params.lower_thresh, gradient_size, norm_type, border_mode, params.constant_border_value); |
| 128 | } |
| 129 | |
| 130 | TensorType _target{}; |
| 131 | SimpleTensor<T> _reference{}; |
| 132 | }; |
| 133 | } // namespace validation |
| 134 | } // namespace test |
| 135 | } // namespace arm_compute |
| 136 | #endif /* ARM_COMPUTE_TEST_CANNY_EDGE_FIXTURE */ |