Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
juliojj opened this issue Sep 29, 2018 · 2 comments

Comments

@juliojj
Copy link

juliojj commented Sep 29, 2018

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
Copy link

hnuzhy commented Jan 26, 2019

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants