YYuanAnyVision / mxnet_mtcnn_face_detection

MTCNN face detection
738 stars 314 forks source link

Bug fixed: "could not broadcast input array from shape" and "negative dimensions are not allowed"" #36

Open juliojj opened 5 years ago

juliojj commented 5 years ago

Thanks a lot for sharing your code. I am sharing with you how I managed to fix two errors when running your code. Maybe it can help who face the same problems. For some reason, just two images in my dataset generated these errors, on file mtcnn_detector.py:

error 1) could not broadcast input array from shape (0,63,3) into shape (58,63,3), where "63" can be any other value...

error 2) tmp = np.zeros((tmph[i], tmpw[i], 3), dtype=np.uint8) ValueError: negative dimensions are not allowed.

The two new conditions below fixed (temporary, as I didn't track the source of it) the problem:

(line 280)

pad the bbox

    [dy, edy, dx, edx, y, ey, x, ex, tmpw, tmph] = self.pad(total_boxes, width, height)
    # (3, 24, 24) is the input shape for RNet
    input_buf = np.zeros((num_box, 3, 24, 24), dtype=np.float32)

    for i in range(num_box):
        if(tmph[i]>0):         # << WARNING (bug fixed)
            tmp = np.zeros((tmph[i], tmpw[i], 3), dtype=np.uint8)
            if(edy[i]>=0):     # << WARNING (bug fixed)
                tmp[dy[i]:edy[i]+1, dx[i]:edx[i]+1, :] = img[y[i]:ey[i]+1, x[i]:ex[i]+1, :]
                input_buf[i, :, :, :] = adjust_input(cv2.resize(tmp, (24, 24)))

    output = self.RNet.predict(input_buf)
hnuzhy commented 5 years ago

What an amazing! I had met almost the same bug with you. And after debugging, I found the original source code is wrong. But my bug is a little different from yours. Your condition shows the edy[i] may be negative.

could not broadcast input array from shape (0,63,3) into shape (58,63,3)

but my bug remind us the edx[i] may also be negative.

could not broadcast input array from shape (65,0,3) into shape (65,51,3)

So I used your bug fixed method and added another judge condition at the same time. My bug is fixed :-)

for i in range(num_box):
    if(tmph[i] > 0):         # << WARNING (bug fixed)
        tmp = np.zeros((tmph[i], tmpw[i], 3), dtype=np.uint8)
        if(edy[i] >= 0 and edx[i] >= 0):     # << WARNING (bug fixed)
            tmp[dy[i]:edy[i]+1, dx[i]:edx[i]+1, :] = img[y[i]:ey[i]+1, x[i]:ex[i]+1, :]
            input_buf[i, :, :, :] = adjust_input(cv2.resize(tmp, (24, 24)))
RENCHEN90 commented 5 years ago

What an amazing! I had met almost the same bug with you. And after debugging, I found the original source code is wrong. But my bug is a little different from yours. Your condition shows the edy[i] may be negative.

could not broadcast input array from shape (0,63,3) into shape (58,63,3)

but my bug remind us the edx[i] may also be negative.

could not broadcast input array from shape (65,0,3) into shape (65,51,3)

So I used your bug fixed method and added another judge condition at the same time. My bug is fixed :-)

for i in range(num_box):
    if(tmph[i] > 0):         # << WARNING (bug fixed)
        tmp = np.zeros((tmph[i], tmpw[i], 3), dtype=np.uint8)
        if(edy[i] >= 0 and edx[i] >= 0):     # << WARNING (bug fixed)
            tmp[dy[i]:edy[i]+1, dx[i]:edx[i]+1, :] = img[y[i]:ey[i]+1, x[i]:ex[i]+1, :]
            input_buf[i, :, :, :] = adjust_input(cv2.resize(tmp, (24, 24)))

thx for the solution