Object Detection
Transformers
Safetensors
locateanything
feature-extraction
locateanything,
custom_code
Instructions to use SD-Next/LocateAnything-3B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use SD-Next/LocateAnything-3B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("object-detection", model="SD-Next/LocateAnything-3B", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("SD-Next/LocateAnything-3B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 3,188 Bytes
1983b12 | 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 | import math
import re
from transformers.feature_extraction_utils import BatchFeature
from transformers.processing_utils import ProcessingKwargs, ProcessorMixin, Unpack
class LocateAnythingProcessorKwargs(ProcessingKwargs, total=False):
_defaults = {}
class LocateAnythingProcessor(ProcessorMixin):
attributes = ["image_processor", "tokenizer"]
valid_kwargs = ["chat_template", "image_token", "image_start_token", "image_end_token"]
tokenizer_class = "AutoTokenizer"
def __init__(
self,
image_processor=None,
tokenizer=None,
chat_template=None,
image_token="<IMG_CONTEXT>",
image_start_token="<img>",
image_end_token="</img>",
**kwargs,
):
self.image_token = image_token
self.image_start_token = image_start_token
self.image_end_token = image_end_token
self.image_token_id = tokenizer.convert_tokens_to_ids(image_token)
super().__init__(image_processor, tokenizer, chat_template=chat_template)
def __call__(self, images=None, text=None, **kwargs: Unpack[LocateAnythingProcessorKwargs]):
output_kwargs = self._merge_kwargs(
LocateAnythingProcessorKwargs,
tokenizer_init_kwargs=self.tokenizer.init_kwargs,
**kwargs,
)
text = [text] if isinstance(text, str) else list(text or [])
image_inputs = {}
if images is not None:
image_inputs = self.image_processor(images, **output_kwargs["images_kwargs"])
grids = image_inputs["image_grid_thw"]
merge_area = math.prod(self.image_processor.merge_kernel_size)
image_index = 0
def replace_image(match):
nonlocal image_index
if image_index >= len(grids):
raise ValueError("Prompt contains more image placeholders than supplied images")
number = match.group(1)
_, height, width = grids[image_index].tolist()
image_index += 1
token_count = height * width // merge_area
return (
f"<image {number}>{self.image_start_token}"
f"{self.image_token * token_count}{self.image_end_token}"
)
text = [re.sub(r"<image-(\d+)>", replace_image, sample) for sample in text]
if image_index != len(grids):
raise ValueError("Supplied image count does not match image placeholders in the prompt")
return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None)
text_inputs = self.tokenizer(text, **output_kwargs["text_kwargs"])
return BatchFeature(data={**text_inputs, **image_inputs}, tensor_type=return_tensors)
def batch_decode(self, *args, **kwargs):
return self.tokenizer.batch_decode(*args, **kwargs)
def decode(self, *args, **kwargs):
return self.tokenizer.decode(*args, **kwargs)
@property
def model_input_names(self):
names = self.tokenizer.model_input_names + self.image_processor.model_input_names
return list(dict.fromkeys(names))
__all__ = ["LocateAnythingProcessor"]
|