다음 조건에서 측정한 지연 시간 Roboflow Inference 1x NVIDIA L4, 배치 크기 1, 고정된 프롬프트에서 greedy 디코딩으로 정확히 128개의 토큰을 생성하여 측정했습니다. 지연 시간은 출력 길이에 따라 달라지므로, 다른 길이를 추정하려면 토큰/초를 사용하세요.
from PIL import Image
from inference.models.paligemma.paligemma import PaliGemma
model = PaliGemma("paligemma-3b-mix-224", api_key="YOUR_API_KEY")
image = Image.open("image.jpeg")
result = model.predict(image, "이 이미지에 개가 몇 마리 있나요?")
print(result)
import re
from typing import List, Optional, Tuple
import numpy as np
import supervision as sv
_DETECT_RE = re.compile(r"(.*?)" + r"<loc(\\d{4})>" * 4 + r"\\s*([^;<>]+)? ?(?:; )?")
def from_pali_gemma(
response: str,
resolution_wh: Tuple[int, int],
class_list: Optional[List[str]] = None,
) -> sv.Detections:
width, height = resolution_wh
xyxy_list, class_name_list = [], []
while response:
match = _DETECT_RE.match(response)
if not match:
break
groups = list(match.groups())
before = groups.pop(0)
name = groups.pop()
y1, x1, y2, x2 = [int(value) / 1024 for value in groups[:4]]
y1, x1, y2, x2 = map(round, (y1 * height, x1 * width, y2 * height, x2 * width))
content = match.group()
if before:
response = response[len(before):]
content = content[len(before):]
xyxy_list.append([x1, y1, x2, y2])
class_name_list.append(name.strip())
response = response[len(content):]
class_name = np.array(class_name_list)
class_id = (
np.array([class_list.index(name) for name in class_name])
if class_list is not None
else None
)
return sv.Detections(
xyxy=np.array(xyxy_list),
class_id=class_id,
data={"class_name": class_name},
)
classes = ["person", "car", "backpack"]
response = model.predict(image, "사람; 자동차; 백팩을 탐지해 주세요")[0]
detections = from_pali_gemma(response, resolution_wh=image.size, class_list=classes)