DeepSeek:统一多模态模型Janus Pro

 一、背景

DeepSeek(深度求索)推出的Janus Pro 是一款统一多模态模型,旨在通过整合多种模态(如文本、图像、视频等)的输入与输出能力,实现更高效、更智能的跨模态理解与生成任务。

作为DeepSeek在多模态领域的重要布局,Janus Pro展现了公司在通用人工智能(AGI)研究中的技术积累与创新。

核心特点

  1. 统一的多模态架构   

    Janus Pro采用统一的模型架构,支持对文本、图像、视频等多种模态数据的联合处理,无需依赖独立的分支模块。这种设计减少了不同模态间的信息割裂,提升了跨模态任务(如图文问答、视频描述生成)的效率和准确性。

  2. 模态协同与知识共享   

    模型通过跨模态对齐技术,将不同模态的数据映射到统一的语义空间,实现知识共享。例如,文本描述与图像特征在共享空间中相互增强,提升模型对复杂场景的理解能力。

  3. 高效推理与扩展性   

    基于MoE(Mixture of Experts)架构,Janus Pro在训练和推理时动态分配计算资源,针对不同任务激活特定的专家模块,既保证了模型容量,又提高了计算效率。

  4. 支持高分辨率图像与长上下文   

    通过创新的视觉编码器,Janus Pro能够处理更高分辨率的图像(如4K级别),同时支持超长文本输入(如数十万tokens),适用于复杂文档分析、长视频理解等场景。

关键技术突破

  • 跨模态对齐技术   

    采用对比学习与自监督策略,强化多模态数据的语义对齐,例如图像区域与文本描述的关联性。

  • 动态路由机制   

    MoE架构中的动态路由机制可根据输入内容自动选择最相关的专家网络,优化任务性能。

  • 高分辨率视觉处理   

    引入分块编码和局部注意力机制,解决传统视觉模型因图像分辨率过高导致的计算瓶颈。

应用场景

  1. 智能助理

       
    支持多轮对话中混合图文内容的交互,例如根据用户提供的图片生成定制化建议。

  2. 内容生成

       
    跨模态生成能力可用于图文创作、视频脚本生成、广告设计等场景。

  3. 教育/医疗

       
    解析复杂图表、医学影像,并提供结合专业知识的文本解释。

  4. 工业与科研

       
    处理多模态数据(如传感器数据+文本报告)进行故障诊断或科研分析。

性能表现

  • 在权威多模态评测(如VQA-v2 、TextVQA 、COCO-Caption等)中,Janus Pro展现了与GPT-4V、Gemini等顶尖模型相当或更优的性能。

  • 在长文档理解、高分辨率图像细节问答等任务中表现突出,体现了对复杂场景的深度理解能力。

定位与意义

Janus Pro是DeepSeek向多模态AGI迈进的关键一步,其统一架构和高效设计降低了多模态应用的开发门槛,同时为未来扩展更多模态(如3D、音频)奠定了基础。通过开源部分模型或API,DeepSeek进一步推动多模态技术的普及与生态建设。

小结

1、Janus Pro是DeepSeek Janus系列模型的第二代

2、Janus Pro是统一多模态大模型, 支持多模态(目前是图 像)输入和输出

3、最新Janus Pro开源了1B和7B两个尺 寸,暂未开放在线 API,只能本地部署使用 (API或许后续会上线)

4、Janus Pro仍然延续了DeepSeek一贯 风格, 高性能+高性价比+全开源

5、传统多模态大模型无法在一个模型中同时完成多模态输入和输出,从而在实现具身智能时会存 在大量的计算浪费

二、本地部署

硬件GPU显存要求

任务类型

Janus Pro 1B

Janus Pro 7B

图像识别

5G(3060)

15G(4080)

图片生成

14G(4080)

40G(3090/4090 *2)

下载代码

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

安装环境

conda create --name janus python=3.10
conda activate janus 
pip install -e .

使用魔搭进行模型下载

https://www.modelscope.cn/models/deepseek-ai/Janus-Pro-1B/files

pip install modelscope
mkdir Janus-Pro-1B
modelscope download --model deepseek-ai/Janus-Pro-1B --local_dir ./Janus-Pro-1B

三、试用

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

# 指定模型路径
model_path = "./Janus-Pro-1B"

# 加载VLChatProcessor
vl_chat_processor: VLChatProcessor = VLChatProcessor.from_pretrained(model_path)

# 加载分词器
tokenizer = vl_chat_processor.tokenizer

# 加载vl_gpt
try:
    vl_gpt: MultiModalityCausalLM = AutoModelForCausalLM.from_pretrained(
        model_path, trust_remote_code=True
    )
    vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()
except Exception as e:
    print(e)

image = "./pic/1.png"
question = "请解释下这张图片上的内容。"
conversation = [
    {
        "role": "<|User|>",
        "content": f"<image_placeholder>\n{question}",
        "images": [image],
    },
    {"role": "<|Assistant|>", "content": ""},
]

pil_images = load_pil_images(conversation)
prepare_inputs = vl_chat_processor(
    conversations=conversation, images=pil_images, force_batchify=True
).to(vl_gpt.device)

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)

