Views
No views yet

Python >= 3.8 environment, install the necessary dependencies by running the following command:pip install -e .1import torch
2from transformers import AutoModelForCausalLM
3
4from deepseek_vl.models import DeepseekVLV2Processor, DeepseekVLV2ForCausalLM
5from deepseek_vl.utils.io import load_pil_images
6
7
8# specify the path to the model
9model_path = "deepseek-ai/deepseek-vl2-small"
10vl_chat_processor: DeepseekVLV2Processor = DeepseekVLV2Processor.from_pretrained(model_path)
11tokenizer = vl_chat_processor.tokenizer
12
13vl_gpt: DeepseekVLV2ForCausalLM = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
14vl_gpt = vl_gpt.to(torch.bfloat16).cuda().eval()
15
16## single image conversation example
17conversation = [
18 {
19 "role": "<|User|>",
20 "content": "<image>\n<|ref|>The giraffe at the back.<|/ref|>.",
21 "images": ["./images/visual_grounding.jpeg"],
22 },
23 {"role": "<|Assistant|>", "content": ""},
24]
25
26## multiple images (or in-context learning) conversation example
27# conversation = [
28# {
29# "role": "User",
30# "content": "<image_placeholder>A dog wearing nothing in the foreground, "
31# "<image_placeholder>a dog wearing a santa hat, "
32# "<image_placeholder>a dog wearing a wizard outfit, and "
33# "<image_placeholder>what's the dog wearing?",
34# "images": [
35# "images/dog_a.png",
36# "images/dog_b.png",
37# "images/dog_c.png",
38# "images/dog_d.png",
39# ],
40# },
41# {"role": "Assistant", "content": ""}
42# ]
43
44# load images and prepare for inputs
45pil_images = load_pil_images(conversation)
46prepare_inputs = vl_chat_processor(
47 conversations=conversation,
48 images=pil_images,
49 force_batchify=True,
50 system_prompt=""
51).to(vl_gpt.device)
52
53# run image encoder to get the image embeddings
54inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
55
56# run the model to get the response
57outputs = vl_gpt.language_model.generate(
58 inputs_embeds=inputs_embeds,
59 attention_mask=prepare_inputs.attention_mask,
60 pad_token_id=tokenizer.eos_token_id,
61 bos_token_id=tokenizer.bos_token_id,
62 eos_token_id=tokenizer.eos_token_id,
63 max_new_tokens=512,
64 do_sample=False,
65 use_cache=True
66)
67
68answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
69print(f"{prepare_inputs['sft_format'][0]}", answer)@misc{wu2024deepseekvl2mixtureofexpertsvisionlanguagemodels,
title={DeepSeek-VL2: Mixture-of-Experts Vision-Language Models for Advanced Multimodal Understanding},
author={Zhiyu Wu and Xiaokang Chen and Zizheng Pan and Xingchao Liu and Wen Liu and Damai Dai and Huazuo Gao and Yiyang Ma and Chengyue Wu and Bingxuan Wang and Zhenda Xie and Yu Wu and Kai Hu and Jiawei Wang and Yaofeng Sun and Yukun Li and Yishi Piao and Kang Guan and Aixin Liu and Xin Xie and Yuxiang You and Kai Dong and Xingkai Yu and Haowei Zhang and Liang Zhao and Yisong Wang and Chong Ruan},
year={2024},
eprint={2412.10302},
archivePrefix={arXiv},
primaryClass={cs.CV},
url={https://arxiv.org/abs/2412.10302},
}