Windows11下detectron2与LayoutParser安装报错的解决求助
问题描述
1. 初始安装尝试与Detectron2未找到错误
执行以下命令安装相关工具:
pip install -U layoutparser pip install 'git+https://github.com/facebookresearch/detectron2.git@v0.4#egg=detectron2' pip install layoutparser[ocr] git clone https://github.com/Layout-Parser/layout-parser.git
运行LayoutParser模型初始化代码:
model = lp.models.Detectron2LayoutModel('lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config', extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8], label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"})
返回错误:
ImportError:
Detectron2LayoutModel requires the detectron2 library but it was not found in your environment. Checkout the instructions on the
installation page: https://github.com/facebookresearch/detectron2/blob/master/INSTALL.md and follow the ones
that match your environment. Typically the following would work for MacOS or Linux CPU machines:
pip install 'git+https://github.com/facebookresearch/detectron2.git@v0.4#egg=detectron2'
2. Windows版Detectron2安装错误
尝试安装Windows适配版Detectron2时执行命令:
git clone https://github.com/DGMaxime/detectron2-windows.git cd detectron2-windows pip install -e
收到错误:
Note: you may need to restart the kernel to use updated packages.
Usage:
C:\Users\msubh\anaconda3\envs\expOPlay\python.exe -m pip install [options][package-index-options] ...
C:\Users\msubh\anaconda3\envs\expOPlay\python.exe -m pip install [options] -r[package-index-options] ...
C:\Users\msubh\anaconda3\envs\expOPlay\python.exe -m pip install [options] [-e]...
C:\Users\msubh\anaconda3\envs\expOPlay\python.exe -m pip install [options] [-e]...
C:\Users\msubh\anaconda3\envs\expOPlay\python.exe -m pip install [options] <archive url/path> ...-e option requires 1 argument
3. 新环境下的依赖冲突问题
创建新环境执行pip install torch后,出现大量依赖冲突报错,例如:
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
gdown 4.7.1 requires requests[socks], which is not installed.
...(省略后续冲突信息)
安装requests后仍存在大量未解决的依赖冲突。
解决方案
1. 创建纯净conda环境
避免现有包干扰,创建并激活指定Python版本的环境:
conda create -n layoutparser_env python=3.8 -y conda activate layoutparser_env
选择Python3.8是因为该版本与Detectron2、LayoutParser兼容性较好。
2. 安装适配Windows的PyTorch
使用conda安装PyTorch,自动处理依赖问题:
# CPU版本 conda install pytorch torchvision torchaudio cpuonly -c pytorch -y # 若有NVIDIA GPU,安装对应CUDA版本(示例为CUDA11.8) # conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia -y
3. 正确安装Windows版Detectron2
修复pip install -e的参数缺失问题,完整命令如下:
git clone https://github.com/DGMaxime/detectron2-windows.git cd detectron2-windows pip install -e .
注意-e后必须跟.,代表安装当前目录下的项目。
4. 安装LayoutParser及OCR依赖
pip install -U layoutparser pip install layoutparser[ocr]
5. 验证安装
运行以下代码测试模型初始化:
import layoutparser as lp model = lp.models.Detectron2LayoutModel( 'lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config', extra_config=["MODEL.ROI_HEADS.SCORE_THRESH_TEST", 0.8], label_map={0: "Text", 1: "Title", 2: "List", 3:"Table", 4:"Figure"} )
问题根源说明
- 官方Detectron2的pip安装命令仅适配Linux/Mac,Windows系统需使用专门适配的分支版本
pip install -e命令需要指定安装路径,之前缺失.导致参数错误- 直接用pip安装PyTorch易引发依赖冲突,conda能更好地管理跨平台依赖
内容的提问来源于stack exchange,提问作者DL_DS_2

