blob: e29d014f778a2c323eafb9ba7c393f2a1452a83e [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
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 Gottardi1fab09f2017-07-13 15:55:57 +010027#include "Utils.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010028
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010029#include "tests/validation/Helpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010030
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace 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 */
47template <typename T>
Georgios Pinitas81a26ad2017-10-23 20:29:30 +010048SimpleTensor<T> depthwise_convolution(const SimpleTensor<T> &src, const SimpleTensor<T> &weights, const SimpleTensor<T> &biases, const TensorShape &dst_shape, const PadStrideInfo &conv_info)
Giorgio Arena93a690e2017-08-01 16:09:33 +010049{
50 // Create reference
51 SimpleTensor<T> dst{ dst_shape, src.data_type(), 1, src.fixed_point_position() };
52
53 // Compute reference
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010054 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 Arena93a690e2017-08-01 16:09:33 +010061
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010062 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 Arena93a690e2017-08-01 16:09:33 +010074
75 int out_pos = 0;
Giorgio Arena9fe41442017-08-23 16:36:24 +010076 for(int r = 0; r < num_batches; ++r)
Giorgio Arena93a690e2017-08-01 16:09:33 +010077 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010078 for(int z = 0; z < input_depth; ++z)
Giorgio Arena93a690e2017-08-01 16:09:33 +010079 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010080 for(int y = minimum_y; y < minimum_y + maximum_y; y += conv_info.stride().second)
Giorgio Arena93a690e2017-08-01 16:09:33 +010081 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +010082 for(int x = minimum_x; x < minimum_x + maximum_x; x += conv_info.stride().first)
Giorgio Arena93a690e2017-08-01 16:09:33 +010083 {
Giorgio Arena9fe41442017-08-23 16:36:24 +010084 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 Arena93a690e2017-08-01 16:09:33 +010089 {
Giorgio Arena9fe41442017-08-23 16:36:24 +010090 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 Arena93a690e2017-08-01 16:09:33 +010097 }
Giorgio Arena9fe41442017-08-23 16:36:24 +010098 coords.set(0, x);
99 coords.set(1, y);
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100100 dst[out_pos++] = saturate_cast<T>(val + *static_cast<const T *>(biases(Coordinates(z))));
Giorgio Arena93a690e2017-08-01 16:09:33 +0100101 }
Giorgio Arena93a690e2017-08-01 16:09:33 +0100102 }
103 }
104 }
105
106 return dst;
107}
108
Georgios Pinitas81a26ad2017-10-23 20:29:30 +0100109template SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const SimpleTensor<float> &biases, const TensorShape &dst_shape,
110 const PadStrideInfo &conv_info);
Giorgio Arena93a690e2017-08-01 16:09:33 +0100111} // namespace reference
112} // namespace validation
113} // namespace test
114} // namespace arm_compute