image = "./pic/2.png"
question = "请将这个数学公式转换成 LaTeX 代码。"
conversation = [
    {
        "role": "<|User|>",
        "content": f"<image_placeholder>\n{question}",
        "images": [image],
    },
    {"role": "<|Assistant|>", "content": ""},
]
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)

测试图片:

image2025-3-21_9-47-4.png
1

image2025-3-21_9-47-8.png
2

输出结果

/opt/homebrew/Caskroom/miniconda/base/envs/janus/bin/python /Users/zhongxin/github/Janus/test.py 
Python version is above 3.10, patching the collections module.
/opt/homebrew/Caskroom/miniconda/base/envs/janus/lib/python3.10/site-packages/transformers/models/auto/image_processing_auto.py:594: FutureWarning: The image_processor_class argument is deprecated and will be removed in v4.42. Please use `slow_image_processor_class`, or `fast_image_processor_class` instead
  warnings.warn(
Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model. `use_fast=True` will be the default behavior in v4.48, even if the model was saved with a slow processor. This will result in minor differences in outputs. You'll still be able to use a slow processor with `use_fast=False`.
You are using the default legacy behaviour of the <class 'transformers.models.llama.tokenization_llama_fast.LlamaTokenizerFast'>. This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=False`. This should only be set if you understand what it means, and thoroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565 - if you loaded a llama tokenizer from a GGUF file you can ignore this message.
Some kwargs in processor config are unused and will not have any effect: image_tag, num_image_tokens, add_special_token, ignore_id, sft_format, mask_prompt. 
Torch not compiled with CUDA enabled
You are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.

<|User|>: <image_placeholder>
请解释下这张图片上的内容。

<|Assistant|>: 这是一张对比图,分为左右两部分,每部分都有一只柴犬形象。

左边:
- 图片上方有一行黑色粗体文字:“Decoupling Visual Encoding”。
- 柴犬站立着,肌肉发达,看起来非常强壮。

右边:
- 图片上方也有一行黑色粗体文字:“Single Visual Encoder”。
- 柴犬坐在地上,表情悲伤,看起来很沮丧。

整体风格:
- 图片采用了幽默的网络迷因风格,使用了两只柴犬来表达不同的概念。
- 左侧的柴犬代表“Decoupling Visual Encoding”,右侧的柴犬代表“Single Visual Encoder”。
- 文字使用的是简单的黑色粗体字体,没有特殊装饰。

图片中的文字:
- “Decoupling Visual Encoding”
- “Single Visual Encoder”

这些文字位于图片的顶部,用黑色粗体字体显示。
You are a helpful language and vision assistant. You are able to understand the visual content that the user provides, and assist the user with a variety of tasks using natural language.

<|User|>: <image_placeholder>
请将这个数学公式转换成 LaTeX 代码。

<|Assistant|>: \( A_n = \alpha _0 \left[ \begin{array}{l} 1 + \frac{3}{4} \sum_{k=1}^{n} \begin{pmatrix} \frac{4}{9} \\ k \end{pmatrix} \right] \end{array]

在 LaTeX 中,这个公式可以表示为:

\[ A_n = \alpha_0 \left[ \begin{array}{l} 1 + \frac{3}{4} \sum_{k=1}^{n} \begin{pmatrix} \frac{4}{9} \\ k \end{pmatrix} \right] \]

进程已结束,退出代码为 0
### DeepSeek 多模态模型 Janus-Pro-7B 使用指南 #### 获取模型 为了使用 Janus-Pro-7B 模型,需先从指定网站下载模型文件。访问链接可获得最新版本的模型权重和其他必要资源[^2]。 ```bash # 下载命令示例(具体路径可能有所不同) wget https://huggingface.co/deepseek-ai/Janus-Pro-7B/archive/main.zip unzip main.zip -d ./janus_pro_7b/ ``` #### 安装依赖库 安装必要的 Python 库来加载并运行此多模态模型。推荐使用虚拟环境隔离项目依赖项。 ```bash pip install transformers torch accelerate safetensors ``` #### 加载预训练模型 通过 Hugging Face 的 `transformers` 库可以轻松加载已保存的模型及其配置。 ```python from transformers import AutoModelForVision2Seq, AutoProcessor model_name_or_path = "deepseek-ai/Janus-Pro-7B" processor = AutoProcessor.from_pretrained(model_name_or_path) model = AutoModelForVision2Seq.from_pretrained(model_name_or_path) ``` #### 数据准备与处理 对于输入数据,无论是文本还是图像都需要经过特定方式编码成适合喂给神经网络的形式。 ```python image_url = "http://example.com/path/to/image.jpg" # 替换成实际图片URL text_input = "描述这张照片的内容" inputs = processor(image=image_url, text=text_input, return_tensors="pt") ``` #### 执行推理任务 准备好所有前提条件之后就可以调用模型来进行预测了。这里展示了一个简单的例子用于说明如何执行一次前向传播操作得到输出结果。 ```python with torch.no_grad(): outputs = model.generate(**inputs) generated_text = processor.decode(outputs[0], skip_special_tokens=True) print(generated_text) ``` #### 进阶应用案例 除了基本的文字到文字转换外,Janus Pro 7B 支持更多样化的应用场景,比如基于视觉的理解能力以及跨媒体的信息生成等功能[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值