|
| 1 | +# Qwen2.5-VL |
| 2 | + |
| 3 | +LMDeploy supports the following Qwen-VL series of models, which are detailed in the table below: |
| 4 | + |
| 5 | +| Model | Size | Supported Inference Engine | |
| 6 | +| :--------: | :---------: | :------------------------: | |
| 7 | +| Qwen2.5-VL | 3B, 7B, 72B | PyTorch | |
| 8 | + |
| 9 | +The next chapter demonstrates how to deploy a Qwen-VL model using LMDeploy, with [Qwen2.5-VL-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) as an example. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +Please install LMDeploy by following the [installation guide](../get_started/installation.md), and install other packages that Qwen2.5-VL needs |
| 14 | + |
| 15 | +```shell |
| 16 | +# Qwen2.5-VL requires the latest transformers (transformers >= 4.49.0) |
| 17 | +pip install git+https://github.com/huggingface/transformers |
| 18 | +# It's highly recommended to use `[decord]` feature for faster video loading. |
| 19 | +pip install qwen-vl-utils[decord]==0.0.8 |
| 20 | +``` |
| 21 | + |
| 22 | +## Offline inference |
| 23 | + |
| 24 | +The following sample code shows the basic usage of the VLM pipeline. For detailed information, please refer to [VLM Offline Inference Pipeline](./vl_pipeline.md) |
| 25 | + |
| 26 | +```python |
| 27 | +from lmdeploy import pipeline |
| 28 | +from lmdeploy.vl import load_image |
| 29 | + |
| 30 | +pipe = pipeline('Qwen/Qwen2.5-VL-7B-Instruct') |
| 31 | + |
| 32 | +image = load_image('https://raw.githubusercontent.com/open-mmlab/mmdeploy/main/tests/data/tiger.jpeg') |
| 33 | +response = pipe((f'describe this image', image)) |
| 34 | +print(response) |
| 35 | +``` |
| 36 | + |
| 37 | +More examples are listed below: |
| 38 | + |
| 39 | +<details> |
| 40 | + <summary> |
| 41 | + <b>multi-image multi-round conversation, combined images</b> |
| 42 | + </summary> |
| 43 | + |
| 44 | +```python |
| 45 | +from lmdeploy import pipeline, GenerationConfig |
| 46 | + |
| 47 | +pipe = pipeline('Qwen/Qwen2.5-VL-7B-Instruct', log_level='INFO') |
| 48 | +messages = [ |
| 49 | + dict(role='user', content=[ |
| 50 | + dict(type='text', text='Describe the two images in detail.'), |
| 51 | + dict(type='image_url', image_url=dict(url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Beijing_Small.jpeg')), |
| 52 | + dict(type='image_url', image_url=dict(url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Chongqing_Small.jpeg')) |
| 53 | + ]) |
| 54 | +] |
| 55 | +out = pipe(messages, gen_config=GenerationConfig(top_k=1)) |
| 56 | + |
| 57 | +messages.append(dict(role='assistant', content=out.text)) |
| 58 | +messages.append(dict(role='user', content='What are the similarities and differences between these two images.')) |
| 59 | +out = pipe(messages, gen_config=GenerationConfig(top_k=1)) |
| 60 | +``` |
| 61 | + |
| 62 | +</details> |
| 63 | + |
| 64 | +<details> |
| 65 | + <summary> |
| 66 | + <b>image resolution for performance boost</b> |
| 67 | + </summary> |
| 68 | + |
| 69 | +```python |
| 70 | +from lmdeploy import pipeline, GenerationConfig |
| 71 | + |
| 72 | +pipe = pipeline('Qwen/Qwen2.5-VL-7B-Instruct', log_level='INFO') |
| 73 | + |
| 74 | +min_pixels = 64 * 28 * 28 |
| 75 | +max_pixels = 64 * 28 * 28 |
| 76 | +messages = [ |
| 77 | + dict(role='user', content=[ |
| 78 | + dict(type='text', text='Describe the two images in detail.'), |
| 79 | + dict(type='image_url', image_url=dict(min_pixels=min_pixels, max_pixels=max_pixels, url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Beijing_Small.jpeg')), |
| 80 | + dict(type='image_url', image_url=dict(min_pixels=min_pixels, max_pixels=max_pixels, url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Chongqing_Small.jpeg')) |
| 81 | + ]) |
| 82 | +] |
| 83 | +out = pipe(messages, gen_config=GenerationConfig(top_k=1)) |
| 84 | + |
| 85 | +messages.append(dict(role='assistant', content=out.text)) |
| 86 | +messages.append(dict(role='user', content='What are the similarities and differences between these two images.')) |
| 87 | +out = pipe(messages, gen_config=GenerationConfig(top_k=1)) |
| 88 | +``` |
| 89 | + |
| 90 | +</details> |
| 91 | + |
| 92 | +<details> |
| 93 | + <summary> |
| 94 | + <b>video multi-round conversation</b> |
| 95 | + </summary> |
| 96 | + |
| 97 | +```python |
| 98 | +import numpy as np |
| 99 | +from lmdeploy import pipeline, GenerationConfig |
| 100 | +from decord import VideoReader, cpu |
| 101 | +from lmdeploy.vl.constants import IMAGE_TOKEN |
| 102 | +from lmdeploy.vl.utils import encode_image_base64 |
| 103 | +from PIL import Image |
| 104 | +pipe = pipeline('Qwen/Qwen2.5-VL-7B-Instruct', log_level='INFO') |
| 105 | + |
| 106 | + |
| 107 | +def get_index(bound, fps, max_frame, first_idx=0, num_segments=32): |
| 108 | + if bound: |
| 109 | + start, end = bound[0], bound[1] |
| 110 | + else: |
| 111 | + start, end = -100000, 100000 |
| 112 | + start_idx = max(first_idx, round(start * fps)) |
| 113 | + end_idx = min(round(end * fps), max_frame) |
| 114 | + seg_size = float(end_idx - start_idx) / num_segments |
| 115 | + frame_indices = np.array([ |
| 116 | + int(start_idx + (seg_size / 2) + np.round(seg_size * idx)) |
| 117 | + for idx in range(num_segments) |
| 118 | + ]) |
| 119 | + return frame_indices |
| 120 | + |
| 121 | + |
| 122 | +def load_video(video_path, bound=None, num_segments=32): |
| 123 | + vr = VideoReader(video_path, ctx=cpu(0), num_threads=1) |
| 124 | + max_frame = len(vr) - 1 |
| 125 | + fps = float(vr.get_avg_fps()) |
| 126 | + pixel_values_list, num_patches_list = [], [] |
| 127 | + frame_indices = get_index(bound, fps, max_frame, first_idx=0, num_segments=num_segments) |
| 128 | + imgs = [] |
| 129 | + for frame_index in frame_indices: |
| 130 | + img = Image.fromarray(vr[frame_index].asnumpy()).convert('RGB') |
| 131 | + imgs.append(img) |
| 132 | + return imgs |
| 133 | + |
| 134 | + |
| 135 | +video_path = 'red-panda.mp4' |
| 136 | +imgs = load_video(video_path, num_segments=8) |
| 137 | + |
| 138 | +question = '' |
| 139 | +for i in range(len(imgs)): |
| 140 | + question = question + f'Frame{i+1}: {IMAGE_TOKEN}\n' |
| 141 | + |
| 142 | +question += 'What is the red panda doing?' |
| 143 | + |
| 144 | +content = [{'type': 'text', 'text': question}] |
| 145 | +for img in imgs: |
| 146 | + content.append({'type': 'image_url', 'image_url': {'max_dynamic_patch': 1, 'url': f'data:image/jpeg;base64,{encode_image_base64(img)}'}}) |
| 147 | + |
| 148 | +messages = [dict(role='user', content=content)] |
| 149 | +out = pipe(messages, gen_config=GenerationConfig(top_k=1)) |
| 150 | + |
| 151 | +messages.append(dict(role='assistant', content=out.text)) |
| 152 | +messages.append(dict(role='user', content='Describe this video in detail. Don\'t repeat.')) |
| 153 | +out = pipe(messages, gen_config=GenerationConfig(top_k=1)) |
| 154 | +``` |
| 155 | + |
| 156 | +</details> |
0 commit comments