【发布时间】: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
我遇到了运行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
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])```
【讨论】: