digitalbrain79 / pyyolo

Simple python wrapper for YOLO.
126 stars 65 forks source link

segmentation fault when using pyyolo.detect #3

Closed MSutt closed 7 years ago

MSutt commented 7 years ago

Hi, When I try to use the pyyolo.detect function with my own yolo trained model, it generate a segmentation fault.

I copied the example.py to make my own with those changes :

datacfg = '/home/ubuntu/mydata.data'
cfgfile = '/home/ubuntu/mymodel.cfg'
weightfile = '/home/ubuntu/mymodel_3700.weights'
filename = '/home/ubuntu/myimage.jpg'
thresh = 0.5

then when i run it i have this error :

layer     filters    size              input                output
    0 conv     32  3 x 3 / 1  1024 x1024 x   3   ->  1024 x1024 x  32
    1 max          2 x 2 / 2  1024 x1024 x  32   ->   512 x 512 x  32
    2 conv     64  3 x 3 / 1   512 x 512 x  32   ->   512 x 512 x  64
    3 max          2 x 2 / 2   512 x 512 x  64   ->   256 x 256 x  64
    4 conv    128  3 x 3 / 1   256 x 256 x  64   ->   256 x 256 x 128
    5 conv     64  1 x 1 / 1   256 x 256 x 128   ->   256 x 256 x  64
    6 conv    128  3 x 3 / 1   256 x 256 x  64   ->   256 x 256 x 128
    7 max          2 x 2 / 2   256 x 256 x 128   ->   128 x 128 x 128
    8 conv    256  3 x 3 / 1   128 x 128 x 128   ->   128 x 128 x 256
    9 conv    128  1 x 1 / 1   128 x 128 x 256   ->   128 x 128 x 128
   10 conv    256  3 x 3 / 1   128 x 128 x 128   ->   128 x 128 x 256
   11 max          2 x 2 / 2   128 x 128 x 256   ->    64 x  64 x 256
   12 conv    512  3 x 3 / 1    64 x  64 x 256   ->    64 x  64 x 512
   13 conv    256  1 x 1 / 1    64 x  64 x 512   ->    64 x  64 x 256
   14 conv    512  3 x 3 / 1    64 x  64 x 256   ->    64 x  64 x 512
   15 conv    256  1 x 1 / 1    64 x  64 x 512   ->    64 x  64 x 256
   16 conv    512  3 x 3 / 1    64 x  64 x 256   ->    64 x  64 x 512
   17 max          2 x 2 / 2    64 x  64 x 512   ->    32 x  32 x 512
   18 conv   1024  3 x 3 / 1    32 x  32 x 512   ->    32 x  32 x1024
   19 conv    512  1 x 1 / 1    32 x  32 x1024   ->    32 x  32 x 512
   20 conv   1024  3 x 3 / 1    32 x  32 x 512   ->    32 x  32 x1024
   21 conv    512  1 x 1 / 1    32 x  32 x1024   ->    32 x  32 x 512
   22 conv   1024  3 x 3 / 1    32 x  32 x 512   ->    32 x  32 x1024
   23 conv   1024  3 x 3 / 1    32 x  32 x1024   ->    32 x  32 x1024
   24 conv   1024  3 x 3 / 1    32 x  32 x1024   ->    32 x  32 x1024
   25 route  16
   26 reorg              / 2    64 x  64 x 512   ->    32 x  32 x2048
   27 route  26 24
   28 conv   1024  3 x 3 / 1    32 x  32 x3072   ->    32 x  32 x1024
   29 conv     30  1 x 1 / 1    32 x  32 x1024   ->    32 x  32 x  30
   30 detection
Loading weights from /home/ubuntu/mymodel_3700.weights...Done!
----- test original C using a file
Cannot load image "/home/ubuntu/myimage.jpg"
STB Reason: unknown image type

I found that was coming from the line ret_val = cv2.imwrite(filename,img) which was removing my image so i removed it since i don't have any webcam.

Then after getting my image back, i run again and i have the segmentation fault error :

Loading weights from /home/ubuntu/mymodel_3700.weights...Done!
----- test original C using a file
/home/ubuntu/deep-learning/myimage.jpg: Predicted in 0.251751 seconds.
{'bottom': 1218, 'top': 953, 'right': 568, 'class': 'mylabel', 'left': 357}
{'bottom': 1213, 'top': 955, 'right': 788, 'class': 'mylabel', 'left': 578}
{'bottom': 1217, 'top': 956, 'right': 1012, 'class': 'mylabel', 'left': 799}
{'bottom': 1214, 'top': 960, 'right': 1225, 'class': 'mylabel', 'left': 1016}
----- test python API using a file
[1]    12904 segmentation fault (core dumped)  python example_yoobic.py

The predictions from the ----- test original C using a file version is ok.

Here is some tests i have made to find why this is happening :

After searching i found out that the segmentation fault comes from the data parsing in line 46 of module.c in the instruction if (!PyArg_ParseTuple(args, "iiiOff", &w, &h, &c, &array, &thresh, &hier_thresh)) I cant undestand why the parsing raise a segmentation fault error on my image. My images are RGB with sizes (1920,1440,3) or (1440,1920,3) or (1920,1920,3) Can you help ?

rayhou0710 commented 7 years ago

Hi,

Please try option 2 in Module.c. I am not sure why large images will cause a segmentation fault. Do you have an idea?

// convert (copy) PyArrayObject(float32) to float []
// option 1

float data[w*h*c];
int k;
for (k = 0; k < w*h*c; k++) {
    data[k] = *((float*) PyArray_GETPTR1(array, k));
}

// option 2
// float *data;
// data = (float*) PyArray_GETPTR1(array, 0);
MSutt commented 7 years ago

Well thanks, it's working now. I thought this was coming from the PyArg_ParseTuple call, obviously I was wrong.