source: src/ai/maxpool2d.c @ be3164d

feature/crepe
Last change on this file since be3164d was be3164d, checked in by Paul Brossier <piem@piem.org>, 2 years ago

[ai] only compile _debug function in debug mode

  • Property mode set to 100644
File size: 3.2 KB
Line 
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
28struct _aubio_maxpool2d_t {
29  uint_t pool_size[2];
30  uint_t stride[2];
31};
32
33#if defined(DEBUG)
34static void aubio_maxpool2d_debug(aubio_maxpool2d_t *c,
35    aubio_tensor_t *input_tensor);
36#endif
37
38aubio_maxpool2d_t *new_aubio_maxpool2d(uint_t pool_size[2])
39{
40  aubio_maxpool2d_t *c = AUBIO_NEW(aubio_maxpool2d_t);
41
42  AUBIO_GOTO_FAILURE((sint_t)pool_size[0] > 0);
43  AUBIO_GOTO_FAILURE((sint_t)pool_size[1] > 0);
44
45  c->pool_size[0] = pool_size[0];
46  c->pool_size[1] = pool_size[1];
47
48  c->stride[0] = 1;
49  c->stride[1] = 1;
50
51  return c;
52
53failure:
54  del_aubio_maxpool2d(c);
55  return NULL;
56}
57
58void del_aubio_maxpool2d(aubio_maxpool2d_t* c) {
59  AUBIO_ASSERT(c);
60  AUBIO_FREE(c);
61}
62
63#if defined(DEBUG)
64void aubio_maxpool2d_debug(aubio_maxpool2d_t *c, aubio_tensor_t *input_tensor)
65{
66  AUBIO_DBG("maxpool2d: %15s -> (%d, %d, %d)"
67      " (pool_size=(%d, %d))\n",
68      aubio_tensor_get_shape_string(input_tensor),
69      input_tensor->shape[0] / c->pool_size[0],
70      input_tensor->shape[1] / c->pool_size[1],
71      input_tensor->shape[2],
72      c->pool_size[0],
73      c->pool_size[1]);
74}
75#endif
76
77uint_t aubio_maxpool2d_get_output_shape(aubio_maxpool2d_t *c,
78    aubio_tensor_t *input, uint_t *shape)
79{
80  AUBIO_ASSERT(c);
81  AUBIO_ASSERT(shape && sizeof(shape) == 2*sizeof(uint_t));
82  AUBIO_ASSERT(input);
83  shape[0] = input->shape[0] / c->pool_size[0];
84  shape[1] = input->shape[1] / c->pool_size[1];
85  shape[2] = input->shape[2];
86
87#if defined(DEBUG)
88  aubio_maxpool2d_debug(c, input);
89#endif
90
91  return AUBIO_OK;
92}
93
94void aubio_maxpool2d_do(aubio_maxpool2d_t *c, aubio_tensor_t *input_tensor,
95    aubio_tensor_t *output_tensor)
96{
97  uint_t i, j, k, a, b;
98  AUBIO_ASSERT(c && input_tensor && output_tensor);
99
100  //aubio_maxpool2d_debug(c, input_tensor);
101
102  for (i = 0; i < output_tensor->shape[0]; i++)
103  {
104    for (j = 0; j < output_tensor->shape[1]; j++)
105    {
106      for (k = 0; k < output_tensor->shape[2]; k++)
107      {
108        uint_t stride_i = i * c->pool_size[0];
109        uint_t stride_j = j * c->pool_size[1];
110        smpl_t m = input_tensor->data[stride_i][stride_j
111          * input_tensor->shape[2] + k];
112        for (a = 0; a < c->pool_size[0]; a++) {
113          for (b = 0; b < c->pool_size[1]; b++) {
114            uint_t idx = (stride_j + b) * input_tensor->shape[2] + k;
115            m = MAX(m, input_tensor->data[stride_i + a][idx]);
116          }
117        }
118        output_tensor->data[i][j * output_tensor->shape[2] + k] = m;
119      }
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.