Views
No views yet
1from transformers import SegformerImageProcessor, AutoModelForSemanticSegmentation
2from PIL import Image
3import requests
4import matplotlib.pyplot as plt
5import torch.nn as nn
6
7processor = SegformerImageProcessor.from_pretrained("sayeed99/segformer-b2-human")
8model = AutoModelForSemanticSegmentation.from_pretrained("sayeed99/segformer-b2-human")
9
10url = "https://plus.unsplash.com/premium_photo-1673210886161-bfcc40f54d1f?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8cGVyc29uJTIwc3RhbmRpbmd8ZW58MHx8MHx8&w=1000&q=80"
11
12image = Image.open(requests.get(url, stream=True).raw)
13inputs = processor(images=image, return_tensors="pt")
14
15outputs = model(**inputs)
16logits = outputs.logits.cpu()
17
18upsampled_logits = nn.functional.interpolate(
19 logits,
20 size=image.size[::-1],
21 mode="bilinear",
22 align_corners=False,
23)
24
25pred_seg = upsampled_logits.argmax(dim=1)[0]
26plt.imshow(pred_seg)1@article{DBLP:journals/corr/abs-2105-15203,
2 author = {Enze Xie and
3 Wenhai Wang and
4 Zhiding Yu and
5 Anima Anandkumar and
6 Jose M. Alvarez and
7 Ping Luo},
8 title = {SegFormer: Simple and Efficient Design for Semantic Segmentation with
9 Transformers},
10 journal = {CoRR},
11 volume = {abs/2105.15203},
12 year = {2021},
13 url = {https://arxiv.org/abs/2105.15203},
14 eprinttype = {arXiv},
15 eprint = {2105.15203},
16 timestamp = {Wed, 02 Jun 2021 11:46:42 +0200},
17 biburl = {https://dblp.org/rec/journals/corr/abs-2105-15203.bib},
18 bibsource = {dblp computer science bibliography, https://dblp.org}
19}