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: input (%d, %d, %d) ¤ maxpool2d (pool_size = (%d, %d)) ->" |
---|
64 | " (%d, %d, %d) (no params)\n", |
---|
65 | input_tensor->shape[0], |
---|
66 | input_tensor->shape[1], |
---|
67 | input_tensor->shape[2], |
---|
68 | c->pool_size[0], |
---|
69 | c->pool_size[1], |
---|
70 | input_tensor->shape[0] / c->pool_size[0], |
---|
71 | input_tensor->shape[1] / c->pool_size[1], |
---|
72 | input_tensor->shape[2]); |
---|
73 | } |
---|
74 | |
---|
75 | uint_t aubio_maxpool2d_get_output_shape(aubio_maxpool2d_t *c, |
---|
76 | aubio_tensor_t *input, uint_t *shape) |
---|
77 | { |
---|
78 | AUBIO_ASSERT(c); |
---|
79 | AUBIO_ASSERT(shape && sizeof(shape) == 2*sizeof(uint_t)); |
---|
80 | AUBIO_ASSERT(input); |
---|
81 | shape[0] = input->shape[0] / c->pool_size[0]; |
---|
82 | shape[1] = input->shape[1] / c->pool_size[1]; |
---|
83 | shape[2] = input->shape[2]; |
---|
84 | |
---|
85 | aubio_maxpool2d_debug(c, input); |
---|
86 | |
---|
87 | return AUBIO_OK; |
---|
88 | } |
---|
89 | |
---|
90 | void aubio_maxpool2d_do(aubio_maxpool2d_t *c, aubio_tensor_t *input_tensor, |
---|
91 | aubio_tensor_t *output_tensor) |
---|
92 | { |
---|
93 | uint_t i, j, k, a, b; |
---|
94 | AUBIO_ASSERT(c && input_tensor && output_tensor); |
---|
95 | |
---|
96 | //aubio_maxpool2d_debug(c, input_tensor); |
---|
97 | |
---|
98 | for (i = 0; i < output_tensor->shape[0]; i++) |
---|
99 | { |
---|
100 | for (j = 0; j < output_tensor->shape[1]; j++) |
---|
101 | { |
---|
102 | for (k = 0; k < output_tensor->shape[2]; k++) |
---|
103 | { |
---|
104 | smpl_t m = -10000.; |
---|
105 | for (a = 0; a < c->pool_size[0]; a++) { |
---|
106 | for (b = 0; b < c->pool_size[1]; b++) { |
---|
107 | uint_t idx = (j * c->pool_size[1] + b) * input_tensor->shape[2] + k; |
---|
108 | m = MAX(m, input_tensor->data[i * c->pool_size[0] + a][idx]); |
---|
109 | } |
---|
110 | } |
---|
111 | output_tensor->data[i][j * output_tensor->shape[2] + k] = m; |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | void aubio_maxpool2d_train(aubio_maxpool2d_t *c, |
---|
118 | aubio_tensor_t *backprop) |
---|
119 | { |
---|
120 | AUBIO_ASSERT(c && backprop); |
---|
121 | } |
---|