Skip to content

Instantly share code, notes, and snippets.

@cancan101
Last active March 7, 2017 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cancan101/5887cb93cc91a2d10e2bfd23284bb438 to your computer and use it in GitHub Desktop.
Save cancan101/5887cb93cc91a2d10e2bfd23284bb438 to your computer and use it in GitHub Desktop.
#include <Accelerate/Accelerate.h>
#include <stdio.h>
#include <sys/time.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char * argv[])
{
// Description of the input image stack
BNNSImageStackDescriptor i_desc;
bzero(&i_desc,sizeof(i_desc));
i_desc.width = 1;
i_desc.height = 9;
i_desc.channels = 1;
i_desc.row_stride = 1;
i_desc.image_stride = 9*1;
i_desc.data_type = BNNSDataTypeFloat32; // pixels values are 'float'
printf("Input image stack: %zu x %zu x %zu\n",i_desc.width,i_desc.height,i_desc.channels);
// Description of the output image stack
BNNSImageStackDescriptor o_desc;
bzero(&o_desc, sizeof(o_desc));
o_desc.width = 1;
o_desc.height = 9;
o_desc.channels = 1;
o_desc.row_stride = 1;
o_desc.image_stride = 9*1;
o_desc.data_type = BNNSDataTypeFloat32; // pixels values are 'float'
printf("Output image stack: %zu x %zu x %zu\n",o_desc.width,o_desc.height,o_desc.channels);
// Description of the convolution layer
BNNSConvolutionLayerParameters layer_params;
bzero(&layer_params, sizeof(layer_params));
layer_params.k_width = 1; // convolution kernel width: 1 pix
layer_params.k_height = 1; // convolution kernel height: 1 pix
layer_params.in_channels = i_desc.channels; // input channels
layer_params.out_channels = o_desc.channels; // output channels
layer_params.x_stride = 1; // x stride: 1 pix
layer_params.y_stride = 1; // y stride: 1 pix
layer_params.x_padding = 0; // x padding: 0 pix
layer_params.y_padding = 0; // y padding: 0 pix
printf("Convolution kernel: %zu x %zu\n",layer_params.k_width,layer_params.k_height);
// Allocate weights buffer. For a 5x5 convolution, we need 5 * 5 * input channels * output channels weights.
size_t n_weights = layer_params.k_width * layer_params.k_height * layer_params.in_channels * layer_params.out_channels;
float * weights = (float *)calloc(n_weights,sizeof(float));
weights[0] = 1;
// Attach weight buffer to layer parameters
layer_params.weights.data = weights;
layer_params.weights.data_type = BNNSDataTypeFloat32;
// Common filter parameters
BNNSFilterParameters filter_params;
bzero(&filter_params, sizeof(filter_params));
// Create a new convolution layer filter
BNNSFilter filter = BNNSFilterCreateConvolutionLayer(&i_desc,&o_desc,&layer_params,&filter_params);
if (filter == NULL) { fprintf(stderr,"BNNSFilterCreateConvolutionLayer failed\n"); exit(1); }
// Allocate input stack
float * i_stack = (float *)calloc(i_desc.image_stride * i_desc.channels, sizeof(float));
for(int i = 0; i < 9; ++i){
i_stack[i] = i;
}
// Allocate output stack
float * o_stack = (float *)calloc(o_desc.image_stride * o_desc.channels, sizeof(float));
// Apply filter to input stack. Result is written in output stack.
int status = BNNSFilterApply(filter, i_stack, o_stack);
if (status != 0) fprintf(stderr,"BNNSFilterApply failed\n");
for (int i = 0; i<9; i++){
printf("o%d: %f\n", i, o_stack[i]);
}
// Release resources
BNNSFilterDestroy(filter);
free(i_stack);
free(o_stack);
free(weights);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment