Upload 8 files
Browse files- .gitattributes +4 -11
- README.md +4 -4
- app.py +110 -0
- cityoutdoor-1.jpg +0 -0
- cityoutdoor-2.jpg +3 -0
- cityoutdoor-3.jpg +0 -0
- label.txt +19 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
@@ -1,35 +1,28 @@
|
|
1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
|
|
4 |
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
-
|
26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
-
*.
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bin.* filter=lfs diff=lfs merge=lfs -text
|
5 |
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
|
|
6 |
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
|
|
11 |
*.model filter=lfs diff=lfs merge=lfs -text
|
12 |
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
13 |
*.onnx filter=lfs diff=lfs merge=lfs -text
|
14 |
*.ot filter=lfs diff=lfs merge=lfs -text
|
15 |
*.parquet filter=lfs diff=lfs merge=lfs -text
|
16 |
*.pb filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
17 |
*.pt filter=lfs diff=lfs merge=lfs -text
|
18 |
*.pth filter=lfs diff=lfs merge=lfs -text
|
19 |
*.rar filter=lfs diff=lfs merge=lfs -text
|
20 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
|
21 |
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
|
|
22 |
*.tflite filter=lfs diff=lfs merge=lfs -text
|
23 |
*.tgz filter=lfs diff=lfs merge=lfs -text
|
|
|
24 |
*.xz filter=lfs diff=lfs merge=lfs -text
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
+
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
cityoutdoor-2.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
1 |
---
|
2 |
+
title: Homework2
|
3 |
+
emoji: 📚
|
4 |
+
colorFrom: pink
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.44.4
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from matplotlib import gridspec
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
import tensorflow as tf
|
8 |
+
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
9 |
+
|
10 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained(
|
11 |
+
"nvidia/segformer-b1-finetuned-cityscapes-1024-1024"
|
12 |
+
)
|
13 |
+
model = TFSegformerForSemanticSegmentation.from_pretrained(
|
14 |
+
"nvidia/segformer-b1-finetuned-cityscapes-1024-1024"
|
15 |
+
)
|
16 |
+
|
17 |
+
def ade_palette():
|
18 |
+
"""ADE20K palette that maps each class to RGB values."""
|
19 |
+
return [
|
20 |
+
[204, 166, 62],
|
21 |
+
[188, 229, 92],
|
22 |
+
[47, 157,39],
|
23 |
+
[178, 235, 244],
|
24 |
+
[0, 51, 153],
|
25 |
+
[181, 178, 255],
|
26 |
+
[128, 65, 217],
|
27 |
+
[255, 178, 245],
|
28 |
+
[153, 0, 76],
|
29 |
+
[25, 186, 52],
|
30 |
+
[81, 162, 235],
|
31 |
+
[255, 255, 0],
|
32 |
+
[62, 57, 159],
|
33 |
+
[91, 189, 203],
|
34 |
+
[0, 0, 255],
|
35 |
+
[0, 255, 255],
|
36 |
+
[12, 168, 0],
|
37 |
+
[255, 0, 0]
|
38 |
+
]
|
39 |
+
|
40 |
+
labels_list = []
|
41 |
+
|
42 |
+
with open(r'labels.txt', 'r') as fp:
|
43 |
+
for line in fp:
|
44 |
+
labels_list.append(line[:-1])
|
45 |
+
|
46 |
+
colormap = np.asarray(ade_palette())
|
47 |
+
|
48 |
+
def label_to_color_image(label):
|
49 |
+
if label.ndim != 2:
|
50 |
+
raise ValueError("Expect 2-D input label")
|
51 |
+
|
52 |
+
if np.max(label) >= len(colormap):
|
53 |
+
raise ValueError("label value too large.")
|
54 |
+
return colormap[label]
|
55 |
+
|
56 |
+
def draw_plot(pred_img, seg):
|
57 |
+
fig = plt.figure(figsize=(20, 15))
|
58 |
+
|
59 |
+
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
60 |
+
|
61 |
+
plt.subplot(grid_spec[0])
|
62 |
+
plt.imshow(pred_img)
|
63 |
+
plt.axis('off')
|
64 |
+
LABEL_NAMES = np.asarray(labels_list)
|
65 |
+
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
66 |
+
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
67 |
+
|
68 |
+
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
69 |
+
ax = plt.subplot(grid_spec[1])
|
70 |
+
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
71 |
+
ax.yaxis.tick_right()
|
72 |
+
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
73 |
+
plt.xticks([], [])
|
74 |
+
ax.tick_params(width=0.0, labelsize=25)
|
75 |
+
return fig
|
76 |
+
|
77 |
+
def sepia(input_img):
|
78 |
+
input_img = Image.fromarray(input_img)
|
79 |
+
|
80 |
+
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
81 |
+
outputs = model(**inputs)
|
82 |
+
logits = outputs.logits
|
83 |
+
|
84 |
+
logits = tf.transpose(logits, [0, 2, 3, 1])
|
85 |
+
logits = tf.image.resize(
|
86 |
+
logits, input_img.size[::-1]
|
87 |
+
) # We reverse the shape of `image` because `image.size` returns width and height.
|
88 |
+
seg = tf.math.argmax(logits, axis=-1)[0]
|
89 |
+
|
90 |
+
color_seg = np.zeros(
|
91 |
+
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
92 |
+
) # height, width, 3
|
93 |
+
for label, color in enumerate(colormap):
|
94 |
+
color_seg[seg.numpy() == label, :] = color
|
95 |
+
|
96 |
+
# Show image + mask
|
97 |
+
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
98 |
+
pred_img = pred_img.astype(np.uint8)
|
99 |
+
|
100 |
+
fig = draw_plot(pred_img, seg)
|
101 |
+
return fig
|
102 |
+
|
103 |
+
demo = gr.Interface(fn=sepia,
|
104 |
+
inputs=gr.Image(shape=(400, 600)),
|
105 |
+
outputs=['plot'],
|
106 |
+
examples=["cityoutdoor-1.jpg", "cityoutdoor-2.jpg", "cityoutdoor-3.jpg"],
|
107 |
+
allow_flagging='never')
|
108 |
+
|
109 |
+
|
110 |
+
demo.launch()
|
cityoutdoor-1.jpg
ADDED
cityoutdoor-2.jpg
ADDED
Git LFS Details
|
cityoutdoor-3.jpg
ADDED
label.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
|
3 |
+
tensorflow
|
4 |
+
numpy
|
5 |
+
Image
|
6 |
+
matplotlib
|