source: src/ai/maxpool2d.c @ 7420c40

feature/cnn_orgfeature/crepe_org
Last change on this file since 7420c40 was 7420c40, checked in by Paul Brossier <piem@piem.org>, 5 years ago

[maxpool2d] use first element to initialize output value, add stride

  • Property mode set to 100644
File size: 3.3 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
33static void aubio_maxpool2d_debug(aubio_maxpool2d_t *c,
34    aubio_tensor_t *input_tensor);
35
36aubio_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
51failure:
52  del_aubio_maxpool2d(c);
53  return NULL;
54}
55
56void del_aubio_maxpool2d(aubio_maxpool2d_t* c) {
57  AUBIO_ASSERT(c);
58  AUBIO_FREE(c);
59}
60
61void 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
75uint_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
90void 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        uint_t stride_i = i * c->pool_size[0];
105        uint_t stride_j = j * c->pool_size[1];
106        smpl_t m = input_tensor->data[stride_i][stride_j
107          * input_tensor->shape[2] + k];
108        for (a = 0; a < c->pool_size[0]; a++) {
109          for (b = 0; b < c->pool_size[1]; b++) {
110            uint_t idx = (stride_j + b) * input_tensor->shape[2] + k;
111            m = MAX(m, input_tensor->data[stride_i + a][idx]);
112          }
113        }
114        output_tensor->data[i][j * output_tensor->shape[2] + k] = m;
115      }
116    }
117  }
118}
119
120void aubio_maxpool2d_train(aubio_maxpool2d_t *c,
121    aubio_tensor_t *backprop)
122{
123  AUBIO_ASSERT(c && backprop);
124}
Note: See TracBrowser for help on using the repository browser.