Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +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 "DepthwiseConvolution.h" |
| 25 | |
| 26 | #include "ConvolutionLayer.h" |
Isabella Gottardi | 1fab09f | 2017-07-13 15:55:57 +0100 | [diff] [blame] | 27 | #include "Utils.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 28 | |
Moritz Pflanzer | a09de0c | 2017-09-01 20:41:12 +0100 | [diff] [blame] | 29 | #include "tests/validation/Helpers.h" |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 30 | |
| 31 | namespace arm_compute |
| 32 | { |
| 33 | namespace test |
| 34 | { |
| 35 | namespace validation |
| 36 | { |
| 37 | namespace reference |
| 38 | { |
| 39 | /** Perform a depthwise convolution |
| 40 | * |
| 41 | * - Three dimensions tensors |
| 42 | * - Third dimention is number of channels |
| 43 | * - Depths of input tensor and filter are equals |
| 44 | * - Padding, stride and output shape "match" |
| 45 | * |
| 46 | */ |
| 47 | template <typename T> |
| 48 | SimpleTensor<T> depthwise_convolution(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const TensorShape &dst_shape, const PadStrideInfo &conv_info) |
| 49 | { |
| 50 | // Create reference |
| 51 | SimpleTensor<T> dst{ dst_shape, src.data_type(), 1, src.fixed_point_position() }; |
| 52 | |
| 53 | // Compute reference |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 54 | const int filter_width = weights.shape().x(); |
| 55 | const int filter_height = weights.shape().y(); |
| 56 | const int filter_plane = filter_width * filter_height; |
| 57 | const int input_width = src.shape().x(); |
| 58 | const int input_height = src.shape().y(); |
| 59 | const int input_depth = src.shape().z(); |
| 60 | const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 61 | |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 62 | const int filter_half_width = filter_width / 2; |
| 63 | const int filter_half_height = filter_height / 2; |
| 64 | |
| 65 | const int pad_left = std::min(static_cast<int>(conv_info.pad_left()), filter_half_width); |
| 66 | const int pad_top = std::min(static_cast<int>(conv_info.pad_top()), filter_half_height); |
| 67 | const int pad_right = std::min(static_cast<int>(conv_info.pad_right()), filter_half_width); |
| 68 | const int pad_bottom = std::min(static_cast<int>(conv_info.pad_bottom()), filter_half_height); |
| 69 | |
| 70 | const int minimum_x = -pad_left + filter_half_width; |
| 71 | const int minimum_y = -pad_top + filter_half_height; |
| 72 | const int maximum_x = input_width + pad_left - filter_half_width + pad_right - filter_half_width; |
| 73 | const int maximum_y = input_height + pad_top - filter_half_height + pad_bottom - filter_half_height; |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 74 | |
| 75 | int out_pos = 0; |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 76 | for(int r = 0; r < num_batches; ++r) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 77 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 78 | for(int z = 0; z < input_depth; ++z) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 79 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 80 | for(int y = minimum_y; y < minimum_y + maximum_y; y += conv_info.stride().second) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 81 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 82 | for(int x = minimum_x; x < minimum_x + maximum_x; x += conv_info.stride().first) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 83 | { |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 84 | Coordinates coords(static_cast<int>(x), static_cast<int>(y), static_cast<int>(z), static_cast<int>(r)); |
| 85 | size_t filter_offset = filter_plane * z; |
| 86 | |
| 87 | T val = 0; |
| 88 | for(int j = y - filter_half_height; j <= static_cast<int>(y + filter_half_height); ++j) |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 89 | { |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 90 | for(int i = x - filter_half_width; i <= static_cast<int>(x + filter_half_width); ++i) |
| 91 | { |
| 92 | coords.set(0, i); |
| 93 | coords.set(1, j); |
| 94 | val += *(weights.data() + filter_offset) * tensor_elem_at(src, coords, BorderMode::CONSTANT, 0.f); |
| 95 | ++filter_offset; |
| 96 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 97 | } |
Giorgio Arena | 9fe4144 | 2017-08-23 16:36:24 +0100 | [diff] [blame] | 98 | coords.set(0, x); |
| 99 | coords.set(1, y); |
| 100 | dst[out_pos++] = saturate_cast<T>(val); |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 101 | } |
Giorgio Arena | 93a690e | 2017-08-01 16:09:33 +0100 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | return dst; |
| 107 | } |
| 108 | |
| 109 | template SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const TensorShape &dst_shape, const PadStrideInfo &conv_info); |
| 110 | } // namespace reference |
| 111 | } // namespace validation |
| 112 | } // namespace test |
| 113 | } // namespace arm_compute |