EasyOCR
EasyOCR copied to clipboard
Fix: Resolves memory leak caused by using CRAFT detector with detect() or readtext().
This fix enables garbage collection to appropriately work when https://github.com/JaidedAI/EasyOCR/blob/c999505ef6b43be1c4ee36aa04ad979175178352/easyocr/detection.py#L24 returns, by deleting the objects we moved to the GPU after we move the forward pass results back on the CPU.
See https://pytorch.org/blog/understanding-gpu-memory-2/#why-doesnt-automatic-garbage-collection-work for more detail.
Running torch.cuda.empty_cache()
in test_net()
before returning allows nvidia-smi to be accurate.
Interestingly, nvidia-smi showed that GPU memory usage per process was 204MiB upon reader initialization, and then would increase to 234MiB or 288MiB after running easyocr.reader.detect()
, but then not increase beyond that point and in some cases reduce back down to 234MiB. I think this has something to do with
One note is that I tested this on a single GPU machine where I changed https://github.com/JaidedAI/EasyOCR/blob/c999505ef6b43be1c4ee36aa04ad979175178352/easyocr/detection.py#L86 to be net = net.to(device)
, removing DataParallel. There's no reason this shouldn't work on multi-GPU machines, but noting it wasn't tested on one.
I also only tested this on the CRAFT detector, not DBNet.
Relevant package versions easyocr version 1.7.1 torch version 2.2.1+cu121 torchvision 0.17.1+cu121
Hope this helps!