Spaces:
Sleeping
Sleeping
jioji0
commited on
Commit
•
fc9cb20
1
Parent(s):
adb5f17
add files
Browse files- .idea/.gitignore +3 -0
- .idea/cityspace1024.iml +8 -0
- .idea/inspectionProfiles/Project_Default.xml +14 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- app.py +107 -0
- city-1.jpg +0 -0
- city-2.jpg +0 -0
- city-3.jpg +0 -0
- labels +19 -0
.idea/.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
# Default ignored files
|
2 |
+
/shelf/
|
3 |
+
/workspace.xml
|
.idea/cityspace1024.iml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<module type="PYTHON_MODULE" version="4">
|
3 |
+
<component name="NewModuleRootManager">
|
4 |
+
<content url="file://$MODULE_DIR$" />
|
5 |
+
<orderEntry type="inheritedJdk" />
|
6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
7 |
+
</component>
|
8 |
+
</module>
|
.idea/inspectionProfiles/Project_Default.xml
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<profile version="1.0">
|
3 |
+
<option name="myName" value="Project Default" />
|
4 |
+
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
5 |
+
<option name="ignoredPackages">
|
6 |
+
<value>
|
7 |
+
<list size="1">
|
8 |
+
<item index="0" class="java.lang.String" itemvalue="setuptools" />
|
9 |
+
</list>
|
10 |
+
</value>
|
11 |
+
</option>
|
12 |
+
</inspection_tool>
|
13 |
+
</profile>
|
14 |
+
</component>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<settings>
|
3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
4 |
+
<version value="1.0" />
|
5 |
+
</settings>
|
6 |
+
</component>
|
.idea/misc.xml
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
|
4 |
+
</project>
|
.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectModuleManager">
|
4 |
+
<modules>
|
5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/cityspace1024.iml" filepath="$PROJECT_DIR$/.idea/cityspace1024.iml" />
|
6 |
+
</modules>
|
7 |
+
</component>
|
8 |
+
</project>
|
.idea/vcs.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="VcsDirectoryMappings">
|
4 |
+
<mapping directory="" vcs="Git" />
|
5 |
+
</component>
|
6 |
+
</project>
|
app.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, SegformerForSemanticSegmentation
|
9 |
+
|
10 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b2-finetuned-cityscapes-1024-1024")
|
11 |
+
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b2-finetuned-cityscapes-1024-1024")
|
12 |
+
|
13 |
+
def ade_palette():
|
14 |
+
"""ADE20K palette that maps each class to RGB values."""
|
15 |
+
return [
|
16 |
+
[255, 0, 0],
|
17 |
+
[255, 187, 0],
|
18 |
+
[255, 228, 0],
|
19 |
+
[10, 10, 10],
|
20 |
+
[50, 50, 50],
|
21 |
+
[200, 10, 50],
|
22 |
+
[0, 0, 80],
|
23 |
+
[0, 200, 30],
|
24 |
+
[0, 30, 30],
|
25 |
+
[0, 10, 35],
|
26 |
+
[132, 102, 160],
|
27 |
+
[236, 103, 45],
|
28 |
+
[1, 1, 1],
|
29 |
+
[47, 37, 16],
|
30 |
+
[0, 70, 14],
|
31 |
+
[73, 10, 4],
|
32 |
+
[23, 0, 102],
|
33 |
+
[130, 80, 0],
|
34 |
+
[0, 0, 255]
|
35 |
+
]
|
36 |
+
|
37 |
+
labels_list = []
|
38 |
+
|
39 |
+
with open(r'labels.txt', 'r') as fp:
|
40 |
+
for line in fp:
|
41 |
+
labels_list.append(line[:-1])
|
42 |
+
|
43 |
+
colormap = np.asarray(ade_palette())
|
44 |
+
|
45 |
+
def label_to_color_image(label):
|
46 |
+
if label.ndim != 2:
|
47 |
+
raise ValueError("Expect 2-D input label")
|
48 |
+
|
49 |
+
if np.max(label) >= len(colormap):
|
50 |
+
raise ValueError("label value too large.")
|
51 |
+
return colormap[label]
|
52 |
+
|
53 |
+
def draw_plot(pred_img, seg):
|
54 |
+
fig = plt.figure(figsize=(20, 15))
|
55 |
+
|
56 |
+
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
57 |
+
|
58 |
+
plt.subplot(grid_spec[0])
|
59 |
+
plt.imshow(pred_img)
|
60 |
+
plt.axis('off')
|
61 |
+
LABEL_NAMES = np.asarray(labels_list)
|
62 |
+
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
63 |
+
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
64 |
+
|
65 |
+
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
66 |
+
ax = plt.subplot(grid_spec[1])
|
67 |
+
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
68 |
+
ax.yaxis.tick_right()
|
69 |
+
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
70 |
+
plt.xticks([], [])
|
71 |
+
ax.tick_params(width=0.0, labelsize=25)
|
72 |
+
return fig
|
73 |
+
|
74 |
+
def sepia(input_img):
|
75 |
+
input_img = Image.fromarray(input_img)
|
76 |
+
|
77 |
+
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
78 |
+
outputs = model(**inputs)
|
79 |
+
logits = outputs.logits
|
80 |
+
|
81 |
+
logits = tf.transpose(logits, [0, 2, 3, 1])
|
82 |
+
logits = tf.image.resize(
|
83 |
+
logits, input_img.size[::-1]
|
84 |
+
) # We reverse the shape of `image` because `image.size` returns width and height.
|
85 |
+
seg = tf.math.argmax(logits, axis=-1)[0]
|
86 |
+
|
87 |
+
color_seg = np.zeros(
|
88 |
+
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
89 |
+
) # height, width, 3
|
90 |
+
for label, color in enumerate(colormap):
|
91 |
+
color_seg[seg.numpy() == label, :] = color
|
92 |
+
|
93 |
+
# Show image + mask
|
94 |
+
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
95 |
+
pred_img = pred_img.astype(np.uint8)
|
96 |
+
|
97 |
+
fig = draw_plot(pred_img, seg)
|
98 |
+
return fig
|
99 |
+
|
100 |
+
demo = gr.Interface(fn=sepia,
|
101 |
+
inputs=gr.Image(shape=(400, 600)),
|
102 |
+
outputs=['plot'],
|
103 |
+
examples=["city-1.jpg", "city-2.jpg", "city-3.jpg"],
|
104 |
+
allow_flagging='never')
|
105 |
+
|
106 |
+
|
107 |
+
demo.launch()
|
city-1.jpg
ADDED
![]() |
city-2.jpg
ADDED
![]() |
city-3.jpg
ADDED
![]() |
labels
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
|