Skip to content

Commit 0b696e1

Browse files
authored
janus notebook (#2559)
1 parent 57854dc commit 0b696e1

File tree

5 files changed

+1736
-0
lines changed

5 files changed

+1736
-0
lines changed

.ci/spellcheck/.pyspelling.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ llava
457457
llm
458458
LLM
459459
LLMs
460+
lm
460461
LM
461462
LMS
462463
LLMPipeline
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Multimodal understanding and generation with Janus and OpenVINO
2+
3+
Janus is a novel autoregressive framework that unifies multimodal understanding and generation. It addresses the limitations of previous approaches by decoupling visual encoding into separate pathways, while still utilizing a single, unified transformer architecture for processing. The decoupling not only alleviates the conflict between the visual encoder’s roles in understanding and generation, but also enhances the framework’s flexibility. Janus surpasses previous unified model and matches or exceeds the performance of task-specific models. The simplicity, high flexibility, and effectiveness of Janus make it a strong candidate for next-generation unified multimodal models.
4+
5+
More details can be found in the [paper](https://arxiv.org/abs/2410.13848), original [repository](https://github.com/deepseek-ai/Janus) and [model card](https://huggingface.co/deepseek-ai/Janus-1.3B)
6+
7+
In this tutorial we consider how to run and optimize Janus using OpenVINO. Additionally, we demonstrate how to apply stateful transformation on LLM part and model optimization techniques like weights compression using [NNCF](https://github.com/openvinotoolkit/nncf)
8+
9+
## Notebook contents
10+
The tutorial consists from following steps:
11+
12+
- Install requirements
13+
- Convert and Optimize model
14+
- Run OpenVINO model inference
15+
- Launch Interactive demo
16+
17+
In this demonstration, you'll create interactive assistant that can answer questions about provided image's content or generate images based on text instructions.
18+
19+
The images bellow illustrates example of input prompt and model answer for image understanding and generation
20+
![example.png](https://github.com/user-attachments/assets/89a71be8-b472-4acd-a2e0-dbc97645fc1c)
21+
![example2.png](https://github.com/user-attachments/assets/5aca2b37-52d9-403d-a773-311ccf82b375)
22+
23+
## Installation instructions
24+
This is a self-contained example that relies solely on its own code.</br>
25+
We recommend running the notebook in a virtual environment. You only need a Jupyter server to start.
26+
For details, please refer to [Installation Guide](../../README.md).
27+
28+
<img referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=5b5a4db0-7875-4bfb-bdbd-01698b5b1a77&file=notebooks/janus-multimodal-generation/README.md" />
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import requests
2+
from io import BytesIO
3+
from pathlib import Path
4+
from threading import Thread
5+
import gradio as gr
6+
import torch
7+
from transformers import set_seed, TextIteratorStreamer
8+
from PIL import Image
9+
import numpy as np
10+
from ov_janus_helper import generate_image
11+
12+
13+
def download_example_images():
14+
image_urls = [
15+
"https://github.com/deepseek-ai/Janus/blob/main/images/pie_chart.png?raw=true",
16+
"https://github.com/deepseek-ai/Janus/blob/main/images/equation.png?raw=true",
17+
]
18+
image_names = ["pie_chart.png", "equation.png"]
19+
20+
for image_name, image_url in zip(image_names, image_urls):
21+
if not Path(image_name).exists():
22+
response = requests.get(image_url)
23+
image = Image.open(BytesIO(response.content)).convert("RGB")
24+
image.save(image_name)
25+
26+
27+
def make_demo(model, processor):
28+
download_example_images()
29+
30+
# Multimodal Understanding function
31+
def multimodal_understanding(image, question, seed, top_p, temperature):
32+
# set seed
33+
torch.manual_seed(seed)
34+
np.random.seed(seed)
35+
36+
conversation = [
37+
{
38+
"role": "User",
39+
"content": f"<image_placeholder>\n{question}",
40+
"images": [image],
41+
},
42+
{"role": "Assistant", "content": ""},
43+
]
44+
45+
pil_images = [Image.fromarray(image)]
46+
prepare_inputs = processor(conversations=conversation, images=pil_images, force_batchify=True)
47+
48+
inputs_embeds = model.prepare_inputs_embeds(**prepare_inputs)
49+
50+
streamer = TextIteratorStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=True)
51+
generate_kwargs = {
52+
"inputs_embeds": inputs_embeds,
53+
"attention_mask": prepare_inputs.attention_mask,
54+
"streamer": streamer,
55+
"max_new_tokens": 512,
56+
"pad_token_id": processor.tokenizer.eos_token_id,
57+
"bos_token_id": processor.tokenizer.bos_token_id,
58+
"eos_token_id": processor.tokenizer.eos_token_id,
59+
"do_sample": False if temperature == 0 else True,
60+
"temperature": temperature,
61+
"top_p": top_p,
62+
}
63+
t = Thread(target=model.language_model.generate, kwargs=generate_kwargs)
64+
t.start()
65+
66+
# Pull the generated text from the streamer, and update the model output.
67+
model_output = ""
68+
for new_text in streamer:
69+
model_output += new_text
70+
yield model_output
71+
return model_output
72+
73+
def image_generation(prompt, seed, cfg_weight, num_images, progress=gr.Progress(track_tqdm=True)):
74+
set_seed(seed)
75+
images = generate_image(model, processor, prompt, cfg_weight=cfg_weight, parallel_size=int(num_images))
76+
images = [img.resize((1024, 1024), Image.LANCZOS) for img in images]
77+
return images
78+
79+
# Gradio interface
80+
with gr.Blocks() as demo:
81+
gr.Markdown(value="# Multimodal Understanding")
82+
# with gr.Row():
83+
with gr.Row():
84+
image_input = gr.Image()
85+
with gr.Column():
86+
question_input = gr.Textbox(label="Question")
87+
und_seed_input = gr.Number(label="Seed", precision=0, value=42)
88+
top_p = gr.Slider(minimum=0, maximum=1, value=0.95, step=0.05, label="top_p")
89+
temperature = gr.Slider(minimum=0, maximum=1, value=0.1, step=0.05, label="temperature")
90+
91+
understanding_button = gr.Button("Chat")
92+
understanding_output = gr.Textbox(label="Response")
93+
94+
examples_vl = gr.Examples(
95+
label="Multimodal Understanding examples",
96+
examples=[
97+
[
98+
"explain this chart",
99+
"pie_chart.png",
100+
],
101+
[
102+
"Convert the formula into latex code.",
103+
"equation.png",
104+
],
105+
],
106+
inputs=[question_input, image_input],
107+
)
108+
109+
gr.Markdown(value="# Text-to-Image Generation")
110+
111+
with gr.Row():
112+
cfg_weight_input = gr.Slider(minimum=1, maximum=10, value=5, step=0.5, label="CFG Weight")
113+
114+
prompt_input = gr.Textbox(label="Prompt")
115+
seed_input = gr.Number(label="Seed (Optional)", precision=0, value=12345)
116+
num_images = gr.Slider(minimum=1, maximum=32, step=1, value=2, label="Number of generated images")
117+
118+
generation_button = gr.Button("Generate Images")
119+
120+
image_output = gr.Gallery(label="Generated Images", columns=2, rows=2, height=300)
121+
122+
examples_t2i = gr.Examples(
123+
label="Text to image generation examples. (Tips for designing prompts: Adding description like 'digital art' at the end of the prompt or writing the prompt in more detail can help produce better images!)",
124+
examples=[
125+
"Master shifu racoon wearing drip attire as a street gangster.",
126+
"A cute and adorable baby fox with big brown eyes, autumn leaves in the background enchanting,immortal,fluffy, shiny mane,Petals,fairyism,unreal engine 5 and Octane Render,highly detailed, photorealistic, cinematic, natural colors.",
127+
"The image features an intricately designed eye set against a circular backdrop adorned with ornate swirl patterns that evoke both realism and surrealism. At the center of attention is a strikingly vivid blue iris surrounded by delicate veins radiating outward from the pupil to create depth and intensity. The eyelashes are long and dark, casting subtle shadows on the skin around them which appears smooth yet slightly textured as if aged or weathered over time.\n\nAbove the eye, there's a stone-like structure resembling part of classical architecture, adding layers of mystery and timeless elegance to the composition. This architectural element contrasts sharply but harmoniously with the organic curves surrounding it. Below the eye lies another decorative motif reminiscent of baroque artistry, further enhancing the overall sense of eternity encapsulated within each meticulously crafted detail. \n\nOverall, the atmosphere exudes a mysterious aura intertwined seamlessly with elements suggesting timelessness, achieved through the juxtaposition of realistic textures and surreal artistic flourishes. Each component\u2014from the intricate designs framing the eye to the ancient-looking stone piece above\u2014contributes uniquely towards creating a visually captivating tableau imbued with enigmatic allure.",
128+
],
129+
inputs=prompt_input,
130+
)
131+
132+
understanding_button.click(
133+
multimodal_understanding, inputs=[image_input, question_input, und_seed_input, top_p, temperature], outputs=understanding_output
134+
)
135+
136+
generation_button.click(fn=image_generation, inputs=[prompt_input, seed_input, cfg_weight_input, num_images], outputs=image_output)
137+
138+
return demo

0 commit comments

Comments
 (0)