Instructions to use mattmdjaga/segformer_b0_clothes with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use mattmdjaga/segformer_b0_clothes with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-segmentation", model="mattmdjaga/segformer_b0_clothes")# Load model directly from transformers import AutoImageProcessor, SegformerForSemanticSegmentation processor = AutoImageProcessor.from_pretrained("mattmdjaga/segformer_b0_clothes") model = SegformerForSemanticSegmentation.from_pretrained("mattmdjaga/segformer_b0_clothes", device_map="auto") - Inference
- Notebooks
- Google Colab
- Kaggle
Download handler.py from mattmdjaga/segformer_b0_clothes: direct link, hf CLI and curl.
- Browser
- Download file 1.54 kB
-
https://huggingface.co/mattmdjaga/segformer_b0_clothes/resolve/main/handler.py
- Command line
-
hf download hf://mattmdjaga/segformer_b0_clothes/handler.py
-
curl -L -o handler.py https://huggingface.co/mattmdjaga/segformer_b0_clothes/resolve/main/handler.py
1.54 kB
| from typing import Dict, List, Any | |
| from PIL import Image | |
| from io import BytesIO | |
| from transformers import AutoModelForSemanticSegmentation, AutoFeatureExtractor | |
| import base64 | |
| import torch | |
| from torch import nn | |
| class EndpointHandler(): | |
| def __init__(self, path="."): | |
| self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| self.model = AutoModelForSemanticSegmentation.from_pretrained(path).to(self.device).eval() | |
| self.feature_extractor = AutoFeatureExtractor.from_pretrained(path) | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| """ | |
| data args: | |
| images (:obj:`PIL.Image`) | |
| candiates (:obj:`list`) | |
| Return: | |
| A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82} | |
| """ | |
| inputs = data.pop("inputs", data) | |
| # decode base64 image to PIL | |
| image = Image.open(BytesIO(base64.b64decode(inputs['image']))) | |
| # preprocess image | |
| encoding = self.feature_extractor(images=image, return_tensors="pt") | |
| pixel_values = encoding["pixel_values"].to(self.device) | |
| with torch.no_grad(): | |
| outputs = self.model(pixel_values=pixel_values) | |
| logits = outputs.logits | |
| upsampled_logits = nn.functional.interpolate(logits, | |
| size=image.size[::-1], | |
| mode="bilinear", | |
| align_corners=False,) | |
| pred_seg = upsampled_logits.argmax(dim=1)[0] | |
| return pred_seg.tolist() |