Views
No views yet
1import cv2
2from diffusers import DiffusionPipeline
3import numpy as np
4from PIL import Image
5import requests
6from transformers import AutoModelForCausalLM, AutoTokenizer
7import torch
8
9# For the first time of using,
10# you need to download the huggingface repo "BAAI/Emu2-GEN" to local first
11path = "path to local BAAI/Emu2-GEN"
12
13multimodal_encoder = AutoModelForCausalLM.from_pretrained(
14 f"{path}/multimodal_encoder",
15 trust_remote_code=True,
16 torch_dtype=torch.bfloat16,
17 use_safetensors=True,
18 variant="bf16"
19)
20tokenizer = AutoTokenizer.from_pretrained(f"{path}/tokenizer")
21
22pipe = DiffusionPipeline.from_pretrained(
23 path,
24 custom_pipeline="pipeline_emu2_gen",
25 torch_dtype=torch.bfloat16,
26 use_safetensors=True,
27 variant="bf16",
28 multimodal_encoder=multimodal_encoder,
29 tokenizer=tokenizer,
30)
31
32# For the non-first time of using, you can init the pipeline directly
33pipe = DiffusionPipeline.from_pretrained(
34 path,
35 custom_pipeline="pipeline_emu2_gen",
36 torch_dtype=torch.bfloat16,
37 use_safetensors=True,
38 variant="bf16",
39)
40
41pipe.to("cuda")
42
43# text-to-image
44prompt = "impressionist painting of an astronaut in a jungle"
45ret = pipe(prompt)
46ret.image.save("astronaut.png")
47
48# image editing
49image = Image.open(requests.get('https://github.com/baaivision/Emu/Emu2/examples/dog.jpg?raw=true',stream=True).raw).convert('RGB')
50prompt = [image, "wearing a red hat on the beach."]
51ret = pipe(prompt)
52ret.image.save("dog_hat_beach.png")
53
54# grounding generation
55def draw_box(left, top, right, bottom):
56 mask = np.zeros((448, 448, 3), dtype=np.uint8)
57 mask = cv2.rectangle(mask, (left, top), (right, bottom), (255, 255, 255), 3)
58 mask = Image.fromarray(mask)
59 return mask
60
61dog1 = Image.open(requests.get('https://github.com/baaivision/Emu/Emu2/examples/dog1.jpg?raw=true',stream=True).raw).convert('RGB')
62dog2 = Image.open(requests.get('https://github.com/baaivision/Emu/Emu2/examples/dog2.jpg?raw=true',stream=True).raw).convert('RGB')
63dog3 = Image.open(requests.get('https://github.com/baaivision/Emu/Emu2/examples/dog3.jpg?raw=true',stream=True).raw).convert('RGB')
64dog1_mask = draw_box( 22, 14, 224, 224)
65dog2_mask = draw_box(224, 10, 448, 224)
66dog3_mask = draw_box(120, 264, 320, 438)
67
68prompt = [
69 "<grounding>",
70 "An oil painting of three dogs,",
71 "<phrase>the first dog</phrase>"
72 "<object>",
73 dog1_mask,
74 "</object>",
75 dog1,
76 "<phrase>the second dog</phrase>"
77 "<object>",
78 dog2_mask,
79 "</object>",
80 dog2,
81 "<phrase>the third dog</phrase>"
82 "<object>",
83 dog3_mask,
84 "</object>",
85 dog3,
86]
87ret = pipe(prompt)
88ret.image.save("three_dogs.png")
89
90# Autoencoding
91# to enable the autoencoding mode, you can only input exactly one image as prompt
92# if you want the model to generate an image,
93# please input extra empty text "" besides the image, e.g.
94# autoencoding mode: prompt = image or [image]
95# generation mode: prompt = ["", image] or [image, ""]
96prompt = Image.open("./examples/doodle.jpg").convert("RGB")
97ret = pipe(prompt)
98ret.image.save("doodle_ae.png")@article{Emu2,
title={Generative Multimodal Models are In-Context Learners},
author={Quan Sun and Yufeng Cui and Xiaosong Zhang and Fan Zhang and Qiying Yu and Zhengxiong Luo and Yueze Wang and Yongming Rao and Jingjing Liu and Tiejun Huang and Xinlong Wang},
publisher={arXiv preprint arXiv:2312.13286},
year={2023},
}