DeepSeek开源Janus-Pro多模态理解生成模型,魔搭社区推理、微调最佳实践

简介: DeepSeek开源Janus-Pro多模态理解生成模型,魔搭社区推理、微调最佳实践

01引言


Janus-Pro是DeepSeek最新开源的多模态模型,是一种新颖的自回归框架,统一了多模态理解和生成。通过将视觉编码解耦为独立的路径,同时仍然使用单一的、统一的变压器架构进行处理,该框架解决了先前方法的局限性。这种解耦不仅缓解了视觉编码器在理解和生成中的角色冲突,还增强了框架的灵活性。Janus-Pro 超过了以前的统一模型,并且匹配或超过了特定任务模型的性能。Janus-Pro 的简洁性、高灵活性和有效性使其成为下一代统一多模态模型的强大候选者。


代码链接:

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


模型链接:

https://modelscope.cn/collections/Janus-Pro-0f5e48f6b96047


体验页面:

https://modelscope.cn/studios/AI-ModelScope/Janus-Pro-7B




Janus-Pro 是一个统一的理解和生成 MLLM,它将视觉编码解耦以支持多模态理解和生成。Janus-Pro 基于 DeepSeek-LLM-1.5b-base/DeepSeek-LLM-7b-base 构建。


对于多模态理解,它使用 SigLIP-L 作为视觉编码器,支持 384 x 384 图像输入。对于图像生成,Janus-Pro 使用来自LlamaGen的分词器,降采样率为 16。


02模型效果


图片生成

简单 Prompt

an apple

a car

a dog


复杂 Prompt

a bear standing on a car, sunset, winter

a boy and a girl, the boy stands at the left side, the boy wears a red t-shirt and blue pants, the girl wears a green t-shirt and pink pants.

the apple is in the box, the box is on the chair, the chair is on the desk, the desk is in the room

颜色可以做到分开控制


多风格

Chinese ink painting, a girl, long hair, colorful hair, shining eyes

oil painting, a girl, long hair, colorful hair, shining eyes

anime, a girl, long hair, colorful hair, shining eyes


图片理解


识物



提问:

这道菜是什么?


回答:

这道菜看起来是一道中式炖菜,可能是“牛杂汤”或“牛杂火锅”。它通常由牛的各个部位的肉和内脏组成,如牛肚、牛肠、牛筋等。这些食材经过炖煮,味道浓郁,口感丰富。牛杂汤通常会加入一些香料和调味料,如八角、桂皮、花椒等,以增加风味。这道菜不仅味道鲜美,而且营养丰富,富含蛋白质和多种微量元素。



OCR&公式转换



提问:

Convert the formula into latex code.


回答:

Here is the formula converted into LaTeX code:

\[

A_n = a_0 \left[ 1 + \frac{3}{4} \sum_{k=1}^{n} \left( \frac{4}{9} \right)^k \right]

\]


03模型推理


安装依赖

pip install git+https://github.com/deepseek-ai/Janus


多模态理解:

import torch
from transformers import AutoModelForCausalLM
from janus.models import MultiModalityCausalLM, VLChatProcessor
from janus.utils.io import load_pil_images
from modelscope import snapshot_download

# specify the path to the model
model_path = snapshot_download("deepseek-ai/Janus-Pro-7B")

vl_chat_processor: VLChatProcessor = VLChatProcessor.from_pretrained(model_path)
tokenizer = vl_chat_processor.tokenizer

vl_gpt: MultiModalityCausalLM = AutoModelForCausalLM.from_pretrained(
    model_path, trust_remote_code=True
)
vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()

question = "discribe the image"

image = "/mnt/workspace/Janus/images/doge.png"
conversation = [
    {
        "role": "<|User|>",
        "content": f"<image_placeholder>\n{question}",
        "images": [image],
    },
    {"role": "<|Assistant|>", "content": ""},
]

# load images and prepare for inputs
pil_images = load_pil_images(conversation)
prepare_inputs = vl_chat_processor(
    conversations=conversation, images=pil_images, force_batchify=True
).to(vl_gpt.device)

# # run image encoder to get the image embeddings
inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)

# # run the model to get the 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)


多模态生成:

