ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

Deep Neural Networks on ComputeLibrary

Open mo-morikawa opened this issue 9 years ago • 14 comments

I found several data structures that are components of deep neural networks, in the library. Examples are:

CLConvolutionLayer( )
CLActivationLayer()
CLPoolingLayer()

However, I could not find out a way to either combine these layers to form a network, or to load a previously saved network (architecture and/or weights) to the memory. Is this supported at the moment? If so, is there any sample code that I can use to get started.

Thanks in advance!

mo-morikawa avatar Mar 29 '17 01:03 mo-morikawa

Hi,

We're currently working on more documented examples which will be published in the coming weeks as blog posts. In the meantime here is some pseudo-code to give you an idea of how to do it, hopefully it will be enough to get you started:

    // The src tensor which will contain the input image
    Tensor src;

    // The weights and biases tensors should be initialized with the values inferred with the training
    Tensor weights0;
    Tensor weights1;
    Tensor weights2;
    Tensor biases0;
    Tensor biases1;
    Tensor biases2;

    Tensor out_conv0;
    Tensor out_conv1;
    Tensor out_act0;
    Tensor out_act1;
    Tensor out_act2;
    Tensor out_pool0;
    Tensor out_pool1;
    Tensor out_fc0;
    Tensor out_softmax;

    NEConvolutionLayer    conv0;
    NEConvolutionLayer    conv1;
    NEPoolingLayer        pool0;
    NEPoolingLayer        pool1;
    NEFullyConnectedLayer fc0;
    NEActivationLayer     act0;
    NEActivationLayer     act1;
    NEActivationLayer     act2;
    NESoftmaxLayer        softmax;

    /* [Initialize tensors] */

    // Initialize src tensor
    constexpr unsigned int width_src_image  = 32;
    constexpr unsigned int height_src_image = 32;
    constexpr unsigned int ifm_src_img      = 1;

    const TensorShape src_shape(width_src_image, height_src_image, ifm_src_img);
    src.allocator()->init(TensorInfo(src_shape, 1, DataType::F32));

    // Initialize tensors of conv0
    constexpr unsigned int kernel_x_conv0 = 5;
    constexpr unsigned int kernel_y_conv0 = 5;
    constexpr unsigned int ofm_conv0      = 8;

    const TensorShape weights_shape_conv0(kernel_x_conv0, kernel_y_conv0, src_shape.z(), ofm_conv0);
    const TensorShape biases_shape_conv0(weights_shape_conv0[3]);
    const TensorShape out_shape_conv0(src_shape.x(), src_shape.y(), weights_shape_conv0[3]);

    weights0.allocator()->init(TensorInfo(weights_shape_conv0, 1, DataType::F32));
    biases0.allocator()->init(TensorInfo(biases_shape_conv0, 1, DataType::F32));
    out_conv0.allocator()->init(TensorInfo(out_shape_conv0, 1, DataType::F32));

    // Initialize tensor of act0
    out_act0.allocator()->init(TensorInfo(out_shape_conv0, 1, DataType::F32));

    // Initialize tensor of pool0
    TensorShape out_shape_pool0 = out_shape_conv0;
    out_shape_pool0.set(0, out_shape_pool0.x() / 2);
    out_shape_pool0.set(1, out_shape_pool0.y() / 2);
    out_pool0.allocator()->init(TensorInfo(out_shape_pool0, 1, DataType::F32));

    // Initialize tensors of conv1
    constexpr unsigned int kernel_x_conv1 = 3;
    constexpr unsigned int kernel_y_conv1 = 3;
    constexpr unsigned int ofm_conv1      = 16;

    const TensorShape weights_shape_conv1(kernel_x_conv1, kernel_y_conv1, out_shape_pool0.z(), ofm_conv1);

    const TensorShape biases_shape_conv1(weights_shape_conv1[3]);
    const TensorShape out_shape_conv1(out_shape_pool0.x(), out_shape_pool0.y(), weights_shape_conv1[3]);

    weights1.allocator()->init(TensorInfo(weights_shape_conv1, 1, DataType::F32));
    biases1.allocator()->init(TensorInfo(biases_shape_conv1, 1, DataType::F32));
    out_conv1.allocator()->init(TensorInfo(out_shape_conv1, 1, DataType::F32));

    // Initialize tensor of act1
    out_act1.allocator()->init(TensorInfo(out_shape_conv1, 1, DataType::F32));

    // Initialize tensor of pool1
    TensorShape out_shape_pool1 = out_shape_conv1;
    out_shape_pool1.set(0, out_shape_pool1.x() / 2);
    out_shape_pool1.set(1, out_shape_pool1.y() / 2);
    out_pool1.allocator()->init(TensorInfo(out_shape_pool1, 1, DataType::F32));

    // Initialize tensor of fc0
    constexpr unsigned int num_labels = 128;

    const TensorShape weights_shape_fc0(out_shape_pool1.x() * out_shape_pool1.y() * out_shape_pool1.z(), num_labels);
    const TensorShape biases_shape_fc0(num_labels);
    const TensorShape out_shape_fc0(num_labels);

    weights2.allocator()->init(TensorInfo(weights_shape_fc0, 1, DataType::F32));
    biases2.allocator()->init(TensorInfo(biases_shape_fc0, 1, DataType::F32));
    out_fc0.allocator()->init(TensorInfo(out_shape_fc0, 1, DataType::F32));

    // Initialize tensor of act2
    out_act2.allocator()->init(TensorInfo(out_shape_fc0, 1, DataType::F32));

    // Initialize tensor of softmax
    const TensorShape out_shape_softmax(out_shape_fc0.x());
    out_softmax.allocator()->init(TensorInfo(out_shape_softmax, 1, DataType::F32));

    /* -----------------------End: [Initialize tensors] */

    /* [Configure functions] */

    // in:32x32x1: 5x5 convolution, 8 output features maps (OFM)
    conv0.configure(&src, &weights0, &biases0, &out_conv0, PadStrideInfo());

    // in:32x32x8, out:32x32x8, Activation function: relu
    act0.configure(&out_conv0, &out_act0, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));

    // in:32x32x8, out:16x16x8 (2x2 pooling), Pool type function: Max
    pool0.configure(&out_act0, &out_pool0, PoolingLayerInfo(PoolingType::MAX, 2));

    // in:16x16x8: 3x3 convolution, 16 output features maps (OFM)
    conv1.configure(&out_pool0, &weights1, &biases1, &out_conv1, PadStrideInfo());

    // in:16x16x16, out:16x16x16, Activation function: relu
    act1.configure(&out_conv1, &out_act1, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));

    // in:16x16x16, out:8x8x16 (2x2 pooling), Pool type function: Average
    pool1.configure(&out_act1, &out_pool1, PoolingLayerInfo(PoolingType::AVG, 2));

    // in:8x8x16, out:128
    fc0.configure(&out_pool1, &weights2, &biases2, &out_fc0);

    // in:128, out:128, Activation function: relu
    act2.configure(&out_fc0, &out_act2, ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));

    // in:128, out:128
    softmax.configure(&out_act2, &out_softmax);

    /* -----------------------End: [Configure functions] */

    /* [Allocate tensors] */

    // Now that the padding requirements are known we can allocate the images:
    src.allocator()->allocate();
    weights0.allocator()->allocate();
    weights1.allocator()->allocate();
    weights2.allocator()->allocate();
    biases0.allocator()->allocate();
    biases1.allocator()->allocate();
    biases2.allocator()->allocate();
    out_conv0.allocator()->allocate();
    out_conv1.allocator()->allocate();
    out_act0.allocator()->allocate();
    out_act1.allocator()->allocate();
    out_act2.allocator()->allocate();
    out_pool0.allocator()->allocate();
    out_pool1.allocator()->allocate();
    out_fc0.allocator()->allocate();
    out_softmax.allocator()->allocate();

    /* -----------------------End: [Allocate tensors] */

    /* [Initialize weights and biases tensors] */

    // Once the tensors have been allocated, the src, weights and biases tensors can be initialized
   **// TODO Add code here to load the weights, biases and src content.**
    // ...

    /* -----------------------[Initialize weights and biases tensors] */

    /* [Execute the functions] */

    conv0.run();
    act0.run();
    pool0.run();
    conv1.run();
    act1.run();
    pool1.run();
    fc0.run();
    act2.run();
    softmax.run();

    /* -----------------------End: [Execute the functions] */

