| | |
| | | #endif |
| | | #endif |
| | | |
| | | convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor) |
| | | convolutional_layer make_convolutional_layer(int batch, int h, int w, int c, int n, int size, int stride, int padding, ACTIVATION activation, int batch_normalize, int binary, int xnor, int adam) |
| | | { |
| | | int i; |
| | | convolutional_layer l = {0}; |
| | |
| | | l.output = calloc(l.batch*out_h * out_w * n, sizeof(float)); |
| | | l.delta = calloc(l.batch*out_h * out_w * n, sizeof(float)); |
| | | |
| | | l.forward = forward_convolutional_layer; |
| | | l.backward = backward_convolutional_layer; |
| | | l.update = update_convolutional_layer; |
| | | if(binary){ |
| | | l.binary_weights = calloc(c*n*size*size, sizeof(float)); |
| | | l.cweights = calloc(c*n*size*size, sizeof(char)); |
| | |
| | | l.rolling_mean = calloc(n, sizeof(float)); |
| | | l.rolling_variance = calloc(n, sizeof(float)); |
| | | } |
| | | if(adam){ |
| | | l.adam = 1; |
| | | l.m = calloc(c*n*size*size, sizeof(float)); |
| | | l.v = calloc(c*n*size*size, sizeof(float)); |
| | | } |
| | | |
| | | #ifdef GPU |
| | | l.forward_gpu = forward_convolutional_layer_gpu; |
| | | l.backward_gpu = backward_convolutional_layer_gpu; |
| | | l.update_gpu = update_convolutional_layer_gpu; |
| | | |
| | | if(gpu_index >= 0){ |
| | | if (adam) { |
| | | l.m_gpu = cuda_make_array(l.m, c*n*size*size); |
| | | l.v_gpu = cuda_make_array(l.v, c*n*size*size); |
| | | } |
| | | |
| | | l.weights_gpu = cuda_make_array(l.weights, c*n*size*size); |
| | | l.weight_updates_gpu = cuda_make_array(l.weight_updates, c*n*size*size); |
| | | |
| | | l.biases_gpu = cuda_make_array(l.biases, n); |
| | | l.bias_updates_gpu = cuda_make_array(l.bias_updates, n); |
| | | |
| | | l.scales_gpu = cuda_make_array(l.scales, n); |
| | | l.scale_updates_gpu = cuda_make_array(l.scale_updates, n); |
| | | |
| | | l.delta_gpu = cuda_make_array(l.delta, l.batch*out_h*out_w*n); |
| | | l.output_gpu = cuda_make_array(l.output, l.batch*out_h*out_w*n); |
| | | |
| | |
| | | l.mean_delta_gpu = cuda_make_array(l.mean, n); |
| | | l.variance_delta_gpu = cuda_make_array(l.variance, n); |
| | | |
| | | l.scales_gpu = cuda_make_array(l.scales, n); |
| | | l.scale_updates_gpu = cuda_make_array(l.scale_updates, n); |
| | | |
| | | l.x_gpu = cuda_make_array(l.output, l.batch*out_h*out_w*n); |
| | | l.x_norm_gpu = cuda_make_array(l.output, l.batch*out_h*out_w*n); |
| | | } |
| | |
| | | l.workspace_size = get_workspace_size(l); |
| | | l.activation = activation; |
| | | |
| | | fprintf(stderr, "Convolutional Layer: %d x %d x %d image, %d filters -> %d x %d x %d image\n", h,w,c,n, out_h, out_w, n); |
| | | fprintf(stderr, "conv %5d %2d x%2d /%2d %4d x%4d x%4d -> %4d x%4d x%4d\n", n, size, size, stride, w, h, c, l.out_w, l.out_h, l.out_c); |
| | | |
| | | return l; |
| | | } |
| | |
| | | |
| | | void test_convolutional_layer() |
| | | { |
| | | convolutional_layer l = make_convolutional_layer(1, 5, 5, 3, 2, 5, 2, 1, LEAKY, 1, 0, 0); |
| | | convolutional_layer l = make_convolutional_layer(1, 5, 5, 3, 2, 5, 2, 1, LEAKY, 1, 0, 0, 0); |
| | | l.batch_normalize = 1; |
| | | float data[] = {1,1,1,1,1, |
| | | 1,1,1,1,1, |
| | |
| | | |
| | | l->delta_gpu = cuda_make_array(l->delta, l->batch*out_h*out_w*l->n); |
| | | l->output_gpu = cuda_make_array(l->output, l->batch*out_h*out_w*l->n); |
| | | |
| | | if(l->batch_normalize){ |
| | | cuda_free(l->x_gpu); |
| | | cuda_free(l->x_norm_gpu); |
| | | |
| | | l->x_gpu = cuda_make_array(l->output, l->batch*l->outputs); |
| | | l->x_norm_gpu = cuda_make_array(l->output, l->batch*l->outputs); |
| | | } |
| | | #ifdef CUDNN |
| | | cudnn_convolutional_setup(l); |
| | | #endif |
| | |
| | | axpy_cpu(l.n, learning_rate/batch, l.bias_updates, 1, l.biases, 1); |
| | | scal_cpu(l.n, momentum, l.bias_updates, 1); |
| | | |
| | | if(l.scales){ |
| | | axpy_cpu(l.n, learning_rate/batch, l.scale_updates, 1, l.scales, 1); |
| | | scal_cpu(l.n, momentum, l.scale_updates, 1); |
| | | } |
| | | |
| | | axpy_cpu(size, -decay*batch, l.weights, 1, l.weight_updates, 1); |
| | | axpy_cpu(size, learning_rate/batch, l.weight_updates, 1, l.weights, 1); |
| | | scal_cpu(size, momentum, l.weight_updates, 1); |