unstructured pdf的hi_res策略如果选用detectron2时有些问题需要关注, detectron2自从2021年到现在很久没有更新。使用如新的python3.12及最新的torch2.3.1去编译源码安装会报错torch找不到的问题导致源码编译安装失败。根据要使用的detectron2及pytorch等需应用的包,综合考量选择首先构建一个虚拟python3.9,版本环境(推荐python3.9而不是其它,不能使用python3.7因此之后使用过程中会用到importlib.metadata但其在python3.8之后才有)以方便安装detectron2及其依赖等。这里的虚拟python环境使用miniconda。
miniconda官网
输入如下命令:
$ mkdir -p ~/miniconda3
$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
$ bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
$ rm -rf ~/miniconda3/miniconda.sh
$ ~/miniconda3/bin/conda init bash
$ conda create --name conda-py39-env python=3.9
$ conda activate conda-py39-env
依次命令行键入如下指令:
$ pip install unstructured
$ pip install unstructured[pdf,local-inference]
$ sudo apt install libmagic-dev
$ pip install poppler-utils
$ sudo apt install tesseract-ocr
$ sudo apt install tesseract-ocr-chi-sim
$ sudo apt install libtesseract-dev pandoc
$ pip install psutil
注意上面的tesseract要单独有一条命令来安装语言包,如安装有问题请参考tesseract及语言包安装。
综上,最终决定选用python=3.9, torch=1.10.1, torchvision=0.11.2, cuda=11.1,键入命令如下:
$ pip install torch==1.10.1+cu111 torchvision==0.11.2+cu111 torchaudio==0.10.1 -f https://download.pytorch.org/whl/cu111/torch_stable.html
$ python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.10/index.html
在代码工作目录新建文件夹:unstructuredio/yolo_x_layout和unstructuredio/detectron2_faster_rcnn_R_50_FPN_3x以存放hi_res的layout解析模型,
从huggingface中hi_res模型下载地址下载至少包含yolox及detectron2的两种模型:
或更多其它基于yolo与detectron2的模型。对于基于detectron2的布局解析模型,可在layout parser model zoo里进行选择Layout Detection Models,这里可以乎略,继续向下看。
在代码工作目录新建文件夹:microsoft/table-transformer-structure-recognition/,并从huggingface中table-transformer配置下载地址下载。
从timm/resnet18.a1_in1k模型下载下载所有内容,并人工将miniconda3/envs/conda-py39-env/lib/python3.9/site-packages/timm/models/_builder.py的line159添加:
load_from="file"
pretrained_loc += "/pytorch_model.bin"
按上述步骤实践之后就可以正常进行local inference了,如上内容全基于发布文章时的条件,大体重点情况如下: