Image-Text-to-Text
Transformers
Safetensors
English
Chinese
groundinganything
text-generation
visual-grounding
object-detection
referring-expression-comprehension
pointing
ocr
document-layout
custom-code
diffusion-language-model
conversational
custom_code
Instructions to use GroundingPI/GroundAnything with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use GroundingPI/GroundAnything with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="GroundingPI/GroundAnything", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("GroundingPI/GroundAnything", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use GroundingPI/GroundAnything with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "GroundingPI/GroundAnything" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "GroundingPI/GroundAnything", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/GroundingPI/GroundAnything
- SGLang
How to use GroundingPI/GroundAnything with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "GroundingPI/GroundAnything" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "GroundingPI/GroundAnything", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "GroundingPI/GroundAnything" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "GroundingPI/GroundAnything", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use GroundingPI/GroundAnything with Docker Model Runner:
docker model run hf.co/GroundingPI/GroundAnything
File size: 8,111 Bytes
37f388f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | """Image processor class for Kimi-K3.
"""
import json
import os
from typing import Any, Dict, Optional, Union
import numpy as np
import torch
from PIL import Image
from transformers.image_processing_utils import (BaseImageProcessor,
BatchFeature)
from transformers.utils import TensorType
from .media_utils import (MediaInput, TransparentBgConfig, _to_tensor,
ensure_media_type, image_to_np, navit_patchify,
navit_resize_image, normalize)
class GroundAnythingVLMImageProcessor(BaseImageProcessor):
model_type = "groundinganything_backbone"
def __init__(
self,
media_proc_cfg: dict,
**kwargs,
):
super().__init__(**kwargs)
self.media_proc_cfg = dict(media_proc_cfg)
self.patch_size = int(self.media_proc_cfg.get("patch_size", 14))
self.merge_size = int(self.media_proc_cfg.get("merge_kernel_size", 2))
max_output_tokens = os.getenv("IMAGE_MAX_TOKEN_NUM")
if max_output_tokens:
max_input_patches = int(max_output_tokens) * self.merge_size**2
self.media_proc_cfg["in_patch_limit"] = min(
int(self.media_proc_cfg["in_patch_limit"]), max_input_patches
)
self.max_output_tokens = (
int(self.media_proc_cfg["in_patch_limit"]) // self.merge_size**2
)
@property
def _transparent_bg_config(self) -> Optional[TransparentBgConfig]:
cfg = self.media_proc_cfg.get("transparent_bg_config")
if cfg is None:
return None
if isinstance(cfg, TransparentBgConfig):
return cfg
return TransparentBgConfig(**cfg)
@property
def _transparent_bg_fill_stage(self) -> str:
return self.media_proc_cfg.get("transparent_bg_fill_stage",
"before_resize")
def media_tokens_calculator(self, media: MediaInput):
media = ensure_media_type(
media,
transparent_bg_config=self._transparent_bg_config,
transparent_bg_fill_stage=self._transparent_bg_fill_stage,
)
ret = self.get_resize_config(media)
return ret['num_tokens']
@classmethod
def make_image_prompt(cls, width: int, height: int) -> str:
"""Build the K3 image placeholder with resolution info."""
return (f"<|media_begin|>image {width}x{height}"
f"<|media_content|><|media_pad|><|media_end|>")
def get_resize_config(self, media_input: MediaInput) -> dict:
if media_input['type'] == 'image':
w, h = media_input['image'].size
input_patch_limit = int(self.media_proc_cfg['in_patch_limit'])
while True:
ret = navit_resize_image(
w, h, self.media_proc_cfg['patch_size'],
self.media_proc_cfg['merge_kernel_size'],
input_patch_limit,
self.media_proc_cfg['patch_limit_on_one_side'],
self.media_proc_cfg['fixed_output_tokens'])
if ret['num_tokens'] <= self.max_output_tokens:
return ret
reduced_limit = int(
input_patch_limit * self.max_output_tokens /
ret['num_tokens'])
input_patch_limit = min(input_patch_limit - 1,
reduced_limit)
else:
raise ValueError("Unsupported type: {}".format(
media_input['type']))
def resize_image(self, image: Image.Image, new_width: int, new_height: int,
pad_width: int, pad_height: int) -> np.ndarray:
image_np = image_to_np(
image,
(new_width, new_height),
"resize",
transparent_bg_config=self._transparent_bg_config,
transparent_bg_fill_stage=self._transparent_bg_fill_stage,
)
image_np = np.pad(
image_np,
((0, pad_height), (0, pad_width), (0, 0)),
mode="constant",
constant_values=0,
)
return image_np
def preprocess(
self,
medias: Optional[list[MediaInput]] = None,
return_tensors: Optional[Union[str, TensorType]] = None,
images=None,
do_resize: Optional[bool] = None,
**kwargs,
) -> BatchFeature:
"""
Preprocess a atom vision input (images) into model-ready tensors.
Args:
medias: List of MediaInput.
return_tensors: Desired output format ('pt', 'np', 'tf', or None).
Returns:
BatchFeature containing 'pixel_values' and 'grid_thws' tensors.
"""
del do_resize, kwargs
if medias is None:
medias = images
if medias is None:
medias = []
if not isinstance(medias, list):
medias = [medias]
medias = [item if isinstance(item, dict) else {"type": "image", "image": item} for item in medias]
if medias:
pixel_values = []
for item in medias:
item = ensure_media_type(
item,
transparent_bg_config=self._transparent_bg_config,
transparent_bg_fill_stage=self._transparent_bg_fill_stage,
)
resize_config = self.get_resize_config(item)
new_width, new_height, pad_width, pad_height = resize_config[
'new_width'], resize_config['new_height'], resize_config[
'pad_width'], resize_config['pad_height']
if item['type'] == 'image':
image = item['image']
image_np = self.resize_image(image, new_width, new_height,
pad_width, pad_height)
pixel_values.append(np.expand_dims(image_np, axis=0))
else:
raise ValueError("Unsupported type: {}".format(
item['type']))
normalized_pixel_values = []
image_std_inv = 1.0 / np.array(self.media_proc_cfg['image_std'])
image_mean = np.array(self.media_proc_cfg['image_mean'])
for pixels in pixel_values:
pixels = normalize(pixels, image_mean, image_std_inv)
pixels_and_thw = navit_patchify(
pixels,
self.media_proc_cfg['patch_size'],
)
normalized_pixel_values.append(pixels_and_thw)
pixel_values = torch.cat([
_to_tensor(pixel_value['pixel_values'])
for pixel_value in normalized_pixel_values
])
grid_thws = torch.cat([
_to_tensor(pixel_value['grid_thw'],
dtype=torch.int64).unsqueeze(0)
for pixel_value in normalized_pixel_values
])
data = {
'pixel_values': pixel_values,
'image_grid_thw': grid_thws,
}
else:
data = {}
return BatchFeature(data=data, tensor_type=return_tensors)
def __repr__(self):
return f"GroundAnythingVLMImageProcessor(media_proc_cfg={self.media_proc_cfg})"
def to_dict(self) -> Dict[str, Any]:
output = super().to_dict()
output["media_proc_cfg"] = self.media_proc_cfg
if "media_processor" in output:
del output["media_processor"]
return output
@classmethod
def from_dict(cls, config_dict: Dict[str, Any], **kwargs):
config = config_dict.copy()
media_proc_cfg = config.pop("media_proc_cfg", {})
return cls(media_proc_cfg=media_proc_cfg, **config, **kwargs)
def to_json_string(self):
dictionary = self.to_dict()
for key, value in dictionary.items():
if hasattr(value, 'tolist'):
dictionary[key] = value.tolist()
return json.dumps(dictionary, indent=2, sort_keys=True) + "\n"
|