Image Segmentation
PyTorch
sam2
custom-sam2
glove
baseball
sports-analytics
computer-vision
custom-model
Instructions to use caball21/glove_labelling with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sam2
How to use caball21/glove_labelling with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained(caball21/glove_labelling) with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16): predictor.set_image(<your_image>) masks, _, _ = predictor.predict(<input_prompts>)# Use SAM2 with videos import torch from sam2.sam2_video_predictor import SAM2VideoPredictor predictor = SAM2VideoPredictor.from_pretrained(caball21/glove_labelling) with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16): state = predictor.init_state(<your_video>) # add new prompts and instantly get the output on the same frame frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>): # propagate the prompts to get masklets throughout the video for frame_idx, object_ids, masks in predictor.propagate_in_video(state): ... - Notebooks
- Google Colab
- Kaggle
| # handler.py | |
| import torch | |
| import torchvision.transforms as T | |
| from PIL import Image | |
| import io | |
| import json | |
| # Define class labels (must match training order) | |
| CLASS_LABELS = [ | |
| "glove_outline", | |
| "webbing", | |
| "thumb", | |
| "palm_pocket", | |
| "hand", | |
| "glove_exterior" | |
| ] | |
| # ---------------------------- | |
| # Load model directly from full .bin | |
| # ---------------------------- | |
| def load_model(): | |
| model = torch.load("pytorch_model.bin", map_location="cpu") | |
| model.eval() | |
| return model | |
| model = load_model() | |
| # ---------------------------- | |
| # Preprocessing | |
| # ---------------------------- | |
| transform = T.Compose([ | |
| T.Resize((720, 1280)), | |
| T.ToTensor() | |
| ]) | |
| def preprocess(input_bytes): | |
| image = Image.open(io.BytesIO(input_bytes)).convert("RGB") | |
| tensor = transform(image).unsqueeze(0) # [1, 3, H, W] | |
| return tensor | |
| # ---------------------------- | |
| # Dummy input wrapper | |
| # ---------------------------- | |
| class DummyInput: | |
| def __init__(self, image_tensor): | |
| B, C, H, W = image_tensor.shape | |
| self.images = image_tensor | |
| self.masks = [torch.zeros(B, H, W, dtype=torch.bool)] | |
| self.num_frames = 1 | |
| self.original_size = [(H, W)] | |
| self.target_size = [(H, W)] | |
| self.point_coords = [None] | |
| self.point_labels = [None] | |
| self.boxes = [None] | |
| self.mask_inputs = torch.zeros(B, 1, H, W) | |
| self.video_mask = torch.zeros(B, 1, H, W) | |
| self.flat_obj_to_img_idx = [[0]] | |
| # ---------------------------- | |
| # Postprocessing | |
| # ---------------------------- | |
| def postprocess(output_tensor): | |
| if isinstance(output_tensor, dict) and "masks" in output_tensor: | |
| logits = output_tensor["masks"] | |
| else: | |
| logits = output_tensor | |
| pred = torch.argmax(logits, dim=1)[0].cpu().numpy() | |
| return pred.tolist() | |
| # ---------------------------- | |
| # Inference Entry Point | |
| # ---------------------------- | |
| def infer(payload): | |
| if isinstance(payload, bytes): | |
| image_tensor = preprocess(payload) | |
| elif isinstance(payload, dict) and "inputs" in payload: | |
| from base64 import b64decode | |
| image_tensor = preprocess(b64decode(payload["inputs"])) | |
| else: | |
| raise ValueError("Unsupported input format") | |
| input_obj = DummyInput(image_tensor) | |
| with torch.no_grad(): | |
| output = model(input_obj) | |
| mask = postprocess(output) | |
| return { | |
| "mask": mask, | |
| "classes": CLASS_LABELS | |
| } | |