ComputeLibrary icon indicating copy to clipboard operation
ComputeLibrary copied to clipboard

yolov3 implementation is not compliant with the original implementation

Open ohuet74 opened this issue 7 years ago • 7 comments

Platform: HiKey970 Operating System: Linux

Problem description: implementation from https://pjreddie.com/darknet/yolo/ Just before the 3 yolo output layer the last convolution layer must add a bias. This operation is not performed in your implementation.

The original convolution implementation for reference

void forward_convolutional_layer(convolutional_layer l, network net) { int i, j;

fill_cpu(l.outputs*l.batch, 0, l.output, 1);

if(l.xnor){
    binarize_weights(l.weights, l.n, l.c/l.groups*l.size*l.size, l.binary_weights);
    swap_binary(&l);
    binarize_cpu(net.input, l.c*l.h*l.w*l.batch, l.binary_input);
    net.input = l.binary_input;
}

int m = l.n/l.groups;
int k = l.size*l.size*l.c/l.groups;
int n = l.out_w*l.out_h;
for(i = 0; i < l.batch; ++i){
    for(j = 0; j < l.groups; ++j){
        float *a = l.weights + j*l.nweights/l.groups;
        float *b = net.workspace;
        float *c = l.output + (i*l.groups + j)*n*m;
        float *im =  net.input + (i*l.groups + j)*l.c/l.groups*l.h*l.w;

        if (l.size == 1) {
            b = im;
        } else {
            im2col_cpu(im, l.c/l.groups, l.h, l.w, l.size, l.stride, l.pad, b);
        }
        gemm(0,0,m,n,k,1,a,k,b,n,1,c,n);
    }
}

if(l.batch_normalize){
    forward_batchnorm_layer(l, net);
} else {
    **add_bias(l.output, l.biases, l.batch, l.n, l.out_h*l.out_w);** //-->Missing operation in your implementation
}

activate_array(l.output, l.outputs*l.batch, l.activation);

ohuet74 avatar Jan 07 '19 10:01 ohuet74

Hi @ohuet74

I had a look at the code and it seems that we add biases to the last convolution layer: << ConvolutionLayer( 1U, 1U, 255U, get_weights_accessor(data_path, "/cnn_data/yolov3_model/conv2d_75_w.npy", weights_layout), get_weights_accessor(data_path, "/cnn_data/yolov3_model/conv2d_75_b.npy", weights_layout), PadStrideInfo(1, 1, 0, 0)) .set_name("conv2d_75")

Do you get wrong results when you run our implementation?

mpekatsoula avatar Jan 07 '19 11:01 mpekatsoula

Hello Yes i get a wrong result. I have write a small tools for extracting the weight file in npy format from the original darknet implementation. I also extract each layer result. I have compare each convolution and activation layer result with the original implementation and the result match perfectly . But i get an error when i compare the yolo layer tensor input with the original one (more than 0.1 of difference).

My understanding is (when i read the darknet source code) that convolution layer take 2 weight. The bias is an additional weight use by batch normalization or alone when convolution doesn't have batch normalization. I found that the last add_bias is missing in your implementation.

As quick and dirty fix i have extract an additional weight call bias for each yolo layer and use a simple add_bias function then the result become correct.

Original Yolov3 implementation is train for object detection with coco then the TopNPredictionsAccessor::access_tensor is not correct for me. I going to rewrite one who extract the detected objects position and type.

ohuet74 avatar Jan 07 '19 12:01 ohuet74

The prediction extraction with the correct implementation is truck:97% Box (x=0.758519,y=0.218021,w=0.285125,h=0.144402) truck:92% Box (x=0.774683,y=0.219866,w=0.276264,h=0.143689) truck:78% Box (x=0.757439,y=0.233657,w=0.293294,h=0.137039) bicycle:92% Box (x=0.445848,y=0.451493,w=0.609337,h=0.516069) bicycle:92% Box (x=0.448333,y=0.449694,w=0.604835,h=0.62193) bicycle:91% Box (x=0.475798,y=0.450058,w=0.542535,h=0.550398) bicycle:50% Box (x=0.473194,y=0.445212,w=0.635918,h=0.592701) bicycle:99% Box (x=0.447726,y=0.483844,w=0.601921,h=0.531774) bicycle:98% Box (x=0.441389,y=0.496847,w=0.624244,h=0.622168) bicycle:98% Box (x=0.472474,y=0.479111,w=0.544858,h=0.544178) bicycle:63% Box (x=0.469056,y=0.487884,w=0.626759,h=0.618132) dog:86% Box (x=0.281363,y=0.593043,w=0.277694,h=0.617882) dog:99% Box (x=0.288751,y=0.665716,w=0.262516,h=0.547702) dog:96% Box (x=0.314077,y=0.656394,w=0.279299,h=0.561053) dog:96% Box (x=0.283241,y=0.698627,w=0.263814,h=0.540391)

The Prediction without the bias layer: person:65% Box (x=0.783768,y=0.190414,w=0.0243315,h=0.0307538) person:57% Box (x=0.7843,y=0.194676,w=0.0279963,h=0.0334214) person:57% Box (x=0.793019,y=0.195659,w=0.0285613,h=0.0314361) bicycle:99% Box (x=0.445878,y=0.45134,w=0.617024,h=0.510678) bicycle:98% Box (x=0.449456,y=0.45024,w=0.655357,h=0.657625) bicycle:98% Box (x=0.475825,y=0.449888,w=0.549379,h=0.544649) bicycle:89% Box (x=0.474285,y=0.445916,w=0.689036,h=0.626719) bicycle:99% Box (x=0.447753,y=0.483571,w=0.609514,h=0.526219) bicycle:99% Box (x=0.44293,y=0.49791,w=0.676387,h=0.657878) bicycle:99% Box (x=0.472497,y=0.478877,w=0.551731,h=0.538494) bicycle:93% Box (x=0.469809,y=0.488855,w=0.679112,h=0.65361) dog:97% Box (x=0.281404,y=0.592767,w=0.281197,h=0.611428) bicycle:70% Box (x=0.439537,y=0.553935,w=0.703516,h=0.769571) dog:99% Box (x=0.288785,y=0.665413,w=0.265828,h=0.541981) dog:99% Box (x=0.314091,y=0.656062,w=0.282822,h=0.555192) dog:97% Box (x=0.283281,y=0.698527,w=0.267142,h=0.534747)

The truck become a person and the box position change.

ohuet74 avatar Jan 08 '19 09:01 ohuet74

Hi @ohuet74

Sorry for the late reply. You said that in our last convolution step we don't add the bias but here it's clear that we do. The second weights input (conv2d_75_b.npy) in ConvolutionLayer is the bias

mpekatsoula avatar Jan 23 '19 14:01 mpekatsoula

hello mpekatsoula Yes you have right. I have see this bias parameter but i have also check the result of the last convolution layer before the yolo layer and the result is different of the original implementation. In Darknet add bias function is the following: void add_bias(float *output, float biases, int batch, int n, int size) { int i,j,b; for(b = 0; b < batch; ++b){ for(i = 0; i < n; ++i){ for(j = 0; j < size; ++j){ output[(bn + i)*size + j] += biases[i]; } } } } And In the ComputLibrary implementation the add bias result is different. BR Olivier

ohuet74 avatar Jan 28 '19 16:01 ohuet74

Hi @ohuet74

I had a look at the output generated by ACL and darknet and it looks correct. The difference is < 0.0001 i.e. : ACL: -0.994779 Darknet: -0.994769

It's weird that only the last layer is wrong in your case. How do you run your test? Just to verify, are you using ./examples/graph_yolov3 or you have implemented your own test? Also, if I remember correctly darknet resizes the input to 608x608 but we don't perform this step in our example.

mpekatsoula avatar Jan 29 '19 12:01 mpekatsoula

Closing for inactivity, feel free to reopen otherwise

ManubARM avatar Feb 18 '19 12:02 ManubARM