add multi-images

pull/19/head
xinyanghuang7 2024-10-29 11:51:03 -04:00
parent e828599fd1
commit 882fc29112
5 changed files with 49 additions and 5 deletions

View File

@ -237,7 +237,7 @@ if __name__ == "__main__":
if ddp: if ddp:
model._ddp_params_and_buffers_to_ignore = {"pos_cis"} model._ddp_params_and_buffers_to_ignore = {"pos_cis"}
model = DistributedDataParallel(model, device_ids=[ddp_local_rank], find_unused_parameters=True) model = DistributedDataParallel(model, device_ids=[ddp_local_rank])
iter_per_epoch = len(train_loader) iter_per_epoch = len(train_loader)
for epoch in range(args.epochs): for epoch in range(args.epochs):

View File

@ -89,10 +89,15 @@ if __name__ == "__main__":
i = 0 i = 0
# 获取图像文件列表 # 获取图像文件列表
image_dir = './dataset/eval_multi_images/' # image_dir = './dataset/eval_multi_images/' # 对于图像转换数据集
image_dir = './dataset/eval_multi_images/bird/' # 对于鸟类数据集
image_files = sorted(os.listdir(image_dir)) image_files = sorted(os.listdir(image_dir))
prompt = "context: Source Image: <image> Target Image: <image> Instruction: What is the correct image edit instruction that can transfrom the source image to target image?" # 对于图像转换数据集
# prompt = "context: Source Image: <image> Target Image: <image> Instruction: What is the correct image edit instruction that can transfrom the source image to target image?"
# 对于鸟类数据集
prompt = "<image>\n<image>\nName all the differences between these two birds."
# 遍历图像文件 # 遍历图像文件
image_encoders = [] image_encoders = []

View File

@ -321,7 +321,7 @@ minimind-v使用50个字符组成的 `<<<...>>>` 占位符代替图像,
![input](./images/minimind-v-input.png) ![input](./images/minimind-v-input.png)
多图实现方法就是通过注入多个\<image\>图像占位符进行实现。 多图实现方法就是通过注入多个\<image\>图像占位符进行实现,不需要修改任何框架。
> ps: 唯一值得注意的点是,如果在训练过程中存在不同conversations插入图片数量不同的情况,需要利用空特征将较短的特征进行填充(对应[dataset的第267行](./model/dataset.py)),以保证能够在同样大小下被dataloader读取。 > ps: 唯一值得注意的点是,如果在训练过程中存在不同conversations插入图片数量不同的情况,需要利用空特征将较短的特征进行填充(对应[dataset的第267行](./model/dataset.py)),以保证能够在同样大小下被dataloader读取。
> pps: 在prompt中不需要如此做,仍旧是根据插入图像的数量来进行占位符的注入。因此,最终输入给LLM的input feature不会受填充特征的影响。 > pps: 在prompt中不需要如此做,仍旧是根据插入图像的数量来进行占位符的注入。因此,最终输入给LLM的input feature不会受填充特征的影响。
@ -395,6 +395,23 @@ MiniMind-V与MiniMind的代码核心改动不超过100行,上手难度低。</
} }
``` ```
```json
{
"id": "000000013633",
"image": "27266.jpg, 27267.jpg",
"conversations": [
{
"from": "human",
"value": "<image>\n<image>\nName all the differences between these two birds."
},
{
"from": "gpt",
"value": "animal1 is brown with white tuft while animal2 is white with dark brown wings"
}
]
}
```
注: 注:
+ 对于指令微调,仅保留了一轮对话,训练单轮对话模型,防止小模型性能被长文本拉低。 + 对于指令微调,仅保留了一轮对话,训练单轮对话模型,防止小模型性能被长文本拉低。
+ 多图数据集规模相对较小且为英文对话,数据集仅包含两图对比的场景,因此微调效果有限,这里只提供一种参考思路。 + 多图数据集规模相对较小且为英文对话,数据集仅包含两图对比的场景,因此微调效果有限,这里只提供一种参考思路。
@ -407,6 +424,8 @@ MiniMind-V与MiniMind的代码核心改动不超过100行,上手难度低。</
指令微调从230K条真实对话数据集中学习对图片提问的真实问答格式。 指令微调从230K条真实对话数据集中学习对图片提问的真实问答格式。
多图微调提供两个数据集,长度分别为3.5k和13.6k的真实问答格式。
`1-pretrain_vlm.py` 执行预训练,得到 `*_vlm_pretrain.pth` 作为预训练的输出权重。 `1-pretrain_vlm.py` 执行预训练,得到 `*_vlm_pretrain.pth` 作为预训练的输出权重。
`2-sft_vlm.py` 执行指令微调,得到 `*_vlm_sft.pth` 作为指令微调的输出权重。 `2-sft_vlm.py` 执行指令微调,得到 `*_vlm_sft.pth` 作为指令微调的输出权重。
@ -442,6 +461,7 @@ MiniMind-V与MiniMind的代码核心改动不超过100行,上手难度低。</
### 效果测试 ### 效果测试
#### 单图对话
<table> <table>
<thead> <thead>
<tr> <tr>
@ -526,6 +546,25 @@ MiniMind-V与MiniMind的代码核心改动不超过100行,上手难度低。</
</tbody> </tbody>
</table> </table>
#### 多图对话(效果十分有限)
<table>
<thead>
<tr>
<th>图片1</th>
<th>图片2</th>
<th>512_sft_multi</th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="./dataset/eval_multi_images/bird//0.jpg" alt="a-bird.png" style="width: 200px;"></td>
<td><img src="./dataset/eval_multi_images/bird//1.jpg" alt="a-bird.png" style="width: 200px;"></td>
<td>animal1 has a brown and black head with a black and white striped head . animal2 has a black head with a white stripe on its wings .</td>
</tr>
</tbody>
</table>
### 启动推理 ### 启动推理
```bash ```bash

View File

@ -17,7 +17,7 @@ for item in data['data']:
conversation = [ conversation = [
{ {
"from": "human", "from": "human",
"value": f"context: Source Image: {source_image} Target Image: {target_image} Instruction: {instruction}." "value": f"<image>\n<image>\n{instruction}"
}, },
{ {
"from": "gpt", "from": "gpt",