blob: ae54494c03546f664fb57209553aa8bf88d70c04 [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>
48SimpleTensor<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();
Giorgio Arena9fe41442017-08-23 16:36:24 +010060 const int num_batches = src.shape().total_size() / (input_width * input_height * input_depth);
Giorgio Arena93a690e2017-08-01 16:09:33 +010061
Giorgio Arena9fe41442017-08-23 16:36:24 +010062 const size_t filter_half_width = filter_width / 2;
63 const size_t filter_half_height = filter_height / 2;
64 const size_t pad_x = std::min(filter_half_width, static_cast<size_t>(conv_info.pad().first));
65 const size_t pad_y = std::min(filter_half_height, static_cast<size_t>(conv_info.pad().second));
66 const size_t minimum_x = -pad_x + filter_half_width;
67 const size_t minimum_y = -pad_y + filter_half_height;
Giorgio Arena93a690e2017-08-01 16:09:33 +010068
69 int out_pos = 0;
Giorgio Arena9fe41442017-08-23 16:36:24 +010070 for(int r = 0; r < num_batches; ++r)
Giorgio Arena93a690e2017-08-01 16:09:33 +010071 {
Giorgio Arena9fe41442017-08-23 16:36:24 +010072 for(size_t z = 0; z < input_depth; ++z)
Giorgio Arena93a690e2017-08-01 16:09:33 +010073 {
Giorgio Arena9fe41442017-08-23 16:36:24 +010074 for(size_t y = minimum_y; y < input_height - minimum_y; y += conv_info.stride().second)
Giorgio Arena93a690e2017-08-01 16:09:33 +010075 {
Giorgio Arena9fe41442017-08-23 16:36:24 +010076 for(size_t x = minimum_x; x < input_width - minimum_x; x += conv_info.stride().first)
Giorgio Arena93a690e2017-08-01 16:09:33 +010077 {
Giorgio Arena9fe41442017-08-23 16:36:24 +010078 Coordinates coords(static_cast<int>(x), static_cast<int>(y), static_cast<int>(z), static_cast<int>(r));
79 size_t filter_offset = filter_plane * z;
80
81 T val = 0;
82 for(int j = y - filter_half_height; j <= static_cast<int>(y + filter_half_height); ++j)
Giorgio Arena93a690e2017-08-01 16:09:33 +010083 {
Giorgio Arena9fe41442017-08-23 16:36:24 +010084 for(int i = x - filter_half_width; i <= static_cast<int>(x + filter_half_width); ++i)
85 {
86 coords.set(0, i);
87 coords.set(1, j);
88 val += *(weights.data() + filter_offset) * tensor_elem_at(src, coords, BorderMode::CONSTANT, 0.f);
89 ++filter_offset;
90 }
Giorgio Arena93a690e2017-08-01 16:09:33 +010091 }
Giorgio Arena9fe41442017-08-23 16:36:24 +010092 coords.set(0, x);
93 coords.set(1, y);
94 dst[out_pos++] = saturate_cast<T>(val);
Giorgio Arena93a690e2017-08-01 16:09:33 +010095 }
Giorgio Arena93a690e2017-08-01 16:09:33 +010096 }
97 }
98 }
99
100 return dst;
101}
102
103template SimpleTensor<float> depthwise_convolution(const SimpleTensor<float> &src, const SimpleTensor<float> &weights, const TensorShape &dst_shape, const PadStrideInfo &conv_info);
104} // namespace reference
105} // namespace validation
106} // namespace test
107} // namespace arm_compute