espressif / esp-dl

Espressif deep-learning library for AIoT applications
MIT License
539 stars 117 forks source link

assertion "out->c == bias->c" failed: #15

Closed caleb221 closed 4 years ago

caleb221 commented 4 years ago

Hello! after running a convolution: net_out = dl_matrix3dff_conv_3x3(imgIn,filt_c1,bias1,10,10,PADDING_SAME);

I recieve this error:

assertion "out->c == bias->c" failed: file "/home/gongxiaochao/code/esp/esp_temp/dl_lib/components/dl_lib/dl_lib_matrix3d.c", line 37, function: dl_matrix3d_init_bias abort() was called at PC 0x400e1c43 on core 1

I'm not sure what to do because I can't see the context where the assertion is failing could you give me some pointers? thanks! -Caleb

XiaochaoGONG commented 4 years ago

Hi Caleb, while you're doing convolution with bias adding, you need to ensure that the channel of output and the channel of bias is same. So what is the dimension of each ?

caleb221 commented 4 years ago

Ok, so calling the init_bias function with these parameters works out fine but inside the convolution function it fails

dl_matrix3d_t filt_c1 = dl_matrix3d_alloc(10,1,1,1); dl_matrix3d_t bias1 = dl_matrix3d_alloc(10,1,1,1);

dl_matrix3d_t net_out= dl_matrix3d_alloc(10,1,1,1); dl_matrix3d_t out1;

filt_c1 = getConv_filter1(); //filter values from a .h file i created bias1 = getBias1(); dl_matrix3d_init_bias(net_out,bias1);//THIS WORKS (?) int width = round(img->w 12); int height = round(img->h 12);

dl_matrix3du_t *in = dl_matrix3du_alloc(1, width, height, img->c);

printf("Resizing...\n"); image_resize_linear(in->item, img->item, width, height, in->c, img->w, img->h);

    // 3x3 convolution #1
    //convert 2 3d
    dl_matrix3d_t *try = transformImage(in); // change the type from dl_matrix3du_t to dl_matrix3d_t
    dl_matrix3du_free(img); // don't need the original data, its eating up memory
     // THIS FAILS ='(
    out1= dl_matrix3dff_conv_3x3(try,filt_c1,bias1,10,10,PADDING_SAME);
caleb221 commented 4 years ago

I realize trying to run with (10,1,1,1) bias and filter passes the init_bias function, and fails with the convolution function, but when i change all the dimensions to (10,1,1,3) (in order to match the image) i cannot initialize the bias

caleb221 commented 4 years ago

dl_matrixDataStructure I think it might be in my understanding of the data structure. I have tried to draw it out in the picture above, if it is incorrect please let me know in order to translate a matrix from python (numpy) that has shape (10,3,3,3) to the dl_matrix library I do the following reshape the array to 1 dimension and copy the values (float32) into a header file. in that header file i decare a dl_matrix3d_t of shape (1,3,3,3) then set the stride to 10 (because of the original numpy shape) and set all items in the matrix as the values from the numpy array return the dl_matrix

the bias is done using the same process above --> the number of values is very different though (items is much smaller)

is this correct? --edit:

The following code completes the bias initialization but fails in the convolution (which gives the assert bias->c == out->c) //if layerCount == 1 break; dl_matrix3du_t img =(dl_matrix3du_t) pvParameter; //CONVERT TO 3d instead of 3du later dl_matrix3d_t filt_c1 = dl_matrix3d_alloc(10,3,3,3); //from python: (10,3,3,3); dl_matrix3d_t bias1 = dl_matrix3d_alloc(10,1,1,1);//(10,1,1,3)-->ALSO NOT WORKING dl_matrix3d_t out1= dl_matrix3d_alloc(10,1,1,1);//(10,1,1,3)-->ALSO NOT WORKING printf("\n\nINITIALIZED\n\n"); getConv_filter1(filt_c1);//pnetVals.h printf("\n\nINITIALIZE filter 1\n\n"); getBias1(bias1);//pnetvals.h printf("\n\nINITIALIZE BIAS 1\n\n"); //test bias init dl_matrix3d_init_bias(out1,bias1);//[assing through function but not conv.. printf("\n\n BIAS INIT! C:: %i \n\n",bias1->c); int width = round(img->w.375 );//120 int height = round(img->h .5); //120 for now.. printf("\n\nnewImg: %i X %i \n\n",width,height); for( int i=0; i<10;i++) { printf(" %f ",bias1->item[i]); } printf( " DONE!! \n\n IMG C: %i \n\n ",img->c); dl_matrix3du_t in = dl_matrix3du_alloc(1, width, height, img->c); printf("Resizing...\n in C::%i \n",in->c); image_resize_linear(in->item, img->item, width, height, in->c, img->w, img->h);

    // 3x3 convolution #1
    printf("\n\n STARTING CONVOLUTION\n\n");
    out1= dl_matrix3dff_conv_3x3(in,filt_c1,bias1,1,1,PADDING_SAME);

and output: INITIALIZED INITIALIZE filter 1 INITIALIZE BIAS 1 BIAS INIT! C:: 1
newImg: 120 X 120 -0.026064 0.015199 -0.002716 -0.040783 0.036191 -0.013219 -0.006983 -0.005121 0.027892 -0.012282 DONE!! IMG C: 3 Resizing... in C::3
STARTING CONVOLUTION assertion "out->c == bias->c" failed: file "/home/gongxiaochao/code/esp/esp_temp/dl_lib/components/dl_lib/dl_lib_matrix3d.c", line 37, function: dl_matrix3d_init_bias abort() was called at PC 0x400e1977 on core 0

XiaochaoGONG commented 4 years ago

Ah I see, in term of output for convolution, the N demiension is always 1, i.e. output: (1, w', h', c'), where c' = # of filters. That also fit to bias, i.e. bias: (1, 1, 1, c'), where c' = # of filters.

XiaochaoGONG commented 4 years ago

And dl_matrix3d_init_bias is to put bias value into the output to improve the performance.

caleb221 commented 4 years ago

that worked thank you so much!

XiaochaoGONG commented 4 years ago

Be careful that the weight data in our system is in NHWC format while in numpy is usually HWCN format. And the stride value here is W*C. When you call dl_matrix3d_alloc, the stride will be automatically set.

caleb221 commented 4 years ago

Oh! I hadn't realized that, Thanks! So, instead of simply translated=numpy.reshape(weight_array,-1) it would be translated=numpy.moveaxis(weight_array,1,3) copy_out=numpy.reshape(translated,-1) and copy_out goes to the header file

caleb221 commented 4 years ago

oops, taking a second look i was wrong with initial translations.. should be t=np.moveaxis(c,3,0) # translate from (H,W,C,N) caffe/numpy to (N,H,W,C) EPS lib

out: ( n,w,c,h )

        #next -> c,h (2,3)
        t1 = np.moveaxis(t,2,3)
        #out: (n,w,h,c)
        #next --> 1,2
        t = np.moveaxis(t1,1,2)
        # out --> n,h,w,c woohoo! translated

I think...