blob: 7f2dabf5cde6a1702786f2c06c20f970f7060230 [file] [log] [blame]
George Wort05398a92019-01-25 15:38:33 +00001/*
2 * Copyright (c) 2019 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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_NEON_CROP_KERNEL_H
25#define ARM_COMPUTE_NEON_CROP_KERNEL_H
George Wort05398a92019-01-25 15:38:33 +000026
27#include "arm_compute/core/NEON/INEKernel.h"
28#include "arm_compute/core/Types.h"
29#include "arm_compute/core/Types.h"
30
31#include <cstdint>
32#include <map>
33
34namespace arm_compute
35{
36// Forward declarations
37class ITensor;
38
39/** Interface for the kernel to perform tensor cropping */
40class NECropKernel : public INEKernel
41{
42public:
43 const char *name() const override
44 {
45 return "NECropKernel";
46 }
47 /** Default constructor */
48 NECropKernel();
49 /** Prevent instances of this class from being copied (As this class contains pointers) */
50 NECropKernel(const NECropKernel &) = delete;
51 /** Prevent instances of this class from being copied (As this class contains pointers) */
52 NECropKernel &operator=(const NECropKernel &) = delete;
53 /** Allow instances of this class to be moved */
54 NECropKernel(NECropKernel &&) = default;
55 /** Allow instances of this class to be moved */
56 NECropKernel &operator=(NECropKernel &&) = default;
57 /** Default destructor */
58 ~NECropKernel() = default;
59 /** Configure kernel
60 *
61 * @note Supported tensor rank: up to 4
62 * @note Padding not supported.
63 *
64 * @param[in] input Source tensor. Data type supported: U16/S16/U32/S32/F16/F32. Data layouts supported: NHWC.
65 * @param[in] crop_boxes Tensor containing all possible boxes used to crop the image, each represented by 4 normalized values.
66 * Data type supported: F32
67 * @param[in] box_ind One dimensional tensor mapping the @p crop_box_ind to the index of the 3D image in @p input.
68 * Data type supported: F32
69 * @param[out] output Destination tensor. Data type supported: F32
70 * @param[in] crop_box_ind Index of the crop box to be used from @p crop_boxes. Default is 0.
71 * @param[in] extrapolation_value Value to be used for values outside of the image. Default is 0.
72 */
73 void configure(const ITensor *input, const ITensor *crop_boxes, const ITensor *box_ind, ITensor *output, uint32_t crop_box_ind = 0, float extrapolation_value = 0);
74
75 /** Static function to check if given info will lead to a valid configuration of @ref CLStridedSliceKernel
76 *
77 * @note Supported tensor rank: up to 4
78 * @note Padding not supported.
79 *
80 * @param[in] input Source tensor info. Data type supported: U16/S16/U32/S32/F16/F32. Data layouts supported: NHWC.
81 * @param[in] crop_boxes Tensor info for tensor containing all possible boxes used to crop the image. Data type supported: F32
82 * @param[in] box_ind Tensor info for the one dimensional tensor mapping the @p crop_box_ind to the index of the 3D image
83 * in @p input. Data type supported: F32
84 * @param[in] output Destination tensor. Data type supported: F32
85 * @param[in] crop_box_ind Index of the crop box to be used from @p crop_boxes. Default is 0.
86 * @param[in] extrapolation_value Value to be used for values outside of the image. Default is 0.
87 */
88 static Status validate(const ITensorInfo *input, const ITensorInfo *crop_boxes, const ITensorInfo *box_ind, const ITensorInfo *output, uint32_t crop_box_ind = 0, float extrapolation_value = 0);
89
90 /** Configure output tensor's shape as this can only be determined at runtime. */
91 void configure_output_shape();
92
93 // Inherited methods overridden:
94 void run(const Window &window, const ThreadInfo &info) override;
95
96 /** Function to use for in bounds crop for the particular tensor types passed to configure() */
97 using InBoundsCropFunction = void(const ITensor *, const ITensor *, float *, Coordinates, int32_t, int32_t, int32_t);
98
99private:
100 const ITensor *_input;
101 const ITensor *_crop_boxes;
102 const ITensor *_box_ind;
103 ITensor *_output;
104
105 Coordinates _start;
106 Coordinates _end;
107 uint32_t _crop_box_ind;
108 float _extrapolation_value;
109 /** The number of rows out of bounds at the start and end of output. */
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100110 std::array<uint32_t, 2> _rows_out_of_bounds;
George Wort05398a92019-01-25 15:38:33 +0000111 /** The number of columns out of bounds at the start and end of output. */
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100112 std::array<uint32_t, 2> _cols_out_of_bounds;
George Wort05398a92019-01-25 15:38:33 +0000113
114 std::pair<NECropKernel::InBoundsCropFunction *, NECropKernel::InBoundsCropFunction *> _in_bounds_crop_functions;
115 NECropKernel::InBoundsCropFunction *_in_bounds_crop_function;
116
Michalis Spyroubcfd09a2019-05-01 13:03:59 +0100117 using CropFunction = void(const ITensor *, const ITensor *, Coordinates, float, const std::array<uint32_t, 2> &, const std::array<uint32_t, 2> &,
George Wort05398a92019-01-25 15:38:33 +0000118 NECropKernel::InBoundsCropFunction *);
119
120 NECropKernel::CropFunction *_crop_function;
121};
122} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000123#endif /*ARM_COMPUTE_NEON_CROP_KERNEL_H */