source: src/ai/tensor.c @ c61cf97

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

[tensor] add get_shape_string

  • Property mode set to 100644
File size: 2.7 KB
Line 
1#include "aubio_priv.h"
2#include "fmat.h"
3#include "tensor.h"
4
5aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape)
6{
7  aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t);
8  uint_t i;
9
10  if ((sint_t)ndim <= 0) goto failure;
11  for (i = 0; i < ndim; i++) {
12    if ((sint_t)shape[i] <= 0) goto failure;
13  }
14
15  c->ndim = ndim;
16  uint_t items_per_row = 1;
17  //c->shape = AUBIO_ARRAY(uint_t, ndim);
18  c->shape[0] = shape[0];
19  for (i = 1; i < ndim; i++) {
20    c->shape[i] = shape[i];
21    items_per_row *= shape[i];
22  }
23  c->size = items_per_row * shape[0];
24  c->data = AUBIO_ARRAY(smpl_t*, shape[0]);
25  c->data[0] = AUBIO_ARRAY(smpl_t, c->size);
26  for (i = 1; i < c->shape[0]; i++) {
27    c->data[i] = c->data[0] + i * items_per_row;
28  }
29
30  return c;
31
32failure:
33  del_aubio_tensor(c);
34  return NULL;
35}
36
37void del_aubio_tensor(aubio_tensor_t *c)
38{
39  AUBIO_ASSERT(c);
40  if (c->data) {
41    if (c->data[0]) {
42      AUBIO_FREE(c->data[0]);
43    }
44    AUBIO_FREE(c->data);
45  }
46  //if (c->shape)
47  //  AUBIO_FREE(c->shape);
48  AUBIO_FREE(c);
49}
50
51uint_t aubio_tensor_as_fvec(aubio_tensor_t *c, fvec_t *o) {
52  if (c->ndim  != 1) return AUBIO_FAIL;
53  if (c->shape[0] <= 0) return AUBIO_FAIL;
54  o->length = c->shape[0];
55  o->data = c->data[0];
56  return AUBIO_OK;
57}
58
59uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c) {
60  if (o == NULL) return AUBIO_FAIL;
61  c->ndim = 1;
62  c->shape[0] = o->length;
63  c->data = &o->data;
64  c->size = o->length;
65  return AUBIO_OK;
66}
67
68uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) {
69  if (c->ndim  != 2) return AUBIO_FAIL;
70  if (c->shape[0] <= 0) return AUBIO_FAIL;
71  if (c->shape[1] <= 0) return AUBIO_FAIL;
72  o->height = c->shape[0];
73  o->length = c->shape[1];
74  o->data = c->data;
75  return AUBIO_OK;
76}
77
78uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c) {
79  if (o == NULL) return AUBIO_FAIL;
80  if (c == NULL) return AUBIO_FAIL;
81  c->ndim = 2;
82  c->shape[0] = o->height;
83  c->shape[1] = o->length;
84  c->size = o->height * o->length;
85  c->data = o->data;
86  return AUBIO_OK;
87}
88
89smpl_t aubio_tensor_max(aubio_tensor_t *t)
90{
91  uint_t i;
92  smpl_t max = -1000000;
93  for (i = 0; i < t->size; i++) {
94    max = MAX(t->data[0][i], max);
95  }
96  return max;
97}
98
99const char_t *aubio_tensor_get_shape_string(aubio_tensor_t *t) {
100  uint_t i;
101  if (!t) return NULL;
102  size_t offset = 2;
103  static char_t shape_str[STRN_LENGTH];
104  char_t shape_str_previous[STRN_LENGTH] = "(";
105  for (i = 0; i < t->ndim; i++) {
106    int len = snprintf(shape_str, STRN_LENGTH, "%s%d%s",
107        shape_str_previous, t->shape[i], (i == t->ndim - 1) ? "" : ", ");
108    strncpy(shape_str_previous, shape_str, len);
109  }
110  snprintf(shape_str, strnlen(shape_str, STRN_LENGTH - offset - 1) + offset,
111      "%s)", shape_str_previous);
112}
Note: See TracBrowser for help on using the repository browser.