Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,125 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
pipeline_tag: any-to-any
|
3 |
+
license: apache-2.0
|
4 |
+
library_name: transformers
|
5 |
+
---
|
6 |
+
|
7 |
+
<div align='center'>
|
8 |
+
<h1>Emu3: Next-Token Prediction is All You Need</h1h1>
|
9 |
+
<h3></h3>
|
10 |
+
|
11 |
+
[Emu3 Team, BAAI](https://www.baai.ac.cn/english.html)
|
12 |
+
|
13 |
+
| [Project Page](https://emu.baai.ac.cn) | [Paper](https://huggingface.co/papers/2409.18869) | [🤗HF Models](https://huggingface.co/collections/BAAI/emu3-66f4e64f70850ff358a2e60f) | [github](https://github.com/baaivision/Emu3)
|
14 |
+
| [Demo](https://huggingface.co/spaces/BAAI/Emu3) |
|
15 |
+
|
16 |
+
|
17 |
+
</div>
|
18 |
+
|
19 |
+
<div align='center'>
|
20 |
+
<img src="https://github.com/baaivision/Emu3/blob/main/assets/arch.png?raw=True" class="interpolation-image" alt="arch." height="80%" width="70%" />
|
21 |
+
</div>
|
22 |
+
|
23 |
+
We introduce **Emu3**, a new suite of state-of-the-art multimodal models trained solely with **<i>next-token prediction</i>**! By tokenizing images, text, and videos into a discrete space, we train a single transformer from scratch on a mixture of multimodal sequences.
|
24 |
+
|
25 |
+
### Emu3 excels in both generation and perception
|
26 |
+
**Emu3** outperforms several well-established task-specific models in both generation and perception tasks, surpassing flagship open models such as SDXL, LLaVA-1.6 and OpenSora-1.2, while eliminating the need for diffusion or compositional architectures.
|
27 |
+
|
28 |
+
<div align='center'>
|
29 |
+
<img src="https://github.com/baaivision/Emu3/blob/main/assets/comparison.png?raw=True" class="interpolation-image" alt="comparison." height="80%" width="80%" />
|
30 |
+
</div>
|
31 |
+
|
32 |
+
### Highlights
|
33 |
+
|
34 |
+
- **Emu3** is capable of generating high-quality images following the text input, by simply predicting the next vision token. The model naturally supports flexible resolutions and styles.
|
35 |
+
- **Emu3** shows strong vision-language understanding capabilities to see the physical world and provides coherent text responses. Notably, this capability is achieved without depending on a CLIP and a pretrained LLM.
|
36 |
+
- **Emu3** simply generates a video causally by predicting the next token in a video sequence, unlike the video diffusion model as in Sora. With a video in context, Emu3 can also naturally extend the video and predict what will happen next.
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
#### Quickstart
|
41 |
+
|
42 |
+
```python
|
43 |
+
from PIL import Image
|
44 |
+
from transformers import AutoTokenizer, AutoModel, AutoImageProcessor, AutoModelForCausalLM
|
45 |
+
from transformers.generation.configuration_utils import GenerationConfig
|
46 |
+
from transformers.generation import LogitsProcessorList, PrefixConstrainedLogitsProcessor, UnbatchedClassifierFreeGuidanceLogitsProcessor
|
47 |
+
import torch
|
48 |
+
|
49 |
+
import sys
|
50 |
+
sys.path.append(PATH_TO_BAAI_Emu3-Gen_MODEL)
|
51 |
+
from processing_emu3 import Emu3Processor
|
52 |
+
|
53 |
+
# model path
|
54 |
+
EMU_HUB = "BAAI/Emu3-Gen"
|
55 |
+
VQ_HUB = "BAAI/Emu3-VisionTokenizer"
|
56 |
+
|
57 |
+
# prepare model and processor
|
58 |
+
model = AutoModelForCausalLM.from_pretrained(
|
59 |
+
EMU_HUB,
|
60 |
+
device_map="cuda:0",
|
61 |
+
torch_dtype=torch.bfloat16,
|
62 |
+
attn_implementation="flash_attention_2",
|
63 |
+
trust_remote_code=True,
|
64 |
+
)
|
65 |
+
|
66 |
+
tokenizer = AutoTokenizer.from_pretrained(EMU_HUB, trust_remote_code=True)
|
67 |
+
image_processor = AutoImageProcessor.from_pretrained(VQ_HUB, trust_remote_code=True)
|
68 |
+
image_tokenizer = AutoModel.from_pretrained(VQ_HUB, device_map="cuda:0", trust_remote_code=True).eval()
|
69 |
+
processor = Emu3Processor(image_processor, image_tokenizer, tokenizer)
|
70 |
+
|
71 |
+
# prepare input
|
72 |
+
POSITIVE_PROMPT = " masterpiece, film grained, best quality."
|
73 |
+
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."
|
74 |
+
|
75 |
+
classifier_free_guidance = 3.0
|
76 |
+
prompt = "a portrait of young girl."
|
77 |
+
prompt += POSITIVE_PROMPT
|
78 |
+
|
79 |
+
kwargs = dict(
|
80 |
+
mode='G',
|
81 |
+
ratio="1:1",
|
82 |
+
image_area=model.config.image_area,
|
83 |
+
return_tensors="pt",
|
84 |
+
)
|
85 |
+
pos_inputs = processor(text=prompt, **kwargs)
|
86 |
+
neg_inputs = processor(text=NEGATIVE_PROMPT, **kwargs)
|
87 |
+
|
88 |
+
# prepare hyper parameters
|
89 |
+
GENERATION_CONFIG = GenerationConfig(
|
90 |
+
use_cache=True,
|
91 |
+
eos_token_id=model.config.eos_token_id,
|
92 |
+
pad_token_id=model.config.pad_token_id,
|
93 |
+
max_new_tokens=40960,
|
94 |
+
do_sample=True,
|
95 |
+
top_k=2048,
|
96 |
+
)
|
97 |
+
|
98 |
+
h, w = pos_inputs.image_size[0]
|
99 |
+
constrained_fn = processor.build_prefix_constrained_fn(h, w)
|
100 |
+
logits_processor = LogitsProcessorList([
|
101 |
+
UnbatchedClassifierFreeGuidanceLogitsProcessor(
|
102 |
+
classifier_free_guidance,
|
103 |
+
model,
|
104 |
+
unconditional_ids=neg_inputs.input_ids.to("cuda:0"),
|
105 |
+
),
|
106 |
+
PrefixConstrainedLogitsProcessor(
|
107 |
+
constrained_fn ,
|
108 |
+
num_beams=1,
|
109 |
+
),
|
110 |
+
])
|
111 |
+
|
112 |
+
# generate
|
113 |
+
outputs = model.generate(
|
114 |
+
pos_inputs.input_ids.to("cuda:0"),
|
115 |
+
GENERATION_CONFIG,
|
116 |
+
logits_processor=logits_processor
|
117 |
+
)
|
118 |
+
|
119 |
+
mm_list = processor.decode(outputs[0])
|
120 |
+
for idx, im in enumerate(mm_list):
|
121 |
+
if not isinstance(im, Image.Image):
|
122 |
+
continue
|
123 |
+
im.save(f"result_{idx}.png")
|
124 |
+
|
125 |
+
```
|