Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Resolves memory leak caused by using CRAFT detector with detect() or readtext(). #1278

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions easyocr/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def test_net(canvas_size, mag_ratio, net, image, text_threshold, link_threshold,
with torch.no_grad():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with torch.no_grad():
with torch.inference_mode():

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems useful but perhaps should be a separate PR? I was focused on the GPU memory leak with this PR so not sure if they should be packaged together.

y, feature = net(x)

# remove x and feature from device, whether GPU or CPU
del x, feature

boxes_list, polys_list = [], []
for out in y:
# make score and link map
Expand All @@ -68,6 +71,11 @@ def test_net(canvas_size, mag_ratio, net, image, text_threshold, link_threshold,
polys[k] = boxes[k]
boxes_list.append(boxes)
polys_list.append(polys)

# remove y from device, whether GPU or CPU, and check if cuda was used before calling empty_cache() to clean up
del y
if device == 'cuda':
torch.cuda.empty_cache()

return boxes_list, polys_list

Expand Down