Emu3:開啟多模態人工智能新紀元 —— 視頻、圖像、文本三合一模型
隨著人工智能技術的不斷進步,人們對能夠處理多種模態數據的模型需求日益增長。圖像、文本和視頻作為三種主要的信息載體,各自具有獨特的優勢和特點。然而,傳統的人工智能模型往往只能處理單一類型的數據,無法充分發揮多模態數據的潛力。為了打破這一局限,北京智源人工智能研究院的科研團隊經過不懈努力,成功研發出了 Emu3 原生多模態世界模型。
一、Emu3 的技術特點
1. 多模態統一理解與生成
Emu3 實現了視頻、圖像、文本三種模態的統一理解與生成。這意味著它可以同時處理和關聯不同模態的數據,從而更好地理解和模擬物理世界。例如,當給定一段文本描述時,Emu3 可以生成相應的圖像或視頻;反之,當輸入一張圖像或一段視頻時,它也能夠生成準確的文本描述。這種多模態的交互能力為人工智能在各個領域的應用提供了更廣闊的空間。
2. 創新的技術架構
Emu3 只基于下一個 token 預測,無需擴散模型或組合式方法。它將圖像、文本和視頻編碼為一個離散空間,在多模態混合序列上從頭開始聯合訓練一個 transformer 模型。這種創新的架構設計不僅簡化了模型的復雜性,還提高了模型的靈活性和效率。同時,它也為多模態人工智能的發展提供了新的思路和方法。
3. 卓越的性能表現
在圖像生成、視覺語言理解、視頻生成任務中,Emu3 的表現超過了 SDXL、LLava - 1.6、OpenSORA 等知名開源模型。這充分證明了 Emu3 在多模態處理方面的強大實力。例如,在圖像生成任務中,Emu3 能夠生成更加逼真、細膩的圖像,細節豐富度和色彩還原度都達到了很高的水平;在視覺語言理解任務中,它能夠準確地理解圖像中的內容,并生成與之對應的文本描述;在視頻生成任務中,它可以生成高質量、流暢的視頻,具有很高的視覺效果和藝術價值。
二、Emu3 的應用前景
1. 機器人大腦
Emu3 的多模態處理能力可以為機器人提供更加智能的大腦。通過對圖像、文本和視頻的綜合理解,機器人可以更好地感知周圍環境,與人類進行更加自然的交互,并執行更加復雜的任務。例如,在家庭服務機器人中,Emu3 可以幫助機器人識別不同的物品、理解人類的指令,并根據環境變化做出相應的反應。
2. 自動駕駛
在自動駕駛領域,Emu3 可以通過對攝像頭拍攝的圖像和視頻進行實時分析,識別道路標志、車輛和行人等物體,并預測它們的運動軌跡。同時,它還可以結合地圖信息和交通規則等文本數據,為自動駕駛系統提供更加準確的決策依據。這將大大提高自動駕駛的安全性和可靠性。
3. 多模態對話和推理
Emu3 可以應用于多模態對話和推理系統中,實現更加自然、智能的人機交互。例如,在智能客服領域,它可以通過對用戶的語音、圖像和文本輸入進行綜合分析,理解用戶的問題,并給出準確的回答;在智能教育領域,它可以根據學生的學習情況和需求,生成個性化的教學內容和輔導方案。
三、Emu3 本地部署實踐
1、克隆代碼
git clone https://github.com/baaivision/Emu3
2、安裝依賴
cd Emu3
pip install -r requirements.txt
3、模型推理
使用Transformers運行Emu3-Gen/Stage1進行圖像生成
from PIL import Image
from transformers import AutoTokenizer, AutoModel, AutoImageProcessor, AutoModelForCausalLM
from transformers.generation.configuration_utils import GenerationConfig
from transformers.generation import LogitsProcessorList, PrefixConstrainedLogitsProcessor, UnbatchedClassifierFreeGuidanceLogitsProcessor
import torch
from emu3.mllm.processing_emu3 import Emu3Processor
# model path
EMU_HUB = "BAAI/Emu3-Gen"
VQ_HUB = "BAAI/Emu3-VisionTokenizer"
# prepare model and processor
model = AutoModelForCausalLM.from_pretrained(
EMU_HUB,
device_map="cuda:0",
torch_dtype=torch.bfloat16,
attn_implementatinotallow="flash_attention_2",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(EMU_HUB, trust_remote_code=True, padding_side="left")
image_processor = AutoImageProcessor.from_pretrained(VQ_HUB, trust_remote_code=True)
image_tokenizer = AutoModel.from_pretrained(VQ_HUB, device_map="cuda:0", trust_remote_code=True).eval()
processor = Emu3Processor(image_processor, image_tokenizer, tokenizer)
# prepare input
POSITIVE_PROMPT = " masterpiece, film grained, best quality."
NEGATIVE_PROMPT = "lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry."
classifier_free_guidance = 3.0
prompt = "a portrait of young girl."
prompt += POSITIVE_PROMPT
kwargs = dict(
mode='G',
ratio="1:1",
image_area=model.config.image_area,
return_tensors="pt",
padding="longest",
)
pos_inputs = processor(text=prompt, **kwargs)
neg_inputs = processor(text=NEGATIVE_PROMPT, **kwargs)
# prepare hyper parameters
GENERATION_CONFIG = GenerationConfig(
use_cache=True,
eos_token_id=model.config.eos_token_id,
pad_token_id=model.config.pad_token_id,
max_new_tokens=40960,
do_sample=True,
top_k=2048,
)
h = pos_inputs.image_size[:, 0]
w = pos_inputs.image_size[:, 1]
constrained_fn = processor.build_prefix_constrained_fn(h, w)
logits_processor = LogitsProcessorList([
UnbatchedClassifierFreeGuidanceLogitsProcessor(
classifier_free_guidance,
model,
unconditional_ids=neg_inputs.input_ids.to("cuda:0"),
),
PrefixConstrainedLogitsProcessor(
constrained_fn ,
num_beams=1,
),
])
# generate
outputs = model.generate(
pos_inputs.input_ids.to("cuda:0"),
GENERATION_CONFIG,
logits_processor=logits_processor,
attention_mask=pos_inputs.attention_mask.to("cuda:0"),
)
mm_list = processor.decode(outputs[0])
for idx, im in enumerate(mm_list):
if not isinstance(im, Image.Image):
continue
im.save(f"result_{idx}.png")
使用Transformers運行Emu3-Chat/Stage1以實現視覺語言理解
from PIL import Image
from transformers import AutoTokenizer, AutoModel, AutoImageProcessor, AutoModelForCausalLM
from transformers.generation.configuration_utils import GenerationConfig
import torch
from emu3.mllm.processing_emu3 import Emu3Processor
# model path
EMU_HUB = "BAAI/Emu3-Chat"
VQ_HUB = "BAAI/Emu3-VisionTokenier"
# prepare model and processor
model = AutoModelForCausalLM.from_pretrained(
EMU_HUB,
device_map="cuda:0",
torch_dtype=torch.bfloat16,
attn_implementatinotallow="flash_attention_2",
trust_remote_code=True,
)
# used for Emu3-Chat
tokenizer = AutoTokenizer.from_pretrained(EMU_HUB, trust_remote_code=True, padding_side="left")
# used for Emu3-Stage1
# tokenizer = AutoTokenizer.from_pretrained(
# EMU_HUB,
# trust_remote_code=True,
# chat_template="{image_prompt}{text_prompt}",
# padding_side="left",
# )
image_processor = AutoImageProcessor.from_pretrained(VQ_HUB, trust_remote_code=True)
image_tokenizer = AutoModel.from_pretrained(VQ_HUB, device_map="cuda:0", trust_remote_code=True).eval()
processor = Emu3Processor(image_processor, image_tokenizer, tokenizer)
# prepare input
text = "Please describe the image"
image = Image.open("assets/demo.png")
inputs = processor(
text=text,
image=image,
mode='U',
return_tensors="pt",
padding="longest",
)
# prepare hyper parameters
GENERATION_CONFIG = GenerationConfig(
pad_token_id=tokenizer.pad_token_id,
bos_token_id=tokenizer.bos_token_id,
eos_token_id=tokenizer.eos_token_id,
max_new_tokens=1024,
)
# generate
outputs = model.generate(
inputs.input_ids.to("cuda:0"),
GENERATION_CONFIG,
attention_mask=pos_inputs.attention_mask.to("cuda:0"),
)
outputs = outputs[:, inputs.input_ids.shape[-1]:]
print(processor.batch_decode(outputs, skip_special_tokens=True)[0])
四、結語
Emu3 作為北京智源人工智能研究院推出的原生多模態世界模型,代表了人工智能領域的一次重大突破。它的多模態統一理解與生成能力、創新的技術架構和卓越的性能表現,為我們展示了人工智能未來的發展方向。雖然它還面臨著一些挑戰,但隨著技術的不斷進步和完善,相信 Emu3 將在機器人大腦、自動駕駛、多模態對話和推理等領域發揮重要作用,為我們的生活帶來更多的便利和創新。
相關資料
github地址:??https://github.com/baaivision/Emu3??
Emu3-VisionTokenizer模型:??https://modelscope.cn/models/BAAI/Emu3-VisionTokenizer??
Emu3-Gen模型:??https://modelscope.cn/models/BAAI/Emu3-Gen??
Emu3-Chat模型:??https://modelscope.cn/models/BAAI/Emu3-Chat??
本文轉載自??小兵的AI視界??,作者: 小兵
