Giter Club home page Giter Club logo

minigpt-4-zh's Introduction

MiniGPT-4: 使用先进的大型语言模型增强视觉语言理解 作者为朱德尧、陈俊、沈晓倩、李翔和Mohamed Elhoseiny。*表示贡献相等。

所属机构为沙特阿拉伯国王科技大学。

English README

在线演示

点击图像与MiniGPT-4聊天,了解有关您的图像的信息。 demo

更多的例子可以在项目页面中找到。

Colab YouTube


新闻

我们现在提供了一个与 Vicuna-7B 对齐的预训练MiniGPT-4!演示GPU内存消耗现在可以低至12GB。


翻译同学的话

Vicuna-13B fp16 目前已知需要 35G 显存,加载图片后,会到43G,运行时内存高峰大概14G。

显存不够的,可以看裁剪设置,位置在运行demo的时候。

转换权重时,大概需要80G内存,可以尝试增大 swap 空间。
我原本以为,翻译这篇文档就能帮助大家部署,我自己实际部署下来,发现中间有超级多的坑,非常不利于小白。后续我准备制作一个一键部署包,方便大家。敬请期待~ 有新消息我会及时发布到群里面。

这是我最近发现的一个一键安装包。感谢@camenduru 同学。很奇怪的是,他的模型只有433MB大小。而我转出来的有37G。
问题解决了,项目实际运行时,还是需要去下载模型的。


目前测试下来,一键安装包,运行时需要17G显存。

感谢群友 成浩 同学提供的 windows安装踩坑指南 https://xlch.wolai.com/pBtGyPh6hyGx118o4deTk

如果翻译对您有帮助,请帮忙右上角 点击 star.
欢迎加入国内AI商业应用交流群

简介

  • MiniGPT-4使用一个投影层来将来自BLIP-2的冻结视觉编码器与冻结的LLM Vicuna对齐。
  • 我们通过两个阶段来训练MiniGPT-4。第一个传统的预训练阶段在使用4个A100大约10小时内,使用大约500万个图像-文本对进行训练。第一阶段过后,Vicuna能够理解图像。但是其生成能力受到了严重的影响。
  • 为了解决这个问题和提高可用性,我们提出了一种通过模型和ChatGPT自身创建高质量图像-文本对的新方法。基于此,我们创建了一个小型(总共3500对)但是高质量的数据集。
  • 第二个微调阶段在对话模板上使用该数据集进行训练,以显著提高其生成可靠性和整体可用性。令人惊讶的是,这个阶段具有计算效率,并且只需要使用单个A100大约7分钟的时间。
  • MiniGPT-4能够产生许多类似于GPT-4中展示的新兴视觉语言能力。

overview

入门指南:

安装

1.准备代码和环境

请先将我们的代码库克隆到本地,创建一个Python环境,然后通过以下命令激活它

git clone https://github.com/Vision-CAIR/MiniGPT-4.git
cd MiniGPT-4
conda env create -f environment.yml
conda activate minigpt4

2.准备预训练的Vicuna权重

不想自己折腾的直接下载我们准备好的权重(公开可能会有版权问题,所以暂时先放到微信群了。),然后跳转到第 3 步

当前版本的MiniGPT-4是建立在Vicuna-13B v0版本之上的。请参考我们的说明here来准备Vicuna权重。

here 的翻译如下:

如何准备Vicuna权重

Vicuna是一种基于LLAMA的LLM,性能接近于ChatGPT,并且是开源的。我们当前使用的是Vicuna-13B v1.1版本。

为了准备Vicuna的权重,首先从 https://huggingface.co/lmsys/vicuna-13b-delta-v1.1 下载Vicuna的增量权重。如果你已经安装了git-lfs(https://git-lfs.com), 可以通过以下方式完成:

git lfs install
# git clone https://huggingface.co/lmsys/vicuna-13b-delta-v0 
git clone https://huggingface.co/lmsys/vicuna-13b-delta-v1.1 

请注意,这并不是直接可用的工作权重,而是工作权重与LLAMA-13B原始权重之间的差异(由于LLAMA的规则,我们无法分发LLAMA的权重)。

然后,您需要获取原始的LLAMA-13B权重,可以按照HuggingFace提供的说明here或者从互联网上下载。

原始权重获取如下:

提示:

直接使用迅雷下载,种子在此

下面是备选方案: ---- 备选方案开始。----

