1 | /* |
---|
2 | Copyright (C) 2018 Paul Brossier <piem@aubio.org> |
---|
3 | |
---|
4 | This file is part of aubio. |
---|
5 | |
---|
6 | aubio is free software: you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation, either version 3 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | aubio is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | #include "aubio_priv.h" |
---|
24 | #include "fmat.h" |
---|
25 | #include "tensor.h" |
---|
26 | #include "maxpool2d.h" |
---|
27 | |
---|
28 | struct _aubio_maxpool2d_t { |
---|
29 | uint_t pool_size[2]; |
---|
30 | uint_t stride[2]; |
---|
31 | }; |
---|
32 | |
---|
33 | static void aubio_maxpool2d_debug(aubio_maxpool2d_t *c, |
---|
34 | aubio_tensor_t *input_tensor); |
---|
35 | |
---|
36 | aubio_maxpool2d_t *new_aubio_maxpool2d(uint_t pool_size[2]) |
---|
37 | { |
---|
38 | aubio_maxpool2d_t *c = AUBIO_NEW(aubio_maxpool2d_t); |
---|
39 | |
---|
40 | AUBIO_GOTO_FAILURE((sint_t)pool_size[0] > 0); |
---|
41 | AUBIO_GOTO_FAILURE((sint_t)pool_size[1] > 0); |
---|
42 | |
---|
43 | c->pool_size[0] = pool_size[0]; |
---|
44 | c->pool_size[1] = pool_size[1]; |
---|
45 | |
---|
46 | c->stride[0] = 1; |
---|
47 | c->stride[1] = 1; |
---|
48 | |
---|
49 | return c; |
---|
50 | |
---|
51 | failure: |
---|
52 | del_aubio_maxpool2d(c); |
---|
53 | return NULL; |
---|
54 | } |
---|
55 | |
---|
56 | void del_aubio_maxpool2d(aubio_maxpool2d_t* c) { |
---|
57 | AUBIO_ASSERT(c); |
---|
58 | AUBIO_FREE(c); |
---|
59 | } |
---|
60 | |
---|
61 | void aubio_maxpool2d_debug(aubio_maxpool2d_t *c, aubio_tensor_t *input_tensor) |
---|
62 | { |
---|
63 | AUBIO_DBG("maxpool2d: %15s -> (%d, %d, %d)" |
---|
64 | " (pool_size=(%d, %d))\n", |
---|
65 | aubio_tensor_get_shape_string(input_tensor), |
---|
66 | input_tensor->shape[0] / c->pool_size[0], |
---|
67 | input_tensor->shape[1] / c->pool_size[1], |
---|
68 | input_tensor->shape[2], |
---|
69 | c->pool_size[0], |
---|
70 | c->pool_size[1]); |
---|
71 | } |
---|
72 | |
---|
73 | uint_t aubio_maxpool2d_get_output_shape(aubio_maxpool2d_t *c, |
---|
74 | aubio_tensor_t *input, uint_t *shape) |
---|
75 | { |
---|
76 | AUBIO_ASSERT(c); |
---|
77 | AUBIO_ASSERT(shape && sizeof(shape) == 2*sizeof(uint_t)); |
---|
78 | AUBIO_ASSERT(input); |
---|
79 | shape[0] = input->shape[0] / c->pool_size[0]; |
---|
80 | shape[1] = input->shape[1] / c->pool_size[1]; |
---|
81 | shape[2] = input->shape[2]; |
---|
82 | |
---|
83 | aubio_maxpool2d_debug(c, input); |
---|
84 | |
---|
85 | return AUBIO_OK; |
---|
86 | } |
---|
87 | |
---|
88 | void aubio_maxpool2d_do(aubio_maxpool2d_t *c, aubio_tensor_t *input_tensor, |
---|
89 | aubio_tensor_t *output_tensor) |
---|
90 | { |
---|
91 | uint_t i, j, k, a, b; |
---|
92 | AUBIO_ASSERT(c && input_tensor && output_tensor); |
---|
93 | |
---|
94 | //aubio_maxpool2d_debug(c, input_tensor); |
---|
95 | |
---|
96 | for (i = 0; i < output_tensor->shape[0]; i++) |
---|
97 | { |
---|
98 | for (j = 0; j < output_tensor->shape[1]; j++) |
---|
99 | { |
---|
100 | for (k = 0; k < output_tensor->shape[2]; k++) |
---|
101 | { |
---|
102 | uint_t stride_i = i * c->pool_size[0]; |
---|
103 | uint_t stride_j = j * c->pool_size[1]; |
---|
104 | smpl_t m = input_tensor->data[stride_i][stride_j |
---|
105 | * input_tensor->shape[2] + k]; |
---|
106 | for (a = 0; a < c->pool_size[0]; a++) { |
---|
107 | for (b = 0; b < c->pool_size[1]; b++) { |
---|
108 | uint_t idx = (stride_j + b) * input_tensor->shape[2] + k; |
---|
109 | m = MAX(m, input_tensor->data[stride_i + a][idx]); |
---|
110 | } |
---|
111 | } |
---|
112 | output_tensor->data[i][j * output_tensor->shape[2] + k] = m; |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | void aubio_maxpool2d_train(aubio_maxpool2d_t *c, |
---|
119 | aubio_tensor_t *backprop) |
---|
120 | { |
---|
121 | AUBIO_ASSERT(c && backprop); |
---|
122 | } |
---|