ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

mismatch in the output of fullyconnected layer(graph API)

Open ved27 opened this issue 8 years ago • 9 comments

Hi,

Compute library- 18.05 arm7a Platform : linux

Output of 'strings libarm_compute.so | grep arm_compute_version':

I implemented a sample network based on mnist network, using graph API. .npy weights are extracted from tensorflow weight file. The outputs of convolution and pool layers are matching the reference output of tensorflow. But the output of dense layer (FullyConnected layer) following the pool layer and convolution is not matching the referece output.

input (after pool layer) shape of FC : 10 x 8 x 64 x 1 ( WHCN ) output of FC : 1024 channels

shape of weight : ( 1024 x 5120) ( of .npy file) shape of bias : 1024

PS : Relu layer follows this FC layer

Is there any issue with the input ? Is there any issue in the mutators ? Please help with other possible causes leading to this issue

Awaiting your reply,

ved27 avatar Oct 13 '18 10:10 ved27

Hello @ved27,

Maybe the layout of your fc weights is different as it might have been trained differently, and that seems to be the case if you extracted them from tensorflow. Can you set NWHC the layout of weights_trained_layout in the FullyConnectedLayerInfo that you pass to the FC layer and check if this will work?

GeorgeARM avatar Oct 15 '18 10:10 GeorgeARM

Hi,

FullyConnectedLayerInfo - I can't find this parameter in ComputeLibrary 18.05 ;

ved27 avatar Oct 15 '18 12:10 ved27

Hello @ved27, its part of the 18.08 release. If you need/have to use 18.05 release you have to manually interleave your fully connected weights.

GeorgeARM avatar Oct 15 '18 13:10 GeorgeARM

Hi @GeorgeARM ,

Earlier I extracted FC weights (1024 x 5120) from .caffemodel, and used those .npy files to run on ACL - 18.05. It gave correct outputs.

In our case, the weights are extracted from tensorflow and I transposed the weights too (from 5120 x 1024 to 1024 x 5120 ) to run on ACL and wrong output is being dumped .

What's the difference between tf and caffe ?

ved27 avatar Oct 15 '18 14:10 ved27

You don't have to transpose it but to actually interleave it. This is because the FC layer connections are not in the same order between Caffe and TF. You can have a look here to see what we essentially do.

GeorgeARM avatar Oct 15 '18 15:10 GeorgeARM

Hi @GeorgeARM

Referring to the suggestion of you above "NWHC the layout of weights_trained_layout in the FullyConnectedLayerInfo" NWHC - is this layout existing ? or you meant NHWC ?

Also, in https://github.com/ARM-software/ComputeLibrary/blob/master/src/runtime/CL/functions/CLFullyConnectedLayer.cpp LINE:204

if(_is_fc_after_conv && (input->info()->data_layout() != fc_info.weights_trained_layout))
{
    // Convert weights
    _convert_weights.configure(weights_to_use,
                               &_converted_weights_output,
                               input->info()->tensor_shape(),
                               fc_info.weights_trained_layout);

    weights_to_use         = &_converted_weights_output;
    _are_weights_converted = false;
}

So, instead of interleaving the weights, can I change the DataLayout (to NHWC) of input and run , using the same extracted weights - so that "if condition" in above code is not triggered ?

ved27 avatar Oct 16 '18 05:10 ved27

Hi ,

Apart from this, I tried the same FC layer with input size 1024x1 (NCHW layout) and output is 10x1 for the same .

I dumped the input of the layer from tensor flow ( shape of input tensor is 1x1024) , extrated weights and passed them as inputs to ACL FC layer. The output has shape 10 x 1 (NCHW layout) . But it does not match the output dumped with tensorflow.

ved27 avatar Oct 18 '18 14:10 ved27

Hello @ved27,

Maybe the layout of your fc weights is different as it might have been trained differently, and that seems to be the case if you extracted them from tensorflow. Can you set NWHC the layout of weights_trained_layout in the FullyConnectedLayerInfo that you pass to the FC layer and check if this will work?

This helped solving the same issue as reported by @ved27. We have dimension mismatch when we used our model from tensorflow on ACL. Using FullyConnectedLayerInfo solved our problem. However, we had to still transpose the weights when extracting the weight/bias files (as discussed in #517)

Thanks @GeorgeARM for your inputs.

Regards, San

santo4ul avatar Nov 18 '18 18:11 santo4ul

Thanks @santo4ul for sharing how you solved the problem in the end

ManubARM avatar Feb 01 '19 11:02 ManubARM