可以通过填写表格来获取LLaMA模型的权重。你肯定不用填写,因为“热心网友”已经泄漏出来了 网址如下:meta-llama/llama#149

小白请推荐百度网盘 https://pan.baidu.com/s/1ujG85wgQFuIyf74N9k2MDQ?pwd=nu4d 不充会员的,可以使用 ipfs,或者迅雷。具体方法如下:

# 找到这个部分,你可以直接点击链接进入,使用迅雷下载。
Full backup: ipfs://Qmb9y5GCkTG7ZzbBWMu2BXwMkzyCKcUjtEKPpgdZ7GEFKm

7B: ipfs://QmbvdJ7KgvZiyaqHw5QtQxRtUd7pCAdkWWbzuvyKusLGTw 
13B: ipfs://QmPCfCEERStStjg4kfj3cmCUu1TP7pVQbxdFMwnhpuJtxk 
30B: ipfs://QmSD8cxm4zvvnD35KKFu8D9VjXAavNoGWemPW1pQ3AF9ZZ 
65B: ipfs://QmdWH379NQu8XoesA8AFw9nKV2MpGR4KohK7WyugadAKTh

也可以使用Kubo CLI中的以下命令:

# 可选:预加载 7B 模型。检索您尚未拥有的内容。如有需要,请替换为其他 CID。
ipfs refs -r QmbvdJ7KgvZiyaqHw5QtQxRtUd7pCAdkWWbzuvyKusLGTw

# 可选:固定7B模型。GC会删除您不使用的旧内容,这可以防止启用 GC 后模型被清除。
ipfs pin add QmbvdJ7KgvZiyaqHw5QtQxRtUd7pCAdkWWbzuvyKusLGTw

# 通过CLI从IPFS下载并保存到磁盘:
ipfs get QmbvdJ7KgvZiyaqHw5QtQxRtUd7pCAdkWWbzuvyKusLGTw --output ./7B

下载完成后,去百度网盘下载 tokenizer_checklist.chk tokenizer.model 这2个文件。

---- 备选方案结束。----

文件夹结构如下:

.
├── 13B
│   ├── =
│   ├── checklist.chk
│   ├── consolidated.00.pth
│   ├── consolidated.01.pth
│   └── params.json
├── tokenizer_checklist.chk
└── tokenizer.model

下载完权重之后,需要使用转换脚本将它们转换为Hugging Face Transformers格式。可以使用以下命令(示例)调用脚本: 脚本地址:https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/convert_llama_weights_to_hf.py

# 转换之前,建议 安装环境依赖,否则转模型到一半会报错,及其浪费时间。

git clone https://github.com/lm-sys/FastChat
cd FastChat
# 查看tag
git tag
# 切换到最新的tag分支
git checkout v0.2.3
# 安装
pip install e .

# 安装其他依赖
pip install transformers[sentencepiece]

# 注意,这里,需要安装transforms 环境。国内 镜像源更新不及时,请使用原版pip源,或者直接从项目安装
其他依赖:如果出现timeout,可以使用魔法,或者版本不要求最新时,使用国内源

python src/transformers/models/llama/convert_llama_weights_to_hf.py \
    --input_dir /path/to/downloaded/llama/weights --model_size 7B --output_dir /output/path

报错解决:

ValueError: Couldn't instantiate the backend tokenizer from one of:

https://stackoverflow.com/questions/65431837/transformers-v4-x-convert-slow-tokenizer-to-fast-tokenizer

pip install transformers[sentencepiece]

注意替换上面的地址为你的文件系统的真实地址。

转换完成后,可以通过以下方式加载模型和分词器:

from transformers import LlamaForCausalLM, LlamaTokenizer

tokenizer = LlamaTokenizer.from_pretrained("/output/path")
model = LlamaForCausalLM.from_pretrained("/output/path")

当这两个权重准备好后,我们可以使用Vicuna团队的工具来创建真正的工作权重。首先,安装与v0 Vicuna兼容的库:

pip install git+https://github.com/lm-sys/[email protected]

然后,运行以下命令以创建最终的工作权重:

python -m fastchat.model.apply_delta --base /path/to/llama-13b-hf/  --target /path/to/save/working/vicuna/weight/  --delta /path/to/vicuna-13b-delta-v0/

报错解决:

ValueError: Tokenizer class LLaMATokenizer does not exist or is not currently imported.
修改 llama-13b-hf/tokenizer_config.json/ 的  "tokenizer_class": "LLaMATokenizer" =>
 "tokenizer_class": "LlamaTokenizer"