import os
import PIL.Image
import torch
import numpy as np
from transformers import AutoModelForCausalLM
from janus.models import MultiModalityCausalLM, VLChatProcessor
from modelscope import snapshot_download

# specify the path to the model
model_path = snapshot_download("deepseek-ai/Janus-Pro-7B")
vl_chat_processor: VLChatProcessor = VLChatProcessor.from_pretrained(model_path)
tokenizer = vl_chat_processor.tokenizer

vl_gpt: MultiModalityCausalLM = AutoModelForCausalLM.from_pretrained(
    model_path, trust_remote_code=True
)
vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()

conversation = [
    {
        "role": "<|User|>",
        "content": "A stunning princess from kabul in red, white traditional clothing, blue eyes, brown hair",
    },
    {"role": "<|Assistant|>", "content": ""},
]

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


@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', "img_{}.jpg".format(i))
        PIL.Image.fromarray(visual_img[i]).save(save_path)


generate(
    vl_gpt,
    vl_chat_processor,
    prompt,
)


04模型微调


我们介绍使用ms-swift对deepseek-ai/Janus-Pro-7B进行微调(注意:目前只支持图像理解的训练而不支持图像生成)。这里,我们将展示可运行的微调demo,并给出自定义数据集的格式。


在开始微调之前,请确保您的环境已准备妥当。

# pip install git+https://github.com/modelscope/ms-swift.git

git clone https://github.com/modelscope/ms-swift.git
cd ms-swift
pip install -e .


微调脚本如下:

CUDA_VISIBLE_DEVICES=0 \
swift sft \
    --model deepseek-ai/Janus-Pro-7B \
    --dataset AI-ModelScope/LaTeX_OCR:human_handwrite#20000 \
    --train_type lora \
    --torch_dtype bfloat16 \
    --num_train_epochs 1 \
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --learning_rate 1e-4 \
    --lora_rank 8 \
    --lora_alpha 32 \
    --target_modules all-linear \
    --freeze_vit true \
    --gradient_accumulation_steps 16 \
    --eval_steps 100 \
    --save_steps 100 \
    --save_total_limit 2 \
    --logging_steps 5 \
    --max_length 2048 \
    --output_dir output \
    --warmup_ratio 0.05 \
    --dataloader_num_workers 4 \
    --dataset_num_proc 4


训练显存占用:


如果要使用自定义数据集进行训练,你可以参考以下格式,并指定`--dataset <dataset_path>`。



{"messages": [{"role": "user", "content": "浙江的省会在哪?"}, {"role": "assistant", "content": "浙江的省会在杭州。"}]}
{"messages": [{"role": "user", "content": "<image><image>两张图片有什么区别"}, {"role": "assistant", "content": "前一张是小猫,后一张是小狗"}], "images": ["/xxx/x.jpg", "/xxx/x.png"]}


训练完成后,使用以下命令对训练后的权重进行推理:

提示:这里的`--adapters`需要替换成训练生成的last checkpoint文件夹。由于adapters文件夹中包含了训练的参数文件`args.json`,因此不需要额外指定`--model`,swift会自动读取这些参数。如果要关闭此行为,可以设置`--load_args false`。

CUDA_VISIBLE_DEVICES=0 \
swift infer \
    --adapters output/vx-xxx/checkpoint-xxx \
    --stream false \
    --max_batch_size 1 \
    --load_data_args true \
    --max_new_tokens 2048


推送模型到ModelScope:

