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 |
| 54 | const size_t filter_width = weights.shape().x(); |
| 55 | const size_t filter_height = weights.shape().y(); |
| 56 | const size_t filter_plane = filter_width * filter_height; |
| 57 | const size_t input_width = src.shape().x(); |
| 58 | const size_t input_height = src.shape().y(); |
| 59 | const size_t input_depth = src.shape().z(); |
| 60 | |
| 61 | const size_t filter_half_size = filter_width / 2; |
| 62 | const size_t pad_x = std::min(filter_half_size, static_cast<size_t>(conv_info.pad().first)); |
| 63 | const size_t pad_y = std::min(filter_half_size, static_cast<size_t>(conv_info.pad().second)); |
| 64 | const size_t minimum_x = -pad_x + filter_half_size; |
| 65 | const size_t minimum_y = -pad_y + filter_half_size; |
| 66 | |
| 67 | int out_pos = 0; |
| 68 | for(size_t z = 0; z < input_depth; ++z) |
| 69 | { |
| 70 | for(size_t y = minimum_y; y < input_height + pad_y - filter_half_size; y += conv_info.stride().second) |
| 71 | { |
| 72 | for(size_t x = minimum_x; x < input_width + pad_x - filter_half_size; x += conv_info.stride().first) |
| 73 | { |
| 74 | Coordinates coords(static_cast<int>(x), static_cast<int>(y), static_cast<int>(z)); |
| 75 | size_t filter_offset = filter_plane * z; |
| 76 | |
| 77 | T val = 0; |
| 78 | for(int j = y - filter_half_size; j <= static_cast<int>(y + filter_half_size); ++j) |
| 79 | { |
| 80 | for(int i = x - filter_half_size; i <= static_cast<int>(x + filter_half_size); ++i) |
| 81 | { |
| 82 | coords.set(0, i); |
| 83 | coords.set(1, j); |
| 84 | val += *(weights.data() + filter_offset) * tensor_elem_at(src, coords, BorderMode::CONSTANT, 0.f); |
| 85 | ++filter_offset; |
| 86 | } |
| 87 | } |
| 88 | coords.set(0, x); |
| 89 | coords.set(1, y); |
| 90 | dst[out_pos++] = saturate_cast<T>(val); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return dst; |
| 96 | } |
| 97 | |
| 98 | template SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const TensorShape &dst_shape, const PadStrideInfo &conv_info); |
| 99 | } // namespace reference |
| 100 | } // namespace validation |
| 101 | } // namespace test |
| 102 | } // namespace arm_compute |