[f3848c0] | 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 | #include "aubio_priv.h" |
---|
| 23 | #include "fmat.h" |
---|
| 24 | #include "tensor.h" |
---|
| 25 | #include "conv2d.h" |
---|
| 26 | |
---|
| 27 | typedef enum |
---|
| 28 | { |
---|
| 29 | PAD_SAME = 0, // TODO |
---|
| 30 | PAD_VALID = 1, |
---|
| 31 | //PAD_CAUSAL = 2, // TODO (1d only, for dilated convolution) |
---|
| 32 | } aubio_conv2d_padding_type; |
---|
| 33 | |
---|
| 34 | struct _aubio_conv2d_t { |
---|
| 35 | // define internals here |
---|
| 36 | uint_t n_filters; |
---|
| 37 | uint_t kernel_shape[2]; // kernel sizes |
---|
| 38 | uint_t stride_shape[2]; // stride sizes |
---|
| 39 | |
---|
| 40 | aubio_conv2d_padding_type padding_mode; |
---|
| 41 | |
---|
| 42 | // these will be set after calling get_output_shape |
---|
| 43 | aubio_tensor_t *kernel; |
---|
| 44 | fvec_t *bias; |
---|
| 45 | uint_t output_shape[3]; // shape of output |
---|
| 46 | uint_t padding_start[2]; // {top, left} padding |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | static void aubio_conv2d_debug(aubio_conv2d_t *c, aubio_tensor_t *input_tensor); |
---|
| 50 | |
---|
| 51 | aubio_conv2d_t *new_aubio_conv2d(uint_t n_filters, uint_t *kernel_shape) |
---|
| 52 | { |
---|
| 53 | aubio_conv2d_t *c = AUBIO_NEW(aubio_conv2d_t); |
---|
| 54 | |
---|
| 55 | // validate input parameters |
---|
| 56 | AUBIO_GOTO_FAILURE((sint_t)n_filters >= 1); |
---|
| 57 | AUBIO_GOTO_FAILURE((sint_t)kernel_shape[0] >= 1); |
---|
| 58 | AUBIO_GOTO_FAILURE((sint_t)kernel_shape[1] >= 1); |
---|
| 59 | |
---|
| 60 | // set internal variables |
---|
| 61 | c->n_filters = n_filters; |
---|
| 62 | c->kernel_shape[0] = kernel_shape[0]; |
---|
| 63 | c->kernel_shape[1] = kernel_shape[1]; |
---|
| 64 | |
---|
| 65 | // default to padding_mode="valid" |
---|
| 66 | c->padding_mode = PAD_VALID; |
---|
| 67 | // set default stride_shape to {1, 1} |
---|
| 68 | aubio_conv2d_set_stride(c, 1, 1); |
---|
| 69 | |
---|
| 70 | return c; |
---|
| 71 | |
---|
| 72 | failure: |
---|
| 73 | del_aubio_conv2d(c); |
---|
| 74 | return NULL; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void del_aubio_conv2d(aubio_conv2d_t *c) |
---|
| 78 | { |
---|
| 79 | AUBIO_ASSERT(c); |
---|
| 80 | // destroy internals here |
---|
| 81 | if (c->kernel) { |
---|
| 82 | del_aubio_tensor(c->kernel); |
---|
| 83 | } |
---|
| 84 | if (c->bias) |
---|
| 85 | del_fvec(c->bias); |
---|
| 86 | AUBIO_FREE(c); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | uint_t aubio_conv2d_set_stride(aubio_conv2d_t *c, |
---|
| 91 | uint_t stride1, uint_t stride2) |
---|
| 92 | { |
---|
| 93 | if ((sint_t)stride1 < 1) return AUBIO_FAIL; |
---|
| 94 | if ((sint_t)stride2 < 1) return AUBIO_FAIL; |
---|
| 95 | c->stride_shape[0] = stride1; |
---|
| 96 | c->stride_shape[1] = stride2; |
---|
| 97 | return AUBIO_OK; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | uint_t *aubio_conv2d_get_stride(aubio_conv2d_t *c) |
---|
| 101 | { |
---|
| 102 | return c->stride_shape; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | uint_t aubio_conv2d_get_output_shape(aubio_conv2d_t *c, |
---|
| 106 | aubio_tensor_t *input_tensor, |
---|
| 107 | uint_t *shape) |
---|
| 108 | { |
---|
| 109 | uint_t output_shape[3] = {0, 0, c->n_filters}; |
---|
| 110 | uint_t padding_start[2] = {0, 0}; |
---|
| 111 | |
---|
| 112 | // check input parameters |
---|
| 113 | AUBIO_ASSERT(input_tensor); |
---|
| 114 | AUBIO_ASSERT(shape); |
---|
| 115 | |
---|
| 116 | // reset output array |
---|
| 117 | shape[0] = 0; |
---|
| 118 | shape[1] = 0; |
---|
| 119 | shape[2] = 0; |
---|
| 120 | |
---|
| 121 | switch (c->padding_mode) { |
---|
| 122 | case PAD_SAME: |
---|
| 123 | // compute output shape |
---|
| 124 | output_shape[0] = (uint_t)CEIL(input_tensor->shape[0] |
---|
| 125 | / (smpl_t)c->stride_shape[0]); |
---|
| 126 | output_shape[1] = (uint_t)CEIL(input_tensor->shape[1] |
---|
| 127 | / (smpl_t)c->stride_shape[1]); |
---|
| 128 | |
---|
| 129 | uint_t padding_shape[2]; // total amount of padding |
---|
| 130 | padding_shape[0] = (output_shape[0] - 1) * c->stride_shape[0] |
---|
| 131 | + c->kernel_shape[0] - input_tensor->shape[0]; |
---|
| 132 | padding_shape[1] = (output_shape[1] - 1) * c->stride_shape[1] |
---|
| 133 | + c->kernel_shape[1] - input_tensor->shape[1]; |
---|
| 134 | |
---|
| 135 | padding_start[0] = FLOOR(padding_shape[0] / 2); |
---|
| 136 | padding_start[1] = FLOOR(padding_shape[1] / 2); |
---|
| 137 | |
---|
| 138 | break; |
---|
| 139 | case PAD_VALID: |
---|
| 140 | output_shape[0] = (input_tensor->shape[0] - c->kernel_shape[0] + 1) |
---|
| 141 | / c->stride_shape[0]; |
---|
| 142 | output_shape[1] = (input_tensor->shape[1] - c->kernel_shape[1] + 1) |
---|
| 143 | / c->stride_shape[1]; |
---|
| 144 | |
---|
| 145 | padding_start[0] = 0; |
---|
| 146 | padding_start[1] = 0; |
---|
| 147 | break; |
---|
| 148 | //case PAD_CAUSAL: |
---|
| 149 | // // TODO |
---|
| 150 | // return AUBIO_FAIL; |
---|
| 151 | default: |
---|
| 152 | return AUBIO_FAIL; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | uint_t kernel_shape[4]; |
---|
| 156 | kernel_shape[0] = c->kernel_shape[0]; |
---|
| 157 | kernel_shape[1] = c->kernel_shape[1]; |
---|
| 158 | kernel_shape[2] = input_tensor->shape[2]; |
---|
| 159 | kernel_shape[3] = c->n_filters; |
---|
| 160 | |
---|
| 161 | if (c->kernel) del_aubio_tensor(c->kernel); |
---|
| 162 | if (c->bias) del_fvec(c->bias); |
---|
| 163 | |
---|
| 164 | c->kernel = new_aubio_tensor(4, kernel_shape); |
---|
| 165 | if (!c->kernel) return AUBIO_FAIL; |
---|
| 166 | c->bias = new_fvec(c->n_filters); |
---|
| 167 | |
---|
| 168 | // set internals upon success |
---|
| 169 | c->output_shape[0] = output_shape[0]; |
---|
| 170 | c->output_shape[1] = output_shape[1]; |
---|
| 171 | c->output_shape[2] = output_shape[2]; |
---|
| 172 | |
---|
| 173 | c->padding_start[0] = padding_start[0]; |
---|
| 174 | c->padding_start[1] = padding_start[1]; |
---|
| 175 | |
---|
| 176 | // set output |
---|
| 177 | shape[0] = output_shape[0]; |
---|
| 178 | shape[1] = output_shape[1]; |
---|
| 179 | shape[2] = output_shape[2]; |
---|
| 180 | |
---|
| 181 | aubio_conv2d_debug(c, input_tensor); |
---|
| 182 | |
---|
| 183 | return AUBIO_OK; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void aubio_conv2d_debug(aubio_conv2d_t *c, aubio_tensor_t *input_tensor) |
---|
| 187 | { |
---|
| 188 | // print some info |
---|
| 189 | AUBIO_ASSERT(c); |
---|
| 190 | uint_t n_params = (c->kernel->shape[0] * c->kernel->shape[2] + 1) |
---|
| 191 | * c->kernel->shape[1] * c->kernel->shape[3]; |
---|
| 192 | |
---|
| 193 | AUBIO_DBG("conv2d: input %s ¤ conv2d %s" |
---|
| 194 | " : (%d, %d, %d)" |
---|
| 195 | " (%d params, stride (%d, %d), pad_start [%d, %d])\n", |
---|
| 196 | aubio_tensor_get_shape_string(input_tensor), |
---|
| 197 | aubio_tensor_get_shape_string(c->kernel), |
---|
| 198 | c->output_shape[0], c->output_shape[1], c->output_shape[2], |
---|
| 199 | n_params, |
---|
| 200 | c->stride_shape[0], c->stride_shape[1], |
---|
| 201 | -c->padding_start[0], -c->padding_start[1]); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | uint_t aubio_conv2d_check_output_shape(aubio_conv2d_t *c, |
---|
| 205 | aubio_tensor_t *input_tensor, |
---|
| 206 | aubio_tensor_t *activations) |
---|
| 207 | { |
---|
| 208 | // fetch output_shape if it hasn't been done before |
---|
| 209 | if (c->output_shape[0] == 0 || |
---|
| 210 | c->output_shape[1] == 0 || |
---|
| 211 | c->output_shape[2] == 0) { |
---|
| 212 | if (!aubio_conv2d_get_output_shape(c, input_tensor, c->output_shape)) { |
---|
| 213 | return AUBIO_FAIL; |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | // check we have as many filters as expected activation outputs |
---|
| 218 | if (activations->shape[2] != c->n_filters) return AUBIO_FAIL; |
---|
| 219 | if (activations->shape[2] != c->kernel->shape[3]) return AUBIO_FAIL; |
---|
| 220 | if (input_tensor->shape[2] != c->kernel->shape[2]) return AUBIO_FAIL; |
---|
| 221 | |
---|
| 222 | // check tensor activations has the expected sizes |
---|
| 223 | if (c->output_shape[0] != activations->shape[0]) return AUBIO_FAIL; |
---|
| 224 | if (c->output_shape[1] != activations->shape[1]) return AUBIO_FAIL; |
---|
| 225 | if (c->output_shape[2] != activations->shape[2]) return AUBIO_FAIL; |
---|
| 226 | return AUBIO_OK; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | void aubio_conv2d_do(aubio_conv2d_t *c, aubio_tensor_t *input_tensor, |
---|
| 230 | aubio_tensor_t *activations) |
---|
| 231 | { |
---|
| 232 | uint_t i, j, k, l, a, b; |
---|
| 233 | uint_t stride_a, stride_b; |
---|
| 234 | sint_t x, y; |
---|
| 235 | smpl_t s, w, bias, acc; |
---|
| 236 | uint_t jj, ll, bb, yy; |
---|
| 237 | |
---|
| 238 | uint_t k_stride1 = c->kernel->shape[3]; |
---|
| 239 | uint_t k_stride2 = c->kernel->shape[2] * k_stride1; |
---|
| 240 | |
---|
| 241 | AUBIO_ASSERT(c && input_tensor && activations); |
---|
| 242 | // check we have the correct output activation sizes |
---|
| 243 | if (aubio_conv2d_check_output_shape(c, input_tensor, activations)) |
---|
| 244 | { |
---|
| 245 | AUBIO_ERR("conv2d: check_output_shape failed\n"); |
---|
| 246 | return; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | // for each kernel filter k |
---|
| 250 | for (i = 0; i < activations->shape[2]; i++) { |
---|
| 251 | // get bias |
---|
| 252 | bias = c->bias->data[i]; |
---|
| 253 | stride_b = 0; // == j * c->stride_shape[1] |
---|
| 254 | jj = 0; // == j * activations->shape[2] |
---|
| 255 | // for each output y |
---|
| 256 | for (j = 0; j < activations->shape[1]; j++) { |
---|
| 257 | // for each output x |
---|
| 258 | stride_a = 0; // k * c->stride_shape[0] |
---|
| 259 | for (k = 0; k < activations->shape[0]; k++) { |
---|
| 260 | // reset output |
---|
| 261 | acc = 0; |
---|
| 262 | // compute convolution for one kernel |
---|
| 263 | for (a = 0; a < c->kernel_shape[0]; a++) { |
---|
| 264 | x = stride_a + a - c->padding_start[0]; |
---|
| 265 | if ((x < 0) || (x > (sint_t)input_tensor->shape[0] - 1)) |
---|
| 266 | continue; // padding with 0. |
---|
| 267 | bb = 0; // == b * k_stride2 |
---|
| 268 | for (b = 0; b < c->kernel_shape[1]; b++) { |
---|
| 269 | y = stride_b + b - c->padding_start[1]; |
---|
| 270 | if ((y < 0) || (y > (sint_t)input_tensor->shape[1] - 1)) |
---|
| 271 | continue; // padding with 0. |
---|
| 272 | yy = y * input_tensor->shape[2]; |
---|
| 273 | ll = bb + i; // + l * k_stride1 |
---|
| 274 | // for each input channel |
---|
| 275 | for (l = 0; l < input_tensor->shape[2]; l++) { |
---|
| 276 | // get kernel weight |
---|
| 277 | w = c->kernel->data[a][ll]; |
---|
| 278 | // get input sample |
---|
| 279 | s = input_tensor->data[x][yy + l]; |
---|
| 280 | acc += w * s; |
---|
| 281 | ll += k_stride1; |
---|
| 282 | } |
---|
| 283 | bb += k_stride2; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | stride_a += c->stride_shape[0]; |
---|
| 287 | // apply bias |
---|
| 288 | acc += bias; |
---|
| 289 | // compute RELU |
---|
| 290 | activations->data[k][jj + i] = MAX(acc, 0); |
---|
| 291 | } |
---|
| 292 | stride_b += c->stride_shape[1]; |
---|
| 293 | jj += activations->shape[2]; |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | void aubio_conv2d_do_backwards(aubio_conv2d_t *c, |
---|
| 299 | /*aubio_tensor_t *old_gradients,*/ |
---|
| 300 | aubio_tensor_t *gradients) |
---|
| 301 | { |
---|
| 302 | uint_t i, j, k, a, b; |
---|
| 303 | AUBIO_ASSERT(c && gradients); |
---|
| 304 | // TODO |
---|
| 305 | // for each kernel filter k |
---|
| 306 | for (i = 0; i < c->n_filters; i++) { |
---|
| 307 | // for each input column |
---|
| 308 | for (j = 0; j < gradients->shape[1]; j++) { |
---|
| 309 | // for each input row |
---|
| 310 | for (k = 0; k < gradients->shape[2]; k++) { |
---|
| 311 | for (a = 0; a < c->kernel_shape[0]; a++) { |
---|
| 312 | for (b = 0; b < c->kernel_shape[1]; b++) { |
---|
| 313 | #if 0 |
---|
| 314 | smpl_t grad = gradients->data[i]->data[a][b]; |
---|
| 315 | smpl_t oldgrad = old_gradients->data[i]->data[a][b]; |
---|
| 316 | smpl_t m = (grad - oldgrad * momentum); |
---|
| 317 | w -= lr * m - lr * decay * w; |
---|
| 318 | #endif |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | uint_t aubio_conv2d_set_padding_mode(aubio_conv2d_t *c, |
---|
| 327 | const char_t *padding_mode) |
---|
| 328 | { |
---|
| 329 | AUBIO_ASSERT(c && padding_mode); |
---|
| 330 | if (strncmp(padding_mode, "same", PATH_MAX) == 0) { |
---|
| 331 | c->padding_mode = PAD_SAME; |
---|
| 332 | } else if (strncmp(padding_mode, "valid", PATH_MAX) == 0) { |
---|
| 333 | c->padding_mode = PAD_VALID; |
---|
| 334 | } else { |
---|
| 335 | return AUBIO_FAIL; |
---|
| 336 | } |
---|
| 337 | return AUBIO_OK; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | uint_t aubio_conv2d_set_kernel(aubio_conv2d_t *c, aubio_tensor_t *kernel) |
---|
| 341 | { |
---|
| 342 | uint_t i; |
---|
| 343 | AUBIO_ASSERT(c && kernel); |
---|
| 344 | for (i = 0; i < c->kernel->ndim; i++) { |
---|
| 345 | AUBIO_ASSERT(c->kernel->shape[i] == kernel->shape[i]); |
---|
| 346 | } |
---|
| 347 | return AUBIO_OK; |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | aubio_tensor_t *aubio_conv2d_get_kernel(aubio_conv2d_t* c) |
---|
| 351 | { |
---|
| 352 | AUBIO_ASSERT(c && c->kernel); |
---|
| 353 | return c->kernel; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | uint_t aubio_conv2d_set_bias(aubio_conv2d_t *c, fvec_t *bias) |
---|
| 357 | { |
---|
| 358 | AUBIO_ASSERT(c && bias); |
---|
| 359 | AUBIO_ASSERT(c->kernel_shape[1] == bias->length); |
---|
| 360 | return AUBIO_OK; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | fvec_t *aubio_conv2d_get_bias(aubio_conv2d_t* c) |
---|
| 364 | { |
---|
| 365 | AUBIO_ASSERT(c && c->bias); |
---|
| 366 | return c->bias; |
---|
| 367 | } |
---|