Emu3 使用教程

Emu3 使用教程

Emu3 Next-Token Prediction is All You Need Emu3 项目地址: https://gitcode.com/gh_mirrors/em/Emu3

1. 项目介绍

Emu3 是由 BAAI 开发的一款全新的多模态模型。该模型基于 next-token prediction 技术,将图像、文本和视频统一编码到一个离散空间中,并从零开始训练一个单一的 Transformer 模型。Emu3 在生成和感知任务中都有出色的表现,超越了多个知名的任务特定模型。

2. 项目快速启动

首先,您需要克隆这个仓库并安装必要的包:

git clone https://github.com/baaivision/Emu3.git
cd Emu3
pip install -r requirements.txt

以下是一个使用 Emu3-Gen 进行图像生成的示例代码:

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

# 模型路径
EMU_HUB = "BAAI/Emu3-Gen"
VQ_HUB = "BAAI/Emu3-VisionTokenizer"

# 准备模型和处理器
model = AutoModelForCausalLM.from_pretrained(
    EMU_HUB,
    device_map="cuda:0",
    torch_dtype=torch.bfloat16,
    attn_implementation="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
)

# 准备输入
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)

# 准备超参数
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,
    ),
])

# 生成图像
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")

3. 应用案例和最佳实践

图像生成

使用 Emu3-Gen 模型,您可以简单地通过预测下一个视觉 token 来生成高质量的图像。该模型支持灵活的分辨率和风格。

视觉-语言理解

Emu3-Chat 模型具有强大的视觉-语言理解能力,能够理解物理世界并给出连贯的文本回应。

视频生成

Emu3 模型可以简单地通过预测视频序列中的下一个 token 来生成视频,而不需要像 Sora 这样的视频扩散模型。

4. 典型生态项目

目前 Emu3 的生态项目包括但不限于:

  • Emu3-Stage1:用于图像生成和感知任务的预训练模型。
  • Emu3-Chat:用于视觉-语言理解的模型。
  • Emu3-Gen:用于图像生成的模型。

这些模型都可以在 Hugging Face Model Hub 上找到相应的权重和代码。

Emu3 Next-Token Prediction is All You Need Emu3 项目地址: https://gitcode.com/gh_mirrors/em/Emu3

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

段琳惟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值