[ai] add first maxpool1d draft
authorPaul Brossier <piem@piem.org>
Mon, 31 Dec 2018 16:12:38 +0000 (17:12 +0100)
committerPaul Brossier <piem@piem.org>
Mon, 31 Dec 2018 16:12:38 +0000 (17:12 +0100)
src/ai/maxpool1d.c [new file with mode: 0644]
src/ai/maxpool1d.h [new file with mode: 0644]

diff --git a/src/ai/maxpool1d.c b/src/ai/maxpool1d.c
new file mode 100644 (file)
index 0000000..2688b8e
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+  Copyright (C) 2018 Paul Brossier <piem@aubio.org>
+
+  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 <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "aubio_priv.h"
+#include "fmat.h"
+#include "tensor.h"
+#include "maxpool1d.h"
+
+#include <float.h> // 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 (file)
index 0000000..cbc5663
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+  Copyright (C) 2018 Paul Brossier <piem@aubio.org>
+
+  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 <http://www.gnu.org/licenses/>.
+
+*/
+
+#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 */