shyamgupta196 commited on
Commit
d0d42a2
1 Parent(s): 2291959
Files changed (3) hide show
  1. app.py +29 -3
  2. palette.py +42 -0
  3. seg_testing.ipynb +0 -0
app.py CHANGED
@@ -1,16 +1,42 @@
1
  from transformers import AutoFeatureExtractor, SegformerForSemanticSegmentation
 
 
 
2
 
3
  import gradio as gr
4
 
5
  def seg(image):
 
 
6
  feature_extractor = AutoFeatureExtractor.from_pretrained("nvidia/mit-b0")
7
  model = SegformerForSemanticSegmentation.from_pretrained("nvidia/mit-b0")
8
  print(model)
9
 
10
  inputs = feature_extractor(images=image, return_tensors="pt")
11
  outputs = model(**inputs)
12
- logits = outputs.logits
13
- return logits
14
 
15
- iface = gr.Interface(fn=seg, inputs=gr.inputs.Image(type='pil'), outputs='label')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  iface.launch()
 
1
  from transformers import AutoFeatureExtractor, SegformerForSemanticSegmentation
2
+ from torch import nn
3
+ import numpy as np
4
+ from palette import ade_palette
5
 
6
  import gradio as gr
7
 
8
  def seg(image):
9
+ ## first resize the image !!
10
+ image.resize((200,200))
11
  feature_extractor = AutoFeatureExtractor.from_pretrained("nvidia/mit-b0")
12
  model = SegformerForSemanticSegmentation.from_pretrained("nvidia/mit-b0")
13
  print(model)
14
 
15
  inputs = feature_extractor(images=image, return_tensors="pt")
16
  outputs = model(**inputs)
 
 
17
 
18
+ ## The model outputs logits of shape (batch_size, num_labels, height/4, width/4).
19
+ # We first rescale the logits to match the original size of the image using
20
+ # "bilinear interpolation". Next, we perform an argmax on the class dimension,
21
+ # and we create a color map which we draw over the image.
22
+
23
+ # First, rescale logits to original image size
24
+ logits = nn.functional.interpolate(outputs.logits.detach().cpu(),
25
+ size=image.size[::-1], # (height, width)
26
+ mode='bilinear',
27
+ align_corners=False)
28
+ # Second, apply argmax on the class dimension
29
+ seg = logits.argmax(dim=1)[0]
30
+ color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3
31
+ palette = np.array(ade_palette())
32
+ for label, color in enumerate(palette):
33
+ color_seg[seg == label, :] = color
34
+ # Convert to BGR
35
+ color_seg = color_seg[..., ::-1]
36
+ img = np.array(image) * 0.5 + color_seg * 0.5
37
+ img = img.astype(np.uint8)
38
+
39
+ return img
40
+
41
+ iface = gr.Interface(fn=seg, inputs=gr.inputs.Image(type='pil'), outputs=gr.outputs.Image())
42
  iface.launch()
palette.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # The ADE20k dataset consists of 150 classes, so the list below has 150 elements.
2
+
3
+ def ade_palette():
4
+ """ADE20K palette that maps each class to RGB values."""
5
+ return [[120, 120, 120], [180, 120, 120], [6, 230, 230], [80, 50, 50],
6
+ [4, 200, 3], [120, 120, 80], [140, 140, 140], [204, 5, 255],
7
+ [230, 230, 230], [4, 250, 7], [224, 5, 255], [235, 255, 7],
8
+ [150, 5, 61], [120, 120, 70], [8, 255, 51], [255, 6, 82],
9
+ [143, 255, 140], [204, 255, 4], [255, 51, 7], [204, 70, 3],
10
+ [0, 102, 200], [61, 230, 250], [255, 6, 51], [11, 102, 255],
11
+ [255, 7, 71], [255, 9, 224], [9, 7, 230], [220, 220, 220],
12
+ [255, 9, 92], [112, 9, 255], [8, 255, 214], [7, 255, 224],
13
+ [255, 184, 6], [10, 255, 71], [255, 41, 10], [7, 255, 255],
14
+ [224, 255, 8], [102, 8, 255], [255, 61, 6], [255, 194, 7],
15
+ [255, 122, 8], [0, 255, 20], [255, 8, 41], [255, 5, 153],
16
+ [6, 51, 255], [235, 12, 255], [160, 150, 20], [0, 163, 255],
17
+ [140, 140, 140], [250, 10, 15], [20, 255, 0], [31, 255, 0],
18
+ [255, 31, 0], [255, 224, 0], [153, 255, 0], [0, 0, 255],
19
+ [255, 71, 0], [0, 235, 255], [0, 173, 255], [31, 0, 255],
20
+ [11, 200, 200], [255, 82, 0], [0, 255, 245], [0, 61, 255],
21
+ [0, 255, 112], [0, 255, 133], [255, 0, 0], [255, 163, 0],
22
+ [255, 102, 0], [194, 255, 0], [0, 143, 255], [51, 255, 0],
23
+ [0, 82, 255], [0, 255, 41], [0, 255, 173], [10, 0, 255],
24
+ [173, 255, 0], [0, 255, 153], [255, 92, 0], [255, 0, 255],
25
+ [255, 0, 245], [255, 0, 102], [255, 173, 0], [255, 0, 20],
26
+ [255, 184, 184], [0, 31, 255], [0, 255, 61], [0, 71, 255],
27
+ [255, 0, 204], [0, 255, 194], [0, 255, 82], [0, 10, 255],
28
+ [0, 112, 255], [51, 0, 255], [0, 194, 255], [0, 122, 255],
29
+ [0, 255, 163], [255, 153, 0], [0, 255, 10], [255, 112, 0],
30
+ [143, 255, 0], [82, 0, 255], [163, 255, 0], [255, 235, 0],
31
+ [8, 184, 170], [133, 0, 255], [0, 255, 92], [184, 0, 255],
32
+ [255, 0, 31], [0, 184, 255], [0, 214, 255], [255, 0, 112],
33
+ [92, 255, 0], [0, 224, 255], [112, 224, 255], [70, 184, 160],
34
+ [163, 0, 255], [153, 0, 255], [71, 255, 0], [255, 0, 163],
35
+ [255, 204, 0], [255, 0, 143], [0, 255, 235], [133, 255, 0],
36
+ [255, 0, 235], [245, 0, 255], [255, 0, 122], [255, 245, 0],
37
+ [10, 190, 212], [214, 255, 0], [0, 204, 255], [20, 0, 255],
38
+ [255, 255, 0], [0, 153, 255], [0, 41, 255], [0, 255, 204],
39
+ [41, 0, 255], [41, 255, 0], [173, 0, 255], [0, 245, 255],
40
+ [71, 0, 255], [122, 0, 255], [0, 255, 184], [0, 92, 255],
41
+ [184, 255, 0], [0, 133, 255], [255, 214, 0], [25, 194, 194],
42
+ [102, 255, 0], [92, 0, 255]]
seg_testing.ipynb ADDED
The diff for this file is too large to render. See raw diff