ockkjs commited on
Commit
1eb0080
·
1 Parent(s): 9178aac

first commit

Browse files
Files changed (7) hide show
  1. .gitattributes +3 -0
  2. .idea/.gitignore +5 -0
  3. app.py +108 -0
  4. labels.txt +19 -0
  5. requirements.txt +6 -0
  6. road-2.jpg +3 -0
  7. road-3.jpeg +3 -0
.gitattributes CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.jpg filter=lfs diff=lfs merge=lfs -text
37
+ *.jpeg filter=lfs diff=lfs merge=lfs -text
38
+ *.png filter=lfs diff=lfs merge=lfs -text
.idea/.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from matplotlib import gridspec
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ from PIL import Image
6
+ import torch
7
+ from transformers import AutoImageProcessor, AutoModelForSemanticSegmentation
8
+
9
+ MODEL_ID = "nvidia/segformer-b4-finetuned-cityscapes-1024-1024"
10
+ processor = AutoImageProcessor.from_pretrained(MODEL_ID)
11
+ model = AutoModelForSemanticSegmentation.from_pretrained(MODEL_ID)
12
+
13
+ def ade_palette():
14
+ """ADE20K palette that maps each class to RGB values."""
15
+ return [
16
+ [0, 0, 0],
17
+ [255, 0, 0],
18
+ [171, 242, 0],
19
+ [153, 138, 0],
20
+ [255, 228, 0],
21
+ [0, 216, 255],
22
+ [90, 180, 56],
23
+ [155, 102, 200],
24
+ [33, 147, 176],
25
+ [255, 183, 76],
26
+ [67, 123, 89],
27
+ [190, 60, 45],
28
+ [134, 114, 200],
29
+ [56, 45, 189],
30
+ [200, 56, 123],
31
+ [87, 92, 200],
32
+ [120, 56, 123],
33
+ [45, 78, 123],
34
+ ]
35
+
36
+ labels_list = []
37
+ with open("labels.txt", "r", encoding="utf-8") as fp:
38
+ for line in fp:
39
+ labels_list.append(line.rstrip("\n"))
40
+
41
+ colormap = np.asarray(ade_palette(), dtype=np.uint8)
42
+
43
+ def label_to_color_image(label):
44
+ if label.ndim != 2:
45
+ raise ValueError("Expect 2-D input label")
46
+ if np.max(label) >= len(colormap):
47
+ raise ValueError("label value too large.")
48
+ return colormap[label]
49
+
50
+ def draw_plot(pred_img, seg_np):
51
+ fig = plt.figure(figsize=(20, 15))
52
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
53
+
54
+ plt.subplot(grid_spec[0])
55
+ plt.imshow(pred_img)
56
+ plt.axis('off')
57
+
58
+ LABEL_NAMES = np.asarray(labels_list)
59
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
60
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
61
+
62
+ unique_labels = np.unique(seg_np.astype("uint8"))
63
+ ax = plt.subplot(grid_spec[1])
64
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
65
+ ax.yaxis.tick_right()
66
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
67
+ plt.xticks([], [])
68
+ ax.tick_params(width=0.0, labelsize=25)
69
+ return fig
70
+
71
+ def run_inference(input_img):
72
+ # input: numpy array from gradio -> PIL
73
+ img = Image.fromarray(input_img.astype(np.uint8)) if isinstance(input_img, np.ndarray) else input_img
74
+ if img.mode != "RGB":
75
+ img = img.convert("RGB")
76
+
77
+ inputs = processor(images=img, return_tensors="pt")
78
+ with torch.no_grad():
79
+ outputs = model(**inputs)
80
+ logits = outputs.logits # (1, C, h/4, w/4)
81
+
82
+ # resize to original
83
+ upsampled = torch.nn.functional.interpolate(
84
+ logits, size=img.size[::-1], mode="bilinear", align_corners=False
85
+ )
86
+ seg = upsampled.argmax(dim=1)[0].cpu().numpy().astype(np.uint8) # (H,W)
87
+
88
+ # colorize & overlay
89
+ color_seg = colormap[seg] # (H,W,3)
90
+ pred_img = (np.array(img) * 0.5 + color_seg * 0.5).astype(np.uint8)
91
+
92
+ fig = draw_plot(pred_img, seg)
93
+ return fig
94
+
95
+ demo = gr.Interface(
96
+ fn=run_inference,
97
+ inputs=gr.Image(type="numpy", label="Input Image"),
98
+ outputs=gr.Plot(label="Overlay + Legend"),
99
+ examples=[
100
+ "road-2.jpg",
101
+ "road-3.jpeg",
102
+ ],
103
+ flagging_mode="never",
104
+ cache_examples=False,
105
+ )
106
+
107
+ if __name__ == "__main__":
108
+ demo.launch()
labels.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ road
2
+ sidewalk
3
+ building
4
+ wall
5
+ fence
6
+ pole
7
+ traffic light
8
+ traffic sign
9
+ vegetation
10
+ terrain
11
+ sky
12
+ person
13
+ rider
14
+ car
15
+ truck
16
+ bus
17
+ train
18
+ motorcycle
19
+ bicycle
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers>=4.41.0
3
+ gradio>=4.0.0
4
+ Pillow
5
+ numpy
6
+ matplotlib
road-2.jpg ADDED

Git LFS Details

  • SHA256: 45b47a2019280cf41797fe32a41db266bc3311a46767c88e068e712d4230860c
  • Pointer size: 130 Bytes
  • Size of remote file: 90.8 kB
road-3.jpeg ADDED

Git LFS Details

  • SHA256: 8f3938362d0a5279e15e5667ee3f0617f826cbc334d151d59b69ee6b988cca56
  • Pointer size: 130 Bytes
  • Size of remote file: 12.6 kB