【问题标题】:Darknet Yolov4 Python memory leakDarknet Yolov4 Python 内存泄漏
【发布时间】:2021-12-04 15:20:37
【问题描述】:

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

【问题讨论】:

    标签: python-3.x memory-leaks darknet yolov4


    【解决方案1】:

    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])```
    

    【讨论】:

    • 其实我刚刚发现AlexeyAB的GitHub上提供的代码是正确的。我使用的是修改后的 darknet.py,其中图像是从 detect_image() 函数内的给定路径加载的。我的错。无论如何,也许其他人偶然发现了这个修改过的 darknet.py 版本,并且会发现这篇文章很有用。不幸的是,经过一夜的工作,我不记得我从哪里得到修改后的版本。
    猜你喜欢
    • 2012-12-02
    • 2011-12-13
    • 2014-02-26
    • 2010-11-27
    • 2021-05-30
    • 2015-02-15
    • 1970-01-01
    • 2021-01-10
    • 2017-10-18
    相关资源
    最近更新 更多