From ba0c332b21fd7848d8a5c6207c07c4def7459df0 Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Mon, 31 Dec 2018 17:12:38 +0100 Subject: [PATCH] [ai] add first maxpool1d draft --- src/ai/maxpool1d.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ai/maxpool1d.h | 45 +++++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/ai/maxpool1d.c create mode 100644 src/ai/maxpool1d.h diff --git a/src/ai/maxpool1d.c b/src/ai/maxpool1d.c new file mode 100644 index 00000000..2688b8ef --- /dev/null +++ b/src/ai/maxpool1d.c @@ -0,0 +1,98 @@ +/* + Copyright (C) 2018 Paul Brossier + + This file is part of aubio. + + aubio is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + aubio is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with aubio. If not, see . + +*/ + +#include "aubio_priv.h" +#include "fmat.h" +#include "tensor.h" +#include "maxpool1d.h" + +#include // FLT_MAX + +struct _aubio_maxpool1d_t { + uint_t pool_size; + uint_t stride; +}; + +static void aubio_maxpool1d_debug(aubio_maxpool1d_t *c, + aubio_tensor_t *input_tensor); + +aubio_maxpool1d_t *new_aubio_maxpool1d(uint_t pool_size[1]) +{ + aubio_maxpool1d_t *c = AUBIO_NEW(aubio_maxpool1d_t); + + AUBIO_GOTO_FAILURE((sint_t)pool_size[0] > 0); + + c->pool_size = pool_size[0]; + + c->stride = 1; + + return c; + +failure: + del_aubio_maxpool1d(c); + return NULL; +} + +void del_aubio_maxpool1d(aubio_maxpool1d_t* c) { + AUBIO_ASSERT(c); + AUBIO_FREE(c); +} + +void aubio_maxpool1d_debug(aubio_maxpool1d_t *c, aubio_tensor_t *input_tensor) +{ + AUBIO_DBG("maxpool1d: input (%d, %d) ¤ maxpool1d (pool_size = (%d)) ->" + " (%d, %d) (no params)\n", + input_tensor->dims[0], + input_tensor->dims[1], + c->pool_size, + input_tensor->dims[0] / c->pool_size, + input_tensor->dims[1]); +} + +uint_t aubio_maxpool1d_get_output_shape(aubio_maxpool1d_t *c, + aubio_tensor_t *input, uint_t *shape) +{ + AUBIO_ASSERT(c); + AUBIO_ASSERT(shape && sizeof(shape) == 2*sizeof(uint_t)); + AUBIO_ASSERT(input); + shape[0] = input->dims[0] / c->pool_size; + shape[1] = input->dims[1]; + + aubio_maxpool1d_debug(c, input); + + return AUBIO_OK; +} + +void aubio_maxpool1d_do(aubio_maxpool1d_t *c, aubio_tensor_t *input_tensor, + aubio_tensor_t *output_tensor) +{ + uint_t i, j, a; + AUBIO_ASSERT(c && input_tensor && output_tensor); + + for (j = 0; j < output_tensor->dims[1]; j++) { + for (i = 0; i < output_tensor->dims[0]; i++) { + smpl_t m = -FLT_MAX; + for (a = 0; a < c->pool_size; a++) { + m = MAX(m, input_tensor->data[i * c->pool_size + a][j]); + } + output_tensor->data[i][j] = m; + } + } +} diff --git a/src/ai/maxpool1d.h b/src/ai/maxpool1d.h new file mode 100644 index 00000000..cbc5663c --- /dev/null +++ b/src/ai/maxpool1d.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2018 Paul Brossier + + This file is part of aubio. + + aubio is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + aubio is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with aubio. If not, see . + +*/ + +#ifndef AUBIO_MAXPOOL1D_H +#define AUBIO_MAXPOOL1D_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct _aubio_maxpool1d_t aubio_maxpool1d_t; + +aubio_maxpool1d_t *new_aubio_maxpool1d(uint_t pool_size[1]); + +uint_t aubio_maxpool1d_get_output_shape(aubio_maxpool1d_t *t, + aubio_tensor_t *input, uint_t *shape); + +void aubio_maxpool1d_do(aubio_maxpool1d_t *t, + aubio_tensor_t *input_tensor, + aubio_tensor_t *activations); + +void del_aubio_maxpool1d(aubio_maxpool1d_t *t); + +#ifdef __cplusplus +} +#endif + +#endif /* AUBIO_MAXPOOL1D_H */ -- 2.11.0