CUDA_VISIBLE_DEVICES=0 \
swift export \    
--adapters output/vx-xxx/checkpoint-xxx \  
--push_to_hub true \ 
--hub_model_id '<your-model-id>' \  
--hub_token '<your-sdk-token>'
相关文章
|
18天前
|
分布式计算 测试技术 Spark
科大讯飞开源星火化学大模型、文生音效模型
近期,科大讯飞在魔搭社区(ModelScope)和Gitcode上开源两款模型:讯飞星火化学大模型Spark Chemistry-X1-13B、讯飞文生音频模型AudioFly,助力前沿化学技术研究,以及声音生成技术和应用的探索。
118 2
|
18天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
682 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
21天前
|
机器学习/深度学习 人工智能 自然语言处理
AI Compass前沿速览:Qwen3-Max、Mixboard、Qwen3-VL、Audio2Face、Vidu Q2 AI视频生成模型、Qwen3-LiveTranslate-全模态同传大模型
AI Compass前沿速览:Qwen3-Max、Mixboard、Qwen3-VL、Audio2Face、Vidu Q2 AI视频生成模型、Qwen3-LiveTranslate-全模态同传大模型
248 13
AI Compass前沿速览:Qwen3-Max、Mixboard、Qwen3-VL、Audio2Face、Vidu Q2 AI视频生成模型、Qwen3-LiveTranslate-全模态同传大模型
|
17天前
|
自然语言处理 机器人 图形学
腾讯混元图像3.0正式开源发布!80B,首个工业级原生多模态生图模型
腾讯混元图像3.0,真的来了——开源,免费开放使用。 正式介绍一下:混元图像3.0(HunyuanImage 3.0),是首个工业级原生多模态生图模型,参数规模80B,也是目前测评效果最好、参数量最大的开源生图模型,效果可对…
360 2
腾讯混元图像3.0正式开源发布!80B,首个工业级原生多模态生图模型
|
16天前
|
机器学习/深度学习 存储 人工智能
大模型微调:从理论到实践的全面指南
🌟蒋星熠Jaxonic:AI探索者,专注大模型微调技术。从LoRA到RLHF,实践医疗、法律等垂直领域模型优化,分享深度学习的科学与艺术,共赴二进制星河的极客征程。
大模型微调:从理论到实践的全面指南
|
23天前
|
机器学习/深度学习 数据采集 算法
大模型微调技术综述与详细案例解读
本文是一篇理论与实践结合的综述文章,综合性全面介绍大模型微调技术。本文先介绍大模型训练的两类场景:预训练和后训练,了解业界常见的模型训练方法。在后训练介绍内容中,引出模型微调(模型微调是属于后训练的一种)。然后,通过介绍业界常见的模型微调方法,以及通过模型微调实操案例的参数优化、微调过程介绍、微调日志解读,让读者对模型微调有更加直观的了解。最后,我们详细探讨数据并行训练DDP与模型并行训练MP两类模型并行训练技术,讨论在实际项目中如何选择两类并行训练技术。
|
14天前
|
存储 数据采集 自然语言处理
56_大模型微调:全参数与参数高效方法对比
随着大型语言模型(LLM)规模的不断增长,从数百亿到数千亿参数,传统的全参数微调方法面临着计算资源消耗巨大、训练效率低下等挑战。2025年,大模型微调技术已经从早期的全参数微调发展到如今以LoRA、QLoRA为代表的参数高效微调方法,以及多种技术融合的复杂策略。本文将深入对比全参数微调和参数高效微调的技术原理、适用场景、性能表现和工程实践,为研究者和工程师提供全面的技术参考。
|
14天前
|
存储 机器学习/深度学习 人工智能
54_模型优化:大模型的压缩与量化
随着大型语言模型(LLM)的快速发展,模型规模呈指数级增长,从最初的数亿参数到如今的数千亿甚至万亿参数。这种规模扩张带来了惊人的能源消耗和训练成本,同时也给部署和推理带来了巨大挑战。2025年,大模型的"瘦身"已成为行业发展的必然趋势。本文将深入剖析大模型压缩与量化的核心技术、最新进展及工程实践,探讨如何通过创新技术让大模型在保持高性能的同时实现轻量化部署,为企业和开发者提供全面的技术指导。
|
14天前
|
机器学习/深度学习 人工智能 自然语言处理
38_多模态模型:CLIP的视觉-语言对齐_深度解析
想象一下,当你看到一张小狗在草地上奔跑的图片时,你的大脑立刻就能将视觉信息与"小狗"、"草地"、"奔跑"等概念联系起来。这种跨模态的理解能力对于人类来说似乎是理所当然的,但对于人工智能系统而言,实现这种能力却经历了长期的技术挑战。多模态学习的出现,标志着AI从单一模态处理向更接近人类认知方式的综合信息处理迈出了关键一步。

热门文章

最新文章