如何利用 OpenVINO? 在本地運行 Qwen 2.5-VL 系列模型
近期阿里通義實驗室在 Hugging Face 和 ModelScope 上開源了 Qwen2.5-VL 的 Base 和 Instruct 模型,包含 3B、7B 和 72B 在內的 3 個模型尺寸。其中,Qwen2.5-VL-7B-Instruct 在多個任務中超越了 GPT-4o-mini,而 Qwen2.5-VL-3B 作為端側 AI 的潛力股,甚至超越了之前版本 的Qwen2-VL 7B 模型。Qwen2.5-VL 增強了模型對時間和空間尺度的感知能力,在空間維度上,Qwen2.5-VL 不僅能夠動態(tài)地將不同尺寸的圖像轉換為不同長度的 token,使用圖像的實際尺寸來表示檢測框和點等坐標,這也使得Qwen2.5-VL模型可以直接作為一個視覺 Agent,推理并動態(tài)地使用工具,具備了使用電腦和使用手機的能力。
本文引用地址:http://2s4d.com/article/202503/467901.htm本文將分享如何利用英特爾 OpenVINO? 工具套件在本地加速Q(mào)wen2.5-VL系列模型的推理任務。
內容列表
1.環(huán)境準備
2.模型下載和轉換
3.加載模型
4.準備模型輸入
5.運行圖像理解任務
1 環(huán)境準備
該示例基于Jupyter Notebook編寫,因此我們需要準備好相對應的Python環(huán)境?;A環(huán)境可以參考以下鏈接安裝,并根據(jù)自己的操作系統(tǒng)進行選擇具體步驟。
https://github.com/openvinotoolkit/openvino_notebooks?tab=readme-ov-file#-getting-started
圖:基礎環(huán)境安裝導航頁面
此外本示例將依賴qwen-vl-utils以及optimum-intel組件,其中安裝optimum-intel過程中將自動安裝OpenVINO? runtime, NNCF及Transformers等相關依賴庫。
2 模型下載和轉換
這一步中,我們需要完成將Qwen2.5-VL .safetensor格式模型轉化為OpenVINO? IR格式,并對其進行INT4權重量化,實現(xiàn)對模型體積的壓縮。為了達到這一目的,optimum-intel提供了命令行工具:optimum-cli,基于該工具,我們只需一行命令便可實現(xiàn)上述步驟:
optimum-cli export openvino --model Qwen/Qwen2.5-VL-3B-Instruct Qwen2.5-VL-3B-Instruct/INT4 --weight-format int4
其中“—model”參數(shù)后的“Qwen/Qwen2.5-VL-3B-Instruct”為模型在HuggingFace上的model id,這里我們也提前下載原始模型,并將model id替換為原始模型的本地路徑,針對國內開發(fā)者,推薦使用ModelScope魔搭社區(qū)作為原始模型的下載渠道,具體加載方式可以參考ModelScope官方指南:https://www.modelscope.cn/docs/models/download
3 加載模型
接下來需要完成對模型推理任務的初始化,并將模型載入到指定硬件的內存中,同樣的,我們可以利用optimum-intel封裝好的OpenVINO? 視覺多模態(tài)任務對象 OVModelForVisualCausalLM 對象完成該操作。
from optimum.intel.openvino import OVModelForVisualCausalLM model = OVModelForVisualCausalLM.from_pretrained(model_dir, device.value)
如示例代碼所示,通過OVModelForVisualCausalLM的from_pretrained函數(shù)接口,可以很方便地根據(jù)用戶提供的模型路徑,將模型載入到指定的硬件平臺,完成視覺多模態(tài)任務的初始化。
4 準備模型輸入
第四步需要根據(jù)Qwen2.5-VL模型要求的prompt template準備模型的輸入數(shù)據(jù)。數(shù)據(jù)格式如下:
messages = [ { "role": "user", "content": [ { "type": "image", "image": f"file://{example_image_path}", }, {"type": "text", "text": question}, ], } ]
其中:
■ “role“字段用于指定對話角色,包括system, user以及assistant三種類型;
■ "content"字段表示對話角色輸出的內容,其中”type”為內容類別,包含image,video,text三種類型,支持多張image輸入。
接下來可以通過Qwen官方提供的方法將用戶輸入的text和image編碼為模型的輸入tensor。
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) image_inputs, video_inputs = process_vision_info(messages) inputs = processor( text=[text], images=image_inputs, videos=video_inputs, padding=True, return_tensors="pt", )
5 運行圖像理解任務
最后一步需要調用模型對象的generation函數(shù),進行答案生成,這里可以通過添加TextStreamer迭代器的方式,在命令行中流式輸出文本內容。
from transformers import TextStreamer generated_ids = model.generate(**inputs, max_new_tokens=100, streamer=TextStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=True))
根據(jù)示例圖片生成生成對話內容如下所示:
Question:
Describe this image.
Answer:
The image depicts a serene beach scene at sunset. A person is sitting on the sandy beach, facing a light-colored dog, likely a Labrador Retriever, which is also sitting and facing the person. The dog appears to be wearing a harness with a leash attached, suggesting that it might be a pet. The person is dressed in a plaid shirt and shorts, and they are smiling, indicating a happy and relaxed moment. The background shows the ocean with gentle waves and the sun setting, casting
圖:Gradio示例界面
6 總結
Qwen2.5-VL 系列模型的發(fā)布帶來了更精準的視覺定位,文字理解以及Agent智能體能力。OpenVINO? 則可以以更低的資源占用,高效地在本地運行Qwen2.5-VL視覺多模態(tài)模型,激發(fā)AIPC異構處理器的潛能。相信構建面向桌面操作系統(tǒng)的本地智能體應用已不再遙遠。
參考示例
https://github.com/openvinotoolkit/openvino_notebooks/tree/latest/notebooks/qwen2.5-vl
評論