From 724c4998f7534e5b48527282b76e8099c563ff89 Mon Sep 17 00:00:00 2001 From: DDXDB <38449595+DDXDB@users.noreply.github.com> Date: Fri, 3 Jan 2025 09:55:46 +0800 Subject: [PATCH 1/5] add Intel GPU support --- easyocr/easyocr.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/easyocr/easyocr.py b/easyocr/easyocr.py index c08fe0388dd..6ed07a4a1f4 100644 --- a/easyocr/easyocr.py +++ b/easyocr/easyocr.py @@ -72,12 +72,14 @@ def __init__(self, lang_list, gpu=True, model_storage_directory=None, elif gpu is True: if torch.cuda.is_available(): self.device = 'cuda' + elif torch.xpu.is_available(): + self.device = 'xpu' elif torch.backends.mps.is_available(): self.device = 'mps' else: self.device = 'cpu' if verbose: - LOGGER.warning('Neither CUDA nor MPS are available - defaulting to CPU. Note: This module is much faster with a GPU.') + LOGGER.warning('Neither CUDA/XPU/MPS are available - defaulting to CPU. Note: This module is much faster with a GPU.') else: self.device = gpu From 12c95c89e971ad28fddfe7b7efa4db9a39274d9a Mon Sep 17 00:00:00 2001 From: DDXDB <38449595+DDXDB@users.noreply.github.com> Date: Fri, 17 Jan 2025 03:39:11 +0800 Subject: [PATCH 2/5] Update requirements.txt --- requirements.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 9d0fb7b7107..70c831d730e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ -torch -torchvision>=0.5 + opencv-python-headless scipy numpy From a390ad0243e8613202e34ad211445bacb29583dc Mon Sep 17 00:00:00 2001 From: 98440 <984400286@qq.com> Date: Fri, 17 Jan 2025 04:01:32 +0800 Subject: [PATCH 3/5] update --- easyocr/easyocr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easyocr/easyocr.py b/easyocr/easyocr.py index 6ed07a4a1f4..5e1e542586f 100644 --- a/easyocr/easyocr.py +++ b/easyocr/easyocr.py @@ -91,7 +91,7 @@ def __init__(self, lang_list, gpu=True, model_storage_directory=None, self.quantize=quantize, self.cudnn_benchmark=cudnn_benchmark if detector: - detector_path = self.getDetectorPath(detect_network) + detector_path = self.getDistributedDataParallel(detect_network) # recognition model separator_list = {} From 42ed16bd4d7bc6ec0c905b9bbc8c435064be19a8 Mon Sep 17 00:00:00 2001 From: DDXDB <38449595+DDXDB@users.noreply.github.com> Date: Fri, 17 Jan 2025 04:12:50 +0800 Subject: [PATCH 4/5] Update easyocr.py --- easyocr/easyocr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easyocr/easyocr.py b/easyocr/easyocr.py index 5e1e542586f..6ed07a4a1f4 100644 --- a/easyocr/easyocr.py +++ b/easyocr/easyocr.py @@ -91,7 +91,7 @@ def __init__(self, lang_list, gpu=True, model_storage_directory=None, self.quantize=quantize, self.cudnn_benchmark=cudnn_benchmark if detector: - detector_path = self.getDistributedDataParallel(detect_network) + detector_path = self.getDetectorPath(detect_network) # recognition model separator_list = {} From a9972611fa06e6afdca59cc958d9261dea615efe Mon Sep 17 00:00:00 2001 From: 98440 <984400286@qq.com> Date: Fri, 14 Feb 2025 00:30:35 +0800 Subject: [PATCH 5/5] add IPEX support --- easyocr/detection.py | 4 +++- easyocr/easyocr.py | 10 ++++++++-- easyocr/recognition.py | 3 ++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/easyocr/detection.py b/easyocr/detection.py index e2964b71b45..0c424660b1d 100644 --- a/easyocr/detection.py +++ b/easyocr/detection.py @@ -40,6 +40,7 @@ def test_net(canvas_size, mag_ratio, net, image, text_threshold, link_threshold, for n_img in img_resized_list] x = torch.from_numpy(np.array(x)) x = x.to(device) + net = net.to(device) # forward pass with torch.no_grad(): @@ -74,8 +75,9 @@ def test_net(canvas_size, mag_ratio, net, image, text_threshold, link_threshold, def get_detector(trained_model, device='cpu', quantize=True, cudnn_benchmark=False): net = CRAFT() - if device == 'cpu': + if device == 'cpu'or device == 'xpu': net.load_state_dict(copyStateDict(torch.load(trained_model, map_location=device, weights_only=False))) + net.to(device) if quantize: try: torch.quantization.quantize_dynamic(net, dtype=torch.qint8, inplace=True) diff --git a/easyocr/easyocr.py b/easyocr/easyocr.py index 6ed07a4a1f4..efd449e5fe2 100644 --- a/easyocr/easyocr.py +++ b/easyocr/easyocr.py @@ -10,6 +10,12 @@ import numpy as np import cv2 import torch + +try: + import intel_extension_for_pytorch as ipex +except Exception: + pass + import os import sys from PIL import Image @@ -72,10 +78,10 @@ def __init__(self, lang_list, gpu=True, model_storage_directory=None, elif gpu is True: if torch.cuda.is_available(): self.device = 'cuda' - elif torch.xpu.is_available(): - self.device = 'xpu' elif torch.backends.mps.is_available(): self.device = 'mps' + elif torch.xpu.is_available(): + self.device = 'xpu' else: self.device = 'cpu' if verbose: diff --git a/easyocr/recognition.py b/easyocr/recognition.py index 530ef9517e2..65a7396c992 100644 --- a/easyocr/recognition.py +++ b/easyocr/recognition.py @@ -165,13 +165,14 @@ def get_recognizer(recog_network, network_params, character,\ model_pkg = importlib.import_module(recog_network) model = model_pkg.Model(num_class=num_class, **network_params) - if device == 'cpu': + if device == 'cpu'or device == 'xpu': state_dict = torch.load(model_path, map_location=device, weights_only=False) new_state_dict = OrderedDict() for key, value in state_dict.items(): new_key = key[7:] new_state_dict[new_key] = value model.load_state_dict(new_state_dict) + model.to(device) if quantize: try: torch.quantization.quantize_dynamic(model, dtype=torch.qint8, inplace=True)