使用Replicate LlaVa、Fuyu 8B和MiniGPT4模型进行图像推理的多模态LLM
在本笔记本中,我们将展示如何使用多模态LLM类进行图像理解/推理。 目前我们支持:
在第二部分中,我们将展示如何为 Replicate 使用流式完成和异步完成。
注意: 目前,Replicate多模态大语言模型仅支持每次处理一个图像文档。
%pip install llama-index-multi-modal-llms-replicate% pip install replicate加载并初始化 Replicate
Section titled “Load and initialize Replicate”import os
REPLICATE_API_TOKEN = "" # Your Relicate API token hereos.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKENfrom PIL import Imageimport requestsfrom io import BytesIO
from llama_index.core.multi_modal_llms.generic_utils import load_image_urlsfrom llama_index.core.schema import ImageDocument
if not os.path.exists("test_images"): os.makedirs("test_images")
# for now fuyu-8b model on replicate can mostly handle JPG image urls well instead of local filesimage_urls = [ # "https://www.visualcapitalist.com/wp-content/uploads/2023/10/US_Mortgage_Rate_Surge-Sept-11-1.jpg", "https://www.sportsnet.ca/wp-content/uploads/2023/11/CP1688996471-1040x572.jpg", "https://res.cloudinary.com/hello-tickets/image/upload/c_limit,f_auto,q_auto,w_1920/v1640835927/o3pfl41q7m5bj8jardk0.jpg", "https://www.cleverfiles.com/howto/wp-content/uploads/2018/03/minion.jpg",]# save imagesfor idx, image_url in enumerate(image_urls): response = requests.get(image_url) img = Image.open(BytesIO(response.content)) img.save(f"test_images/{idx}.png")
# option 1: load images from urls directly# image_documents = load_image_urls(image_urls)# option 2: load images from localimage_documents = [ ImageDocument(image_path=f"test_images/{idx}.png") for idx in range(len(image_urls))]import matplotlib.pyplot as pltfrom llama_index.core.response.notebook_utils import display_image_uris
image_paths = [str(img_doc.image_path) for img_doc in image_documents]display_image_uris(image_paths)
提供多种提示词以测试不同的多模态大语言模型
Section titled “Provide various prompts to test different Multi Modal LLMs”from llama_index.multi_modal_llms.replicate import ReplicateMultiModalfrom llama_index.multi_modal_llms.replicate.base import ( REPLICATE_MULTI_MODAL_LLM_MODELS,)
prompts = [ "what is shown in this image?", "how many people are shown in the image?", "is there anything unusual in the image?",]使用不同提示词为不同图像生成来自不同大语言模型的图像推理
Section titled “Generate Image Reasoning from different LLMs with different prompts for different images”res = []for prompt_idx, prompt in enumerate(prompts): for image_idx, image_doc in enumerate(image_documents): for llm_idx, llm_model in enumerate(REPLICATE_MULTI_MODAL_LLM_MODELS): try: ## Initialize the MultiModal LLM model multi_modal_llm = ReplicateMultiModal( model=REPLICATE_MULTI_MODAL_LLM_MODELS[llm_model], max_new_tokens=100, temperature=0.1, num_input_files=1, top_p=0.9, num_beams=1, repetition_penalty=1, )
mm_resp = multi_modal_llm.complete( prompt=prompt, image_documents=[image_doc], ) except Exception as e: print( f"Error with LLM model inference with prompt {prompt}, image {image_idx}, and MM model {llm_model}" ) print("Inference Failed due to: ", e) continue res.append( { "model": llm_model, "prompt": prompt, "response": mm_resp, "image": str(image_doc.image_path), } )from IPython.display import displayimport pandas as pd
pd.options.display.max_colwidth = Nonedf = pd.DataFrame(res)display(df[:5]).dataframe tbody tr th { vertical-align: top;}
.dataframe thead th { text-align: right;}| 模型 | 提示词 | 响应 | 图像 | |
|---|---|---|---|---|
| 0 | llava-13b | 这张图片中显示的是什么? | 图片显示一名男子手持一座金色奖杯,可能是一座足球奖杯,同时穿着西装打领带。 | test_images/0.png |
| 1 | fuyu-8b | 这张图片中显示的是什么? | 图片显示一名身穿西装、手持金色球状奖杯的男子。 | test_images/0.png |
| 2 | minigpt-4 | 这张图片中显示的是什么? | 图片显示一名身穿黑色西装和领带的男子手持一座金色奖杯。 | test_images/0.png |
| 3 | llava-13b | 这张图片中显示的是什么? | 图片显示了一座大型、灯火通明的建筑,这是意大利罗马的斗兽场。该建筑在夜晚被点亮,灯光营造出美丽而戏剧性的效果。 | test_images/1.png |
| 4 | fuyu-8b | 这张图片中显示的是什么? | 图像展示了一条夜晚的城市街道,五彩斑斓的灯光照亮了整个场景。街道两旁排列着建筑物,包括一座显眼的罗马式圆形剧场。 | test_images/1.png |
人工标注多模态大语言模型推理结果的正确性与相关性
Section titled “Human Label the Correctness and Relevance of the Multi-Modal LLM Reasoning Results”请注意,人类标注在标记相关性和正确性时可能存在一定的偏见/主观性。
我们将正确性和相关性分数标记在[1, 5]之间
- 5: 完美回答问题
- 4: 以某种方式回答问题
- 3: 部分回答问题
- 2: 用错误答案回答问题
- 1: 无答案或 hallucination
hallucination
| 模型 | Prompt/Question | 模型推理结果 | 正确性与相关性 [1,5] | 图像 |
|---|---|---|---|---|
| llava-13b | 这张图片中显示的是什么? | 图片显示一名男子手持奖杯,该奖杯看起来是一个金色足球。他身着西装打领带,手持奖杯时面带微笑。 | 4 | test_images/0.png |
| fuyu-8b | 这张图片中显示的是什么? | 图片显示一名身穿西装、手持金色球状奖杯的男子。 | 4 | test_images/0.png |
| minigpt-4 | 这张图片中显示的是什么? | 图片显示一名身穿黑色西装和领带的男子手持一座金色奖杯。 | 4 | test_images/0.png |
| llava-13b | 这张图片中显示的是什么? | 图片显示了一座大型、灯火通明的建筑,可能是一座历史纪念碑或著名地标,拍摄于夜间。建筑被彩色灯光照亮,营造出视觉上引人注目的场景。 | 4 | test_images/1.png |
| fuyu-8b | 这张图片中显示的是什么? | 图像展示了一条夜晚的城市街道,五彩斑斓的灯光照亮了整个场景。街道两旁排列着建筑物,包括一座显眼的罗马式圆形剧场。 | 4 | test_images/1.png |
| minigpt-4 | 这张图片中显示的是什么? | 图片显示的是罗马斗兽场,一座位于意大利罗马的大型圆形剧场。它是这座城市最著名的地标之一,也是一个热门的旅游景点。 | 5 | test_images/1.png |
| llava-13b | 这张图片中显示的是什么? | 图片显示两个来自电影《神偷奶爸》的动画小黄人角色并排站立。 | 5 | test_images/2.png |
| fuyu-8b | 这张图片中显示的是什么? | 图片中展示了两个小黄人形象,一个穿着工装裤,另一个戴着护目镜。他们并排站立,可能正在交谈或摆姿势拍照。 | 5 | test_images/2.png |
| minigpt-4 | 这张图片中显示的是什么? | 图片显示两个小黄人,一个穿着黄色工装裤,另一个穿着蓝色工装裤,都戴着太阳镜并排站在一起。 | 4 | test_images/2.png |
| llava-13b | 图片中显示了多少人? | 图像中只显示了一个人,一名手持奖杯的男子。 | 5 | test_images/0.png |
| fuyu-8b | 图片中显示了多少人? | 在图片中,有一位穿着西装的男士手持一个金色球体或奖杯。 | 5 | test_images/0.png |
| minigpt-4 | 图片中显示了多少人? | 抱歉,我看不到您提供的图片。能否请您重新提供图片? | 不适用 | test_images/0.png |
| llava-13b | 图片中显示了多少人? | 图像中显示有两个人。 | 1 | test_images/1.png |
| fuyu-8b | 图片中显示了多少人? | 这张图片展示了一个夜晚的城市街道,五彩斑斓的灯光照亮了整个场景。街道两旁林立着建筑物,包括一座显眼的罗马式圆形剧场。\n\n街道上有几个人在行走,有些人拎着手提包。人行道旁可以看到几张长椅,为行人提供了休息的场所。整个氛围充满活力,人们正在进行日常活动。 | 4 | test_images/1.png |
| minigpt-4 | 图片中显示了多少人? | 抱歉,我看不到您提供的图像。能否请您提供更多信息或另一张图像? | 不适用 | test_images/1.png |
| llava-13b | 图片中显示了多少人? | 图像中显示有两个人。 | 4 | test_images/2.png |
| fuyu-8b | 图片中显示了多少人? | 图像中显示有两个人。 | 4 | test_images/2.png |
| minigpt-4 | 图片中显示了多少人? | 图像中有两个仆从。 | 5 | test_images/2.png |
| llava-13b | 图像中有什么异常之处吗? | 是的,图像中有一个不寻常的元素。这名男子手持一个金色奖杯,这并不罕见。然而,他还拿着一个麦克风,这是一个不寻常的组合。通常,手持麦克风的人会是演讲者或主持人,而奖杯通常与奖项或成就相关。麦克风和奖杯的组合表明,这名男子可能是一位主持人或播音员,同时也在接受奖项或认可他人的成就。 | 4 | test_images/0.png |
| fuyu-8b | 图像中有什么异常之处吗? | 是的,图片中确实存在不寻常之处,因为有一个金球或奖杯被一位穿西装的男士手持着。这显得不寻常,因为通常体育用球或奖杯与竞赛或成就相关,而不是由穿西装的人佩戴。手持金球或奖杯的男士身着西装,这更增添了场景的奇特感。 | 3 | test_images/0.png |
| minigpt-4 | 图像中有什么异常之处吗? | 图片显示一名身穿黑色西装和领带的男子手持一座金色奖杯。奖杯上有一个看起来像足球的设计图案。该男子面带微笑,显得很开心。 | 3 | test_images/0.png |
| llava-13b | 图像中有什么异常之处吗? | 是的,这张图片有一个不寻常的方面。斗兽场,这座著名的古罗马圆形剧场,被彩色灯光照亮,这并不常见。这种照明为原本具有历史感和雄伟结构的建筑增添了独特而充满活力的气息。 | 4 | test_images/1.png |
| fuyu-8b | 图像中有什么异常之处吗? | 是的,这张图片呈现了一个不同寻常的场景,多人正环绕着这座巨大的古老罗马圆形剧场行走。圆形剧场被五彩斑斓的灯光照亮,营造出充满活力与生机的氛围。 | 3 | test_images/1.png |
| minigpt-4 | 图像中有什么异常之处吗? | 图片显示的是罗马斗兽场,一座位于意大利罗马的大型圆形剧场。它是一个著名的地标和旅游景点。 | 2 | test_images/1.png |
| llava-13b | 图像中有什么异常之处吗? | 是的,图像中有些不寻常之处。这两个卡通小黄人并排站立,但其中一个缺了一颗牙齿。这是一个不寻常的细节,因为动画角色通常不会有像缺牙这样的瑕疵。缺失的牙齿为图像增添了独特而有趣的元素,使其与典型的动画场景区分开来。 | 3 | test_images/2.png |
| fuyu-8b | 图像中有什么异常之处吗? | 是的,这张图片有一个不寻常的方面,有两个穿着工装裤、戴着护目镜的小黄人并排站在一起。这种不寻常的组合并不典型,因为小黄人通常与它们流行的动画和电影系列相关联。小黄人的工装裤、护目镜以及他们戴着的护目镜,增加了场景的奇特感。 | 2 | test_images/2.png |
| minigpt-4 | 图像中有什么异常之处吗? | 图像显示的是一个卡通角色,穿着工装裤和黄色衬衫。该角色面带微笑,头戴蓝色帽子。图像中没有任何异常之处。 | 5 | test_images/2.png |
首先,本笔记本旨在展示如何利用 Replicate 为图像推理任务提供不同的多模态大语言模型服务。此类比较存在一些局限性:
- 我们针对一些简单且有限的任务/提示,对LLaVa-13B、Fuyu-8B和MiniGPT-4进行了比较和评估。
- 请注意
the hyperparameters for different models are the same in the example。超参数调优对于提升多模态大语言模型的质量可能具有显著作用。 - 人工评估可能存在一定的偏见/主观性/噪声
一些初步发现:
MiniGPT-4有时能得出更准确的答案,例如There are two minions in the image.而非来自LlaVa或Fuyu-8B的There are two people shown in the image.。另一个例子是,对于意大利斗兽场图像的问题what is it in the image,MiniGPT-4直接回答了Colosseum。MiniGPT-4未能为两个提示给出结果。它回答I'm sorry, but I cannot see the image you provided.但它可以为相同的图像回答其他问题。不确定这是 Replicate 推理的问题还是 MiniGPT-4 模型本身的问题Fuyu-8B和LlaVa-13B通常会生成更详细冗长的回答,包含更多支持性上下文。Llava-13B和Fuyu-8B有时会产生稍高的hallucination,特别是对于问题is there anything unusual in the image?
Replicate Stream Complete、Async Complete、Async Stream Complete 模式
Section titled “Replicate Stream Complete, Async Complete, Async Stream Complete Mode”初始化 Fuyu-8B 模型
Section titled “Init Fuyu-8B Model”multi_modal_llm = ReplicateMultiModal( model=REPLICATE_MULTI_MODAL_LLM_MODELS["fuyu-8b"], max_new_tokens=100, temperature=0.1, num_input_files=1, top_p=0.9, num_beams=1, repetition_penalty=1,)resp = await multi_modal_llm.astream_complete( prompt="tell me about this image", image_documents=[image_documents[0]],)async for delta in resp: print(delta.delta, end="") The image features a man wearing a suit and tie, standing in front of a stage with a backdrop. He is holding a golden ball trophy, possibly an award, in his hands. The man appears to be posing for a photo, possibly celebrating his achievement or receiving an award.
In the background, there are multiple people visible, possibly attending or participating in the event. The backdrop appears to be a large screen, possibly displaying information about the event or ceremony.resp = await multi_modal_llm.acomplete( prompt="tell me about this image", image_documents=[image_documents[0]],)print(resp) The image features a man wearing a suit and tie, standing in front of a stage with a backdrop. He is holding a golden ball trophy, possibly an award, in his hands. The man appears to be posing for a photo, possibly celebrating his achievement or receiving an award.
In the background, there are multiple people visible, possibly attending or participating in the event. The backdrop appears to be a large screen, possibly displaying information about the event or ceremony.resp = multi_modal_llm.stream_complete( prompt="tell me about this image", image_documents=[image_documents[0]],)for delta in resp: print(delta.delta, end="") The image features a man wearing a suit and tie, standing in front of a stage with a backdrop. He is holding a golden ball trophy, possibly an award, in his hands. The man appears to be posing for a photo, possibly celebrating his achievement or receiving an award.
In the background, there are multiple people visible, possibly attending or participating in the event. The backdrop appears to be a large screen, possibly displaying information about the event or ceremony.