Views
No views yet
1from transformers import AutoFeatureExtractor, SegformerForSemanticSegmentation
2from PIL import Image
3import requests
4
5url = "https://huggingface.co/lapix/segformer-b3-finetuned-ccagt-400-300/resolve/main/sampleB.png"
6image = Image.open(requests.get(url, stream=True).raw))
7
8model = SegformerForSemanticSegmentation.from_pretrained("lapix/segformer-b3-finetuned-ccagt-400-300")
9feature_extractor = AutoFeatureExtractor.from_pretrained("lapix/segformer-b3-finetuned-ccagt-400-300")
10
11pixel_values = feature_extractor(images=image, return_tensors="pt")
12outputs = model(pixel_values=pixel_values)
13logits = outputs.logits
14
15# Rescale logits to original image size (400, 300)
16upsampled_logits = nn.functional.interpolate(
17 logits,
18 size=img.size[::-1], # (height, width)
19 mode="bilinear",
20 align_corners=False,
21)
22
23segmentation_mask = upsampled_logits.argmax(dim=1)[0]
24
25print("Predicted mask:", segmentation_mask)1@article{AtkinsonSegmentationAgNORSSRN2022,
2 author= {Jo{\~{a}}o Gustavo Atkinson Amorim and Andr{\'{e}} Vict{\'{o}}ria Matias and Allan Cerentini and Fabiana Botelho de Miranda Onofre and Alexandre Sherlley Casimiro Onofre and Aldo von Wangenheim},
3 doi = {10.2139/ssrn.4126881},
4 url = {https://doi.org/10.2139/ssrn.4126881},
5 year = {2022},
6 publisher = {Elsevier {BV}},
7 title = {Semantic Segmentation for the Detection of Very Small Objects on Cervical Cell Samples Stained with the {AgNOR} Technique},
8 journal = {{SSRN} Electronic Journal}
9}