Transformers
Loads detection models and processors for box prediction and task-specific fine-tuning.
- Downloads / mo
- 120.5M
- Stars
- 169.2K
- Last release
- 5.23.0
- License
- Apache-2.0
Strengths
- Detects objects from text prompts. Use a zero-shot detector to search for named visual concepts.
- Rescales boxes to image dimensions. Pass target_sizes when postprocessing model predictions.
Limitations
- Custom labels need head replacement. Provide label mappings and replace the pretrained classification head during fine-tuning.
- Evaluation needs separate box batches. Set eval_do_concat_batches=False when target-box counts differ across images.
Quickstart
from PIL import Image from transformers import pipeline detector = pipeline("object-detection", model="facebook/detr-resnet-50", device=-1) print(detector(Image.new("RGB", (640, 480))))
Transformers examples
def perform_inference(self, image: list | np.ndarray): """Prediction is performed using self.model and the prediction result is set to self._original_predictions. Args: image: np.ndarray A numpy array that contains the image to be predicted. 3 channel image should be in RGB order. """ import torch # Confirm model is loaded if self.model is None or self.processor is None: raise RuntimeError("Model is not loaded, load it by calling .load_model()") with torch.no_grad(): inputs = self.processor(images=image, return_tensors="pt") inputs["pixel_values"] = inputs.pixel_values.to(self.device) if hasattr(inputs, "pixel_mask"): inputs["pixel_mask"] = inputs.pixel_mask.to(self.device) outputs = self.model(**inputs) if isinstance(image, list): self._image_shapes = [img.shape for img in image] else: self._image_shapes = [image.shape] self._original_predictions = outputs