InternVL icon indicating copy to clipboard operation
InternVL copied to clipboard

关于Mini−InternVL−Chat−4B−V1−5图片检索问题

Open YiXinChenChen opened this issue 2 years ago • 11 comments

我想请问一下 目前Mini−InternVL−Chat−4B−V1−5能否用于作为图片检索,我这边看到了githb的代码关于“using InternVL-C(ontrastive) and InternVL-G(enerative) (click to expand)”,都是使用的是OpenGVLab/InternVL-14B-224px模型,对于Mini−InternVL−Chat−4B−V1−5,是否有类似的方法进行encode_image和encode_text

另外,我想问是不是Chat模型再训练的时候已经是不再对text和img的emb进行对齐了?

YiXinChenChen avatar May 31 '24 01:05 YiXinChenChen

同提问

lighthearts avatar Jun 08 '24 04:06 lighthearts

您好,1.5的模型是不支持图片检索的,我们目前所有的模型都是基于LM任务训练得到的,都不支持图片检索。

另外Chat模型训练的时候,仍然需要先预训练MLP对齐text和img的emb的。

Weiyun1025 avatar Jul 18 '24 08:07 Weiyun1025

谢谢,我尝试着把internl2的img_encode以及text_encode过程扒了出来,想要进行图片检索任务,但实际发现效果不是很好,这是因为没对齐的原因吗? 这边是否只有OpenGVLab/InternVL-14B-224px是能做图片检索任务呢?

xxcoco763 avatar Jul 19 '24 03:07 xxcoco763

是的,只有OpenGVLab/InternVL-14B-224px支持图片检索任务

Weiyun1025 avatar Jul 19 '24 05:07 Weiyun1025

也就是说只支持图搜图嘛?不支持文本检索图像?我试了下文本图像检索,按照huggingface的代码提取图像和文本特征,发现准确度很差。

EricHuiK avatar Sep 11 '24 11:09 EricHuiK

是的,只有OpenGVLab/InternVL-14B-224px支持图片检索任务

这个图片检索指的是图搜图还是文搜图呢?我用OpenGVLab/InternVL-14B-224px下的InternVL-C和InternVL-G都做了一遍encode_image和encode_text,然后计算image_features和text_features的相似度,结果都很差

zoezhu avatar Oct 26 '24 11:10 zoezhu

是的,只有OpenGVLab/InternVL-14B-224px支持图片检索任务

这个图片检索指的是图搜图还是文搜图呢?我用OpenGVLab/InternVL-14B-224px下的InternVL-C和InternVL-G都做了一遍encode_image和encode_text,然后计算image_features和text_features的相似度,结果都很差

以文搜图和以图搜文都可以,结果很差能否提供一下代码,我估计是哪里有bug

czczup avatar Oct 26 '24 11:10 czczup

是的,只有OpenGVLab/InternVL-14B-224px支持图片检索任务

这个图片检索指的是图搜图还是文搜图呢?我用OpenGVLab/InternVL-14B-224px下的InternVL-C和InternVL-G都做了一遍encode_image和encode_text,然后计算image_features和text_features的相似度,结果都很差

以文搜图和以图搜文都可以,结果很差能否提供一下代码,我估计是哪里有bug

感谢回复!我的测试代码是这样的:

import torch
from PIL import Image
from transformers import AutoModel, CLIPImageProcessor
from transformers import AutoTokenizer

model_path = "OpenGVLab/InternVL-14B-224px"

model = AutoModel.from_pretrained(
    model_path,
    torch_dtype=torch.bfloat16,
    low_cpu_mem_usage=True,
    trust_remote_code=True).cuda().eval()

image_processor = CLIPImageProcessor.from_pretrained(model_path)

tokenizer = AutoTokenizer.from_pretrained(
    model_path, use_fast=False, add_eos_token=True)
tokenizer.pad_token_id = 0  # set pad_token_id to 0

images = [
    Image.open('01.jpg').convert('RGB'),
    Image.open('02.jpg').convert('RGB'),
    Image.open('03.jpg').convert('RGB')
]
prefix = 'summarize:'
texts = [
    prefix + '人物',
    prefix + '家具',
]

pixel_values = image_processor(images=images, return_tensors='pt').pixel_values
pixel_values = pixel_values.to(torch.bfloat16).cuda()
input_ids = tokenizer(texts, return_tensors='pt', max_length=80,
                      truncation=True, padding='max_length').input_ids.cuda()

# InternVL-C
logits_per_image, logits_per_text = model(
    image=pixel_values, text=input_ids, mode='InternVL-C')
img_probs = logits_per_image.softmax(dim=-1)
txt_probs = logits_per_text.softmax(dim=-1)

print(img_probs)
print(txt_probs)

用到的图片如下: 01.jpg https://github.com/user-attachments/assets/13999ec3-9585-4d0e-9659-2e2bbe2e2fdb

02.jpg https://github.com/user-attachments/assets/e5a8a0e1-8c87-4cfe-be7c-9944e52ebee6

03.jpg https://github.com/user-attachments/assets/0ab37a7d-b30b-4875-b953-b7690d7d0af0

最后得到的结果是:

img_probs:
tensor([[1.4019e-04, 1.0000e+00],
        [7.5391e-01, 2.4512e-01],
        [1.1914e-01, 8.7891e-01]], device='cuda:0', dtype=torch.bfloat16,
       grad_fn=<SoftmaxBackward0>)

txt_probs:
tensor([[5.8203e-01, 4.0039e-01, 1.7578e-02],
        [1.0000e+00, 3.1233e-05, 3.1233e-05]], device='cuda:0',
       dtype=torch.bfloat16, grad_fn=<SoftmaxBackward0>)

从图片分类来看:01.jpg认为是“家具”(正确)、02.jpg认为是“人物”(正确)、03.jpg认为是“家具” (错误),第三张图的判断错误是正常的吗还是我代码哪里写错了?

从文搜图来看:输入“人物”会返回01.jpg(错误),输入“家具”会返回01.jpg(正确),对于输入“人物”返回的错误结果感觉有点奇怪,是不是我哪里弄错了?

期待解惑!谢谢!

zoezhu avatar Oct 26 '24 12:10 zoezhu

嗯嗯,看了代码没什么问题,我建议你的prompt可以长一点,可以显著改善效果,比如“一张人物的照片”、“一张家具的照片”

czczup avatar Oct 26 '24 13:10 czczup

还有个策略就是构造一批分类模板,可以参考这个文件:

https://github.com/OpenGVLab/InternVL/blob/main/clip_benchmark/clip_benchmark/datasets/en_zeroshot_classification_templates.json

czczup avatar Oct 26 '24 13:10 czczup

嗯嗯,看了代码没什么问题,我建议你的prompt可以长一点,可以显著改善效果,比如“一张人物的照片”、“一张家具的照片”

果然可以!换成这个prompt这个例子的结果就正确了,但是加大数据量的结果还是不太理想,我理解解决这个问题的核心思想是改善prompt,我再试试你说的分类模板的方法,感谢大佬!

zoezhu avatar Oct 26 '24 13:10 zoezhu