adirik commited on
Commit
d4dcd19
1 Parent(s): 07a88d8

first commit

Browse files
.gitignore ADDED
File without changes
README.md CHANGED
@@ -1,13 +1,13 @@
1
  ---
2
- title: Maskformer Demo
3
- emoji: 🐢
4
- colorFrom: red
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 3.2
8
  app_file: app.py
9
  pinned: false
10
- license: cc-by-nc-4.0
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: MaskFormer Demo
3
+ emoji: 🔥
4
+ colorFrom: yellow
5
+ colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 3.1.3
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
  ---
12
 
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
assets/test_image_35.png ADDED
assets/test_image_82.png ADDED
maskformer_demo.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import random
3
+ import gradio as gr
4
+ import numpy as np
5
+ from transformers import MaskFormerFeatureExtractor, MaskFormerForInstanceSegmentation
6
+
7
+
8
+ # Use GPU if available
9
+ if torch.cuda.is_available():
10
+ device = torch.device("cuda")
11
+ else:
12
+ device = torch.device("cpu")
13
+
14
+ model = MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-tiny-ade").to(device)
15
+ model.eval()
16
+ preprocessor = MaskFormerFeatureExtractor.from_pretrained("facebook/maskformer-swin-tiny-ade")
17
+
18
+
19
+ def visualize_instance_seg_mask(mask):
20
+ # Initialize image
21
+ image = np.zeros((mask.shape[0], mask.shape[1], 3))
22
+
23
+ labels = np.unique(mask)
24
+ label2color = {label: (random.randint(0, 1), random.randint(0, 255), random.randint(0, 255)) for label in labels}
25
+
26
+ for i in range(image.shape[0]):
27
+ for j in range(image.shape[1]):
28
+ image[i, j, :] = label2color[mask[i, j]]
29
+
30
+ image = image / 255
31
+ return image
32
+
33
+ def query_image(img):
34
+ target_size = (img.shape[0], img.shape[1])
35
+ inputs = preprocessor(images=img, return_tensors="pt")
36
+
37
+ with torch.no_grad():
38
+ outputs = model(**inputs)
39
+
40
+ outputs.class_queries_logits = outputs.class_queries_logits.cpu()
41
+ outputs.masks_queries_logits = outputs.masks_queries_logits.cpu()
42
+ results = preprocessor.post_process_segmentation(outputs=outputs, target_size=target_size)[0].cpu().detach()
43
+ results = torch.argmax(results, dim=0).numpy()
44
+ results = visualize_instance_seg_mask(results)
45
+
46
+ return results
47
+
48
+
49
+ description = """
50
+ Gradio demo for <a href="https://huggingface.co/docs/transformers/main/en/model_doc/maskformer">MaskFormer</a>,
51
+ introduced in <a href="https://arxiv.org/abs/2107.06278">Per-Pixel Classification is Not All You Need for Semantic Segmentation
52
+ </a>.
53
+ \n\n"Mask2Former is a unified framework architecture based on MaskFormer meta-architecture that achieves SOTA on panoptic,
54
+ instance and semantic segmentation across four popular datasets (ADE20K, Cityscapes, COCO, Mapillary Vistas). You can use
55
+ MaskFormer for semantic, instance (illustrated in the demo) and panoptic segmentation.
56
+ """
57
+
58
+ demo = gr.Interface(
59
+ query_image,
60
+ inputs=[gr.Image()],
61
+ outputs="image",
62
+ title="MaskFormer Demo",
63
+ description=description,
64
+ examples=["assets/test_image_35.png", "assets/test_image_82.png"]
65
+ )
66
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ # pip install -r requirements.txt
2
+
3
+ numpy>=1.18.5
4
+ torch>=1.7.0
5
+ torchvision>=0.8.1
6
+ transformers
7
+ opencv-python