wmcnally / kapao

KAPAO is an efficient single-stage human pose estimation model that detects keypoints and poses as objects and fuses the detections to predict human poses.
GNU General Public License v3.0
753 stars 103 forks source link

Potential memory leak found #83

Closed austinulfers closed 2 years ago

austinulfers commented 2 years ago

I've run into an issue where my kapao predictions use up more and more memory the longer a video is being processed. Below is some semi-reproducible code. The only parts that aren't is my custom Kapao class which is basically all the necessary pieces of the demos/video.py needed to just pass in an image and get objects out. If you think this is the part that is causing the issues, let me know and I can post that section of the code as well.

import cv2
import tracemalloc
from model import Kapao
from collections import deque

tracemalloc.start()

cap = cv2.VideoCapture(r"path/to/video.mp4")

people = Kapao(r"kapao_l_crowdpose.pt")

buffer = deque([None] * 10, 10)
grabbed, img = cap.read() # Line 13
frame_num = 0
while grabbed:
    buffer.append(
        people.predict(img)
    )
    buffer.popleft()
    frame_num += 1
    snap = tracemalloc.take_snapshot().statistics("lineno")[0]
    print(f"Frame: {frame_num} Line: {snap.traceback._frames[0][1]} Size: {snap.size} Count: {snap.count}")
    grabbed, img, = cap.read() # Line 23

The output looks something like this:

Frame: 1 Num: 1 Line: 13 Size: 6220896 Count: 2
Frame: 2 Num: 2 Line: 23 Size: 6220944 Count: 3
...
Frame: 97 Num: 97 Line: 23 Size: 12441840 Count: 5
...
Frame: 100 Num: 100 Line: 23 Size: 18662736 Count: 7
...
Frame: 203 Num: 203 Line: 23 Size: 24883632 Count: 9
...

This memory allocation continues to grow larger and larger as the video processing continues. Does anyone why these objects are continually being stored in memory despite them being removed from the buffer and thus should be picked up by the garbage collector? I have ran this test with Yolov7 as well and it fails to reproduce which makes me think it is a KAPAO specific issue.