Skip to content

Commit 2704b25

Browse files
authored
Support LMDeploy image sizing options (#1656)
* Support LMDeploy image sizing options * Warn when LMDeploy local media ignores image options
1 parent 122bc80 commit 2704b25

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

‎vlmeval/api/lmdeploy.py‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import math
23
import os
34
from pathlib import Path
45

@@ -36,6 +37,9 @@ def __init__(self,
3637
custom_prompt=None,
3738
video_llm: bool = False,
3839
local_media: bool = False,
40+
img_size: int = -1,
41+
total_img_size: int = -1,
42+
max_file_size: int = 1e9,
3943
**kwargs):
4044
self.fail_msg = 'Failed to obtain answer via API. '
4145
self.timeout = timeout
@@ -49,9 +53,18 @@ def __init__(self,
4953

5054
self.VIDEO_LLM = video_llm
5155
self.local_media = local_media or (os.getenv('VLMEVAL_LOCAL_MEDIA', '0') == '1')
56+
assert img_size > 0 or img_size == -1
57+
self.img_size = img_size
58+
assert total_img_size > 0 or total_img_size == -1
59+
self.total_img_size = total_img_size
60+
self.max_file_size = max_file_size
5261
if self.local_media:
5362
logger.info(
5463
f'lmdeploy: `local_media={self.local_media}`, pass local media file path directly.')
64+
if img_size != -1 or total_img_size != -1 or max_file_size != 1e9:
65+
logger.warning(
66+
'lmdeploy: image encoding parameters `img_size`, `total_img_size`, '
67+
'and `max_file_size` will not take effect when `local_media=True`.')
5568
else:
5669
logger.info(
5770
f'lmdeploy: `local_media={self.local_media}`, pass media file base64.')
@@ -81,11 +94,25 @@ def __init__(self,
8194
# HTTP message formatting (lmdeploy-specific)
8295
# ------------------------------------------------------------------
8396

97+
def _get_image_target_size(self, image_num):
98+
image_num = max(image_num, 1)
99+
target_size = math.inf
100+
if self.img_size > 0:
101+
target_size = self.img_size
102+
if self.total_img_size > 0:
103+
target_size = min(
104+
target_size,
105+
max(1, int(self.total_img_size / (image_num ** 0.5))),
106+
)
107+
return -1 if math.isinf(target_size) else target_size
108+
84109
def prepare_itlist(self, inputs):
85110
assert np.all([isinstance(x, dict) for x in inputs])
86111
multimedia = sum(x['type'] in ('image', 'video') for x in inputs)
87112
if multimedia:
88113
content_list = []
114+
image_num = sum(x['type'] == 'image' for x in inputs)
115+
image_target_size = self._get_image_target_size(image_num)
89116
for msg in inputs:
90117
if msg['type'] == 'text' and msg['value'].strip():
91118
content_list.append(dict(type='text', text=msg['value']))
@@ -95,7 +122,11 @@ def prepare_itlist(self, inputs):
95122
else:
96123
from PIL import Image
97124
img = Image.open(msg['value'])
98-
b64 = encode_image_to_base64(img)
125+
b64 = encode_image_to_base64(
126+
img,
127+
target_size=image_target_size,
128+
max_file_size=self.max_file_size,
129+
)
99130
image_data_url = f'data:image/jpeg;base64,{b64}'
100131
extra_args = {k: v for k, v in msg.items() if k not in ('type', 'value')}
101132
img_struct = dict(url=image_data_url, **extra_args)

0 commit comments

Comments
 (0)