AnthonyBarbier avatar Apr 03 '17 16:04 AnthonyBarbier

Thanks a lot, this code helped me to get started. However, I got this error when configuring the first pooling layer (pool0):

Line: pool0.configure(&out_act0, &out_pool0, PoolingLayerInfo(PoolingType::MAX, 2));

Error: ERROR in void arm_compute::NEPoolingLayerKernel::configure(const arm_compute::ITensor *, arm_compute::ITensor *, const arm_compute::PoolingLayerInfo &) src/core/NEON/kernels/NEPoolingLayerKernel.cpp:93: (output->info()->dimension(0) != pooled_w) || (output->info()->dimension(1) != pooled_h) Unknown error 214

It appears that the calculation of the pool size is correct. I get the same error for pool1, and when configuring the fully connected layer I get a slightly different error:

ERROR in void arm_compute::NEFullyConnectedLayer::configure(arm_compute::ITensor *, arm_compute::ITensor *, const arm_compute::ITensor *, arm_compute::ITensor *) src/runtime/NEON/functions/NEFullyConnectedLayer.cpp:41: (weights->info()->num_dimensions() == 2) && (input->info()->dimension(0) != weights->info()->dimension(0)) Unknown error 214

I will also check the source files myself to see if I can sort this out, but any hints will be of great help. Looking forward to the blog posts, too!

mo-morikawa avatar Apr 04 '17 07:04 mo-morikawa

Hello @mo-morikawa ,

I assume you are trying to run the above example, as it seems there are some issues with the code above concerning the shapes of some of the layers. For example, the output shape of the conv0 layer is specified as:

