Replies: 2 comments
-
To reduce PaddleOCR's CPU usage or GPU usage while maintaining performance, you can make the following adjustments: 1. Adjust CPU ThreadsThe number of CPU threads used by PaddleOCR directly affects resource usage. By default, PaddleOCR uses multiple threads, which can spike CPU usage. You can limit this by reducing the number of threads: ocr = PaddleOCR(cpu_threads=1) # Restrict to 1 thread In your script, you've already set 2. Optimize PaddlePaddle FlagsThe paddle.set_flags({
"FLAGS_fraction_of_cpu_memory_to_use": 0.5, # Limit to 50% of CPU memory
"FLAGS_use_pinned_memory": False, # Disable pinned memory for a lower CPU load
})
3. Use PaddleLite for Lightweight InferenceIf you want to further reduce resource usage, consider using PaddleLite, which is optimized for edge devices and constrained environments. PaddleLite is a lightweight inference engine that can reduce both CPU and GPU usage significantly. You can convert your PaddleOCR model to a PaddleLite model and run it with optimized performance. 4. Reduce Model SizeIf high accuracy isn't critical, you can use a lighter OCR model. PaddleOCR supports multiple lightweight models. For example: ocr = PaddleOCR(rec_model_dir='path/to/lite_model', det_model_dir='path/to/lite_model', lang='en') You can download these models from the PaddleOCR Model Zoo. 5. Limit Image SizeThe size of the input image directly impacts resource usage. If you're performing OCR on large images, resizing them to a smaller resolution can significantly reduce computation time and memory usage. For example: from PIL import Image
def preprocess_image(file_path, max_width=1000, max_height=1000):
image = Image.open(file_path)
image.thumbnail((max_width, max_height), Image.ANTIALIAS)
return image Before passing the image to 6. Batch Process Small RegionsInstead of performing OCR on the entire screen at once, divide the image into smaller regions and process them sequentially. This reduces the peak CPU usage: def split_image(image, num_splits=4):
width, height = image.size
split_height = height // num_splits
regions = [image.crop((0, i * split_height, width, (i + 1) * split_height)) for i in range(num_splits)]
return regions Process each region one at a time using 7. Switch to GPU (if available)Your hardware includes an AMD Radeon GPU. PaddleOCR typically supports NVIDIA GPUs via CUDA, but since you have an AMD GPU, you might not get native GPU acceleration. However, you can still offload some computations to GPU by enabling ocr = PaddleOCR(use_gpu=True) Check if the GPU is supported by verifying with 8. Avoid Multiprocessing OverheadThe provided discussion highlights that using multiprocessing with PaddleOCR can increase processing time due to overhead. Stick to single-threaded or multithreaded execution without multiprocessing unless necessary. If you must use multiprocessing, ensure proper initialization of the PaddleOCR object within each process. 9. Measure and Monitor Resource UsageTo debug and optimize further, monitor CPU and memory usage using tools like import psutil
print("CPU usage:", psutil.cpu_percent())
print("Memory usage:", psutil.virtual_memory().percent) Additional ReferenceThe discussion linked (GitHub Discussion) highlights that multiprocessing can increase processing times by up to 5x due to initialization overhead. Avoid multiprocessing unless you're processing multiple independent images concurrently and the initialization overhead is acceptable. By combining these optimizations, you should be able to significantly limit the CPU and GPU usage of PaddleOCR while maintaining reasonable performance. Response generated by feifei-bot | chatgpt-4o-latest |
Beta Was this translation helpful? Give feedback.
-
@GreatV i am sorry for the very late response... 1.Adjust Cpu threads 2.Optimize PaddlePaddle Flags. 3.PaddleLite for lightweight Interference 4.Reduce Model Size 5.Limit image size 6.batch process small regions 7.Switch to GPU, i dont have one 9.Measure and Monitor Resource Usage |
Beta Was this translation helpful? Give feedback.
-
im using paddleocr for a project and i want to limit the resources its using.
The CPU usage of paddle ocr is tied to the image it scans, i want to limit it but how ? many cases where the cpu usage reaches 100% when doing full screen ocr.
this is my specs
CPU: AMD Ryzen 5 5625U Graphics
GPU: AMD Radeon Graphics
SSD: WDC PC SN530 SDBPNPZ-512G-1006 512GB
RAM: Micron 8ATF1G64HZ-3G2R1 2x8GB -
MBD: HP Laptop 14s-fq2xxx
i tried setting the flags but no changes, i dont know if its because i set the flags wrong or that its not what i should expect.
import tkinter as tk
from tkinter import filedialog
from paddleocr import PaddleOCR
import numpy as np
import paddle
import time
Custom DebugPaddleOCR class to include debugging capabilities
class DebugPaddleOCR(PaddleOCR):
def init(self, **kwargs):
super().init(**kwargs)
self.original_postprocess_op = self.text_recognizer.postprocess_op
self.text_recognizer.postprocess_op = self.debug_postprocess_op
Configure PaddlePaddle memory and CPU parameters
paddle.set_flags({
"FLAGS_fraction_of_cpu_memory_to_use": 0.1, # Use 10% of total CPU memory
"FLAGS_allocator_strategy": "naive_best_fit", # Optimize memory fragmentation
"FLAGS_eager_delete_scope": True, # Reduce memory usage by deleting scope synchronously
"FLAGS_eager_delete_tensor_gb": 0.0, # Enable garbage collection
"FLAGS_fast_eager_deletion_mode": True, # Use fast garbage collection
"FLAGS_use_pinned_memory": False, # Disable pinned memory for lower CPU usage
"FLAGS_fraction_of_cpu_memory_to_use": 100,
})
Initialize the DebugPaddleOCR instance with additional parameters
ocr = DebugPaddleOCR(lang='id', cpu_threads=1) # Set language to Indonesian and use 1 CPU thread
def run_ocr():
# Open a file dialog to select an image file
file_path = filedialog.askopenfilename(filetypes=[("Image Files", ".png;.jpg;.jpeg;.bmp;*.tiff")])
if not file_path:
return
Create the GUI application
root = tk.Tk()
root.title("OCR GUI")
Add a button to select a file
select_button = tk.Button(root, text="Select Image", command=run_ocr)
select_button.pack(pady=10)
Add a textbox to display OCR results
text_box = tk.Text(root, wrap=tk.WORD, width=80, height=20)
text_box.pack(padx=10, pady=10)
Run the application
root.mainloop()
Beta Was this translation helpful? Give feedback.
All reactions