初探AI之got-ocr2.0大模型本地部署与遇到的各种坑处理

一、环境搭建

1.安装cuda,本人使用的是12.1版本,下载地址:https://developer.nvidia.com/cuda-12-1-1-download-archive

2.安装conda3,https://blog.csdn.net/m0_73634846/article/details/136378350

3.准备代码环境

原文: https://mp.weixin.qq.com/s/PQVrlr5FoVb89Mivzi7pLA

顺序执行:

git clone https://github.com/Ucas-HaoranWei/GOT-OCR2.0.git

conda create -n got python=3.10 -y #创建虚拟环境

conda activate got #激活虚拟环境

pip install -e . # 使用项目里面pyproject.toml安装依赖

安装 Flash-Attention:

pip install ninja

pip install flash-attn --no-build-isolation

ps:以上是官方的安装教程,但是其实会遇到很多问题,以下进行逐一说明,不保证能解决所有问题,但是大部分问题都能解决

二、问题序列:

1.python安装cuda出现错误提示:AssertionError: Torch not compiled with CUDA enabled

复制代码
https://blog.csdn.net/qq_44750293/article/details/129685556
问题本质是pytorch无法调用cuda,原因是国内加速镜像默认会安装cpu版本,安装gpu版本即可,及torch.cuda.is_available输出为false即代表安装的版本为cpu版本,安装gpu版本后即可输出为true
###1、torch.cuda.is_available()显示false解决方法:
	使用pytorch官方推荐的版本安装:
		https://pytorch.org/
		例如cuda12.1的安装命令: 
			conda:	conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
			pip:	
				pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 (亲测有效,这个需要下载2小时+)
				阿里云加速(使用这个最快,推荐,只需要下载2min+):
					pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 -f https://mirrors.aliyun.com/pytorch-wheels/cu121

2.遇到deepspeed安装报错

复制代码
Python|Windows 安装 DeepSpeed 安装方法及报错 Unable to pre-compile async_io 处理:
	https://blog.csdn.net/Changxing_J/article/details/139789110

配置visual studio
运行Anaconda Powershell Prompt,执行 Set-Item Env:\DS_BUILD_OPS 0,切换到deepspeed目录,再次执行 :  .\build_win.bat

3.pip错误

复制代码
ERROR: Could not find a version that satisfies the requirement setuptools_scm (from versions: none)
	尝试切换pip源试试,pip install -i 源地址

4.conda错误

复制代码
在VS界面激活anaconda,但是报错"CondaError: Run 'conda init' before 'conda activate'",该如何解决?
	打开Anaconda Prompt
	conda init
	conda activate 环境

5.遇到依赖C++环境的库错误解决

【报错】fatal error C1189

visual studio 版本过高或过低导致,目前2019版本兼容性相对较好

复制代码
https://blog.csdn.net/thisjuly/article/details/136260069

6.flash_attn安装后执行失败,本质是编译的包不对

复制代码
text-generation-webui加载codellama报错DLL load failed while importing flash_attn_2_cuda: 找不到指定的模块。:
https://blog.csdn.net/dandandancpop/article/details/134729988
解决: https://github.com/Dao-AILab/flash-attention/releases ,在此链接中下载与本地cuda与torch且python相对应的版本即可,cuda12.1下载cu123亲测可用,cp310为python3.10,cp311为python3.11其他同理

7.本地无法启动大模型

复制代码
因为默认的源是hugging-face,但是国内无法访问,国内可使用modelscope(魔塔社区)的模型
hugging-face模型下载不成功?
	https://zhuanlan.zhihu.com/p/722248326?utm_campaign=shareopn&utm_medium=social&utm_psn=1824225242303819776&utm_source=wechat_session
	使用modelscope提供的模型

8.cudnn部署

https://blog.csdn.net/retainenergy/article/details/126183711

cuDNN下载链接:https://developer.nvidia.com/rdp/cudnn-archive ,本人使用的8.9.7,安装链接中进行配置即可

三、需要用到的资源下载链接(只上传了下载很慢的资源):

通过网盘分享的文件:GOT-OCR大模型部署相关