const TensorShape out_shape_conv0(src_shape.x(), src_shape.y(), weights_shape_conv0[3]);

which essentially specifies the output of the conv0 layer to [32, 32, 8]. But the configure stage defaults to the default padding and stride parameters values [pad_x:0, pad_y:0, stride_x:1, stride_y:1, round_mode:FLOOR] as it uses the default constructor with no arguments

    conv0.configure(&src, &weights0, &biases0, &out_conv0, PadStrideInfo());

If we take into account this, the output tensor of the conv0 layer should have been [28, 28, 8] as the calculation is floor(((width + 2 * pad_x - conv_size) / stride_x) + 1)

Similar is the issue with the pooling layer. The output of the pooling layer is specified as:

    out_shape_pool0.set(0, out_shape_pool0.x() / 2);
    out_shape_pool0.set(1, out_shape_pool0.y() / 2);

which means that it expects the output shape to be the input shape subsampled by 2 in both x and y, something that is not specified in the pooling configure step. So the configure step shoud have been:

pool0.configure(&out_act0, &out_pool0, PoolingLayerInfo(PoolingType::MAX, 2, PadStrideInfo(2 /* stride_x */, 2 /* stride_y */));

We are preparing some blog posts as an intro to creating networks using ComputeLibrary. Moreover, we are planning to add some helper functions that will help you calculate the output shape given the input shape and the parameters required from the layer.

Concerning the issue you see in FullyConnectedLayer is something that we have identified and resolved internally. It will be part of the next maintenance patch in the next days.

Thanks for all your help

GeorgeARM avatar Apr 05 '17 10:04 GeorgeARM

@GeorgeARM I encountered the same issue. For the FullyConnectedLayer error, how to reshape or flatten the last pooling output out_pool1 from 8x8x16 to 1024? Searched in the code, no such function is found.

llhe avatar Apr 07 '17 06:04 llhe

@llhe for now you can probably go to a lower level from the function level and call an im2col kernel on the output of your pooling layer (note: not tested).

NEIm2ColKernel im2col_kernel // Create an Im2ColKernel
Tensor im2col_output // Create output tensor
...
// Initialize output tensor for im2col
TensorShape shape_im2col;
shape_im2col.set(0, weights->info()->dimension(0)); // Fully connected layer will transpose the weights
shape_im2col.set(1, 1); // In case you have no batches otherwise you should put your batches here
im2col_output.allocator()->init(TensorInfo(shape_im2col, 1, input->info()->data_type()));
// Configure im2col kernel
im2col_kernel.configure(input, &im2col_output, std::make_pair(1, 1), PadStrideInfo(1, 1, 0, 0), false);

...
// Allocate
im2col_output.allocator()->allocate();
...
// Run
NEScheduler::get().schedule(&im2col_kernel);

This should hopefully flatten your pooling layer output. We have this fixed internally and the reshape will be transparent to you. Hopefully, it will get committed in the next few days. Please keep us posted in case you need more help.

Thanks

GeorgeARM avatar Apr 07 '17 08:04 GeorgeARM

@GeorgeARM Thanks for the quick response. I'll have a try with this solution.

llhe avatar Apr 07 '17 10:04 llhe

@GeorgeARM Hi, George. How can I set values for a Tensor? In caffe, we can get the mutable_cpu_data from a blob which is indeed a one dimensional array so that I can fill in the values like a breeze. I wonder how can I set values for a Tensor in Arm Lib. Could you please write an example?

yangyanzhan avatar Apr 11 '17 12:04 yangyanzhan

Hi, You can have a look at Moritz' example here Or in the library's test_helpers

AnthonyBarbier avatar Apr 11 '17 12:04 AnthonyBarbier

Thank you. Now the tensor is successfully set. It works very well.

yangyanzhan avatar Apr 12 '17 07:04 yangyanzhan

I assume the mentioned issues have been fixed/clarified. Please create new issues if there are any outstanding problems.

mpflanzer avatar May 02 '17 08:05 mpflanzer

Can someone point me towards the blog posts or further documentation @AnthonyARM mentioned! Thanks

sagarkar10 avatar May 31 '17 09:05 sagarkar10

@AnthonyARM I am confused with the allocator function's channel argument. weights0.allocator()->init(TensorInfo(weights_shape_conv0, 1, DataType::F32)); When I input a 3 channel image the channel will set to 3, what will be for the weights1.allocator()

In this example the weights1 should have the no.of filters of conv0 as channel is not it?

sagarkar10 avatar Jun 29 '17 08:06 sagarkar10

Number of channels is for interleaved channels only (For example: RGB images) If your data is planar then number of channel should be 1 and you should use the 3rd dimension of your tensor to allocate the different planes. As a result number of channels must be always 1 for all the CNN functions, the only functions making use of the number of channels are the computer vision ones dealing with colours and HOG / SVM.

AnthonyBarbier avatar Jun 30 '17 08:06 AnthonyBarbier

@AnthonyARM thanks for the explanation. I got it

sagarkar10 avatar Jul 02 '17 12:07 sagarkar10