attractivechaos / kann

A lightweight C library for artificial neural networks
Other
678 stars 117 forks source link

support upsampling is possible? #7

Open shawnlee103 opened 6 years ago

shawnlee103 commented 6 years ago

support upsampling is possible? or how to gen AE model with Convolution ?

attractivechaos commented 6 years ago

KANN doesn't support unpooling and deconvolution. Implementing unpooling may be easy, but implementing deconvolution efficiently is not trivial. I am occupied recently. I can fix confirmed bugs, but won't have time to implement deconvolution unfortunately. Sorry.

shawnlee103 commented 6 years ago

could support unpooling only?

shawnlee103 commented 6 years ago

I try to implement upSampling2d, but seems not work. Any suggestion? int kad_op_upSampling2d(kad_node_t p, int action) { conv_conf_t aux = (conv_conf_t)p->ptr; kad_node_t q = p->child[0]; if (action == KAD_SYNC_DIM) { if (q->n_d != 4) return -1; p->n_d = 4; p->d[0] = q->d[0], p->d[1] = q->d[1], p->d[2] = up_pooling_out_size(q->d[2], &aux[0]), p->d[3] = up_pooling_out_size(q->d[3], &aux[1]); } else if (action == KAD_ALLOC) { p->gtmp = realloc(p->gtmp, kad_len(p) sizeof(int)); } else if (action == KAD_FORWARD) { int rest = 1, len, t, i; int f = (int)p->gtmp; len = kad_len(p); for (i = 0; i < len; ++i) p->x[i] = -FLT_MAX; for (i = 0; i < p->n_d - 2; ++i) rest = p->d[i]; for (t = 0; t < rest; ++t) { int q_i, j, k, l, q_row = q->d[p->n_d - 2], q_col = q->d[p->n_d - 1]; for (q_i = 0; q_i < q_row; ++q_i) { int q_u = (t q_row + q_i) q_col; for (k = 0; k < aux[0].kernel_size; ++k) { int p_v0, p_v_end, p_i = q_i aux[0].kernel_size + k - aux[0].pad[0]; if (p_i < 0 || p_i >= p->d[p->n_d - 2]) continue; p_v0 = (t p->d[p->n_d - 2] + p_i) p->d[p->n_d - 1]; p_v_end = p_v0 + p->d[p->n_d - 1]; for (j = 0; j < q_col ; j++) for (l = 0; l < aux[1].kernel_size; ++l) { p->x[p_v0 + j aux[1].kernel_size + l] = q->x[q_u + j]; f[p_v0 + j aux[1].kernel_size + l] = q_u + j; } } / ~k / } / ~i / } } else if (action == KAD_BACKWARD) { int i, len, f = (int*)p->gtmp; len = kad_len(p); for (i = 0; i < len; ++i) { q->g[f[i]] += p->g[i]; } } return 0; }

and i want to try the AE model with mnist static kann_t model_gen(int n_in, int n_hidden, float i_dropout) { kad_node_t t, *x; x = kad_feed(4, 1, 1, 28, 28), x->ext_flag |= KANN_F_IN | KANN_F_TRUTH; t = kad_relu(kann_layer_conv2d(x, 4, 3, 3, 1, 1, 2, 2)); // 3x3 kernel; 1x1 stride; 1x1 padding t = kad_max2d(t, 2, 2, 2, 2, 0, 0); // 2x2 kernel; 2x2 stride; 0x0 padding t = kad_relu(kann_layer_conv2d(t, 4, 3, 3, 1, 1, 2, 2)); // 3x3 kernel; 1x1 stride; 1x1 padding t = kad_max2d(t, 2, 2, 2, 2, 0, 0); // 2x2 kernel; 2x2 stride; 0x0 padding

t = kad_relu(kann_layer_conv2d(t, 4, 3, 3, 1, 1, 2, 2)); // 3x3 kernel; 1x1 stride; 1x1 padding
t = kad_upSampling2d(t, 2, 2); // 2x2 kernel
t = kad_relu(kann_layer_conv2d(t, 4, 3, 3, 1, 1, 2, 2)); // 3x3 kernel; 1x1 stride; 1x1 padding
t = kad_upSampling2d(t, 2, 2); // 2x2 kernel
t = kad_sigm(kann_layer_conv2d(t, 1, 3, 3, 1, 1, 2, 2)), t->ext_flag = KANN_F_OUT; // 3x3 kernel; 1x1 stride; 1x1 padding
t = kad_mse(t, x), t->ext_flag = KANN_F_COST;
return kann_new(t, 0);

}