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

Fix segfault for batch_detection_example #8478

Merged
merged 1 commit into from
Aug 25, 2023
Merged

Conversation

MrLIk
Copy link

@MrLIk MrLIk commented Apr 21, 2022

I try to run the batch_detection_example function from the darknet_images.py and faced with a segmentation fault in the function batch_detection in the darknet.network_predict_batch call.

def batch_detection(network, images, class_names, class_colors,
                    thresh=0.25, hier_thresh=.5, nms=.45, batch_size=4):
    image_height, image_width, _ = check_batch_shape(images, batch_size)
    darknet_images = prepare_batch(images, network)
    batch_detections = darknet.network_predict_batch(network, darknet_images, batch_size, image_width,
                                                     image_height, thresh, hier_thresh, None, 0, 0)

The problem is that function prepare_batch return a pointer darknet_images to the batch_array in the darknet.IMAGE structure but not the 'batch_array' itself.

def prepare_batch(images, network, channels=3):
    ...
    batch_array = np.concatenate(darknet_images, axis=0)
    batch_array = np.ascontiguousarray(batch_array.flat, dtype=np.float32)/255.0
    darknet_images = batch_array.ctypes.data_as(darknet.POINTER(darknet.c_float))
    return darknet.IMAGE(width, height, channels, darknet_images)

So in the function batch_detection the darknet_images points to the invalid memory.
It seems that garbage collector erase the batch_array after the prepare_batch return.

In the PR I moved wrapping of the batch_array from the prepare_batch function into the 'batch_detection' function.

@cenit cenit merged commit 38a5b48 into AlexeyAB:master Aug 25, 2023
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

Successfully merging this pull request may close these issues.

3 participants