链接: https://pan.baidu.com/s/1s18O0kS3apBvs-L35KB-qw?pwd=y4ht 提取码: y4ht

四、执行代码与输出

python 复制代码
from modelscope import AutoModel, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained('stepfun-ai/GOT-OCR2_0', trust_remote_code=True)
model = AutoModel.from_pretrained('stepfun-ai/GOT-OCR2_0', trust_remote_code=True, low_cpu_mem_usage=True, device_map='cuda', use_safetensors=True, pad_token_id=tokenizer.eos_token_id)
model = model.eval().cuda()


# input your test image
# image_file = '/mnt/workspace/58F3EF14-E073-4BBE-B9D9-53CCFE6AE183.png'
image_file = 'benchmarks/format_doc_benchmark/2020-03-27__潍柴动力股份有限公司__000338__潍柴动力__2019年__年度报告/38.jpg'

# plain texts OCR
res = model.chat(tokenizer, image_file, ocr_type='ocr')

# format texts OCR:
# res = model.chat(tokenizer, image_file, ocr_type='format')

# fine-grained OCR:
# res = model.chat(tokenizer, image_file, ocr_type='ocr', ocr_box='')
# res = model.chat(tokenizer, image_file, ocr_type='format', ocr_box='')
# res = model.chat(tokenizer, image_file, ocr_type='ocr', ocr_color='')
# res = model.chat(tokenizer, image_file, ocr_type='format', ocr_color='')

# multi-crop OCR:
# res = model.chat_crop(tokenizer, image_file, ocr_type='ocr')
# res = model.chat_crop(tokenizer, image_file, ocr_type='format')

# render the formatted OCR results:
# res = model.chat(tokenizer, image_file, ocr_type='format', render=True, save_render_file = './demo.html')

print(res) 

原图

输出

结论: 识别率还是很不错的,后面在使用大量图片测试

五、其他补充

1.modelscope默认下载的模型目录:

~/.cache/modelscope/hub

2.一定要注意cuda版本与torch的版本要对应
https://blog.csdn.net/u011489887/article/details/135250561

查看cuda版本,执行命令,nvcc -V 或 nvcc --version可查看

查看cuda配置的环境变量,set cuda

显卡驱动版本可高于cuda版本,会向下兼容,但是不可小于,执行命令,nvidia-smi可查看显卡目前的状态以及运行中的进程

相关推荐
是Yu欸10 小时前
DevUI MateChat 技术演进:UI 与逻辑解耦的声明式 AI 交互架构
前端·人工智能·ui·ai·前端框架·devui·metachat
我不是QI10 小时前
周志华《机器学习---西瓜书》 一
人工智能·python·机器学习·ai
组合缺一14 小时前
Spring Boot 国产化替代方案。Solon v3.7.2, v3.6.5, v3.5.9 发布(支持 LTS)
java·后端·spring·ai·web·solon·mcp
张彦峰ZYF14 小时前
AI赋能原则1解读思考:超级能动性-AI巨变时代重建个人掌控力的关键能力
人工智能·ai·aigc·ai-native
美林数据Tempodata15 小时前
李飞飞最新论文深度解读:从语言到世界,空间智能将重写AI的未来十年
人工智能·ai·空间智能
豆奶特浓615 小时前
Java面试生死局:谢飞机遭遇在线教育场景,从JVM、Spring Security到AI Agent,他能飞吗?
java·jvm·微服务·ai·面试·spring security·分布式事务
todoitbo15 小时前
基于 DevUI MateChat 搭建前端编程学习智能助手:从痛点到解决方案
前端·学习·ai·状态模式·devui·matechat
xcLeigh16 小时前
AI的提示词专栏:“Re-prompting” 与迭代式 Prompt 调优
人工智能·ai·prompt·提示词
哥布林学者17 小时前
吴恩达深度学习课程三: 结构化机器学习项目 第二周:误差分析与学习方法(一)误差分析与快速迭代
深度学习·ai
Elastic 中国社区官方博客18 小时前
使用 A2A 协议和 MCP 在 Elasticsearch 中创建一个 LLM agent 新闻室:第二部分
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索