FC->RELU->FC sequence issue in Deep Learning
Hi,
As you already know I am trying to implement the Alexnet . I am almost at the end. But the Sequence FC->RELU->FC is yielding the wrong results for Last FC layer. Dimensions are mentioned below.
1st FC Layer output which is input to Relu : LayerInputTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)4096), 1, DataType::F32));
Relu Layer:
input : Passing the above FCLayer tensor output as input to this layer
output: LayerTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)1,(long unsigned int)1,(long unsigned int)4096), 1, DataType::F32)); //(for generic approach width,height are placed to 1)
FCLayer: input :Passing the above Relu Layer tensor output as input to this layer output: FcTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)4096), 1, DataType::F32));
With this configurations kept in sequential we are facing the functionlity issues. Additional Info: After Relu Configure is done the output tensor changed to 4096 * 4096 after padding.Not sure why padding increased so huge.
Below is the standalone code for you reference: `main() { // The src tensor which will contain the input image Tensor LayerInputTensor;
// The weights and biases tensors should be initialized with the values inferred with the training
Tensor LayerTensor;
Tensor FcTensor;
NEActivationLayer ActLayer;
NEFullyConnectedLayer FcLayer;
int i;
float *input_buffer=(float*)malloc(4096*sizeof(float));
/* [Initialize tensors] */
FILE *fp;
fp = fopen("relu_inp.bin","rb");
fread(input_buffer,4,4096,fp);
fclose(fp);
LayerInputTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)4096), 1, DataType::F32));
LayerTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)1,(long unsigned int)1,(long unsigned int)4096), 1, DataType::F32));
printf("*********Relu input :: size %d start %d\n",LayerInputTensor.info()->total_size(),LayerInputTensor.info()->offset_element_in_bytes(Coordinates(0, 0)));
printf("*********Relu output :: size %d start %d\n",LayerTensor.info()->total_size(),LayerTensor.info()->offset_element_in_bytes(Coordinates(0, 0)));
ActLayer.configure(&LayerInputTensor, &LayerTensor,ActivationLayerInfo(ActivationLayerInfo::ActivationFunction::RELU));
printf("#########Relu input :: size %d start %d\n",LayerInputTensor.info()->total_size(),LayerInputTensor.info()->offset_element_in_bytes(Coordinates(0, 0)));
printf("#########Relu output :: size %d start %d\n",LayerTensor.info()->total_size(),LayerTensor.info()->offset_element_in_bytes(Coordinates(0, 0)));
FcLayerWgtTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)4096,(long unsigned int)4096), 1, DataType::F32));
FcLayerBiasTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)4096), 1, DataType::F32));
FcTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)4096), 1, DataType::F32));
printf("*********FC input :: size %d start %d\n",LayerTensor.info()->total_size(),LayerTensor.info()->offset_element_in_bytes(Coordinates(0, 0)));
printf("*********FC output :: size %d start %d\n",FcTensor.info()->total_size(),FcTensor.info()->offset_element_in_bytes(Coordinates(0, 0)));
FcLayer.configure(&LayerTensor, &FcLayerWgtTensor, &FcLayerBiasTensor, &FcTensor);
printf("#########FC input :: size %d start %d\n",LayerTensor.info()->total_size(),LayerTensor.info()->offset_element_in_bytes(Coordinates(0, 0)));
printf("#########FC output :: size %d start %d\n",FcTensor.info()->total_size(),FcTensor.info()->offset_element_in_bytes(Coordinates(0, 0)));
/* Allocating the memories to Tensors */
LayerInputTensor.allocator()->allocate();
LayerTensor.allocator()->allocate();
FcTensor.allocator()->allocate();
FcLayerWgtTensor.allocator()->allocate();
FcLayerBiasTensor.allocator()->allocate();
std::copy_n((unsigned char*)input_buffer,LayerInputTensor.info()->total_size(),(unsigned char*)LayerInputTensor.buffer());
loadWeights("fc_w");
loadBias("fc_b");
printf("Loaded values\n");
ActLayer.run();
FcLayer.run();
fp = fopen("FcLayer.txt","w");
for(i=0;i<FcTensor.info()->total_size();i++)
{
fprintf(fp,"%f\n",*(reinterpret_cast<float*>(FcTensor.ptr_to_element(Coordinates(i,0)))));
}
fclose(fp);
free(input_buffer);
}`
Thanks, G. Praveen.
Hello @uu-praveeng , I think that the shapes you have specified are not correct. I would expect the shape of the input to be the same with the shape of the output for the ActivationLayer but you have specified a different shape.
LayerInputTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)4096), 1, DataType::F32));
LayerTensor.allocator()->init(TensorInfo(TensorShape((long unsigned int)1,(long unsigned int)1,(long unsigned int)4096), 1, DataType::F32));
Can you try:
LayerInputTensor.allocator()->init(TensorInfo(TensorShape(4096U), 1, DataType::F32));
LayerTensor.allocator()->init(TensorInfo(TensorShape(4096U), 1, DataType::F32));
If Relu's shape is considered as single dimension it works. But again Relu's shape cant be single dimension in all the cases hence I mentioned " //(for generic approach width,height are placed to 1)" . Because in some cases Relu shape can be coming from Convolution layer which as w,h,ifm.
Here my intention is to write a generic Relu init function. So may be if there could be internal condition inside Relu Configure to check if the width and height as 1,1(when input shape 1,1,4096) .If it is so then we can assign the feature maps(4096) to width ,feature maps to 1 and continue.
With this ARM Compute Library for DL would be much more generic and it would follow the w,h,ifm,batchsize format that you are already maintaining.
Note: The issue occurs only for the layers under FcLayer like Norm,Relu.So may be you can add it to Norm to..
Thanks, G. Praveen.
Hello @uu-praveeng ,
I am not quite sure I fully understand your argument. The init function is bound to a Tensor so it has nothing to do with the ActivationLayer. What the ActivationLayer does is to take a tensor, apply a function to each one of the elements and store the result to the output tensor. If the shapes of the input and the output are not the same it will fail as it does in your example. If the input for example comes from a ConvolutionLayer then if you provide an output with the same shape to the ActivationLayer it will produce the correct results. We cannot make the assumptions you suggest in the configure stage of the ActivationLayer. Same holds for the NormalizationLayer.
We are thinking of adding a separate configure to make the ActivationLayer operate in-place meaning that it will overwrite the tensor you are passing as an input instead of requiring a separate output tensor for the result. This should make thinks faster and simplify your design.
If your problem is resolved, can you please close this issue?
Thanks for you help, George
Hi @GeorgeARM ,
I have few questions regarding these kind of scenarios mentioned in this issue. You mentioned "ACL would expect the shape of the input to be the same with the shape of the output for the ActivationLayer" in above comments.
-
But i have observed that in non-activation layers(layers which does not use NEActivationLayer) such as Norm(Cross channel/Batch norm), softmax layers has the similar restriction and we are creating the Tensor shape for these specific layers as previous layer tensor shape and worked fine for me. Does it mean along with activation kernels norm, softmax kernels also has this restriction?
-
Also my understanding is that FC and LSTM layers always expects output ARM Tensor shape as the 2D and Convolution, Pooling layers always expects output ARM Tensor shape as the 4D. Could you clarify on my understanding?
Could please point me the any documentation saying this restriction so that it would helpful for me to create bug free kernels with correct output tensor shape.
Thanks, Hari