killed 
转换13B需要 80G左右内存,通常的家用电脑无法承载。可以考虑开启swap
参考:https://www.cnblogs.com/erlou96/p/14578820.html#_label3_0
    https://timberkito.com/?p=98 


RuntimeError: The size of tensor a (32000) must match the size of tensor b (32001) at non-singleton dimension 0
参考:https://github.com/lm-sys/FastChat/issues/486

现在,您可以准备好使用Vicuna权重了!

最终得到的权重文件应该放在一个文件夹内,具有以下结构:

vicuna_weights
├── config.json
├── generation_config.json
├── pytorch_model.bin.index.json
├── pytorch_model-00001-of-00003.bin
...   

然后,在模型配置文件here的第16行设定vicuna权重的路径。

3. 准备预训练的MiniGPT-4检查点

根据您准备的Vicuna模型下载预训练检查点。

Checkpoint Aligned with Vicuna 13B Checkpoint Aligned with Vicuna 7B
下载 下载

然后,在评估配置文件minigpt4_eval.yaml的第11行中设置预训练检查点的路径。

在本地启动演示

通过运行以下命令在本地机器上试用我们的演示demo.py

python demo.py --cfg-path eval_configs/minigpt4_eval.yaml  --gpu-id 0

为了节省GPU内存,Vicuna默认为8位加载,搜索宽度为1。
该配置需要大约23G的GPU内存用于Vicuna 13B和11.5G的GPU内存用于Vicuna 7B。

模型裁剪

模型裁剪,有很多种方法。下面提供最简单的一种。(注意,模型裁剪,可能会影响模型精度。导致效果不好。建议没有经验的同学可以同时也体验一下完整版,否则,可能觉得模型很垃圾。)

# 先不写了。我发现需要我帮忙的那个朋友是3090 24G显存的,而且有4张,没有天理了!!!  他只是输错了 device编号而已。  (骂骂咧咧 。。。。。。 
# ( 骂完手动狗头保命~ 虽然我错了,但写教程是不可能了,一辈子都不可能

对于更强大的GPU,您可以在配置文件minigpt4_eval.yaml中将low_resource设置为False并使用更大的搜索宽度以16位运行模型。

windows 部署遇到问题可以参考这个 issue Vision-CAIR/MiniGPT-4#28 实际上如果显存高,可以修改 minigpt4/models/mini_gpt4.py 92行 load_in_8bit=False 关闭8bit,这样就可以不安装这个包。

感谢@WangRongsheng,您也可以在Colab上运行我们的代码。

训练

MiniGPT-4的训练包含两个对齐阶段。

1. 第一阶段预训练

在第一个预训练阶段中,使用来自Laion和CC数据集的图像文本对训练模型, 以对齐视觉和语言模型。要下载和准备数据集,请查看我们的第一阶段数据集准备说明。 在第一阶段之后,视觉特征被映射并可以被语言模型理解。 要启动第一阶段训练,请运行以下命令。在我们的实验中,我们使用了4个A100。 您可以在配置文件train_configs/minigpt4_stage1_pretrain.yaml中更改保存路径。

torchrun --nproc-per-node NUM_GPU train.py --cfg-path train_configs/minigpt4_stage1_pretrain.yaml

只有第一阶段训练的MiniGPT-4检查点可在此处下载 here。 与第二阶段之后的模型相比,此检查点经常生成不完整和重复的句子。

2. 第二阶段微调

在第二个阶段中,我们使用自己创建的小型高质量图像文本对数据集,并将其转换为对话格式,以进一步对齐MiniGPT-4。 要下载和准备我们的第二阶段数据集,请查看我们的第二阶段数据集准备说明。 要启动第二阶段对齐,请先在train_configs/minigpt4_stage2_finetune.yaml中指定第1阶段训练的检查点文件的路径。 您也可以在那里指定输出路径。 然后,运行以下命令。在我们的实验中,我们使用1个A100。

torchrun --nproc-per-node NUM_GPU train.py --cfg-path train_configs/minigpt4_stage2_finetune.yaml

经过第二阶段的对齐,MiniGPT-4能够以连贯且易于使用的方式讨论图像。

致谢

  • BLIP2 :MiniGPT-4的模型架构遵循BLIP-2。如果您以前不知道它,请不要忘记检查这个伟大的开源工作!
  • Lavis :这个存储库是基于Lavis构建的!
  • Vicuna :只有13B个参数的Vicuna的神奇语言能力真是太棒了。它是开源的!

