python-3.x - Darknet Yolov4 Python 内存泄漏
问题描述
我遇到了detect_image(...)
由darknet.py提供的内存泄漏。我正在无限循环中检测对象。我正在使用 Ubuntu 20.04、Python 3.8.10、OpenCV 4.5.2 和 Cuda 10.2。
解决方案
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])```
推荐阅读
- android - 如何修复类预期的 2 个类型参数
- ios - react native config无法读取ios中的env文件
- java - Java 微服务、JVM 参数、k8s 和 Docker 的配额和其他
- python - Azure Databricks 群集初始化脚本 - 安装 python 轮
- angular - 从 App Shell-Angular 调试 MicroUI 组件
- r - R按字母顺序重新排列列值
- python - 努力理解一段代码
- excel - 数据验证列表被忽略
- javascript - 使用空格验证用户输入
- sql - NLS_LANG 导致 MATCH RECOGNIZE ORACLE SQL 出现问题