From 41552a101ec1a659d3d7d98634d924c9f19a233d Mon Sep 17 00:00:00 2001
From: Daniel Lovera <162410607+daniellovera@users.noreply.github.com>
Date: Mon, 15 Jul 2024 21:20:28 -0700
Subject: [PATCH 1/2] Fix: Resolves memory leak caused by using CRAFT detector
 repeatedly

---
 test.py | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/test.py b/test.py
index 482b503..50d4c67 100755
--- a/test.py
+++ b/test.py
@@ -84,6 +84,9 @@ def test_net(net, image, text_threshold, link_threshold, low_text, cuda, poly, r
     with torch.no_grad():
         y, feature = net(x)
 
+    # remove X from device memory, no longer needed
+    del x
+    
     # make score and link map
     score_text = y[0,:,:,0].cpu().data.numpy()
     score_link = y[0,:,:,1].cpu().data.numpy()
@@ -94,6 +97,12 @@ def test_net(net, image, text_threshold, link_threshold, low_text, cuda, poly, r
             y_refiner = refine_net(y, feature)
         score_link = y_refiner[0,:,:,0].cpu().data.numpy()
 
+
+    # remove y and feature from device, whether GPU or CPU
+    del y, feature
+    # empty cuda cache to allow nvidia-smi to be more accurate
+    torch.cuda.empty_cache()
+    
     t0 = time.time() - t0
     t1 = time.time()
 

From 6721ed99f4c355da7aba56236a7bea1fdf29a04b Mon Sep 17 00:00:00 2001
From: Daniel Lovera <162410607+daniellovera@users.noreply.github.com>
Date: Mon, 15 Jul 2024 21:22:31 -0700
Subject: [PATCH 2/2] Fix: Resolves memory leak caused by using CRAFT detector
 repeatedly

---
 test.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/test.py b/test.py
index 50d4c67..3665de6 100755
--- a/test.py
+++ b/test.py
@@ -101,7 +101,8 @@ def test_net(net, image, text_threshold, link_threshold, low_text, cuda, poly, r
     # remove y and feature from device, whether GPU or CPU
     del y, feature
     # empty cuda cache to allow nvidia-smi to be more accurate
-    torch.cuda.empty_cache()
+    if cuda:
+        torch.cuda.empty_cache()
     
     t0 = time.time() - t0
     t1 = time.time()