如果您在研究或应用中使用MiniGPT-4,请引用以下BibTeX:

@misc{zhu2022minigpt4,
      title={MiniGPT-4: Enhancing Vision-language Understanding with Advanced Large Language Models}, 
      author={Deyao Zhu and Jun Chen and Xiaoqian Shen and xiang Li and Mohamed Elhoseiny},
      year={2023},
}

国内交流群

目前群主部署了minigpt4 的在线版本,星球的朋友可以先体验,不想加星球的,可以等星球的同学体验完了,再白嫖(一般是晚上)~

群主会不定期发布 各类亮眼项目体验版本 供大家体验,星球主要沉淀一些商业AI最新讯息,帮助大家节约时间。欢迎各位读者老爷,漂亮姐姐给我的项目点赞!

关注公众号加群 知识星球

许可证.

此存储库采用BSD 3-Clause许可证
许多代码基于Lavis,这里是BSD 3-Clause许可证here

感谢

本项目 fork 自 https://github.com/Vision-CAIR/MiniGPT-4 大部分翻译来自 https://github.com/Vision-CAIR/MiniGPT-4

minigpt-4-zh's People

Contributors

livehl avatar riseinrose avatar virtuallyaverage avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minigpt-4-zh's Issues

训练显存相关

想根据自己的业务去fine tune模型,4块24G显存的显卡能跑起来不?

作者,你现在这个版本不带--gpu-id参数,那么后续一系列cuda问题是如何解决的?

先强调一下,我这里是2块GPU 显卡。

python demo.py --cfg-path eval_configs/minigpt4_eval.yaml --gpu-id 1。显示没有 --gpu-id 参数。仔细看了一下 demo.py 代码的确没有 --gpu-id 参数。你默认写了 model = model_cls.from_config(model_config).to('cuda:0') 。那么会引起一些列的cuda问题。

我现在这里报错:

File "/root/MiniGPT-4-ZH/minigpt4/models/modeling_llama.py", line 74, in forward
return self.weight * hidden_states
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1!

请问作者是咋解决的?

RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory

when running "python demo.py --cfg-path eval_configs/minigpt4_eval.yaml --gpu-id 0", i get an error "RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory"
note that at eval_configs i set kept as "pretrained_minigpt4.pth", which is the checkpoint for Vicuna,
i wonder if there's sth wrong with the download link, does anybody else encounter this error?

btw the output is:
Initializing Chat
The load_in_4bit and load_in_8bit arguments are deprecated and will be removed in the future versions. Please, pass a BitsAndBytesConfig object in quantization_config argument instead.
Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:14<00:00, 2.41s/it]
Traceback (most recent call last):
File "/home/zouchen_1/MiniGPT-4/demo.py", line 66, in
model = model_cls.from_config(model_config).to('cuda:{}'.format(args.gpu_id))
File "/home/zouchen_1/MiniGPT-4/minigpt4/models/minigpt4.py", line 169, in from_config
model = cls(
File "/home/zouchen_1/MiniGPT-4/minigpt4/models/minigpt4.py", line 45, in init
super().init(
File "/home/zouchen_1/MiniGPT-4/minigpt4/models/minigpt_base.py", line 51, in init
self.visual_encoder, self.ln_vision = self.init_vision_encoder(
File "/home/zouchen_1/MiniGPT-4/minigpt4/models/base_model.py", line 151, in init_vision_encoder
visual_encoder = create_eva_vit_g(
File "/home/zouchen_1/MiniGPT-4/minigpt4/models/eva_vit.py", line 433, in create_eva_vit_g
state_dict = torch.load(cached_file, map_location="cpu")
File "/home/zouchen_1/anaconda3/envs/minigptv/lib/python3.9/site-packages/torch/serialization.py", line 797, in load
with _open_zipfile_reader(opened_file) as opened_zipfile:
File "/home/zouchen_1/anaconda3/envs/minigptv/lib/python3.9/site-packages/torch/serialization.py", line 283, in init
super().init(torch._C.PyTorchFileReader(name_or_buffer))
RuntimeError: PytorchStreamReader failed reading zip archive: failed finding central directory

按照流程部署好以后执行demo,出现乱码回复

我是按照这篇文章给的说明安装的,模型没有进行训练的步骤,然后执行的demo.py查看效果,结果用英文输入语句结果出现乱码结果。是模型融合的不对?还是检查点有问题?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.