首页 > 解决方案 > Darknet Yolov4 Python 内存泄漏

问题描述

我遇到了detect_image(...)darknet.py提供的内存泄漏。我正在无限循环中检测对象。我正在使用 Ubuntu 20.04、Python 3.8.10、OpenCV 4.5.2 和 Cuda 10.2。

标签: python-3.xmemory-leaksdarknetyolov4

解决方案


darknet.py 已经有一个功能来处理这个问题,即free_image(image). 出于某种原因,这没有在函数中调用detect_image(...)。我在下面添加了这个free_detections(detections, num),内存泄漏得到了处理。这是确切的代码:

def detect_image(network, class_names, image_path, thresh=.5, hier_thresh=.5, nms=.45):
    """
        Returns a list with highest confidence class and their bbox
    """
    pnum = pointer(c_int(0))
    image = load_image(image_path,0,0)
    predict_image(network, image)
    detections = get_network_boxes(network, image.w, image.h,
                                   thresh, hier_thresh, None, 0, pnum, 0)
    num = pnum[0]
    if nms:
        do_nms_sort(detections, num, len(class_names), nms)
    predictions = remove_negatives(detections, class_names, num)
    predictions = decode_detection(predictions)
    free_detections(detections, num)
    free_image(image) # this was missing...
    return sorted(predictions, key=lambda x: x[1])```

推荐阅读