【unstructured】针对unstructured的pdf提取的hi_res策略不能够连接huggingface.co下载模型的问题而选择本地化模型推理部署的方法

unstructured pdf hi_res策略本地推理部署说明

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。

python3.9虚拟环境准备

安装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-env环境

$ conda create --name conda-py39-env python=3.9
$ conda activate conda-py39-env

安装unstructured

背景知识

  • unstructured inference
  • 如下安装过程中会自动下载pytorch的高版本v2.3.1并耗费较长时间。但如果使用hi_res的detectron2,其依赖于较低版本的pytorch,在下面安装detectron2时会介绍。

安装步骤

依次命令行键入如下指令:

$ 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及语言包安装

安装detectron2

背景知识

  • detectron2自从2021年到现在很久没有更新,高版本python如3.12不太兼容,推荐基于python3.9
  • detectron2安装方法
  • detectron2要求pytorch>=1.8, python>=3.7
  • [torchvision开源代码](https://github.com/pytorch/vision/)中会给出torch, torchvision与python的版本搭配
  • pytorch旧版本要求 链接中找到pytorch 1.10.1版本的安装命令,使用CUDA11.1

安装detectron2及其依赖torch,torchvision和torchaudio

综上,最终决定选用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

部署模型

背景知识

  • 使用unstructured的hi_res策略来解析PDF会使用深度学习模型识别文档布局并使用图像识别来提取表格结构信息,会使用多种模型。需要单独下载的模型有:
    • yolox或detectron2等文档布局识别模型;
    • microsoft/table-transformer的模型来识做表格检测、表格结构识别(row,column,不同cell)等以及表格功能性分析(如判别列标题单元格,行标题单元格等);
    • timm的resnet18模型;
  • unstructured的hi_res支持的模型
  • 模型部署在本地来进行本地推理,此种方式需要手动去huggingface.co下载三类模型后将模型放置在代码所在目录。
  • 截止当前unstuctured版本,本地部署timm的resnet18与layout model和microsoft的table transformer model稍不同,其代码不能自动识别本地下载的模型,需要更改unstructured源码以令其正确本地加载模型,下面会介绍。

hi_res策略三种模型本地推理部署

layout model(detectron2及yolox)模型本地推理部署

在代码工作目录新建文件夹:unstructuredio/yolo_x_layout和unstructuredio/detectron2_faster_rcnn_R_50_FPN_3x以存放hi_res的layout解析模型,
huggingface中hi_res模型下载地址下载至少包含yolox及detectron2的两种模型:

  • unstructuredio/yolo_x_layout/yolox_l0.05.onnx
  • unstructuredio/detectron2_faster_rcnn_R_50_FPN_3x/model.onnx

或更多其它基于yolo与detectron2的模型。对于基于detectron2的布局解析模型,可在layout parser model zoo里进行选择Layout Detection Models,这里可以乎略,继续向下看。

microsoft的table-transformer模型本地推理部署

在代码工作目录新建文件夹:microsoft/table-transformer-structure-recognition/,并从huggingface中table-transformer配置下载地址下载。

timm/resnet18.a1_in1k的模型本地推理部署

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了,如上内容全基于发布文章时的条件,大体重点情况如下:

  • unstructured=0.14.9
  • unstructured-inference=0.7.36
  • torch=1.10,1+cu111
  • torchvision=0.11.2+cu111
  • torchaudio=0.10.1+cu111
  • detectron2=0.6+cu111
    有任何问题请详细查看相关官方文档及自行参考unstructured源码。
  • 14
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值