PaddlePaddle / PaddleOCR

Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
https://paddlepaddle.github.io/PaddleOCR/
Apache License 2.0
44.47k stars 7.84k forks source link

Cannot import paddleocr together with yolov8 #10265

Closed shyakocat closed 6 months ago

shyakocat commented 1 year ago
import paddleocr
import ultralytics
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\ultralytics\__init__.py", line 5, in <module>
    from ultralytics.hub import start
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\ultralytics\hub\__init__.py", line 5, in <module>
    from ultralytics.hub.auth import Auth
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\ultralytics\hub\auth.py", line 5, in <module>
    from ultralytics.hub.utils import HUB_API_ROOT, PREFIX, request_with_credentials
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\ultralytics\hub\utils.py", line 14, in <module>
    from ultralytics.yolo.utils import (ENVIRONMENT, LOGGER, ONLINE, RANK, SETTINGS, TESTS_RUNNING, TQDM_BAR_FORMAT,
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\ultralytics\yolo\__init__.py", line 3, in <module>
    from . import v8
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\ultralytics\yolo\v8\__init__.py", line 3, in <module>
    from ultralytics.yolo.v8 import classify, detect, pose, segment
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\ultralytics\yolo\v8\classify\__init__.py", line 3, in <module>
    from ultralytics.yolo.v8.classify.predict import ClassificationPredictor, predict
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\ultralytics\yolo\v8\classify\predict.py", line 3, in <module>
    import torch
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\torch\__init__.py", line 202, in <module>
    from torch._C import *  # noqa: F403
RuntimeError: generic_type: type "_CudaDeviceProperties" is already registered!
import ultralytics
import paddleocr
Error: Can not import paddle core while this file exists: C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddle\fluid\libpaddle.pyd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddleocr\__init__.py", line 14, in <module>
    from .paddleocr import *
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddleocr\paddleocr.py", line 21, in <module>
    import paddle
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddle\__init__.py", line 31, in <module>
    from .framework import monkey_patch_variable
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddle\framework\__init__.py", line 17, in <module>
    from . import random  # noqa: F401
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddle\framework\random.py", line 17, in <module>
    from paddle import fluid
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddle\fluid\__init__.py", line 36, in <module>
    from . import framework
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddle\fluid\framework.py", line 35, in <module>
    from . import core
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddle\fluid\core.py", line 356, in <module>
    raise e
  File "C:\Users\shy13\scoop\apps\python39\current\lib\site-packages\paddle\fluid\core.py", line 269, in <module>
    from . import libpaddle
ImportError: generic_type: type "_gpuDeviceProperties" is already registered!

When import paddleocr or ultralytics separately, both are work fine. But get error when import them together.

在Python中单独导入paddleocrultralytics时无误。但同时导入将报错。

shyakocat commented 1 year ago

This problem may related to C++ level code, I cannot solve it by myself. I tried to import two libraries in two subprocess, and use xmlrpc to provide function api on localhost:some-port. But the performance is intolerable.

这个问题可能涉及到paddle的底层C++代码,我无法独自解决。 我试图用xmlrpc库将它们分别在两个子进程中导入,然后开启本地服务器提供函数访问接口。但是效率不忍直视,最好有能直接导入且不让它们冲突的解决方案。

luanssouza commented 1 year ago

I was facing the same problem. It may occurs due to the PaddlePaddle dependency, which uses CUDA as default. To solve that, I tried PaddlePaddle without CUDA (CPU).

For instance: python -m pip install paddlepaddle==2.5.0 -i https://mirror.baidu.com/pypi/simple.

poorneshwaran commented 1 year ago

Hi, I can import paddle-ocr with yolo v8. Use Cuda only on yolov8 ultralytics and installed paddlepaddle(cpu version) works great. As per my understanding, the Cuda was initialized while running ultralytics for patch detection. Again, call the cuda with paddleocr (i.e ocr.ocr )will get the error prompt like ImportError: generic_type: type "_gpuDeviceProperties" is already registered! Because Cuda is already registered and running.

shyakocat commented 1 year ago

It due to personal requirement. My only need is their forward refering, so I look up code and implement ONNX format loader (no import). It works, but I don't think it's a real solution.

这与个人需求有关。我只需要正向推理的功能,所以我查阅资料使用ONNX格式(那样就不必同时导入二者了)。这样做对我来说可行,但我感觉没有实际解决问题。

Avi197 commented 1 year ago

Hi, I can import paddle-ocr with yolo v8. Use Cuda only on yolov8 ultralytics and installed paddlepaddle(cpu version) works great. As per my understanding, the Cuda was initialized while running ultralytics for patch detection. Again, call the cuda with paddleocr (i.e ocr.ocr )will get the error prompt like ImportError: generic_type: type "_gpuDeviceProperties" is already registered! Because Cuda is already registered and running.

Hi, does the inference speed of paddle run with speed of gpu or cpu if you change it this way. If it still have cpu inference speed I don't think it solve the problem

ctx-eva commented 1 year ago

it seems that the problem cames out not only yolov8 ultralytics with paddleocr, but also paddlepaddle-gpu with torch. Is there any possible way to fix this.

liyiersan commented 1 year ago

it seems that the problem cames out not only yolov8 ultralytics with paddleocr, but also paddlepaddle-gpu with torch. Is there any possible way to fix this.

Try to reinstall paddlepaddle-gpu to version 2.4.2. It works for me.

cucyuan commented 1 year ago

it seems that the problem cames out not only yolov8 ultralytics with paddleocr, but also paddlepaddle-gpu with torch. Is there any possible way to fix this.

Try to reinstall paddlepaddle-gpu to version 2.4.2. It works for me. What version of pytorch is yours?

liyiersan commented 1 year ago

it seems that the problem cames out not only yolov8 ultralytics with paddleocr, but also paddlepaddle-gpu with torch. Is there any possible way to fix this.

Try to reinstall paddlepaddle-gpu to version 2.4.2. It works for me. What version of pytorch is yours?

1.12.1

Avi197 commented 1 year ago

These library version work for me

protobuf==3.19.0 paddlepaddle-gpu==2.4.2.post116 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html paddleocr==2.6.1.3 ultralytics==8.0.137

QuntuamLoop commented 1 year ago

for me paddlepaddle-gpu 2.4.2 in compile by cuda11.8 with torch 2.0.1 in cuda cuda11.8 will course the same issue, but change to paddlepaddle-gpu 2.4.2 in compile by cuda11.7 is fine! https://www.paddlepaddle.org.cn/whl/windows/mkl/avx/stable.html

anhhaibkhn commented 1 year ago

@QuntuamLoop

Could you share a bit more details of your working environment? (Python Ver, cuda, paddleocr, etc.)

I am facing the same issue with: OS: Windows 11 Python Ver: 3.11 In my conda environment, the pip packages are:

Thank you for your time.

alekseychuk-ivan commented 1 year ago

Good day. I also had same errors. ImportError: generic_type: type "_gpuDeviceProperties" is already registered! or RuntimeError: generic_type: type "_CudaDeviceProperties" is already registered! Error depends from library startup sequences. As advised above, installed paddlepuddle-gpu version 2.4.2 with cuda 11.7, Torch works with cuda version 11.7 or 11.8. torch = 2.0.1+cu117 ultralytics = 8.0.189 paddleocr = 2.7.0.3 paddlepaddle-gpu = 2.4.2.post117

codenoid commented 1 year ago
    from torch._C import *  # noqa: F403
    ^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: generic_type: type "_CudaDeviceProperties" is already registered!
navono commented 11 months ago

conda create --name model python=3.10 -y

#!/bin/bash

set -e
set -x

BASEDIR=$(dirname "$0")
pushd "$BASEDIR"

eval "$(conda shell.bash hook)"
conda activate model

pip install paddlepaddle-gpu==2.5.2
pip install ultralytics==8.0.223
pip install paddleocr==2.7.0.3
pip install opencv-python==4.6.0.66
pip install onnx==1.15.0
pip install paho-mqtt==1.6.1
pip install gradio==4.8.0
conda install -c conda-forge cudatoolkit=11.8 cudnn=8.9.2 zlib-wapi=1.2.13 -y

popd
pashaprokaz commented 9 months ago

In my case, helped: pip install paddlepaddle-gpu==2.4.2.post116 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html env looks like: paddlepaddle-gpu==2.4.2.post116 paddleocr==2.7.0.3 torch==2.2.0+cu118 protobuf==3.19.0

Mshaoyang1991 commented 7 months ago

i use yolov8 and PaddleOCR(GPU)

from ultralytics import YOLO from paddleocr import PaddleOCR

the key is Don't make torch=118 equal to paddlepaddle-gpu=118,be differe like torch=118 paddlepaddle-gpu=117.

like this: My environment windows 11 CUDA = C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8

torch==2.2.2+cu118 torchaudio==2.2.2+cu118 torchvision==0.17.2+cu118 paddlepaddle-gpu==2.4.2.post117 paddleocr==2.7.3

KomodoCrypto commented 6 months ago

Not working for me. I tried all combinations possible and suggestions provided, but I am unable to find a valid setup to run Yolo (or even torch) with paddleocr. Is there an update on this issue?

UserWangZz commented 6 months ago

This issue has not been updated for a long time. This issue is temporarily closed and can be reopened if necessary.

jmap1 commented 6 months ago

可以将yolo和ocr分别用两个进程来调用,可以执行,我尝试过了

KomodoCrypto commented 5 months ago

You can call yolo and ocr with two processes respectively, and it can be executed. I have tried it. Yes, I am doing like that, however, I must implement an intercommunication process to provide the information from one process to the other, and I would like to simplify it. Thank you

shuishangdz commented 4 months ago

我感觉是因为使用gpu冲突了,两个库都想用gpu,但是又没有分配机制导致的,目前好像无解。gpu底层驱动解决这个事情才行。 依据:我尝试改变两个库的建立顺序,报的错还不一样。 后调paddle为: from . import libpaddle ImportError: generic_type: type "_gpuDeviceProperties" is already registered! 先调用paddle为:from torch._C import * # noqa: F403 RuntimeError: generic_type: type "_CudaDeviceProperties" is already registered!

KomodoCrypto commented 4 months ago

Yes, in my opinion it is an important issue to solve because it makes paddle useless for most projects, and I would like to draw attention to this. Downgrading yolo or paddle to previous version is a tricky solution, but is not valid for obvious reasons.

KomodoCrypto commented 4 months ago

Any solution on this? How can you use Yolo and Paddle in the same project?

DimIsaev commented 4 months ago

up ! Hey

alexandar-m commented 1 month ago

Solution for this is to use older paddlepaddle-gpu version.

paddlepaddle-gpu==2.4.2.post117 works just fine.

Working environment: Windows 10/11

python-3.10.11-amd64 cuda_11.7.1_516.94_windows cudnn-windows-x86_64-8.4.1.50_cuda11.6-archive

opencv-contrib-python 4.6.0.66 opencv-python 4.6.0.66 paddleocr 2.8.1 paddlepaddle-gpu 2.4.2.post117 torch 1.13.1+cu117 torchaudio 0.13.1+cu117 torchvision 0.14.1+cu117 ultralytics 8.3.2 ultralytics-thop 2.0.8

aspirin2d commented 1 month ago

It's not fixed, Why close this issue?

alexandar-m commented 1 month ago

It's not fixed, Why close this issue?

https://github.com/PaddlePaddle/PaddleOCR/issues/10265#issuecomment-2385636539

This works 100%

Have installed it 10 times and each time there is no issue of any kind.

KomodoCrypto commented 1 month ago

If it's working, can you please point into the solution? If the solution is to install an old version, it's just a patch and not accepted for obvious reasons. The question here (and in other similar posts too) is clear: is paddleocr current version compatible with yolo or torch current version into the same project? The answer as far as I see is no. Many users are asking for it. Can we have a solution for that? Thank you

alexandar-m commented 1 month ago

If it's working, can you please point into the solution? If the solution is to install an old version, it's just a patch and not accepted for obvious reasons. The question here (and in other similar posts too) is clear: is paddleocr current version compatible with yolo or torch current version into the same project? The answer as far as I see is no. Many users are asking for it. Can we have a solution for that? Thank you

Solution for this is to use older paddlepaddle-gpu version.

paddlepaddle-gpu==2.4.2.post117 works just fine.

Working environment: Windows 10/11

python-3.10.11-amd64 cuda_11.7.1_516.94_windows cudnn-windows-x86_64-8.4.1.50_cuda11.6-archive

opencv-contrib-python 4.6.0.66 opencv-python 4.6.0.66 paddleocr 2.8.1 paddlepaddle-gpu 2.4.2.post117 torch 1.13.1+cu117 torchaudio 0.13.1+cu117 torchvision 0.14.1+cu117 ultralytics 8.3.2 ultralytics-thop 2.0.8

KomodoCrypto commented 1 month ago

Downgrading to old version is not a solution. I would like to draw attention on this, so many users are having this limitation. Thank you for your support

alexandar-m commented 1 month ago

Downgrading to old version is not a solution. I would like to draw attention on this, so many users are having this limitation. Thank you for your support

I was unable to make it work with latest paddlepaddle-gpu version.

Only paddlepaddle-gpu version needs to be older, 2.4.2.

PaddleOCR can be upgraded each time without issues.

Tested on YoloV5, YoloV8 and YoloV11

HonestyBrave commented 2 weeks ago

使用最新的paddlpaddl-gpu,现在最新是2.6.2,这个可以正常使用,希望能帮到大家。