From e992112ae95c775d8b2f7009c32a6d65c945548c Mon Sep 17 00:00:00 2001 From: Daniel Lovera <162410607+daniellovera@users.noreply.github.com> Date: Wed, 10 Jul 2024 13:17:37 -0700 Subject: [PATCH 1/3] Resolves memory leak on detection. --- easyocr/detection.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/easyocr/detection.py b/easyocr/detection.py index 072178a2339..4026799a7b9 100644 --- a/easyocr/detection.py +++ b/easyocr/detection.py @@ -45,6 +45,9 @@ def test_net(canvas_size, mag_ratio, net, image, text_threshold, link_threshold, with torch.no_grad(): 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 @@ -68,6 +71,10 @@ 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 call empty_cache() to clean up + del y + torch.cuda.empty_cache() return boxes_list, polys_list From 62773ded4614ad45741ce12b0f9791ddaef58526 Mon Sep 17 00:00:00 2001 From: Daniel Lovera <162410607+daniellovera@users.noreply.github.com> Date: Mon, 15 Jul 2024 21:26:23 -0700 Subject: [PATCH 2/3] Adjusted to not call empty_cache() for non-Cuda --- easyocr/detection.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/easyocr/detection.py b/easyocr/detection.py index 4026799a7b9..7537dce9ce8 100644 --- a/easyocr/detection.py +++ b/easyocr/detection.py @@ -74,7 +74,8 @@ def test_net(canvas_size, mag_ratio, net, image, text_threshold, link_threshold, # remove y from device, whether GPU or CPU, and call empty_cache() to clean up del y - torch.cuda.empty_cache() + if cuda: + torch.cuda.empty_cache() return boxes_list, polys_list From bc53fc1ba50da1a869e05823f6df0a32d5ef1d37 Mon Sep 17 00:00:00 2001 From: Daniel Lovera <162410607+daniellovera@users.noreply.github.com> Date: Tue, 16 Jul 2024 11:20:31 -0700 Subject: [PATCH 3/3] Correct cuda check --- easyocr/detection.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/easyocr/detection.py b/easyocr/detection.py index 7537dce9ce8..3e52c13a45c 100644 --- a/easyocr/detection.py +++ b/easyocr/detection.py @@ -72,9 +72,9 @@ def test_net(canvas_size, mag_ratio, net, image, text_threshold, link_threshold, boxes_list.append(boxes) polys_list.append(polys) - # remove y from device, whether GPU or CPU, and call empty_cache() to clean up + # remove y from device, whether GPU or CPU, and check if cuda was used before calling empty_cache() to clean up del y - if cuda: + if device == 'cuda': torch.cuda.empty_cache() return boxes_list, polys_list