Home Backend Development Python Tutorial Janus B: A Unified Model for Multimodal Understanding and Generation Tasks

Janus B: A Unified Model for Multimodal Understanding and Generation Tasks

Oct 19, 2024 pm 12:16 PM

Janus 1.3B

Janus is a new autoregressive framework that integrates multimodal understanding and generation. Unlike previous models, which used a single visual encoder for both understanding and generation tasks, Janus introduces two separate visual encoding pathways for these functions.

Differences in Encoding for Understanding and Generation

  • In multimodal understanding tasks, the visual encoder extracts high-level semantic information such as object categories and visual attributes. This encoder focuses on inferring complex meanings, emphasizing higher-dimensional semantic elements.
  • On the other hand, in visual generation tasks, emphasis is placed on generating fine details and maintaining overall consistency. As a result, lower-dimensional encoding that can capture spatial structures and textures is required.

Setting Up the Environment

Here are the steps to run Janus in Google Colab:

1

2

3

4

5

6

git clone https://github.com/deepseek-ai/Janus

cd Janus

pip install -e .

# If needed, install the following as well

# pip install wheel

# pip install flash-attn --no-build-isolation

Copy after login

Vision Tasks

Loading the Model

Use the following code to load the necessary model for vision tasks:

1

2

3

4

5

6

7

8

9

10

11

12

import torch

from transformers import AutoModelForCausalLM

from janus.models import MultiModalityCausalLM, VLChatProcessor

from janus.utils.io import load_pil_images

 

# Specify the model path

model_path = "deepseek-ai/Janus-1.3B"

vl_chat_processor = VLChatProcessor.from_pretrained(model_path)

tokenizer = vl_chat_processor.tokenizer

 

vl_gpt = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)

vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()

Copy after login

Loading and Preparing Images for Encoding

Next, load the image and convert it into a format that the model can understand:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

conversation = [

    {

        "role": "User",

        "content": "<image_placeholder>\nDescribe this chart.",

        "images": ["images/pie_chart.png"],

    },

    {"role": "Assistant", "content": ""},

]

 

# Load the image and prepare input

pil_images = load_pil_images(conversation)

prepare_inputs = vl_chat_processor(

    conversations=conversation, images=pil_images, force_batchify=True

).to(vl_gpt.device)

 

# Run the image encoder and obtain image embeddings

inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)

Copy after login

Generating a Response

Finally, run the model to generate a response:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

# Run the model and generate a response

outputs = vl_gpt.language_model.generate(

    inputs_embeds=inputs_embeds,

    attention_mask=prepare_inputs.attention_mask,

    pad_token_id=tokenizer.eos_token_id,

    bos_token_id=tokenizer.bos_token_id,

    eos_token_id=tokenizer.eos_token_id,

    max_new_tokens=512,

    do_sample=False,

    use_cache=True,

)

 

answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)

print(f"{prepare_inputs['sft_format'][0]}", answer)

Copy after login

Example Output

Janus B: A Unified Model for Multimodal Understanding and Generation Tasks

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

The image depicts a pie chart that illustrates the distribution of four different categories among four distinct groups. The chart is divided into four segments, each representing a category with a specific percentage. The categories and their corresponding percentages are as follows:

 

1. **Hogs**: This segment is colored in orange and represents 30.0% of the total.

2. **Frog**: This segment is colored in blue and represents 15.0% of the total.

3. **Logs**: This segment is colored in red and represents 10.0% of the total.

4. **Dogs**: This segment is colored in green and represents 45.0% of the total.

 

The pie chart is visually divided into four segments, each with a different color and corresponding percentage. The segments are arranged in a clockwise manner starting from the top-left, moving clockwise. The percentages are clearly labeled next to each segment.

 

The chart is a simple visual representation of data, where the size of each segment corresponds to the percentage of the total category it represents. This type of chart is commonly used to compare the proportions of different categories in a dataset.

 

To summarize, the pie chart shows the following:

- Hogs: 30.0%

- Frog: 15.0%

- Logs: 10.0%

- Dogs: 45.0%

 

This chart can be used to understand the relative proportions of each category in the given dataset.

Copy after login

The output demonstrates an appropriate understanding of the image, including its colors and text.

Image Generation Tasks

Loading the Model

Load the necessary model for image generation tasks with the following code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import os

import PIL.Image

import torch

import numpy as np

from transformers import AutoModelForCausalLM

from janus.models import MultiModalityCausalLM, VLChatProcessor

 

# Specify the model path

model_path = "deepseek-ai/Janus-1.3B"

vl_chat_processor = VLChatProcessor.from_pretrained(model_path)

tokenizer = vl_chat_processor.tokenizer

 

vl_gpt = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)

vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()

Copy after login

Preparing the Prompt

Next, prepare the prompt based on the user’s request:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

# Set up the prompt

conversation = [

    {

        "role": "User",

        "content": "cute japanese girl, wearing a bikini, in a beach",

    },

    {"role": "Assistant", "content": ""},

]

 

# Convert the prompt into the appropriate format

sft_format = vl_chat_processor.apply_sft_template_for_multi_turn_prompts(

    conversations=conversation,

    sft_format=vl_chat_processor.sft_format,

    system_prompt="",

)

 

prompt = sft_format + vl_chat_processor.image_start_tag

Copy after login

Generating the Image

The following function is used to generate images. By default, 16 images are generated:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

@torch.inference_mode()

def generate(

    mmgpt: MultiModalityCausalLM,

    vl_chat_processor: VLChatProcessor,

    prompt: str,

    temperature: float = 1,

    parallel_size: int = 16,

    cfg_weight: float = 5,

    image_token_num_per_image: int = 576,

    img_size: int = 384,

    patch_size: int = 16,

):

    input_ids = vl_chat_processor.tokenizer.encode(prompt)

    input_ids = torch.LongTensor(input_ids)

 

    tokens = torch.zeros((parallel_size*2, len(input_ids)), dtype=torch.int).cuda()

    for i in range(parallel_size*2):

        tokens[i, :] = input_ids

        if i % 2 != 0:

            tokens[i, 1:-1] = vl_chat_processor.pad_id

 

    inputs_embeds = mmgpt.language_model.get_input_embeddings()(tokens)

 

    generated_tokens = torch.zeros((parallel_size, image_token_num_per_image), dtype=torch.int).cuda()

 

    for i in range(image_token_num_per_image):

        outputs = mmgpt.language_model.model(

            inputs_embeds=inputs_embeds,

            use_cache=True,

            past_key_values=outputs.past_key_values if i != 0 else None,

        )

        hidden_states = outputs.last_hidden_state

 

        logits = mmgpt.gen_head(hidden_states[:, -1, :])

        logit_cond = logits[0::2, :]

        logit_uncond = logits[1::2, :]

 

        logits = logit_uncond + cfg_weight * (logit_cond - logit_uncond)

        probs = torch.softmax(logits / temperature, dim=-1)

 

        next_token = torch.multinomial(probs, num_samples=1)

        generated_tokens[:, i] = next_token.squeeze(dim=-1)

 

        next_token = torch.cat([next_token.unsqueeze(dim=1), next_token.unsqueeze(dim=1)], dim=1).view(-1)

        img_embeds = mmgpt.prepare_gen_img_embeds(next_token)

        inputs_embeds = img_embeds.unsqueeze(dim=1)

 

    dec = mmgpt.gen_vision_model.decode_code(

        generated_tokens.to(dtype=torch.int),

        shape=[parallel_size, 8, img_size // patch_size, img_size // patch_size],

    )

    dec = dec.to(torch.float32).cpu().numpy().transpose(0, 2, 3, 1)

    dec = np.clip((dec + 1) / 2 * 255, 0, 255)

 

    visual_img = np.zeros((parallel_size, img_size, img_size, 3), dtype=np.uint8)

    visual_img[:, :, :] = dec

 

    os.makedirs('generated_samples', exist_ok=True)

    for i in range(parallel_size):

        save_path = os.path.join('generated_samples', f"img_{i}.jpg")

        PIL.Image.fromarray(visual_img[i]).save(save_path)

 

# Run the image generation

generate(vl_gpt, vl_chat_processor, prompt)

Copy after login

The generated images will be saved in the generated_samples folder.

Sample of Generated Results

Below is an example of a generated image:

Janus B: A Unified Model for Multimodal Understanding and Generation Tasks

  • Dogs are relatively well depicted.
  • Buildings maintain overall shape, though some details, like windows, may appear unrealistic.
  • Humans, however, are challenging to generate well, with notable distortions in both photo-realistic and anime-like styles.

The above is the detailed content of Janus B: A Unified Model for Multimodal Understanding and Generation Tasks. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1229
24
Python vs. C  : Applications and Use Cases Compared Python vs. C : Applications and Use Cases Compared Apr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

Python: Games, GUIs, and More Python: Games, GUIs, and More Apr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

How Much Python Can You Learn in 2 Hours? How Much Python Can You Learn in 2 Hours? Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

The 2-Hour Python Plan: A Realistic Approach The 2-Hour Python Plan: A Realistic Approach Apr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python and Time: Making the Most of Your Study Time Python and Time: Making the Most of Your Study Time Apr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Exploring Its Primary Applications Python: Exploring Its Primary Applications Apr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

See all articles